package com.jpsoft.smart.schduled; import com.jpsoft.smart.config.WxConfig; import com.jpsoft.smart.modules.base.entity.*; import com.jpsoft.smart.modules.base.service.AlarmConfigService; import com.jpsoft.smart.modules.base.service.CompanyInfoService; import com.jpsoft.smart.modules.base.service.PersonDeviceFilterLogService; import com.jpsoft.smart.modules.base.service.WarningPusherService; import com.jpsoft.smart.modules.common.utils.WechatMessageUtil; import com.jpsoft.smart.modules.common.utils.WeixinUtil; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.joda.time.DateTime; import org.joda.time.Minutes; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.text.SimpleDateFormat; import java.util.List; @Component @Slf4j public class AlarmTask { @Autowired private AlarmConfigService alarmConfigService; @Autowired private WarningPusherService warningPusherService; @Autowired private PersonDeviceFilterLogService personDeviceFilterLogService; @Autowired private CompanyInfoService companyInfoService; @Autowired private WxConfig wxConfig; //每5分钟执行一次 @Scheduled(cron="0 0/5 * * * ?") public void run() { int intervalMinute = 5; List configList = alarmConfigService.list(); DateTime now = DateTime.now(); String date = now.toString("yyyy-MM-dd"); int weekday = now.getDayOfWeek(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm"); for (AlarmConfig alarmConfig : configList) { try { String weekdays = alarmConfig.getWeekdays(); DateTime startTime = new DateTime(sdf.parse(date + " " + alarmConfig.getStartTime())); DateTime endTime = new DateTime(sdf.parse(date + " " + alarmConfig.getEndTime())); if (weekdays.indexOf(String.valueOf(weekday)) != -1) { if (now.compareTo(endTime)>=0) { int minutes = Minutes.minutesBetween(endTime,now).getMinutes(); if (minutes<=intervalMinute){ //todo 进行通知 notice(alarmConfig.getCompanyId(),startTime,endTime); break; } } } } catch (Exception ex){ log.error(ex.getMessage(),ex); } } } private void notice(String companyId, DateTime startTime, DateTime endTime) { CompanyInfo companyInfo = companyInfoService.get(companyId); //todo 查询该单位当前时间段是否有人员未测体温 List unmeasuredList = personDeviceFilterLogService.queryUnmeasuredList(companyId,startTime.toDate(),endTime.toDate()); if (unmeasuredList.size()==0){ return; } StringBuilder sb = new StringBuilder(); int total = 0; //todo 给未测量人推送微信通知 for (PersonInfo personInfo : unmeasuredList) { if (StringUtils.isNotEmpty(personInfo.getOpenId())) { String message = "尊敬的用户:" + personInfo.getName() + ",您未在" + startTime.toString("HH:mm") + "至" + endTime.toString("HH:mm") + "测量体温,请及时补测!"; if (sb.length() != 0) { sb.append(","); } sb.append(personInfo.getName()); WechatMessageUtil.sendUnmeasureAlarmInfo(personInfo.getOpenId(), companyInfo.getName(), message, wxConfig.getAppId(), wxConfig.getAppSecret()); } total++; } //todo 查询该单位的通知人 List pusherList = warningPusherService.findByCompanyId(companyId); //todo 给单位相关人员推送通知 for (WarningPusher pusher: pusherList) { if (StringUtils.isNotEmpty(pusher.getOpenId())) { String message = "尊敬的用户:" + pusher.getName() + ",当天时段" + startTime.toString("HH:mm") + "至" + endTime.toString("HH:mm") + ",有" + total + "人未测量体温。名单如下:" + sb.toString(); WechatMessageUtil.sendUnmeasureAlarmInfo( pusher.getOpenId(), companyInfo.getName(), message, wxConfig.getAppId(), wxConfig.getAppSecret()); } } } }