package com.casic.missiles.modular.alarm.job;

import com.casic.missiles.modular.alarm.service.IAlarmJobService;
import com.casic.missiles.modular.system.model.AlarmJob;
import com.casic.missiles.modular.system.model.User;
import com.casic.missiles.modular.system.util.PushList;
import com.casic.missiles.modular.system.util.WebSocket;
import com.casic.missiles.quartz.service.IQuartzManager;
import com.gexin.rp.sdk.base.IPushResult;
import com.google.gson.GsonBuilder;
import org.quartz.Job;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import cn.hutool.core.util.StrUtil;

import java.text.SimpleDateFormat;
import java.util.*;

@Component
public class getDelayJob implements Job {

    private static final Logger logger = LoggerFactory.getLogger(getDelayJob.class);

    private Integer jobId;
    private IAlarmJobService iAlarmJobService;
    private ICommonUserService iCommonUserService;
    private IQuartzManager iQuartzManager;
    private WebSocket webSocket;

    public void setJobId(Integer jobId) {
        this.jobId = jobId;
    }

    public void setIAlarmJobService(IAlarmJobService iAlarmJobService) {
        this.iAlarmJobService = iAlarmJobService;
    }

    public void setICommonUserService(ICommonUserService iCommonUserService) {
        this.iCommonUserService = iCommonUserService;
    }

    public void setIQuartzManager(IQuartzManager iQuartzManager) {
        this.iQuartzManager = iQuartzManager;
    }

    public void setWebSocket(WebSocket webSocket) {
        this.webSocket = webSocket;
    }

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        //业务逻辑
        SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
        System.out.println("CRON ----> schedule job1 is running ... + " + jobId + "  ---->  " + dateFormat.format(new Date()));
        getJobDelay(jobId);
    }

    private void getJobDelay(Integer id) {
        AlarmJob alarmJob = iAlarmJobService.selectById(id);
        if(!"3".equals(alarmJob.getJobStatus()) &&!"4".equals(alarmJob.getJobStatus())){
            // 超时未处理
            alarmJob.setJobFlagDelay("2"); // 延时标签
            String jobFlag = alarmJob.getJobFlag1(); // 延时记录
            if(StrUtil.isNotEmpty(jobFlag)){
                jobFlag += ",2";
            }else{
                jobFlag = "2";
            }
            alarmJob.setJobFlag1(jobFlag);
            iAlarmJobService.updateById(alarmJob); // 更新记录
            // APP推送
            Long jobBelongTo = alarmJob.getJobBelongTo(); // 只给责任人推送
            String clientId = iAlarmJobService.selectClientIdByUser(jobBelongTo);
            List<String> clientIds = new ArrayList<>();
            if(StrUtil.isNotEmpty(clientId)){
                clientIds.add(clientId);
                IPushResult pushResult = PushList.pushToUser(clientIds,"工单提醒","您有一条工单超时未处理");
                if("ok".equals(pushResult.getResponse().get("result").toString())){
                    logger.info("超时未处理工单推送成功:"+id.toString());
                }else{
                    logger.info("超时未处理工单推送失败:"+id.toString());
                }
            }else{
                logger.info("超时未处理工单找不到责任人，未推送:"+id.toString());
            }
            // PC推送,推给信息办
            //List<User> userList = permissionService.getRoleUsersByTips("1178242562491285506",null,false);
//            EntityWrapper wrapper = new EntityWrapper();
//            wrapper.eq("DEPTID","1178242562491285506");
//            List<User> userList = iCommonUserService.selectUsersList(wrapper,null);
//            List<String> userIds = new ArrayList<>();
//            for (User user : userList) {
//                userIds.add(user.getId().toString());
//            }
//            Map<String,Object> map = new HashMap<String, Object>();
//            map.put("message","有新的超时未处理工单");
//            map.put("type","overtimeHandle");
//            webSocket.sendListMessage(userIds,new GsonBuilder().create().toJson(map));
        }
        String jobName = "getJobDelay" + id;
        iQuartzManager.removeJob(jobName);
    }
}
