PlanTask.java 3.1 KB

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