PlanTask.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package com.ruoyi.web.controller.task;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.date.DateUtil;
  4. import cn.hutool.core.util.StrUtil;
  5. import com.ruoyi.common.core.redis.RedisCache;
  6. import com.ruoyi.common.core.text.Convert;
  7. import com.ruoyi.qdtl.domain.PlanUser;
  8. import com.ruoyi.qdtl.domain.TlInspectionLocation;
  9. import com.ruoyi.qdtl.domain.TlInspectionPlan;
  10. import com.ruoyi.qdtl.domain.TlPlanRecord;
  11. import com.ruoyi.qdtl.service.ITlInspectionLocationService;
  12. import com.ruoyi.qdtl.service.ITlInspectionPlanService;
  13. import com.ruoyi.qdtl.service.ITlPlanRecordService;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.stereotype.Component;
  16. import java.util.ArrayList;
  17. import java.util.Date;
  18. import java.util.List;
  19. import java.util.concurrent.TimeUnit;
  20. /**
  21. * @Description: 巡检计划任务
  22. * @Author: huangcheng
  23. * @Date: 2022/3/30
  24. * @Version V1.0
  25. */
  26. @Component("planTask")
  27. public class PlanTask {
  28. @Autowired
  29. private ITlInspectionPlanService planService;
  30. @Autowired
  31. private ITlPlanRecordService planRecordService;
  32. @Autowired
  33. private ITlInspectionLocationService locationService;
  34. @Autowired
  35. private RedisCache redisCache;
  36. /**
  37. * 根据巡检计划生成下一日巡检计划数据
  38. * params参数:如果传了参数,就根据参数的日期来生成计划(为了测试方便)
  39. */
  40. public void createPlan(String params) {
  41. // 查询符合条件的计划。根据当前时间点,如果是晚20点~24点之间,就去生成第二天的计划,如果是凌晨0点~1点,就生成当天的计划
  42. // 生成过的计划,标记已生成放到redis中
  43. String queryDate;
  44. if (StrUtil.isNotBlank(params)) {
  45. queryDate = params;
  46. } else {
  47. int hour = DateUtil.hour(new Date(), true);
  48. if (20 <= hour && hour <= 24) {
  49. queryDate = DateUtil.formatDate(DateUtil.tomorrow());
  50. } else if (0 <= hour && hour <= 1) {
  51. queryDate = DateUtil.today();
  52. } else {
  53. return;
  54. }
  55. }
  56. Boolean recordFlag = redisCache.getCacheObject("tl:planrecord:" + queryDate);
  57. if (recordFlag != null && recordFlag) {
  58. return;
  59. }
  60. List<TlInspectionPlan> planList = planService.queryPlanByDate(queryDate);
  61. if (planList != null && planList.size() > 0) {
  62. List<TlPlanRecord> planRecords = new ArrayList<>();
  63. for (TlInspectionPlan inspectionPlan : planList) {
  64. // 查询是否满足日期的要求
  65. int week = DateUtil.dayOfWeek(DateUtil.parseDate(queryDate));
  66. if (!CollUtil.contains(StrUtil.split(inspectionPlan.getWeeks(), ","), Convert.toStr(week))) {
  67. continue;
  68. }
  69. // 查询计划的人员.根据计划id查询人员以及设备号
  70. List<PlanUser> userList = planService.queryPlanUser(inspectionPlan.getId());
  71. // 查询计划的线路及点位
  72. List<TlInspectionLocation> lineLocations = locationService.queryLocationByLineId(inspectionPlan.getLineId());
  73. if (userList != null && userList.size() > 0) {
  74. // 组装计划数据
  75. for (PlanUser planUser : userList) {
  76. for (TlInspectionLocation lineLocation : lineLocations) {
  77. for (String s : StrUtil.split(inspectionPlan.getTimeUnit(), ",")) {
  78. for (int i = 0; i < inspectionPlan.getXunNum(); i++) {
  79. TlPlanRecord planRecord = new TlPlanRecord();
  80. planRecord.setPlanId(planUser.getPlanId());
  81. planRecord.setPlanDate(queryDate);
  82. planRecord.setStartTime(s.split("~")[0] + ":00");
  83. planRecord.setEndTime(s.split("~")[1] + ":59");
  84. planRecord.setPlanName(inspectionPlan.getPlanName());
  85. planRecord.setUserId(planUser.getUserId());
  86. planRecord.setNickName(planUser.getNickName());
  87. planRecord.setCard(planUser.getCard());
  88. planRecord.setScore(inspectionPlan.getScore());
  89. planRecord.setCheckpointCard(lineLocation.getLocationCode());
  90. planRecord.setLineId(inspectionPlan.getLineId());
  91. planRecord.setLineName("");
  92. planRecord.setAreaId(lineLocation.getAreaId());
  93. planRecords.add(planRecord);
  94. }
  95. }
  96. }
  97. }
  98. }
  99. }
  100. if (planRecords.size() > 0) {
  101. planRecordService.batchInsert(planRecords);
  102. redisCache.setCacheObject("tl:planrecord:" + queryDate, true, 2, TimeUnit.DAYS);
  103. }
  104. }
  105. }
  106. }