Notice.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package com.fjhx.oa.utils;
  2. import cn.hutool.core.util.ObjectUtil;
  3. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  4. import com.fjhx.common.enums.PushBusinessTypeEnum;
  5. import com.fjhx.oa.entity.schedule.po.ScheduleDetails;
  6. import com.fjhx.oa.entity.schedule.po.ScheduleInfo;
  7. import com.fjhx.oa.service.schedule.ScheduleDetailsService;
  8. import com.fjhx.oa.service.schedule.ScheduleInfoService;
  9. import com.fjhx.socket.core.PushTypeEnum;
  10. import com.fjhx.socket.core.WebSocketPush;
  11. import com.ruoyi.framework.mybatis.holder.TenantHolder;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.context.annotation.Configuration;
  14. import org.springframework.scheduling.annotation.EnableScheduling;
  15. import org.springframework.scheduling.annotation.Scheduled;
  16. import javax.annotation.PostConstruct;
  17. import java.text.SimpleDateFormat;
  18. import java.util.*;
  19. import java.util.concurrent.ConcurrentHashMap;
  20. import java.util.stream.Collectors;
  21. /**
  22. * 工作日程消息通知
  23. */
  24. @Configuration
  25. @EnableScheduling
  26. public class Notice {
  27. private static Map<Long, ScheduleInfo> scheduleInfoMap = new ConcurrentHashMap<>();
  28. private static SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
  29. @Autowired
  30. ScheduleInfoService scheduleInfoService;
  31. @Autowired
  32. ScheduleDetailsService scheduleDetailsService;
  33. /**
  34. * 启动时和每天0点 查询当天的日程
  35. */
  36. @PostConstruct
  37. @Scheduled(cron = "0 0 0 * * ?")
  38. public void getNoticeList() {
  39. TenantHolder.setIgnore(true);
  40. QueryWrapper queryWrapper = new QueryWrapper();
  41. queryWrapper.eq("(date( start_date ) = curdate())", 1);
  42. List<ScheduleInfo> scheduleInfos0 = scheduleInfoService.list(queryWrapper);
  43. scheduleInfoMap = scheduleInfos0.stream().collect(Collectors.groupingBy(ScheduleInfo::getId,
  44. Collectors.collectingAndThen(Collectors.toList(), value -> value.get(0))));
  45. TenantHolder.clear();
  46. }
  47. /**
  48. * 每10秒检查一次
  49. */
  50. @Scheduled(cron = "0/10 * * * * ?")
  51. public void sendNoticeTime() {
  52. TenantHolder.setIgnore(true);
  53. List<ScheduleInfo> scheduleInfos = new ArrayList<>();
  54. // for (Map.Entry<Long, ScheduleInfo> entry : scheduleInfoMap.entrySet()) {
  55. Iterator<Map.Entry<Long, ScheduleInfo>> it = scheduleInfoMap.entrySet().iterator();
  56. while(it.hasNext()){
  57. Map.Entry<Long, ScheduleInfo> entry = it.next();
  58. ScheduleInfo scheduleInfo = entry.getValue();
  59. Date startDate = scheduleInfo.getStartDate();
  60. Date date = new Date();
  61. long diff = startDate.getTime() - date.getTime();
  62. long min = (long) Math.ceil(diff / 60.0 / 1000.0);
  63. if (min < 0) {
  64. // scheduleInfoMap.remove(scheduleInfo.getId());
  65. it.remove();
  66. }
  67. //根据类型判断提前多少分钟通知
  68. if (min == scheduleInfo.getNoticeType()) {
  69. // scheduleInfoMap.remove(scheduleInfo.getId());
  70. it.remove();
  71. scheduleInfos.add(scheduleInfo);
  72. }
  73. }
  74. //查询参与者并发送通知
  75. if (ObjectUtil.isNotEmpty(scheduleInfos)) {
  76. Map<Long, ScheduleInfo> scheduleInfoMap1 = scheduleInfos.stream().collect(Collectors.groupingBy(ScheduleInfo::getId,
  77. Collectors.collectingAndThen(Collectors.toList(), value -> value.get(0))));
  78. List<Long> scheduleInfoIds = scheduleInfos.stream().map(ScheduleInfo::getId).collect(Collectors.toList());
  79. List<ScheduleDetails> scheduleDetailsList = scheduleDetailsService.list(q -> q.in(ScheduleDetails::getScheduleInfoId, scheduleInfoIds));
  80. for (ScheduleDetails scheduleDetails : scheduleDetailsList) {
  81. // 发送消息
  82. ScheduleInfo scheduleInfo = scheduleInfoMap1.get(scheduleDetails.getScheduleInfoId());
  83. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  84. String msg = String.format("您的日程<%s>将于%s开始。", scheduleInfo.getTitle(), sdf.format(scheduleInfo.getStartDate()));
  85. Map<String, Object> msgMap = new HashMap<>();
  86. msgMap.put("businessId", scheduleInfo.getId());
  87. msgMap.put("msg", msg);
  88. // WebSocketServer.sendInfo(scheduleDetails.getParticipantId(), 1, msgMap);
  89. WebSocketPush.byUser(
  90. PushTypeEnum.PUSH_NOTIFICATION,
  91. scheduleDetails.getParticipantId(),
  92. msg,
  93. PushBusinessTypeEnum.SCHEDULE_REMINDER.getType(),
  94. msgMap
  95. );
  96. }
  97. }
  98. TenantHolder.clear();
  99. }
  100. /**
  101. * 判断日期日程的开始时间是否是今天,如果是就添加到待发送消息队列
  102. */
  103. public static void updateNoticeList(ScheduleInfo scheduleInfo) {
  104. Date now = new Date();
  105. //获取今天的日期
  106. String nowDay = sf.format(now);
  107. //对比的时间
  108. String day = sf.format(scheduleInfo.getStartDate());
  109. if (day.equals(nowDay)) {
  110. scheduleInfoMap.put(scheduleInfo.getId(), scheduleInfo);
  111. } else {
  112. scheduleInfoMap.remove(scheduleInfo.getId());
  113. }
  114. }
  115. /**
  116. * 删除日程的通知计划
  117. */
  118. public static void deleteNoticeList(Long id) {
  119. scheduleInfoMap.remove(id);
  120. }
  121. }