Explorar o código

储存超期报警-定时任务

hanqingsong hai 1 ano
pai
achega
457621c108

+ 10 - 12
src/main/java/com/chinaitop/depot/tasks/TempWetTask.java

@@ -1,17 +1,13 @@
1 1
 package com.chinaitop.depot.tasks;
2 2
 
3
-import com.chinaitop.depot.tempWarn.model.TTempWetThreshold;
3
+import com.chinaitop.depot.tempWarn.service.StorageThresholdAlarmService;
4 4
 import com.chinaitop.depot.tempWarn.service.TTempWetAlarmService;
5 5
 import com.chinaitop.depot.tempWarn.service.TTempWetThresholdService;
6 6
 import lombok.extern.slf4j.Slf4j;
7 7
 import org.springframework.scheduling.annotation.Scheduled;
8 8
 import org.springframework.stereotype.Component;
9
-import org.springframework.util.CollectionUtils;
10 9
 
11 10
 import javax.annotation.Resource;
12
-import java.util.Calendar;
13
-import java.util.List;
14
-import java.util.UUID;
15 11
 
16 12
 /**
17 13
  * @author qingsong.han
@@ -25,13 +21,15 @@ public class TempWetTask {
25 21
     private TTempWetThresholdService tempWetThresholdService;
26 22
     @Resource
27 23
     private TTempWetAlarmService tempWetAlarmService;
24
+    @Resource
25
+    private StorageThresholdAlarmService thresholdAlarmService;
28 26
 
29 27
     /**
30 28
      * 每年12月1日进行增一条下一年的设置数据,默认值与上一年保持一致
31 29
      */
32 30
     @Scheduled(cron = "0 0 0 1 12 ?") // 每年12月1号0点0分0秒触发任务
33 31
     public void tempWetYear() {
34
-        log.info("----每年12月1号0点0分0秒触发任务---");
32
+        log.info("----每年12月1号0点0分0秒触发任务----");
35 33
         tempWetThresholdService.saveNextYear();
36 34
     }
37 35
 
@@ -41,18 +39,18 @@ public class TempWetTask {
41 39
 //    @Scheduled(cron = "0 50 16 * * ?") // 测试
42 40
     @Scheduled(cron = "0 0 12 * * ?") // 每天中午12点触发任务
43 41
     public void tempWetWarning() {
44
-        log.info("----每天中午12点触发任务---");
42
+        log.info("----每天中午12点触发任务----");
45 43
         tempWetAlarmService.saveTempWetWarning();
46 44
     }
47 45
 
48 46
     /**
49 47
      * 每日中午12点,定时任务全平台进行算法计算
50 48
      */
51
-//    @Scheduled(cron = "0 50 16 * * ?") // 测试
52
-    @Scheduled(cron = "0 0 12 * * ?") // 每天中午12点触发任务
53
-    public void tempWetWarning1() {
54
-        log.info("----每天中午12点触发任务---");
55
-        tempWetAlarmService.saveTempWetWarning();
49
+    @Scheduled(cron = "0 0 0 11 * *") // 测试
50
+//    @Scheduled(cron = "0 0 0 1 * *") // 每月1号0点执行一次
51
+    public void saveAgeWaring() {
52
+        log.info("----每月1号0点执行一次----");
53
+        thresholdAlarmService.saveAgeWaring();
56 54
     }
57 55
 
58 56
 }

+ 9 - 0
src/main/java/com/chinaitop/depot/tempWarn/controller/StorageThresholdAlarmController.java

@@ -37,4 +37,13 @@ public class StorageThresholdAlarmController {
37 37
             return ResponseEntity.failed("查询失败");
38 38
         }
39 39
     }
40
+
41
+    /**
42
+     * 测试过滤保存超期数据接口
43
+     */
44
+    /*@GetMapping("/saveAgeWaring")
45
+    @ApiOperation(value = "test", notes = "")
46
+    public void saveAgeWaring() {
47
+        thresholdAlarmService.saveAgeWaring();
48
+    }*/
40 49
 }

+ 5 - 0
src/main/java/com/chinaitop/depot/tempWarn/mapper/StorageThresholdSetMapper.java

@@ -1,5 +1,6 @@
1 1
 package com.chinaitop.depot.tempWarn.mapper;
2 2
 
3
+import com.chinaitop.depot.tempWarn.model.StorageThresholdAlarm;
3 4
 import com.chinaitop.depot.tempWarn.model.StorageThresholdSet;
