Browse Source

生产数据报警信息远程调用添加到基础库

yinhao 4 years ago
parent
commit
a7a0f02ee3

+ 58 - 0
huimv-ql-farm/huimv-ql-environment/src/main/java/com/huimv/environment/controller/MainTest.java

@@ -0,0 +1,58 @@
+package com.huimv.environment.controller;
+
+import com.alibaba.fastjson.JSONObject;
+import com.huimv.environment.entity.IndexParameter2Entity;
+import com.huimv.environment.entity.WarningParameter;
+import org.springframework.http.HttpEntity;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.MediaType;
+import org.springframework.web.client.RestTemplate;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+
+/**
+ * <p>
+ *
+ * </p>
+ *
+ * @author yinhao
+ * @date 2021/5/31 19:06
+ */
+public class MainTest {
+
+    public static void main(String[] args) {
+
+        RestTemplate restTemplate = new RestTemplate();
+
+        HttpHeaders headers = new HttpHeaders();
+        MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
+        headers.setContentType(type);
+        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
+        List<IndexParameter2Entity> list = new ArrayList<>();
+        List<IndexParameter2Entity> list2 = new ArrayList<>();
+        IndexParameter2Entity entity = new IndexParameter2Entity();
+        entity.setName("PSY1");
+        entity.setData("25.0");
+        list.add(entity);
+
+        IndexParameter2Entity entity2 = new IndexParameter2Entity();
+        entity2.setName("总存栏量1");
+        entity2.setData("10000");
+        list2.add(entity2);
+
+        HashMap<String, List<IndexParameter2Entity>> param = new HashMap<>();
+        param.put("saveList",list);
+        param.put("updateList",list2);
+
+//        WarningParameter warningParameter = new WarningParameter();
+//        warningParameter.setParameterName("PSY");
+//        warningParameter.setThresholdValue(25.0);
+//        list.add(warningParameter);
+
+//        String belowThresholdList = JSONObject.toJSONString(list);
+        HttpEntity httpEntity = new HttpEntity<>(param, headers);
+        restTemplate.postForObject("http://127.0.0.1:8085/productDataHandle/handleLocalWarningInfo", httpEntity, String.class);
+    }
+}

+ 118 - 0
huimv-ql-farm/huimv-ql-environment/src/main/java/com/huimv/environment/controller/ProductDataHandleController.java

