Selaa lähdekoodia

新建重要指标试点数据(psy):自动取数。

zhuoning 3 vuotta sitten
vanhempi
commit
65725af3c6

+ 241 - 0
huimv-hy-production/huimv.hy.autoGetData/src/main/java/com/huimv/production/autoGetData/entity/ProdImportantIndicatorEntity.java

@@ -0,0 +1,241 @@
+package com.huimv.production.autoGetData.entity;
+
+import javax.persistence.*;
+import java.io.Serializable;
+
+@Entity
+@Table(name = "prod_important_indicator")
+public class ProdImportantIndicatorEntity implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    @Id
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
+    @Column(name = "id", nullable = false)
+    private Integer id;
+
+    /**
+     * 存栏类型
+     */
+    @Column(name = "stock_type")
+    private String stockType;
+
+    /**
+     * 存栏名称
+     */
+    @Column(name = "stock_name")
+    private String stockName;
+
+    /**
+     * 存栏数量
+     */
+    @Column(name = "stock_quantity")
+    private String stockQuantity;
+
+    /**
+     * 占比
+     */
+    @Column(name = "rate")
+    private Float rate;
+
+    /**
+     * 月份
+     */
+    @Column(name = "month")
+    private Integer month;
+
+    /**
+     * 年份
+     */
+    @Column(name = "year")
+    private Integer year;
+
+    /**
+     * 牧场id
+     */
+    @Column(name = "farm_id")
+    private Integer farmId;
+
+    /**
+     * 分区id
+     */
+    @Column(name = "subarea_id")
+    private Integer subareaId;
+
+    /**
+     * 涨跌状态(1:上涨 0:下跌)
+     */
+    @Column(name = "rise_fall")
+    private Integer riseFall;
+
+    /**
+     * 涨跌幅度
+     */
+    @Column(name = "range1")
+    private String range1;
+
+    public void setId(Integer id) {
+        this.id = id;
+    }
+
+    public Integer getId() {
+        return id;
+    }
+
+    /**
+     * 存栏类型
+     */
+    public void setStockType(String stockType) {
+        this.stockType = stockType;
+    }
+
+    /**
+     * 存栏类型
+     */
+    public String getStockType() {
+        return stockType;
+    }
+
+    /**
+     * 存栏名称
+     */
+    public void setStockName(String stockName) {
+        this.stockName = stockName;
+    }
+
+    /**
+     * 存栏名称
+     */
+    public String getStockName() {
+        return stockName;
+    }
+
+    /**
+     * 存栏数量
+     */
+    public void setStockQuantity(String stockQuantity) {
+        this.stockQuantity = stockQuantity;
+    }
+
+    /**
+     * 存栏数量
+     */
+    public String getStockQuantity() {
+        return stockQuantity;
+    }
+
+    /**
+     * 占比
+     */
+    public void setRate(Float rate) {
+        this.rate = rate;
+    }
+
+    /**
+     * 占比
+     */
+    public Float getRate() {
+        return rate;
+    }
+
+    /**
+     * 月份
+     */
+    public void setMonth(Integer month) {
+        this.month = month;
+    }
+
+    /**
+     * 月份
+     */
+    public Integer getMonth() {
+        return month;
+    }
+
+    /**
+     * 年份
+     */
+    public void setYear(Integer year) {
+        this.year = year;
+    }
+
+    /**
+     * 年份
+     */
+    public Integer getYear() {
+        return year;
+    }
+
+    /**
+     * 牧场id
+     */
+    public void setFarmId(Integer farmId) {
+        this.farmId = farmId;
+    }
+
+    /**
+     * 牧场id
+     */
+    public Integer getFarmId() {
+        return farmId;
+    }
+
+    /**
+     * 分区id
+     */
+    public void setSubareaId(Integer subareaId) {
+        this.subareaId = subareaId;
+    }
+
+    /**
+     * 分区id
+     */
+    public Integer getSubareaId() {
+        return subareaId;
+    }
+
+    /**
+     * 涨跌状态(1:上涨 0:下跌)
+     */
+    public void setRiseFall(Integer riseFall) {
+        this.riseFall = riseFall;
+    }
+
+    /**
+     * 涨跌状态(1:上涨 0:下跌)
+     */
+    public Integer getRiseFall() {
+        return riseFall;
+    }
+
+    /**
+     * 涨跌幅度
+     */
+    public void setRange1(String range) {
+        this.range1 = range;
+    }
+
+    /**
+     * 涨跌幅度
+     */
+    public String getRange1() {
+        return range1;
+    }
+
+    @Override
+    public String toString() {
+        return "ProdImportantIndicatorEntity{" +
+                "id=" + id + '\'' +
+                "stockType=" + stockType + '\'' +
+                "stockName=" + stockName + '\'' +
+                "stockQuantity=" + stockQuantity + '\'' +
+                "rate=" + rate + '\'' +
+                "month=" + month + '\'' +
+                "year=" + year + '\'' +
+                "farmId=" + farmId + '\'' +
+                "subareaId=" + subareaId + '\'' +
+                "riseFall=" + riseFall + '\'' +
+                "range1=" + range1 + '\'' +
+                '}';
+    }
+}