4 5
 import com.chinaitop.depot.tempWarn.model.StorageThresholdSetExample;
5 6
 import java.util.List;
@@ -31,4 +32,8 @@ public interface StorageThresholdSetMapper {
31 32
     int updateByPrimaryKey(StorageThresholdSet record);
32 33
 
33 34
     List<StorageThresholdSet> selectByExampleAndKqbm(String kqbm);
35
+
36
+    List<StorageThresholdAlarm> selectGrainCardList();
37
+
38
+    List<StorageThresholdSet> selectAll();
34 39
 }

+ 32 - 0
src/main/java/com/chinaitop/depot/tempWarn/mapper/StorageThresholdSetMapper.xml

@@ -368,4 +368,36 @@
368 368
     left join basic_enum be on ts.lypz = be.gbcode
369 369
     where ts.kqdm = #{kqbm}
370 370
   </select>
371
+  <select id="selectGrainCardList" parameterType="java.lang.Object" resultType="com.chinaitop.depot.tempWarn.model.StorageThresholdAlarm">
372
+    select
373
+        gc.org_id orgId,
374
+        gc.storehouse_id houseId,
375
+        sq.quality hwxz,
376
+        sq.sub_type pz,
377
+        sq.number_update/1000 kcsl,
378
+        gc.scnd scnf,
379
+        be.enumname lypzmc
380
+    from
381
+        storage_grain_card gc
382
+    left join storage_qualitycheck sq on gc.qualitycheck_id = sq.id
383
+    left join basic_enum be on be.enumid = sq.sub_type
384
+    where
385
+        sfgd = '1'
386
+  </select>
387
+  <select id="selectAll" parameterType="java.lang.Object" resultType="com.chinaitop.depot.tempWarn.model.StorageThresholdSet">
388
+    select
389
+        ts.id,
390
+        ts.kqdm,
391
+        ts.lypz,
392
+        ts.mxpz,
393
+        ts.ccnx,
394
+        ts.knccnx,
395
+        ts.sptccnx,
396
+        be.enumname lypzmc,
397
+        oi.org_id orgId
398
+    from
399
+        storage_threshold_set ts
400
+    left join basic_enum be on ts.lypz = be.gbcode
401
+    left join org_info oi on oi.kqbm = ts.kqdm
402
+  </select>
371 403
 </mapper>

+ 14 - 10
src/main/java/com/chinaitop/depot/tempWarn/model/StorageThresholdAlarm.java

@@ -8,25 +8,29 @@ import java.util.Date;
8 8
 public class StorageThresholdAlarm {
9 9
     private String id;
10 10
 
11
+    // 仓房id(basic_storehouse.storehouse_id)
11 12
     private Integer houseId;
12
-
13
+    // 性质id(basic_enum.enumid)
13 14
     private Integer hwxz;
14
-
15
+    // 品种id(basic_enum.enumid)
15 16
     private String pz;
16
-
17
+    // 库存数量(吨)
17 18
     private String kcsl;
18
-
19
+    // 生产年份
19 20
     private String scnf;
20
-
21
+    // 储备年限(年)
21 22
     private Integer knccnx;
22
-
23
+    // 储存超限时间
23 24
     private Date cccxsj;
24
-
25
+    // 报警信息
25 26
     private String bjxx;
26
-
27
+    // 报警时间
27 28
     private Date bjsj;
28
-
29
+    // 机构id(org_info.org_id)
29 30
     private Integer orgId;
30
-
31
+    // 0:市平台预警数据,1:库平台预警数据
31 32
     private Integer dataFlag;
33
+
34
+    // 业务数据
35
+    private String lypzmc;// 品种名称
32 36
 }

+ 6 - 0
src/main/java/com/chinaitop/depot/tempWarn/model/StorageThresholdAlarmExample.java

@@ -826,6 +826,12 @@ public class StorageThresholdAlarmExample {
826 826
             addCriterion("org_id not between", value1, value2, "orgId");
827 827
             return (Criteria) this;
828 828
         }
829
+
830
+        // 后加
831
+        public Criteria andDataFlagEqualTo(Integer value) {
832
+            addCriterion("data_flag =", value, "dataFlag");
833
+            return (Criteria) this;
834
+        }
829 835
     }
830 836
 
