Browse Source

远程获取PSY跟返情率

523096025 4 years ago
parent
commit
b9be3cc857

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

@@ -2,7 +2,9 @@ package com.huimv.production.autoGetData;
 
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.context.annotation.Bean;
 import org.springframework.scheduling.annotation.EnableScheduling;
+import org.springframework.web.client.RestTemplate;
 
 /**
  * @Project : huimv.shiwan
@@ -18,4 +20,11 @@ public class HuimvHyAutoGetDataApplication {
     public static void main(String[] args) {
         SpringApplication.run(HuimvHyAutoGetDataApplication.class, args);
     }
+
+    @Bean
+    public  RestTemplate restTemplate(){
+        return new RestTemplate();
+    }
+
+
 }

+ 7 - 0
huimv-hy-production/huimv.hy.autoGetData/src/main/java/com/huimv/production/autoGetData/config/SaticScheduleTask.java

@@ -31,6 +31,8 @@ public class SaticScheduleTask  {
 //        System.err.println("执行静态定时任务时间: " + LocalDateTime.now());
 //    }
 
+
+
     @Scheduled(cron = "0/5 * * * * ?")
     private void getPsyTask(){
         System.err.println("执行 getPsyTask 执行静态定时任务时间: " + LocalDateTime.now());
@@ -40,11 +42,14 @@ public class SaticScheduleTask  {
     @Scheduled(cron = "* 0/5 * * * ?")
     private void getReEstrusRate(){
         System.err.println("执行 getReEstrusRate 执行静态定时任务时间: " + LocalDateTime.now());
+        iAutoGetErpData.getReEstrusRate();
+
     }
 
     @Scheduled(cron = "* 0/5 * * * ?")
     private void getParturitionRate(){
         System.err.println("执行 getParturitionRate 执行静态定时任务时间: " + LocalDateTime.now());
+        iAutoGetErpData.getParturitionRate();
     }
 
     @Scheduled(cron = "* 0/5 * * * ?")
@@ -72,4 +77,6 @@ public class SaticScheduleTask  {
         System.err.println("执行 getAllStorage 执行静态定时任务时间: " + LocalDateTime.now());
     }
 
+
+
 }

+ 33 - 0
huimv-hy-production/huimv.hy.autoGetData/src/main/java/com/huimv/production/autoGetData/entity/IndexParameter.java

@@ -0,0 +1,33 @@
+package com.huimv.production.autoGetData.entity;
+
+import lombok.Data;
+
+import javax.persistence.*;
+import java.io.Serializable;
+
+@Data
+@Entity
+@Table(name = "index_parameter")
+public class IndexParameter implements Serializable {
+
+	@Id
+	@GeneratedValue(strategy = GenerationType.IDENTITY)
+	@Column(name = "id")
+	private Integer id;
+
+	@Column(name = "name")
+	private String name;
+
+	@Column(name = "data")
+	private String data;
+
+	@Column(name = "create_time")
+	private java.util.Date createTime;
+
+	@Column(name = "update_time")
+	private java.util.Date updateTime;
+
+	@Column(name = "check_parameter")
+	private Boolean checkParameter;
+
+}

+ 14 - 0
huimv-hy-production/huimv.hy.autoGetData/src/main/java/com/huimv/production/autoGetData/repo/IndexParameterRepository.java

@@ -0,0 +1,14 @@
+package com.huimv.production.autoGetData.repo;
+
+import com.huimv.production.autoGetData.entity.IndexParameter;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
+import org.springframework.data.jpa.repository.Query;
+
+import java.util.List;
+
+public interface IndexParameterRepository extends JpaRepository<IndexParameter, Integer>, JpaSpecificationExecutor<IndexParameter> {
+	
+	@Query(nativeQuery = true ,value = "select * from index_parameter where name like %?1% limit ?2 , ?3")
+    List<IndexParameter> findAll(String name, Integer startPage, Integer pageSize);
+}

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

@@ -2,4 +2,8 @@ package com.huimv.production.autoGetData.service;
 
 public interface IAutoGetErpData {
     void getPsy();
+
+    void getReEstrusRate();
+
+    void getParturitionRate();
 }

+ 99 - 2
huimv-hy-production/huimv.hy.autoGetData/src/main/java/com/huimv/production/autoGetData/service/impl/AutoGetErpDataImpl.java

@@ -1,8 +1,18 @@
 package com.huimv.production.autoGetData.service.impl;
 
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.huimv.production.autoGetData.entity.IndexParameter;
+import com.huimv.production.autoGetData.repo.IndexParameterRepository;
 import com.huimv.production.autoGetData.service.IAutoGetErpData;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
+import org.springframework.web.client.RestTemplate;
+
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
 
 /**
  * @Project : huimv.shiwan
@@ -21,10 +31,97 @@ public class AutoGetErpDataImpl implements IAutoGetErpData {
     // 查询结束日期
     @Value("${erp.datasource.endDate}")
     private String endDate;
+    @Autowired
+    private RestTemplate template;
+
+    @Autowired
+    private IndexParameterRepository parameterRepository;
 
+
+    //PSY
     @Override
     public void getPsy() {
-        System.out.println("startDate:"+startDate);
-        System.out.println("endDate:"+endDate);
+        String token = getToken();
+        JSONObject jsonObject = template.getForObject("http://192.168.1.62:9100/erpData/getPsy?startDate="+startDate+"&endDate="+endDate+"&accessToken="+token
+                ,JSONObject.class );
+        String data = jsonObject.getString("data");
+        if (data == null){
+            return;
+        }
+        IndexParameter indexParameter = parameterRepository.findById(7).get();
+        indexParameter.setUpdateTime(new Date());
+        //TODO 将数据加入到数据库
+        indexParameter.setData(data);
+
+        parameterRepository.save(indexParameter);
+
+
+    }
+
+    //返情率
+    @Override
+    public void getReEstrusRate() {
+        String token = getToken();
+        JSONObject jsonObject = template.getForObject("http://192.168.1.62:9100/erpData/getReEstrusRate?startDate="+startDate+"&endDate="+endDate+"&accessToken="+token
+                ,JSONObject.class );
+        String data = jsonObject.getString("data");
+        if (data == null){
+            return;
+        }
+
+        String reestrus_rate ="0.00000";
+        List<Map<String,String>> list =  (List<Map<String,String>> ) JSON.parse(data);
+        for (Map<String, String> stringStringMap : list) {
+            for (String s : stringStringMap.keySet()) {
+                String farm = stringStringMap.get(s);
+                if (farm != null && farm.contains("海盐")){
+                    reestrus_rate  = stringStringMap.get("reestrus_rate");
+                }
+            }
+        }
+        IndexParameter indexParameter = parameterRepository.findById(9).get();
+        indexParameter.setData(reestrus_rate);
+        indexParameter.setUpdateTime(new Date());
+        parameterRepository.save(indexParameter);
+
+    }
+
+    @Override
+    public void getParturitionRate() {
+
+    }
+
+
+    private String getToken(){
+        JSONObject token = template.getForObject("http://192.168.1.62:9100/token/getToken?userId=20210501&timestamp=45546546454&random=1156&sign=7fa431325504e01e9fa87ed0e274c40c",
+                JSONObject.class);
+        return token.getString("accessToken");
+    }
+
+
+
+    public static void main(String[] args) {
+        RestTemplate restTemplate = new RestTemplate();
+
+
+        JSONObject token1 = restTemplate.getForObject("http://192.168.1.62:9100/token/getToken?userId=20210501&timestamp=45546546454&random=1156&sign=7fa431325504e01e9fa87ed0e274c40c",
+                JSONObject.class);
+       String token =  token1.getString("accessToken");
+        JSONObject jsonObject = restTemplate.getForObject("http://192.168.1.62:9100/erpData/getReEstrusRate?startDate=2020.1.1&endDate=2021.12.31&accessToken="+token
+                ,JSONObject.class );
+        String data = jsonObject.getString("data");
+        String reestrus_rate ="0.00000";
+       List<Map<String,String>> list =  (List<Map<String,String>> ) JSON.parse(data);
+        for (Map<String, String> stringStringMap : list) {
+            for (String s : stringStringMap.keySet()) {
+                String farm = stringStringMap.get(s);
+                if (farm != null && farm.contains("海盐")){
+                    reestrus_rate  = stringStringMap.get("reestrus_rate");
+                }
+            }
+        }
+
+
+
     }
 }

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

@@ -5,7 +5,7 @@ spring:
     name: huimv-hy-autoGetData
 
   datasource:
-    url: jdbc:mysql://192.168.1.7:3306/huimv_ql_breed_video?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&serverTimezone=Asia/Shanghai
+    url: jdbc:mysql://192.168.1.7:3306/huimv_ql_farm_haiyan?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&serverTimezone=Asia/Shanghai
     username: root
     password: hm123456
     driver-class-name: com.mysql.cj.jdbc.Driver

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

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