PlanTask.java 4.2 KB

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