package com.casic.missiles.modular.system.controller;


import com.casic.missiles.core.application.context.AbstractPermissionContext;
import com.casic.missiles.core.model.auth.AuthUser;
import com.casic.missiles.model.response.ResponseData;
import com.casic.missiles.modular.system.service.MessagePusherService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

@RestController
public class MessagePusherController {


    private final MessagePusherService messagePusherService;
    @Resource
    private AbstractPermissionContext permissionService;


    public MessagePusherController(MessagePusherService messagePusherService) {
        this.messagePusherService = messagePusherService;
    }


    /**
     * 接收工单
     */
    @RequestMapping(value = "/job/getJob")
    @ResponseBody
    public Object getJob(Long id) {
        AuthUser currentUser = permissionService.getAuthService().getLoginUser();
        if (messagePusherService.getJob(id, currentUser.getDeptId(), currentUser.getId())) {
            return ResponseData.success();
        } else {
            return ResponseData.error("接单失败");
        }
    }


    /***
     * 更新sink生成工单的接单时间，及建立超期未接工单的处理任务
     * @param id
     * @return
     */
    @RequestMapping(value = "/job/updateSinkJob")
    @ResponseBody
    public Map<String, Object> updateSinkJob(@RequestParam(value = "id", required = true) String id,
                                             @RequestParam(value = "msg", required = true) String msg) {
        Map<String, Object> retMap = new HashMap<>();
        try {
            messagePusherService.updateSinkJob(id, URLDecoder.decode(msg, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return retMap;
    }

    @PostMapping("/app/registerGeTui")
    public ResponseData geTuiRegister(@RequestParam String clientid) {
        messagePusherService.registerGeTui(permissionService.getAuthService().getLoginUser().getId(), clientid);
        return ResponseData.success();
    }
}