@@ -0,0 +1,118 @@
+package com.huimv.environment.controller;
+
+import cn.hutool.core.collection.CollectionUtil;
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import com.huimv.environment.entity.IndexParameter2Entity;
+import com.huimv.environment.entity.IndexParameterEntity;
+import com.huimv.environment.entity.WarningInfoEntity;
+import com.huimv.environment.entity.WarningParameter;
+import com.huimv.environment.repo.WarningInfoEntityRepo;
+import com.huimv.environment.result.Result;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+import sun.misc.IOUtils;
+
+import javax.servlet.http.HttpServletRequest;
+import java.io.UnsupportedEncodingException;
+
+import java.util.*;
+
+/**
+ * <p>
+ *
+ * </p>
+ *
+ * @author yinhao
+ * @date 2021/5/31 18:40
+ */
+@RestController
+@RequestMapping("/productDataHandle")
+public class ProductDataHandleController {
+
+    @Autowired
+    private WarningInfoEntityRepo warningInfoEntityRepo;
+
+
+    @RequestMapping("/handleLocalWarningInfo")
+    @Transactional(rollbackFor = Throwable.class)
+    public Result handleLocalWarningInfo(@RequestBody HashMap<String,List<IndexParameter2Entity>> httpEntity) {
+        System.out.println(httpEntity);
+
+        List<IndexParameter2Entity> saveList = httpEntity.get("saveList");
+        List<IndexParameter2Entity> updateList = httpEntity.get("updateList");
+
+        Date date = new Date();
+
+        if (CollectionUtil.isNotEmpty(saveList)) {
+            for (IndexParameter2Entity entity : saveList) {
+                WarningInfoEntity warningInfoEntity = new WarningInfoEntity();
+                warningInfoEntity.setUploadTime(date);
+                warningInfoEntity.setWarningType(3);
+                String type = "本地数据的 " + entity.getName() + " 参数小于阈值!";
+                warningInfoEntity.setShowStatus(0);
+                warningInfoEntity.setType(type);
+                warningInfoEntityRepo.updatePreviousShowStatusByType(type);
+                warningInfoEntityRepo.save(warningInfoEntity);
+            }
+        }
+        if (CollectionUtil.isNotEmpty(updateList)) {
+            List<WarningInfoEntity> list = new ArrayList<>();
+            for (IndexParameter2Entity entity : updateList) {
+                List<WarningInfoEntity> warningInfoEntityList = warningInfoEntityRepo.findDataTypeLikeLeNow("本地数据的 " + entity.getName(), date);
+                list.addAll(warningInfoEntityList);
+            }
+            if (CollectionUtil.isNotEmpty(list)) {
+                for (WarningInfoEntity warningInfoEntity : list) {
+                    warningInfoEntity.setShowStatus(1);
+                    warningInfoEntityRepo.saveAndFlush(warningInfoEntity);
+                }
+            }
+        }
+        return new Result(1000,"请求成功");
+
+    }
+
+    @RequestMapping("/handleERPWarningInfo")
+    @Transactional(rollbackFor = Throwable.class)
+    public Result handleErpWarningInfo(@RequestBody HashMap<String,List<IndexParameterEntity>> httpEntity) {
+        System.out.println(httpEntity);
+
+        List<IndexParameterEntity> saveList = httpEntity.get("saveList");
+        List<IndexParameterEntity> updateList = httpEntity.get("updateList");
+
+        Date date = new Date();
+
+        if (CollectionUtil.isNotEmpty(saveList)) {
+            for (IndexParameterEntity entity : saveList) {
+                WarningInfoEntity warningInfoEntity = new WarningInfoEntity();
+                warningInfoEntity.setUploadTime(date);
+                warningInfoEntity.setWarningType(3);
+                warningInfoEntity.setShowStatus(0);
+                String type = "ERP数据的 " + entity.getName() + " 参数小于阈值!";
+                warningInfoEntity.setType(type);
+                warningInfoEntityRepo.updatePreviousShowStatusByType(type);
+                warningInfoEntityRepo.save(warningInfoEntity);
+            }
+        }
+        if (CollectionUtil.isNotEmpty(updateList)) {
+            List<WarningInfoEntity> list = new ArrayList<>();
+            for (IndexParameterEntity entity : updateList) {
+                List<WarningInfoEntity> warningInfoEntityList = warningInfoEntityRepo.findDataTypeLikeLeNow("ERP数据的 " + entity.getName(), date);
+                list.addAll(warningInfoEntityList);
+            }
+            if (CollectionUtil.isNotEmpty(list)) {
+                for (WarningInfoEntity warningInfoEntity : list) {
+                    warningInfoEntity.setShowStatus(1);
+                    warningInfoEntityRepo.saveAndFlush(warningInfoEntity);
+                }
+            }
+        }
+
+        return new Result(1000,"请求成功");
+
+    }
+}

+ 10 - 0
huimv-ql-farm/huimv-ql-environment/src/main/java/com/huimv/environment/repo/WarningInfoEntityRepo.java

@@ -20,4 +20,14 @@ public interface WarningInfoEntityRepo extends JpaRepository<WarningInfoEntity,
     @Query(nativeQuery = true,value = "update warning_info set show_status = 1 where warning_type = 1  and show_status = 0")
     void updateShowStatusByWarningType();
 
+
+    @Query(nativeQuery = true,value = "SELECT * FROM warning_info WHERE type like CONCAT('%',?1,'%') AND upload_time < ?2 AND warning_type = 3")
+    List<WarningInfoEntity> findDataTypeLikeLeNow(String name, Date date);
+
+    List<WarningInfoEntity> findAllByShowStatus(int showStatus);
+
+    @Modifying
+    @Query(nativeQuery = true,value = "UPDATE warning_info SET show_status = 1 WHERE type = ?1 AND show_status = 0")
+    void updatePreviousShowStatusByType(String type);
+
 }