831 837
     /**

+ 1 - 0
src/main/java/com/chinaitop/depot/tempWarn/model/StorageThresholdSet.java

@@ -37,4 +37,5 @@ public class StorageThresholdSet {
37 37
 
38 38
     // 业务数据
39 39
     private String lypzmc;// 品种名称
40
+    private Integer orgId;// 机构id
40 41
 }

+ 2 - 0
src/main/java/com/chinaitop/depot/tempWarn/service/StorageThresholdAlarmService.java

@@ -12,4 +12,6 @@ import java.util.List;
12 12
  */
13 13
 public interface StorageThresholdAlarmService {
14 14
     List<StorageThresholdAlarm> getPageList(StorageThresholdAlarmPageParam pageParam);
15
+
16
+    void saveAgeWaring();
15 17
 }

+ 107 - 0
src/main/java/com/chinaitop/depot/tempWarn/service/impl/StorageThresholdAlarmServiceImpl.java

@@ -1,34 +1,141 @@
1 1
 package com.chinaitop.depot.tempWarn.service.impl;
2 2
 
3 3
 import com.chinaitop.depot.tempWarn.mapper.StorageThresholdAlarmMapper;
4
+import com.chinaitop.depot.tempWarn.mapper.StorageThresholdSetMapper;
4 5
 import com.chinaitop.depot.tempWarn.model.StorageThresholdAlarm;
5 6
 import com.chinaitop.depot.tempWarn.model.StorageThresholdAlarmExample;
7
+import com.chinaitop.depot.tempWarn.model.StorageThresholdSet;
8
+import com.chinaitop.depot.tempWarn.model.StorageThresholdSetExample;
6 9
 import com.chinaitop.depot.tempWarn.param.StorageThresholdAlarmPageParam;
7 10
 import com.chinaitop.depot.tempWarn.service.StorageThresholdAlarmService;
11
+import com.chinaitop.depot.utils.DateUtil;
12
+import lombok.extern.slf4j.Slf4j;
8 13
 import org.springframework.stereotype.Service;
14
+import org.springframework.util.CollectionUtils;
9 15
 import org.springframework.util.ObjectUtils;
10 16
 
11 17
 import javax.annotation.Resource;
18
+import java.time.LocalDate;
19
+import java.time.ZoneId;
20
+import java.util.Date;
12 21
 import java.util.List;
22
+import java.util.UUID;
13 23
 
14 24
 /**
15 25
  * @author qingsong.han
16 26
  * @description:
17 27
  * @create 2024-12-09 17:08
18 28
  */
29
+@Slf4j
19 30
 @Service