+ 115 - 0
huimv-hy-production/huimv.hy.autoGetData/src/main/java/com/huimv/production/autoGetData/entity/ProdIndicatorDynamicEntity.java

@@ -0,0 +1,115 @@
+package com.huimv.production.autoGetData.entity;
+
+import javax.persistence.*;
+import java.io.Serializable;
+
+@Entity
+@Table(name = "prod_indicator_dynamic")
+public class ProdIndicatorDynamicEntity implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    @Id
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
+    @Column(name = "id", nullable = false)
+    private Integer id;
+
+    @Column(name = "total")
+    private Integer total;
+
+    @Column(name = "up")
+    private Integer up;
+
+    @Column(name = "down")
+    private Integer down;
+
+    @Column(name = "flat")
+    private Integer flat;
+
+    @Column(name = "farm_id")
+    private Integer farmId;
+
+    @Column(name = "year")
+    private Integer year;
+
+    @Column(name = "month")
+    private Integer month;
+
+    public void setId(Integer id) {
+        this.id = id;
+    }
+
+    public Integer getId() {
+        return id;
+    }
+
+    public void setTotal(Integer total) {
+        this.total = total;
+    }
+
+    public Integer getTotal() {
+        return total;
+    }
+
+    public void setUp(Integer up) {
+        this.up = up;
+    }
+
+    public Integer getUp() {
+        return up;
+    }
+
+    public void setDown(Integer down) {
+        this.down = down;
+    }
+
+    public Integer getDown() {
+        return down;
+    }
+
+    public void setFlat(Integer flat) {
+        this.flat = flat;
+    }
+
+    public Integer getFlat() {
+        return flat;
+    }
+
+    public void setFarmId(Integer farmId) {
+        this.farmId = farmId;
+    }
+
+    public Integer getFarmId() {
+        return farmId;
+    }
+
+    public void setYear(Integer year) {
+        this.year = year;
+    }
+
+    public Integer getYear() {
+        return year;
+    }
+
+    public void setMonth(Integer month) {
+        this.month = month;
+    }
+
+    public Integer getMonth() {
+        return month;
+    }
+
+    @Override
+    public String toString() {
+        return "ProdIndicatorDynamicEntity{" +
+                "id=" + id + '\'' +
+                "total=" + total + '\'' +
+                "up=" + up + '\'' +
+                "down=" + down + '\'' +
+                "flat=" + flat + '\'' +
+                "farmId=" + farmId + '\'' +
+                "year=" + year + '\'' +
+                "month=" + month + '\'' +
+                '}';
+    }
+}

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

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

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

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

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

@@ -21,6 +21,7 @@ import org.springframework.stereotype.Service;
 import org.springframework.web.client.RestTemplate;
 
 import java.io.IOException;
+import java.math.BigDecimal;
 import java.sql.Timestamp;
 import java.text.DecimalFormat;
 import java.text.NumberFormat;
