DataFillWarnJob.java 5.2 KB
package com.skua.modules.alarm.quartz;

import com.alibaba.fastjson.JSONObject;
import com.skua.core.constant.CommonConstant;
import com.skua.core.util.DateUtils;
import com.skua.modules.message.websocket.WebSocket;
import com.skua.modules.report.service.IReportWaterDataService;
import com.skua.modules.system.entity.SysAnnouncement;
import com.skua.modules.system.entity.SysAnnouncementSend;
import com.skua.modules.system.entity.SysDepart;
import com.skua.modules.system.service.ISysAnnouncementSendService;
import com.skua.modules.system.service.ISysAnnouncementService;
import com.skua.modules.system.service.ISysDepartService;
import com.skua.modules.system.service.ISysUserDepartService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.*;

/**
 * 数据填报报警
 */
@Slf4j
public class DataFillWarnJob implements Job {

    @Autowired
    private ISysDepartService sysDepartService;
    @Autowired
    private ISysAnnouncementService sysAnnouncementService;
    @Autowired
    private ISysAnnouncementSendService sysAnnouncementSendService;
    @Autowired
    private ISysUserDepartService iSysUserDepartService;
    @Autowired
    private IReportWaterDataService reportWaterDataService;
    @Resource
    private WebSocket webSocket;

    @Override
    public void execute(JobExecutionContext context) {
    	System.out.println("********工艺运行记录表填报监测服务开启********");
        Date date = new Date();
        int nowHour = date.getHours();
        String today = DateUtils.format(date,"yyyy-MM-dd");
        if(nowHour!=0){
            String parentId = "ee0f983f190f4da4a2e19cba70370ea7";
            List<SysDepart> list = sysDepartService.getFactoryListByParent(parentId);
            sendMsgToWeb(list,today,nowHour);
        }
        System.out.println("********工艺运行记录表填报监测服务结束********");
    }

    private void sendMsgToWeb(List<SysDepart> list,String today,int hour) {
        Double d = new Double(Math.floor((hour/2)+1));
        int allCount = d.intValue();
        if(list.size()>0){
            for (SysDepart dept : list) {
                String factoryId = dept.getId();
                int haveCount = reportWaterDataService.getDataCount(factoryId,today);
                if(allCount-haveCount > 0){
                    List<String> userIds= iSysUserDepartService.queryUserIdByfactoryId(factoryId);
                    sendMsg(userIds,factoryId,dept.getDepartName(),allCount-haveCount);
                }
            }
        }

    }

    private void sendMsg(List<String> userIds,String factoryId,String factoryName,int count) {
        String sendMsg = "截止当前时间"+factoryName+"存在"+count+"条未填报情况,请及时填写工艺运行记录表!";
        String usersId = "";
        for (int i = 0; i < userIds.size(); i++) {
            String userId = userIds.get(i);
            usersId = usersId + "," + userId;
        }
        //发送站内通知
        SysAnnouncement sysAnnouncement = new SysAnnouncement();
        //生成uuid
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        sysAnnouncement.setId(uuid);
        sysAnnouncement.setTitile(factoryName+"工艺运行记录表填报通知");
        sysAnnouncement.setMsgContent(sendMsg);
        sysAnnouncement.setPriority(CommonConstant.PRIORITY_M);
        sysAnnouncement.setMsgType(CommonConstant.MSG_TYPE_UESR);
        sysAnnouncement.setSendStatus(CommonConstant.HAS_SEND);
        sysAnnouncement.setSendTime(new Date());
        sysAnnouncement.setStartTime(new Date());
        sysAnnouncement.setEndTime(getDateByAdd(new Date(), 2));
        sysAnnouncement.setUserIds(usersId);
        sysAnnouncement.setFactoryId(factoryId);
        sysAnnouncement.setMsgCategory(CommonConstant.MSG_CATEGORY_2);
        sysAnnouncement.setDelFlag(String.valueOf(CommonConstant.DEL_FLAG_0));
        sysAnnouncement.setSender("admin");
        sysAnnouncementService.save(sysAnnouncement);

        for (String userid : userIds) {
            SysAnnouncementSend announcementSend = new SysAnnouncementSend();
            announcementSend.setAnntId(sysAnnouncement.getId());
            announcementSend.setUserId(userid);
            announcementSend.setReadFlag(CommonConstant.NO_READ_FLAG);
            sysAnnouncementSendService.save(announcementSend);
        }

        com.alibaba.fastjson.JSONObject obj = new JSONObject();
        obj.put("cmd", "topic");
        obj.put("msgId", sysAnnouncement.getId());
        obj.put("msgTxt", sysAnnouncement.getMsgContent());
//        webSocket.sendOneMessage("e9ca23d68d884d4ebb19d07889727dae",obj.toJSONString());
        webSocket.sendAllMessageByFactoryIds(userIds,obj.toJSONString());
    }

    /**
     * @description   获取*单位后的时间
     * @param num 时间单位数(如果num数为负数,说明是此日期前的天数)
     */
    public static Date getDateByAdd(Date date, int num) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR, num);
        return cal.getTime();
    }

}