20 31
 public class StorageThresholdAlarmServiceImpl implements StorageThresholdAlarmService {
21 32
     @Resource
22 33
     private StorageThresholdAlarmMapper thresholdAlarmMapper;
34
+    @Resource
35
+    private StorageThresholdSetMapper thresholdSetMapper;
23 36
 
24 37
     @Override
25 38
     public List<StorageThresholdAlarm> getPageList(StorageThresholdAlarmPageParam pageParam) {
26 39
         StorageThresholdAlarmExample example = new StorageThresholdAlarmExample();
27 40
         StorageThresholdAlarmExample.Criteria criteria = example.createCriteria();
28 41
         criteria.andOrgIdEqualTo(pageParam.getOrgId());
42
+        criteria.andDataFlagEqualTo(1);
29 43
         if (!ObjectUtils.isEmpty(pageParam.getHouseId())) {
30 44
             criteria.andHouseIdEqualTo(pageParam.getHouseId());
31 45
         }
32 46
         return thresholdAlarmMapper.selectByExample(example);
33 47
     }
48
+
49
+    @Override
50
+    public void saveAgeWaring() {
51
+        // 查询库内报警值
52
+        List<StorageThresholdSet> storageThresholdSets = thresholdSetMapper.selectAll();
53
+        if (!CollectionUtils.isEmpty(storageThresholdSets)) {
54
+            // 查询粮情卡数据
55
+            List<StorageThresholdAlarm> listCar = thresholdSetMapper.selectGrainCardList();
56
+            if (!CollectionUtils.isEmpty(listCar)) {
57
+                // 获取当前时间
58
+                LocalDate now = LocalDate.now();
59
+                String nowStr = now.toString();
60
+                log.info("nowStr: {}", nowStr);
61
+                // 转换为储存超限时间类型
62
+                Date nowDate = Date.from(now.atStartOfDay(ZoneId.systemDefault()).toInstant());
63
+                // 比对是否超限
64
+                storageThresholdSets.forEach(set -> {
65
+                    // 市平台设置
66
+                    Integer sptccnx = set.getSptccnx();
67
+                    // 库内设置
68
+                    Integer knccnx = set.getKnccnx();
69
+                    // 任意一个设置都执行过滤报警
70
+                    if (!ObjectUtils.isEmpty(knccnx) || !ObjectUtils.isEmpty(sptccnx)) {
71
+                        listCar.forEach(alarm -> {
72
+                            // 对比品种
73
+                            if (set.getLypzmc().equals(alarm.getLypzmc())) {
74
+                                // 生产年份
75
+                                String scnf = alarm.getScnf();
76
+                                if (set.getOrgId().equals(alarm.getOrgId())) {
77
+                                    if (!ObjectUtils.isEmpty(scnf)) {
78
+                                        // 市平台报警
79
+                                        if (!ObjectUtils.isEmpty(sptccnx)) {
80
+                                            // 生产年份+设置超限年份=已储年份
81
+                                            int sptccnxPK = Integer.parseInt(scnf) + sptccnx;// 到期
82
+                                            // 计算超期时间
83
+                                            String cccxsjStr = String.format("%s%s", sptccnxPK, "-01-01");
84
+                                            Date cccxsj = DateUtil.stringToData(cccxsjStr, "yyyy-MM-dd");
85
+                                            // 是否超限
86
+                                            log.info("市平台 cccxsjStr: {} nowStr: {}", cccxsjStr, nowStr);
87
+                                            if (nowStr.equals(cccxsjStr)) {
88
+                                                alarm.setId(UUID.randomUUID().toString());
89
+                                                alarm.setDataFlag(0);
90
+                                                alarm.setKnccnx(sptccnx);
91
+                                                alarm.setCccxsj(cccxsj);
92
+                                                alarm.setBjxx("已超过存储期限要求");
93
+                                                alarm.setBjsj(nowDate);
94
+                                                thresholdAlarmMapper.insertSelective(alarm);
95
+                                            }
96
+                                        }
97
+                                        // 库平台报警
98
+                                        if (!ObjectUtils.isEmpty(knccnx)) {
99
+                                            // 是否超限
100
+                                            int knccnxPK = Integer.parseInt(scnf) + knccnx;
101
+                                            String cccxsjStr = String.format("%s%s", knccnxPK, "-01-01");
102
+//                                            String cccxsjStr = String.format("%s%s", knccnxPK, "-12-10");// 测试
103
+                                            // 提前3个月报警
104
+                                            String localDate_3 = now.minusMonths(3).toString();
105
+                                            // 提前1个月报警
106
+                                            String localDate_1 = now.minusMonths(1).toString();
107
+                                            // 保存
108
+                                            alarm.setId(UUID.randomUUID().toString());
109
+                                            alarm.setDataFlag(1);
110
+                                            alarm.setKnccnx(knccnx);
111
+                                            alarm.setBjsj(nowDate);
112
+                                            log.info("库平台 cccxsjStr: {} nowStr: {}", cccxsjStr, nowStr);
113
+                                            if (nowStr.equals(cccxsjStr)) {
114
+                                                Date cccxsj = DateUtil.stringToData(cccxsjStr, "yyyy-MM-dd");
115
+                                                alarm.setCccxsj(cccxsj);
116
+                                                alarm.setBjxx("已超过存储期限要求");
117
+                                                thresholdAlarmMapper.insertSelective(alarm);
118
+                                            } else if (localDate_3.equals(cccxsjStr)) {
119
+                                                Date cccxsj = DateUtil.stringToData(localDate_3, "yyyy-MM-dd");
120
+                                                alarm.setCccxsj(cccxsj);
121
+                                                alarm.setBjxx("距离超过储存期限还有3个月");
122
+                                                thresholdAlarmMapper.insertSelective(alarm);
123
+                                            }  else if (localDate_1.equals(cccxsjStr)) {
124
+                                                Date cccxsj = DateUtil.stringToData(localDate_1, "yyyy-MM-dd");
125
+                                                alarm.setCccxsj(cccxsj);
126
+                                                alarm.setBjxx("距离超过储存期限还有1个月");
127
+                                                thresholdAlarmMapper.insertSelective(alarm);
128
+                                            }
129
+                                        }
130
+                                    }
131
+                                }
132
+                            }
133
+                        });
134
+                    }
135
+                });
136
+            }
137
+        }
138
+
139
+    }
140
+
34 141
 }