+ 3 - 1
huimv-ql-farm/huimv-ql-environment/src/main/resources/application.properties

@@ -3,4 +3,6 @@ spring.profiles.active=ycg
 #spring.profiles.active=haiyan
 #spring.profiles.active=wjj
 minTemp=10.0
-maxTemp=30.0
+maxTemp=30.0
+
+logging.level.com.alibaba.druid.pool=error

+ 11 - 0
huimv-ql-farm/huimv-ql-production/src/main/java/com/huimv/production/HuimvProductionApplication.java

@@ -2,8 +2,14 @@ package com.huimv.production;
 
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.context.annotation.Bean;
 import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
+import org.springframework.web.client.RestTemplate;
 
+/**
+ * @author huimv
+ * @since 2021/5/31 18:21
+ */
 @SpringBootApplication
 @EnableJpaAuditing
 public class HuimvProductionApplication {
@@ -12,4 +18,9 @@ public class HuimvProductionApplication {
         SpringApplication.run(HuimvProductionApplication.class, args);
     }
 
+    @Bean
+    public RestTemplate restTemplate() {
+        return new RestTemplate();
+    }
+
 }

+ 108 - 72
huimv-ql-farm/huimv-ql-production/src/main/java/com/huimv/production/task/WarningParameterTask.java

@@ -1,16 +1,21 @@
 package com.huimv.production.task;
 
+import cn.hutool.core.collection.CollectionUtil;
+import cn.hutool.core.date.DateUtil;
+import com.alibaba.fastjson.JSONObject;
 import com.huimv.production.domain.*;
 import com.huimv.production.repo.*;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.annotation.Configuration;
