ソースを参照

添加预警信息入库代码

zhuoning 3 年 前
コミット
bf54f4719a

+ 117 - 0
huimv-hy-production/huimv.hy.autoGetData/src/main/java/com/huimv/production/autoGetData/entity/ProdWarningMsgEntity.java

@@ -0,0 +1,117 @@
+package com.huimv.production.autoGetData.entity;
+
+import javax.persistence.*;
+import java.io.Serializable;
+import java.sql.Timestamp;
+import java.util.Date;
+
+@Entity
+@Table(name = "prod_warning_msg")
+public class ProdWarningMsgEntity implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    @Id
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
+    @Column(name = "id", nullable = false)
+    private Integer id;
+
+    @Column(name = "tip_time")
+    private String tipTime;
+
+    @Column(name = "farm_name")
+    private String farmName;
+
+    @Column(name = "warning_content")
+    private String warningContent;
+
+    @Column(name = "farm_id")
+    private Integer farmId;
+
+    @Column(name = "add_time")
+    private Date addTime;
+
+    @Column(name = "handle_state")
+    private Integer handleState;
+
+    @Column(name = "handle_time")
+    private Timestamp handleTime;
+
+    public void setId(Integer id) {
+        this.id = id;
+    }
+
+    public Integer getId() {
+        return id;
+    }
+
+    public void setTipTime(String tipTime) {
+        this.tipTime = tipTime;
+    }
+
+    public String getTipTime() {
+        return tipTime;
+    }
+
+    public void setFarmName(String farmName) {
+        this.farmName = farmName;
+    }
+
+    public String getFarmName() {
+        return farmName;
+    }
+
+    public void setWarningContent(String warningContent) {
+        this.warningContent = warningContent;
+    }
+
+    public String getWarningContent() {
+        return warningContent;
+    }
+
+    public void setFarmId(Integer armId) {
+        this.farmId = armId;
+    }
+
+    public Integer getFarmId() {
+        return farmId;
+    }
+
+    public void setAddTime(Date addTime) {
+        this.addTime = addTime;
+    }
+
+    public Date getAddTime() {
+        return addTime;
+    }
+
+    public void setHandleState(Integer handleState) {
+        this.handleState = handleState;
+    }
+
+    public Integer getHandleState() {
+        return handleState;
+    }
+
+    public void setHandleTime(Timestamp handleTime) {
+        this.handleTime = handleTime;
+    }
+
+    public Timestamp getHandleTime() {
+        return handleTime;
+    }
+
+    @Override
+    public String toString() {
+        return "ProdWarningMsgEntity{" +
+                "id=" + id + '\'' +
+                "tipTime=" + tipTime + '\'' +
+                "farmName=" + farmName + '\'' +
+                "warningContent=" + warningContent + '\'' +
+                "armId=" + farmId + '\'' +
+                "addTime=" + addTime + '\'' +
+                "handleState=" + handleState + '\'' +
+                "handleTime=" + handleTime + '\'' +
+                '}';
+    }
+}

+ 9 - 0
huimv-hy-production/huimv.hy.autoGetData/src/main/java/com/huimv/production/autoGetData/repo/ProdWarningMsgRepo.java

@@ -0,0 +1,9 @@
+package com.huimv.production.autoGetData.repo;
+
+import com.huimv.production.autoGetData.entity.ProdWarningMsgEntity;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
+
+public interface ProdWarningMsgRepo extends JpaRepository<ProdWarningMsgEntity, Integer>, JpaSpecificationExecutor<ProdWarningMsgEntity> {
+
+}

+ 3 - 0
huimv-hy-production/huimv.hy.autoGetData/src/main/java/com/huimv/production/autoGetData/service/IAutoGetErpData.java

@@ -1,6 +1,7 @@
 package com.huimv.production.autoGetData.service;
 
 import java.io.IOException;