@@ -79,6 +80,10 @@ public class AutoGetErpDataImpl implements IAutoGetErpData {
     private MathUtil mathUtil;
     @Autowired
     private ProdWorkPlanRepo workPlanRepo;
+    @Autowired
+    private ProdImportantIndicatorRepo prodImportantIndicatorRepo;
+    @Autowired
+    private ProdIndicatorDynamicRepo prodIndicatorDynamicRepo;
 
    /* //PSY
     @Override
@@ -1147,7 +1152,7 @@ public class AutoGetErpDataImpl implements IAutoGetErpData {
         log.info("开始取数=" + new Date(System.currentTimeMillis()));
         //
         JSONObject outJo = httpClientUtil.doGet(url, paramsMap, timeoutMap);
-//        log.info("返回结果="+outJo);
+        log.info("返回结果="+outJo);
         log.info("结束取数=" + new Date(System.currentTimeMillis()));
         if (outJo.getBoolean("status")) {
             JSONObject contentJo = outJo.getJSONObject("content");
@@ -1169,6 +1174,7 @@ public class AutoGetErpDataImpl implements IAutoGetErpData {
                 // 添加入库
                 for (int a = 0; a < dataJa.size(); a++) {
                     JSONObject dataJo = dataJa.getJSONObject(a);
+                    System.out.println((a+1)+" "+dataJo);
                     String content = dataJo.getString("xxnr").trim();
                     String[] contentArray = content.split("-");
                     String pigfarm = "";
@@ -1258,6 +1264,7 @@ public class AutoGetErpDataImpl implements IAutoGetErpData {
                     List<ProdFarmNameEntity> farmNameEntityList = prodFarmNameRepo.findAll();
                     for (int a = 0; a < dataJa.size(); a++) {
                         JSONObject dataJo = dataJa.getJSONObject(a);
+                        System.out.println((a+1)+" "+dataJo);
                         ProdWorkPlanEntity workPlanEntity = new ProdWorkPlanEntity();
                         workPlanEntity.setFarmId(_getFarmIdByFullName(dataJo.getString("mcmc"), farmNameEntityList));
                         workPlanEntity.setFarmName(dataJo.getString("mcmc"));
@@ -1624,7 +1631,117 @@ public class AutoGetErpDataImpl implements IAutoGetErpData {
                 addEntity1.setYear(Integer.parseInt(year));
                 produceRepo.saveAndFlush(addEntity1);
             }
+
+            //重点指标表(试点)
+            //PSY
+            //计算前一个月(年、月)
+            Map beforeMonthMap = dateUtil.getBeforeMonth();
+
+            //计算涨跌
+            JSONArray dynamicJa = getBeforeLastMonth(psyJa,beforeMonthMap);
+
+            //删除当前一个月的数据
+            ProdImportantIndicatorEntity delImportantEntity = new ProdImportantIndicatorEntity();
+            delImportantEntity.setYear(Integer.parseInt(year));
+            delImportantEntity.setMonth(Integer.parseInt(month));
+            delImportantEntity.setStockType("psy");
+            Example<ProdImportantIndicatorEntity> delExample2 = Example.of(delImportantEntity);
+            prodImportantIndicatorRepo.deleteAll(prodImportantIndicatorRepo.findAll(delExample2));
+            //
+            for(int a=0;a<psyJa.size();a++){
+                JSONObject psyJo = psyJa.getJSONObject(a);
+                ProdImportantIndicatorEntity addImportantEntity1 = new ProdImportantIndicatorEntity();
+                addImportantEntity1.setFarmId(psyJo.getInteger("farm_id"));
+                addImportantEntity1.setStockType("psy");
+                addImportantEntity1.setStockName("PSY");
+                addImportantEntity1.setStockQuantity(psyJo.getString("stock_quantity"));
+                addImportantEntity1.setMonth(Integer.valueOf(month));
+                addImportantEntity1.setYear(Integer.parseInt(year));
+                //涨跌
+                addImportantEntity1.setRiseFall(psyJo.getInteger("rise_fall"));
+                //幅度
+                addImportantEntity1.setRange1(psyJo.getString("range"));
+                System.out.println("addImportantEntity1>>"+addImportantEntity1.toString());
+                prodImportantIndicatorRepo.saveAndFlush(addImportantEntity1);
+            }
+
+            //添加重要指标动态统计表
+            //删除旧数据
+            ProdIndicatorDynamicEntity delDynamicEntity = new ProdIndicatorDynamicEntity();
+            delDynamicEntity.setYear(Integer.parseInt(year));
+            delDynamicEntity.setMonth(Integer.parseInt(month));
+            Example<ProdIndicatorDynamicEntity> delExample3 = Example.of(delDynamicEntity);
+            prodIndicatorDynamicRepo.deleteAll(prodIndicatorDynamicRepo.findAll(delExample3));
+            //增加信息数据
+            for(int a=0;a<dynamicJa.size();a++){
+                JSONObject dynamicJo = dynamicJa.getJSONObject(a);
+                ProdIndicatorDynamicEntity addDynamicEntity = new ProdIndicatorDynamicEntity();
+                addDynamicEntity.setFarmId(dynamicJo.getInteger("farm_id"));
+                addDynamicEntity.setYear(Integer.parseInt(year));
+                addDynamicEntity.setMonth(Integer.parseInt(month));
+                addDynamicEntity.setTotal(dynamicJo.getInteger("total"));
+                addDynamicEntity.setUp(dynamicJo.getInteger("up"));
+                addDynamicEntity.setDown(dynamicJo.getInteger("down"));
+                addDynamicEntity.setFlat(dynamicJo.getInteger("flat"));
+                prodIndicatorDynamicRepo.saveAndFlush(addDynamicEntity);
+            }
+        }
+    }
+
+    //
+    private JSONArray getBeforeLastMonth(JSONArray psyJa, Map beforeMonthMap) {
+        String beforeMonth = beforeMonthMap.get("month").toString();
+        String beforeYear = beforeMonthMap.get("year").toString();
+        //查找前一个月的数据
+        ProdImportantIndicatorEntity findImportantEntity = new ProdImportantIndicatorEntity();
+        findImportantEntity.setYear(Integer.parseInt(beforeYear));
+        findImportantEntity.setMonth(Integer.parseInt(beforeMonth));
+        findImportantEntity.setStockType("psy");
+        Example<ProdImportantIndicatorEntity> delExample2 = Example.of(findImportantEntity);
+        List<ProdImportantIndicatorEntity> beforeImportantEntityList = prodImportantIndicatorRepo.findAll(delExample2);
+
+        JSONArray dynamicJa = new JSONArray();
+        int up = 0;
+        int down = 0;
+        int flat = 0;
+        for(int a=0;a<psyJa.size();a++){
+            JSONObject psyJo = psyJa.getJSONObject(a);
+            boolean isExist = false;
+            for(ProdImportantIndicatorEntity beforeImportantEntity:beforeImportantEntityList){
+                if(beforeImportantEntity.getFarmId().equals(psyJo.getInteger("farm_id"))){
+                    isExist = true;
+                    BigDecimal beforeQuantityBd = new BigDecimal(beforeImportantEntity.getStockQuantity());
+                    BigDecimal quantityBd = new BigDecimal(psyJo.getString("stock_quantity"));
+                    if(quantityBd.compareTo(beforeQuantityBd) == 0){
+                        psyJo.put("rise_fall",1);
+                        psyJo.put("range","0");
+                        flat++;
+                    }else if(quantityBd.compareTo(beforeQuantityBd) == 1){
+                        psyJo.put("rise_fall",1);
+                        psyJo.put("range",quantityBd.subtract(beforeQuantityBd));
+                        up++;
+                    }else{
+                        psyJo.put("rise_fall",0);
+                        psyJo.put("range",beforeQuantityBd.subtract(quantityBd));
+                        down++;
+                    }
+                    break;
+                }
+            }
+            if(!isExist){
+                psyJo.put("rise_fall",1);
+                psyJo.put("range",psyJo.getString("stock_quantity"));
+                up++;
+            }
+            JSONObject dynamicJo = new JSONObject();
+            dynamicJa.add(dynamicJo);
+            dynamicJo.put("total",up+down+flat);
+            dynamicJo.put("up",up);
+            dynamicJo.put("down",down);
+            dynamicJo.put("flat",flat);
+            dynamicJo.put("farm_id",psyJo.getIntValue("farm_id"));
         }
+        return dynamicJa;
     }
 
     @Override

+ 13 - 3
huimv-hy-production/huimv.hy.autoGetData/src/main/java/com/huimv/production/autoGetData/utils/DateUtil.java

@@ -6,9 +6,7 @@ import org.springframework.stereotype.Component;
 
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
-import java.util.ArrayList;
-import java.util.Calendar;
-import java.util.Date;
+import java.util.*;
 
 /**
  * @Project : huimv.shiwan
@@ -41,6 +39,18 @@ public class DateUtil {
         }
     }
 
+    //获取前一个月
+    public Map getBeforeMonth(){
+        Calendar cal = Calendar.getInstance();
+        cal.add(Calendar.MONTH,-1);
+        int month = cal.get(Calendar.MONTH) + 1;
+        int year = cal.get(Calendar.YEAR);
+        Map newMap = new HashMap<>();
+        newMap.put("month",month);
+        newMap.put("year",year);
+        return newMap;
+    }
+
     //格式化日期时间
     public String formatDateTime(String dateText) throws ParseException {
         if(dateText.indexOf("T") != -1){

+ 33 - 0
huimv-hy-production/huimv.hy.autoGetData/src/main/resources/application-center.yml

@@ -0,0 +1,33 @@
+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
+    url: jdbc:mysql://47.96.4.54:10052/huimv-farm-center?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&serverTimezone=Asia/Shanghai
+#    url: jdbc:mysql://115.238.57.190:3309/huimv-farm-center?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&serverTimezone=Asia/Shanghai
+    username: root
+    password: hm123456789
+    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=v2
+spring.profiles.active=center
 
 erp.datasource.startDate=2020.1.1
 erp.datasource.endDate=2021.12.31

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

@@ -1,14 +1,17 @@
 package com.huimv.production.autoGetData.utils;
 
+import com.alibaba.fastjson.JSONObject;
 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.math.BigDecimal;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.Calendar;
 import java.util.Date;
+import java.util.Map;
 
 /**
  * @Project : huimv.shiwan
@@ -60,5 +63,37 @@ public class DateTest {
 //        dateUtil.formatDateTime(dateText);
     }
 
+    @Test
+    public void testBetBeforeMonthAndYearByLastMonth(){
+        Calendar cal = Calendar.getInstance();
+        cal.add(Calendar.MONTH,-1);
+        int month = cal.get(Calendar.MONTH) + 1;
+        int year = cal.get(Calendar.YEAR);
+
+        System.out.println("当期时间: " + cal.getTime());
+        System.out.println("月份: " + month);
+        System.out.println("年份: " + year);
+    }
+
+    @Test
+    public void testGetBeforeMonth(){
+        Map beforeMonthMap = dateUtil.getBeforeMonth();
+        System.out.println("year>>"+beforeMonthMap.get("year").toString());
+        System.out.println("month>>"+beforeMonthMap.get("month").toString());
+    }
+
+    @Test
+    public void test1(){
+        BigDecimal bd1 = new BigDecimal("100");
+        BigDecimal bd2 = new BigDecimal("80");
+        BigDecimal bd3 = bd2.subtract(bd1);
+        System.out.println(""+bd1);
+        System.out.println(""+bd2);
+        System.out.println("bd3>>"+bd3.toString());
+//        System.out.println(">>"+bd3.setScale(2,BigDecimal.ROUND_HALF_UP));
+        System.out.println("compareTo>>"+bd1.compareTo(bd2));
+        System.out.println("compareTo>>"+bd2.compareTo(bd1));
+    }
+
 
 }