+import org.springframework.http.HttpEntity;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.MediaType;
 import org.springframework.scheduling.annotation.EnableScheduling;
 import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.web.client.RestTemplate;
 
 import javax.transaction.Transactional;
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.List;
+import java.util.*;
 import java.util.stream.Collectors;
 
 /**
@@ -35,93 +40,124 @@ public class WarningParameterTask {
     @Autowired
     private IndexParameterEntityRepo indexParameterEntityRepo;
 
-    @Autowired
-    private WarningInfoEntityRepo warningInfoEntityRepo;
+//    @Autowired
+//    private WarningInfoEntityRepo warningInfoEntityRepo;
 
     @Autowired
     private WarningParameterEntityRepo warningParameterEntityRepo;
 
+    @Autowired
+    private RestTemplate restTemplate;
+
     @Transactional(rollbackOn = Throwable.class)
-    @Scheduled(cron = "0 0 0/12 * * ?")
-//    @Scheduled(cron = "0 41 17 * * ?")
+//    @Scheduled(cron = "0 0 0/12 * * ?")
+    @Scheduled(cron = "0 25 12 * * ?")
     public void warningTask() {
+        System.out.println(DateUtil.date() + ": -----------定时任务开启---------->");
         List<ConfigEntity> all = configEntityRepo.findAll();
-        ConfigEntity configEntity = all.get(0);
-        String propertyValue = configEntity.getValue();
+        ConfigEntity configEntity = null;
+        if (CollectionUtil.isNotEmpty(all)) {
+            configEntity = all.get(0);
+        }
+        String propertyValue = null;
+        if (configEntity != null) {
+            propertyValue = configEntity.getValue();
+        }
 
         List<WarningParameter> warningParameterList = warningParameterEntityRepo.findAll();
 
-        //当前时间
-        Date date = new Date();
+        HttpHeaders headers = new HttpHeaders();
+        MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
+        headers.setContentType(type);
+        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
 
-        if ("true".equals(propertyValue)) {
-            List<IndexParameter2Entity> localList = indexParameter2EntityRepo.findAll();
-            List<IndexParameter2Entity> collect = localList.stream()
-                    .filter(localEntity -> warningParameterList.stream()
-                            .anyMatch(w -> w.getParameterName().equals(localEntity.getName())))
-                    .collect(Collectors.toList());
-
-            List<IndexParameter2Entity> collect1 = collect.stream().filter(s -> warningParameterList.stream()
-                    .anyMatch(warningParameter -> warningParameter.getParameterName().equals(s.getName()) && warningParameter.getThresholdValue() >= Double.parseDouble(s.getData())))
-                    .collect(Collectors.toList());
-
-
-            for (IndexParameter2Entity indexParameter2Entity : collect1) {
-                WarningInfoEntity warningInfoEntity = new WarningInfoEntity();
-                warningInfoEntity.setUploadTime(new Date());
-                warningInfoEntity.setWarningType(3);
-                String type = "本地数据的 " + indexParameter2Entity.getName() + " 参数小于阈值";
-                warningInfoEntity.setType(type);
-                warningInfoEntityRepo.updatePreviousShowStatusByType(type);
-                warningInfoEntityRepo.save(warningInfoEntity);
-            }
 
-            collect.removeAll(collect1);
+        if ("true".equals(propertyValue)) {
 
-            List<WarningInfoEntity> list = new ArrayList<>();
-            for (IndexParameter2Entity indexParameter2Entity : collect) {
-                List<WarningInfoEntity> warningInfoEntityList = warningInfoEntityRepo.findDataTypeLikeLeNow("本地数据的 " + indexParameter2Entity.getName(), date);
-                list.addAll(warningInfoEntityList);
-            }
-            for (WarningInfoEntity warningInfoEntity : list) {
-                warningInfoEntity.setShowStatus(1);
-                warningInfoEntityRepo.saveAndFlush(warningInfoEntity);
+            List<IndexParameter2Entity> localList = indexParameter2EntityRepo.findAll();
+            if (CollectionUtil.isNotEmpty(localList)) {
+                List<IndexParameter2Entity> collect = localList.stream()
+                        .filter(localEntity -> warningParameterList.stream()
+                                .anyMatch(w -> w.getParameterName().equals(localEntity.getName())))
+                        .collect(Collectors.toList());
+
+                List<IndexParameter2Entity> collect1 = collect.stream().filter(s -> warningParameterList.stream()
+                        .anyMatch(warningParameter -> warningParameter.getParameterName().equals(s.getName()) && warningParameter.getThresholdValue() >= Double.parseDouble(s.getData())))
+                        .collect(Collectors.toList());
+
+
+//            for (IndexParameter2Entity indexParameter2Entity : collect1) {
+//                WarningInfoEntity warningInfoEntity = new WarningInfoEntity();
+//                warningInfoEntity.setUploadTime(new Date());
+//                warningInfoEntity.setWarningType(3);
+//                String type = "本地数据的 " + indexParameter2Entity.getName() + " 参数小于阈值";
+//                warningInfoEntity.setType(type);
+//                warningInfoEntityRepo.updatePreviousShowStatusByType(type);
+//                warningInfoEntityRepo.save(warningInfoEntity);
+//            }
+
+//            List<IndexParameter2Entity> tempList = new ArrayList<>(collect);
+//            tempList.removeAll(collect1);
+                collect.removeAll(collect1);
+
+                Map<String, List<IndexParameter2Entity>> param = new HashMap<>();
+                param.put("saveList", collect1);
+                param.put("updateList", collect);
+                HttpEntity httpEntity = new HttpEntity<>(param, headers);
+                restTemplate.postForObject("http://127.0.0.1:8085/productDataHandle/handleLocalWarningInfo", httpEntity, String.class);
             }
-        }
 
-        if ("false".equals(propertyValue)) {
+//            List<WarningInfoEntity> list = new ArrayList<>();
+//            for (IndexParameter2Entity indexParameter2Entity : collect) {
+//                List<WarningInfoEntity> warningInfoEntityList = warningInfoEntityRepo.findDataTypeLikeLeNow("本地数据的 " + indexParameter2Entity.getName(), date);
+//                list.addAll(warningInfoEntityList);
+//            }
+//            for (WarningInfoEntity warningInfoEntity : list) {
+//                warningInfoEntity.setShowStatus(1);
+//                warningInfoEntityRepo.saveAndFlush(warningInfoEntity);
+//            }
+        } else if ("false".equals(propertyValue)) {
             List<IndexParameterEntity> localList = indexParameterEntityRepo.findAll();
-            List<IndexParameterEntity> collect = localList.stream()
-                    .filter(localEntity -> warningParameterList.stream()
-                            .anyMatch(w -> w.getParameterName().equals(localEntity.getName())))
-                    .collect(Collectors.toList());
-
-            List<IndexParameterEntity> collect1 = collect.stream().filter(s -> warningParameterList.stream()
-                    .anyMatch(warningParameter -> warningParameter.getParameterName().equals(s.getName()) && warningParameter.getThresholdValue() >= Double.parseDouble(s.getData())))
-                    .collect(Collectors.toList());
-
-            for (IndexParameterEntity indexParameter2Entity : collect1) {
-                WarningInfoEntity warningInfoEntity = new WarningInfoEntity();
-                warningInfoEntity.setUploadTime(new Date());
-                warningInfoEntity.setWarningType(3);
-                String type = "ERP数据的 " + indexParameter2Entity.getName() + " 参数小于阈值!";
-                warningInfoEntity.setType(type);
-                warningInfoEntityRepo.updatePreviousShowStatusByType(type);
-                warningInfoEntityRepo.save(warningInfoEntity);
-            }
-
-            collect.removeAll(collect1);
-
-            List<WarningInfoEntity> list = new ArrayList<>();
-            for (IndexParameterEntity indexParameterEntity : collect) {
-                List<WarningInfoEntity> warningInfoEntityList = warningInfoEntityRepo.findDataTypeLikeLeNow("ERP数据的 " + indexParameterEntity.getName(), date);
-                list.addAll(warningInfoEntityList);
-            }
-            for (WarningInfoEntity warningInfoEntity : list) {
-                warningInfoEntity.setShowStatus(1);
-                warningInfoEntityRepo.saveAndFlush(warningInfoEntity);
+            if (CollectionUtil.isNotEmpty(localList)) {
+                List<IndexParameterEntity> collect = localList.stream()
+                        .filter(localEntity -> warningParameterList.stream()
+                                .anyMatch(w -> w.getParameterName().equals(localEntity.getName())))
+                        .collect(Collectors.toList());
+
+                List<IndexParameterEntity> collect1 = collect.stream().filter(s -> warningParameterList.stream()
+                        .anyMatch(warningParameter -> warningParameter.getParameterName().equals(s.getName()) && warningParameter.getThresholdValue() >= Double.parseDouble(s.getData())))
+                        .collect(Collectors.toList());
+
+//            for (IndexParameterEntity indexParameter2Entity : collect1) {
+//                WarningInfoEntity warningInfoEntity = new WarningInfoEntity();
+//                warningInfoEntity.setUploadTime(new Date());
+//                warningInfoEntity.setWarningType(3);
+//                String type = "ERP数据的 " + indexParameter2Entity.getName() + " 参数小于阈值!";
+//                warningInfoEntity.setType(type);
+//                warningInfoEntityRepo.updatePreviousShowStatusByType(type);
+//                warningInfoEntityRepo.save(warningInfoEntity);
+//            }
+
+                collect.removeAll(collect1);
+                Map<String, List<IndexParameterEntity>> param = new HashMap<>();
+                param.put("saveLiSt", collect1);
+                param.put("updateList", collect);
+                HttpEntity httpEntity = new HttpEntity<>(param, headers);
+                restTemplate.postForObject("http://127.0.0.1:8085/productDataHandle/handleERPWarningInfo", httpEntity, String.class);
+
+
+//            List<WarningInfoEntity> list = new ArrayList<>();
+//            for (IndexParameterEntity indexParameterEntity : collect) {
+//                List<WarningInfoEntity> warningInfoEntityList = warningInfoEntityRepo.findDataTypeLikeLeNow("ERP数据的 " + indexParameterEntity.getName(), date);
+//                list.addAll(warningInfoEntityList);
+//            }
+//            for (WarningInfoEntity warningInfoEntity : list) {
+//                warningInfoEntity.setShowStatus(1);
+//                warningInfoEntityRepo.saveAndFlush(warningInfoEntity);
+//            }
             }
 
         }
+        System.out.println(DateUtil.date() + ": -----------定时任务执行完毕---------->");
     }
 }