+import java.text.ParseException;
 import java.util.List;
 
 public interface IAutoGetErpData {
@@ -29,4 +30,6 @@ public interface IAutoGetErpData {
     void getManyFarmStock() throws IOException;
 
     void getCostWithManyFarm() throws IOException;
+
+    void getWarningMessage() throws IOException, ParseException;
 }

+ 85 - 1
huimv-hy-production/huimv.hy.autoGetData/src/main/java/com/huimv/production/autoGetData/service/impl/AutoGetErpDataImpl.java

@@ -7,18 +7,23 @@ import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 //import com.huimv.common.date.DateUtil;
 import com.huimv.production.autoGetData.entity.IndexParameter;
+import com.huimv.production.autoGetData.entity.ProdWarningMsgEntity;
 import com.huimv.production.autoGetData.entity.ProductionGroup;
 import com.huimv.production.autoGetData.repo.IndexParameterRepository;
+import com.huimv.production.autoGetData.repo.ProdWarningMsgRepo;
 import com.huimv.production.autoGetData.repo.ProductionGroupRepo;
 import com.huimv.production.autoGetData.service.IAutoGetErpData;
 import com.huimv.production.autoGetData.utils.HttpTemplete;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
+import org.springframework.data.domain.Example;
 import org.springframework.stereotype.Service;
 import org.springframework.web.client.RestTemplate;
 
 import java.io.IOException;
+import java.sql.Timestamp;
+import java.text.ParseException;
 import java.util.*;
 
 /**
@@ -54,6 +59,8 @@ public class AutoGetErpDataImpl implements IAutoGetErpData {
     private ProductionGroupRepo productionGroupRepo;
     @Autowired
     private com.huimv.production.autoGetData.utils.DateUtil dateUtil;
+    @Autowired
+    private ProdWarningMsgRepo warningMsgRepo;
 
     //PSY
     @Override
@@ -520,11 +527,12 @@ public class AutoGetErpDataImpl implements IAutoGetErpData {
     public void getCostWithManyFarm() throws IOException {
         //
         String token = getRemoteToken();
+        //
         Map<String, String> paramsMap = new HashMap<String, String>();
         paramsMap.put("accessToken", token);
         paramsMap.put("year", dateUtil.getThisYear());
         paramsMap.put("month", dateUtil.getThisMonth());
-        System.out.println("paramsMap>>"+paramsMap);
+        //
         Map<String, Integer> timeoutMap = new HashMap<String, Integer>();
         timeoutMap.put("connectTimeout", 120000);
         timeoutMap.put("requestTimeout", 120000);
@@ -558,6 +566,82 @@ public class AutoGetErpDataImpl implements IAutoGetErpData {
         }
     }
 
+    /**
+     * @Method      : getWarningMessage
+     * @Description : 
+     * @Params      : []
+     * @Return      : void
+     * 
+     * @Author      : ZhuoNing
+     * @Date        : 2021/11/19       
+     * @Time        : 15:48
+     */
+    @Override
+    public void getWarningMessage() throws IOException, ParseException {
+        //
+        String token = getRemoteToken();
+        //
+        Map<String, String> paramsMap = new HashMap<String, String>();
+        paramsMap.put("accessToken", token);
+        //
+        Map<String, Integer> timeoutMap = new HashMap<String, Integer>();
+        timeoutMap.put("connectTimeout", 120000);
+        timeoutMap.put("requestTimeout", 120000);
+        timeoutMap.put("socketTimeout", 120000);
+        String url = apiIp + apiPort + serviceName +"/getWarningMessage";
+        log.info("获取预警数据访问地址="+url);
+        log.info("业务请求参数="+paramsMap.toString());
+        log.info("超时参数="+timeoutMap.toString());
+        log.info("开始取数="+new Date(System.currentTimeMillis()));
+        //
+        JSONObject outJo = httpClientUtil.doGet(url,paramsMap,timeoutMap);
+        log.info("返回结果="+outJo);
+        log.info("结束取数="+new Date(System.currentTimeMillis()));
+        if(outJo.getBoolean("status")){
+            JSONObject contentJo = outJo.getJSONObject("content");
+            if(contentJo.getBoolean("success")){
+                JSONArray dataJa = contentJo.getJSONArray("data");
+                if(dataJa.size()==0){
+                    log.info("未取到数据 。");
+                }
+                //删除当天旧数据
+                ProdWarningMsgEntity delWarningMsgEntity = new ProdWarningMsgEntity();
+                delWarningMsgEntity.setAddTime(dateUtil.getTodayDate());
+                Example<ProdWarningMsgEntity> example = Example.of(delWarningMsgEntity);
+                warningMsgRepo.deleteAll(warningMsgRepo.findAll(example));
+
+                // 添加入库
+                for(int a=0;a<dataJa.size();a++){
+                    JSONObject dataJo = dataJa.getJSONObject(a);
+                    String content = dataJo.getString("xxnr").trim();
+                    String[] contentArray = content.split("-");
+                    String pigfarm = "";
+                    String message = "";
+                    if(contentArray.length>=2){
+                        pigfarm = contentArray[0];
+                        message = content.replace(pigfarm+"-","").trim();
+                    }else{
+                        pigfarm = content.substring(0,3);
+                        message=  content.substring(3,content.length());;
+                    }
+//                    System.out.println((a+1)+" "+dateUtil.formatDateTime(dataJo.getString("tsrq"))+","+pigfarm+","+message+" length="+contentArray.length);
+                    ProdWarningMsgEntity newWarningMsgEntity = new ProdWarningMsgEntity();
+                    newWarningMsgEntity.setTipTime(dateUtil.formatDateTime(dataJo.getString("tsrq")));
+                    newWarningMsgEntity.setFarmName(pigfarm);
+                    newWarningMsgEntity.setWarningContent(message);
+                    newWarningMsgEntity.setFarmId(0);
+                    newWarningMsgEntity.setAddTime(new Timestamp(new Date().getTime()));
+                    newWarningMsgEntity.setHandleState(0);
+                    ProdWarningMsgEntity outWarningMsgEntity = warningMsgRepo.saveAndFlush(newWarningMsgEntity);
+                }
+            }else{
+                System.out.println("取数失败");
+            }
+        }else{
+            System.out.println("取数失败");
+        }
+    }
+
    /* public static void main(String[] args) {
         RestTemplate restTemplate = new RestTemplate();
 

+ 24 - 0
huimv-hy-production/huimv.hy.autoGetData/src/main/java/com/huimv/production/autoGetData/utils/DateUtil.java

@@ -2,7 +2,10 @@ package com.huimv.production.autoGetData.utils;
 
 import org.springframework.stereotype.Component;
 
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
 import java.util.Calendar;
+import java.util.Date;
 
 /**
  * @Project : huimv.shiwan
@@ -15,12 +18,14 @@ import java.util.Calendar;
 @Component
 public class DateUtil {
 
+    //格式化本年
     public String getThisYear(){
         Calendar cal = Calendar.getInstance();
         int year = cal.get(Calendar.YEAR);
         return String.valueOf(year);
     }
 
+    //格式化本月
     public String getThisMonth(){
         Calendar cal = Calendar.getInstance();
         int month = cal.get(Calendar.MONTH) + 1;
@@ -31,4 +36,23 @@ public class DateUtil {
             return String.valueOf(month);
         }
     }
+
+    //格式化日期时间
+    public String formatDateTime(String dateText) throws ParseException {
+        if(dateText.indexOf("T") != -1){
+            dateText = dateText.replace("T"," ");
+        }
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+        Date date = sdf.parse(dateText);
+        return sdf.format(date);
+    }
+
+    //
+    public Date getTodayDate() throws ParseException {
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+        return sdf.parse(sdf.format(new Date()));
+    }
+
+
+
 }

+ 31 - 0
huimv-hy-production/huimv.hy.autoGetData/src/main/resources/application-v2.yml

@@ -0,0 +1,31 @@
+server:
+  port: 9105
+spring:
+  application:
+    name: huimv-hy-autoGetData
+
+  datasource:
+    url: jdbc:mysql://192.168.1.7:3306/huimv_farm_v2?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&serverTimezone=Asia/Shanghai
+    username: root
+    password: hm123456
+    driver-class-name: com.mysql.cj.jdbc.Driver
+  jpa:
+    show-sql: true
+    database: mysql
+    hibernate:
+      ddl-auto: update
+    database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
+    open-in-view: true
+
+    #  redis:
+#    database: 0
+#    host: 119.3.84.55
+#    port: 6379
+#    password: hm123456
+#    jedis:
+#      pool:
+#        max-active: 20
+#        max-wait: -1
+#        max-idle: 10
+#        min-idle: 0
+#    timeout: 5000

+ 1 - 1
huimv-hy-production/huimv.hy.autoGetData/src/main/resources/application.properties

@@ -1,4 +1,4 @@
-spring.profiles.active=dev
+spring.profiles.active=v2
 
 erp.datasource.startDate=2020.1.1
 erp.datasource.endDate=2021.12.31

+ 29 - 0
huimv-hy-production/huimv.hy.autoGetData/src/test/java/com/huimv/production/autoGetData/utils/AutoGetDataTEst.java

@@ -0,0 +1,29 @@
+package com.huimv.production.autoGetData.utils;
+
+import com.huimv.production.autoGetData.service.IAutoGetErpData;
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+
+import java.io.IOException;
+import java.text.ParseException;
+
+/**
+ * @Project : huimv.shiwan
+ * @Package : com.huimv.biosafety.uface.controller
+ * @Description : TODO
+ * @Version : 1.0
+ * @Author : ZhuoNing
+ * @Create : 2020-12-25
+ **/
+@SpringBootTest
+public class AutoGetDataTEst {
+    @Autowired
+    private IAutoGetErpData autoGetErpData;
+
+    @Test
+    public void testGetWarningMessage() throws IOException, ParseException {
+        //
+        autoGetErpData.getWarningMessage();
+    }
+}

+ 24 - 0
huimv-hy-production/huimv.hy.autoGetData/src/test/java/com/huimv/production/autoGetData/utils/DateTest.java

@@ -1,10 +1,14 @@
 package com.huimv.production.autoGetData.utils;
 
+import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
 import org.junit.jupiter.api.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
 
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
 import java.util.Calendar;
+import java.util.Date;
 
 /**
  * @Project : huimv.shiwan
@@ -37,4 +41,24 @@ public class DateTest {
         System.out.println("month="+dateUtil.getThisMonth());;
     }
 
+    @Test
+    public void testFormatDatetime() throws ParseException {
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
+        String dateText = "2021-11-15T16:10:01.570+00:00";
+        System.out.println("dateText="+dateText);
+//        sdf.setLenient(true);
+//        Date date = sdf.parse(dateText);//要先捕获异常
+//        System.out.println("getTime="+date.getTime());
+//        String date2 = sdf.format(date);
+//        System.out.println("date2="+date2);
+
+        System.out.println( dateUtil.formatDateTime(dateText));
+
+
+//        Date date1 = new Date(dateText);
+//        System.out.println("date="+date.getTime());
+//        dateUtil.formatDateTime(dateText);
+    }
+
+
 }