AlarmTask.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package com.jpsoft.smart.schduled;
  2. import com.jpsoft.smart.config.WxConfig;
  3. import com.jpsoft.smart.modules.base.entity.*;
  4. import com.jpsoft.smart.modules.base.service.AlarmConfigService;
  5. import com.jpsoft.smart.modules.base.service.CompanyInfoService;
  6. import com.jpsoft.smart.modules.base.service.PersonDeviceFilterLogService;
  7. import com.jpsoft.smart.modules.base.service.WarningPusherService;
  8. import com.jpsoft.smart.modules.common.utils.WechatMessageUtil;
  9. import com.jpsoft.smart.modules.common.utils.WeixinUtil;
  10. import lombok.extern.slf4j.Slf4j;
  11. import org.apache.commons.lang3.StringUtils;
  12. import org.joda.time.DateTime;
  13. import org.joda.time.Minutes;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.scheduling.annotation.Scheduled;
  16. import org.springframework.stereotype.Component;
  17. import java.text.SimpleDateFormat;
  18. import java.util.List;
  19. @Component
  20. @Slf4j
  21. public class AlarmTask {
  22. @Autowired
  23. private AlarmConfigService alarmConfigService;
  24. @Autowired
  25. private WarningPusherService warningPusherService;
  26. @Autowired
  27. private PersonDeviceFilterLogService personDeviceFilterLogService;
  28. @Autowired
  29. private CompanyInfoService companyInfoService;
  30. @Autowired
  31. private WxConfig wxConfig;
  32. //每5分钟执行一次
  33. @Scheduled(cron="0 0/5 * * * ?")
  34. public void run() {
  35. int intervalMinute = 5;
  36. List<AlarmConfig> configList = alarmConfigService.list();
  37. DateTime now = DateTime.now();
  38. String date = now.toString("yyyy-MM-dd");
  39. int weekday = now.getDayOfWeek();
  40. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
  41. for (AlarmConfig alarmConfig : configList) {
  42. try {
  43. String weekdays = alarmConfig.getWeekdays();
  44. DateTime startTime = new DateTime(sdf.parse(date + " " + alarmConfig.getStartTime()));
  45. DateTime endTime = new DateTime(sdf.parse(date + " " + alarmConfig.getEndTime()));
  46. if (weekdays.indexOf(String.valueOf(weekday)) != -1) {
  47. if (now.compareTo(endTime)>=0) {
  48. int minutes = Minutes.minutesBetween(endTime,now).getMinutes();
  49. if (minutes<=intervalMinute){
  50. //todo 进行通知
  51. notice(alarmConfig.getCompanyId(),startTime,endTime);
  52. break;
  53. }
  54. }
  55. }
  56. }
  57. catch (Exception ex){
  58. log.error(ex.getMessage(),ex);
  59. }
  60. }
  61. }
  62. private void notice(String companyId, DateTime startTime, DateTime endTime) {
  63. CompanyInfo companyInfo = companyInfoService.get(companyId);
  64. //todo 查询该单位当前时间段是否有人员未测体温
  65. List<PersonInfo> unmeasuredList = personDeviceFilterLogService.queryUnmeasuredList(companyId,startTime.toDate(),endTime.toDate());
  66. if (unmeasuredList.size()==0){
  67. return;
  68. }
  69. StringBuilder sb = new StringBuilder();
  70. int total = 0;
  71. //todo 给未测量人推送微信通知
  72. for (PersonInfo personInfo : unmeasuredList) {
  73. if (StringUtils.isNotEmpty(personInfo.getOpenId())) {
  74. String message = "尊敬的用户:" + personInfo.getName()
  75. + ",您未在" + startTime.toString("HH:mm") + "至" + endTime.toString("HH:mm") + "测量体温,请及时补测!";
  76. if (sb.length() != 0) {
  77. sb.append(",");
  78. }
  79. sb.append(personInfo.getName());
  80. WechatMessageUtil.sendUnmeasureAlarmInfo(personInfo.getOpenId(), companyInfo.getName(), message, wxConfig.getAppId(), wxConfig.getAppSecret());
  81. }
  82. total++;
  83. }
  84. //todo 查询该单位的通知人
  85. List<WarningPusher> pusherList = warningPusherService.findByCompanyId(companyId);
  86. //todo 给单位相关人员推送通知
  87. for (WarningPusher pusher: pusherList) {
  88. if (StringUtils.isNotEmpty(pusher.getOpenId())) {
  89. String message = "尊敬的用户:" + pusher.getName()
  90. + ",当天时段" + startTime.toString("HH:mm") + "至" + endTime.toString("HH:mm")
  91. + ",有" + total + "人未测量体温。名单如下:" + sb.toString();
  92. WechatMessageUtil.sendUnmeasureAlarmInfo(
  93. pusher.getOpenId(),
  94. companyInfo.getName(),
  95. message, wxConfig.getAppId(), wxConfig.getAppSecret());
  96. }
  97. }
  98. }
  99. }