package com.mh.quartz.task; import com.mh.common.constant.Constants; import com.mh.common.core.domain.entity.AlarmRecords; import com.mh.common.core.domain.wechat.*; import com.mh.common.utils.DateUtils; import com.mh.common.utils.DictUtils; import com.mh.system.service.ISysParamsService; import com.mh.system.service.IWechatService; import com.mh.system.service.impl.SysParamsServiceImpl; import com.mh.system.service.operation.IAlarmRecordsService; import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @author LJF * @version 1.0 * @project EEMCS * @description 微信推送数据 * @date 2025-06-27 10:54:23 */ @Slf4j @Component("pushDataToWechatTask") public class PushDataToWechatTask { @Resource private IAlarmRecordsService alarmRecordsService; @Resource private IWechatService wechatService; @Resource private ISysParamsService sysParamsService; public void pushDataToWechat() { // 查询报警内容 AlarmRecords alarmRecords = new AlarmRecords(); alarmRecords.setStatus(0); alarmRecords.setIsSend(0); Map param = new HashMap<>(); param.put("beginTime", DateUtils.dateToString(new Date(), "yyyy-MM-dd 00:00:00")); param.put("endTime", DateUtils.dateToString(new Date(), "yyyy-MM-dd 23:59:59")); alarmRecords.setParams(param); List alarmRecords1 = alarmRecordsService.selectAlarmRecordsList(alarmRecords); if (null == alarmRecords1 || alarmRecords1.isEmpty()) { return; } alarmRecords1.forEach(alarmRecords2 -> { // 每个小时推送两次 if (alarmRecords2.getSendNum() <= 3) { // 查询需要推送的微信用户 List> wechatUserList = wechatService.queryWechatUser(0); if (null != wechatUserList && !wechatUserList.isEmpty()) { // 开始推送数据 for (Map map : wechatUserList) { PushMsgEntity pushMsgEntity = new PushMsgEntity(); try { // 判断模板id种类 switch (map.get("template_id").toString()) { case "fqAXCFXSBCqHLJjBLIjD-Wr_dN8RLsTcsatUQa3Ktx4": // 设备异常告警提醒 pushDeviceExceptionParams(map, pushMsgEntity, alarmRecords2); break; case "SiyBtZeZuF0Qo8V3NlvGwhc95-vX-a6wsvIxpAq3d_Y": // 设备告警通知 pushDeviceArmParams(map, pushMsgEntity, alarmRecords2); break; default: break; } } catch (Exception e) { throw new RuntimeException(e); } if (!pushMsgEntity.getTouser().isEmpty()) { String result = wechatService.pushMsg(pushMsgEntity); if (Constants.SUCCESS.equals(result)) { // 更新数据:已发送通知 alarmRecordsService.updateAlarmRecordById(alarmRecords2.getId(), 1); } } } } } }); } private void pushDeviceArmParams(Map map, PushMsgEntity pushMsgEntity, AlarmRecords alarmRecords2) { pushMsgEntity.setTouser(map.get("open_id").toString()); pushMsgEntity.setTemplateId(map.get("template_id").toString()); // pushMsgEntity.setUrl("http://jnd2.mhwsh.net:8765/"); First first = new First(); first.setValue("设备告警通知"); pushMsgEntity.setFirst(first); // 项目名称 Key1 key1 = new Key1(); key1.setValue(sysParamsService.queryList().get(0).getProName()); pushMsgEntity.setKey1(key1); // 设备名称 Key2 key2 = new Key2(); key2.setValue(alarmRecords2.getDeviceName()); pushMsgEntity.setKey2(key2); // 告警等级 Key3 key3 = new Key3(); // 通过告警等级id得到value值 String alarmLevel = alarmRecords2.getAlarmLevel(); String alarmLevel1 = DictUtils.getDictLabel("alarm_level", alarmLevel); key3.setValue(alarmLevel1); pushMsgEntity.setKey3(key3); // 异常原因 Key4 key4 = new Key4(); String content = alarmRecords2.getContent(); key4.setValue(content.length() > 20 ? content.substring(0, 20) : content); pushMsgEntity.setKey4(key4); // 发生时间 Key5 key5 = new Key5(); key5.setValue(DateUtils.dateToString(alarmRecords2.getCreateTime(), "yyyy-MM-dd HH:mm:ss")); pushMsgEntity.setKey5(key5); } /** * 推送设备异常告警提醒 * * @param map * @param pushMsgEntity * @param alarmRecords2 */ private void pushDeviceExceptionParams(Map map, PushMsgEntity pushMsgEntity, AlarmRecords alarmRecords2) { pushMsgEntity.setTouser(map.get("open_id").toString()); pushMsgEntity.setTemplateId(map.get("template_id").toString()); // pushMsgEntity.setUrl("http://jnd2.mhwsh.net:8765/"); First first = new First(); first.setValue("设备异常告警提醒"); pushMsgEntity.setFirst(first); Key1 key1 = new Key1(); key1.setValue(alarmRecords2.getDeviceName()); pushMsgEntity.setKey1(key1); Key2 key2 = new Key2(); // 通过告警等级id得到value值 String alarmLevel = alarmRecords2.getAlarmLevel(); String alarmLevel1 = DictUtils.getDictLabel("alarm_level", alarmLevel); key2.setValue(alarmLevel1); pushMsgEntity.setKey2(key2); Key3 key3 = new Key3(); // 通过告警类型id得到value值 String alarmType = alarmRecords2.getAlarmType(); String alarmType1 = DictUtils.getDictLabel("alarm_type", alarmType); key3.setValue(alarmType1); pushMsgEntity.setKey3(key3); Key4 key4 = new Key4(); String content = alarmRecords2.getContent(); key4.setValue(content.length() > 20 ? content.substring(0, 20) : content); pushMsgEntity.setKey4(key4); Key5 key5 = new Key5(); key5.setValue(DateUtils.dateToString(alarmRecords2.getCreateTime(), "yyyy-MM-dd HH:mm:ss")); pushMsgEntity.setKey5(key5); } }