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

import cn.hutool.core.util.StrUtil;
import com.casic.missiles.modular.alarm.service.IAlarmJobService;
import com.casic.missiles.modular.system.model.AlarmJob;
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 org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.stereotype.Component;

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

@Component
public class DelayJob implements Job {

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

    private Long jobId;
    private IAlarmJobService iAlarmJobService;
    private AbstractApplicationContext iCommonUserService;
    private IQuartzManager iQuartzManager;
    private WebSocket webSocket;

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

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

    public void setICommonUserService(AbstractApplicationContext 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(Long id) {
        AlarmJob alarmJob = iAlarmJobService.getById(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);
                String appMsg="您有一条编号为["+id+"]的工单未处理，请及时处理";
                IPushResult pushResult = PushList.pushToUser(clientIds, "工单提醒", appMsg);
                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);
    }
}
