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

import com.casic.missiles.core.common.service.ICommonUserService;
import com.casic.missiles.modular.alarm.service.IAlarmJobService;
import com.casic.missiles.modular.system.dto.UserClientView;
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 org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
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 HandleAlarmJob implements Job{

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

    private Integer jobId;
    private IAlarmJobService iAlarmJobService;
    private ICommonUserService iCommonUserService;
    private IQuartzManager iQuartzManager;
    private WebSocket webSocket;
    private List<UserClientView> userClientViewList;

    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("0".equals(alarmJob.getJobStatus())){
            // 超时未接收
            alarmJob.setJobFlagDelay("1"); // 延时标签
            alarmJob.setJobFlag1("1");
            iAlarmJobService.updateById(alarmJob); // 更新记录
            // APP推送 推给井盖所属权属单位
            List<String> clientIds = new ArrayList<>();
            for(UserClientView userClientView:userClientViewList){
                if(StrUtil.isNotEmpty(userClientView.getClientid())){
                    clientIds.add(userClientView.getClientid());
                }
            }

            if(clientIds.size()>0){
                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","overtimeGet");
//            webSocket.sendListMessage(userIds,new GsonBuilder().create().toJson(map));
        }
        String jobName = "getJobDelay" + id;
        iQuartzManager.removeJob(jobName);
    }
}
