Преглед на файлове

新增电子秤上传批次数据功能。

zhuoning преди 3 години
родител
ревизия
b35f58c5ca

+ 128 - 0
huimv-farm-device/huimv-farm-local-weight/src/main/java/com/huimv/weight/dao/entity/ProdBatchWeightEntity.java

@@ -0,0 +1,128 @@
+package com.huimv.weight.dao.entity;
+
+import javax.persistence.*;
+import java.io.Serializable;
+import java.sql.Timestamp;
+
+@Entity
+@Table(name = "prod_batch_weight")
+public class ProdBatchWeightEntity implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    @Id
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
+    @Column(name = "id", nullable = false)
+    private Integer id;
+
+    @Column(name = "batch_code")
+    private String batchCode;
+
+    @Column(name = "batch_sort")
+    private Integer batchSort;
+
+    @Column(name = "gross_weight")
+    private Float grossWeight;
+
+    @Column(name = "tare_weight")
+    private Float tareWeight;
+
+    @Column(name = "net_weight")
+    private Float netWeight;
+
+    @Column(name = "trade_id")
+    private Integer tradeId;
+
+    @Column(name = "farm_id")
+    private Integer farmId;
+
+    @Column(name = "add_time")
+    private Timestamp addTime;
+
+    public void setId(Integer id) {
+        this.id = id;
+    }
+
+    public Integer getId() {
+        return id;
+    }
+
+    public void setBatchCode(String batchCode) {
+        this.batchCode = batchCode;
+    }
+
+    public String getBatchCode() {
+        return batchCode;
+    }
+
+    public void setBatchSort(Integer batchSort) {
+        this.batchSort = batchSort;
+    }
+
+    public Integer getBatchSort() {
+        return batchSort;
+    }
+
+    public void setGrossWeight(Float grossWeight) {
+        this.grossWeight = grossWeight;
+    }
+
+    public Float getGrossWeight() {
+        return grossWeight;
+    }
+
+    public void setTareWeight(Float tareWeight) {
+        this.tareWeight = tareWeight;
+    }
+
+    public Float getTareWeight() {
+        return tareWeight;
+    }
+
+    public void setNetWeight(Float netWeight) {
+        this.netWeight = netWeight;
+    }
+
+    public Float getNetWeight() {
+        return netWeight;
+    }
+
+    public void setTradeId(Integer tradeId) {
+        this.tradeId = tradeId;
+    }
+
+    public Integer getTradeId() {
+        return tradeId;
+    }
+
+    public void setFarmId(Integer farmId) {
+        this.farmId = farmId;
+    }
+
+    public Integer getFarmId() {
+        return farmId;
+    }
+
+    public void setAddTime(Timestamp addTime) {
+        this.addTime = addTime;
+    }
+
+    public Timestamp getAddTime() {
+        return addTime;
+    }
+
+    @Override
+    public String toString() {
+        return "ProdBatchWeightEntity{" +
+                "id=" + id + '\'' +
+                "batchCode=" + batchCode + '\'' +
+                "batchSort=" + batchSort + '\'' +
+                "grossWeight=" + grossWeight + '\'' +
+                "tareWeight=" + tareWeight + '\'' +
+                "netWeight=" + netWeight + '\'' +
+                "tradeId=" + tradeId + '\'' +
+                "farmId=" + farmId + '\'' +
+                "addTime=" + addTime + '\'' +
+                '}';
+    }
+}

+ 12 - 0
huimv-farm-device/huimv-farm-local-weight/src/main/java/com/huimv/weight/dao/entity/ProdWeightEntity.java

@@ -39,6 +39,9 @@ public class ProdWeightEntity implements Serializable {
     @Column(name = "farm_id")
     private Integer farmId;
 
+    @Column(name = "batch_id")
+    private Integer batchId;
+
     public void setId(Integer id) {
         this.id = id;
     }
@@ -105,6 +108,14 @@ public class ProdWeightEntity implements Serializable {
         this.farmId = farmId;
     }
 
+    public Integer getBatchId() {
+        return batchId;
+    }
+
+    public void setBatchId(Integer batchId) {
+        this.batchId = batchId;
+    }
+
     @Override
     public String toString() {
         return "ProdWeightEntity{" +
@@ -114,6 +125,7 @@ public class ProdWeightEntity implements Serializable {
                 ", tareWeight=" + tareWeight +
                 ", netWeight=" + netWeight +
                 ", farmId=" + farmId +
+                ", batchId=" + batchId +
                 '}';
     }
 }

+ 15 - 0
huimv-farm-device/huimv-farm-local-weight/src/main/java/com/huimv/weight/dao/repo/ProdBatchWeightRepo.java

@@ -0,0 +1,15 @@
+package com.huimv.weight.dao.repo;
+
+import com.huimv.weight.dao.entity.ProdBatchWeightEntity;
+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 ProdBatchWeightRepo extends JpaRepository<ProdBatchWeightEntity, Integer>, JpaSpecificationExecutor<ProdBatchWeightEntity> {
+
+    //
+    @Query(nativeQuery = true,value ="SELECT * FROM prod_batch_weight WHERE farm_id=?1 AND  DATE_FORMAT(add_time,'%Y-%m-%d')= DATE_FORMAT(?2,'%Y-%m-%d') ORDER BY id DESC")
+    List<ProdBatchWeightEntity> findByFarmIdAndToday(Integer farmID, String todayText);
+}

+ 1 - 1
huimv-farm-device/huimv-farm-local-weight/src/main/java/com/huimv/weight/server/WeightDataHandle.java

@@ -21,7 +21,7 @@ public class WeightDataHandle {
 
     //
     public void handleReceiveData(String data) throws ParseException {
-        System.out.println("WeightDataHandle.data=" + data);
+//        System.out.println("WeightDataHandle.data=" + data);
         //
         weight.handleReceiveData(data);
     }

+ 2 - 0
huimv-farm-device/huimv-farm-local-weight/src/main/java/com/huimv/weight/service/IWeight.java

@@ -6,6 +6,8 @@ public interface IWeight {
     //
     void handleReceiveData(String receiveInfo) throws ParseException;
 
+    void sendBatchWeightToCenter();
+
     //发送日总重
     void sendDayWeightToCenter();
 }

+ 140 - 40
huimv-farm-device/huimv-farm-local-weight/src/main/java/com/huimv/weight/service/impl/WeightImpl.java

@@ -2,8 +2,10 @@ package com.huimv.weight.service.impl;
 
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
+import com.huimv.weight.dao.entity.ProdBatchWeightEntity;
 import com.huimv.weight.dao.entity.ProdDayWeightEntity;
 import com.huimv.weight.dao.entity.ProdWeightEntity;
+import com.huimv.weight.dao.repo.ProdBatchWeightRepo;
 import com.huimv.weight.dao.repo.ProdDayWeightRepo;
 import com.huimv.weight.dao.repo.ProdWeightRepo;
 import com.huimv.weight.service.IWeight;
@@ -41,90 +43,188 @@ public class WeightImpl implements IWeight {
     @Autowired
     private ProdDayWeightRepo dayWeightRepo;
     @Autowired
+    private ProdBatchWeightRepo batchWeightRepo;
+    @Autowired
     private HttpTemplete httpTemplete;
     //牧场ID
     @Value("${local.farmID}")
     private Integer farmID;
     //数据中心地址
     @Value("${dataCenter.ipAddr}")
-    private String ipAddr ;
+    private String ipAddr;
     //数据中心地址
     @Value("${dataCenter.port}")
-    private String port ;
+    private String port;
+    //发送日总称重服务
+    @Value("${dataCenter.service.dayWeight}")
+    private String dayWeightService;
     //发送称重服务
-    @Value("${dataCenter.service.weight}")
-    private String weightService;
+    @Value("${dataCenter.service.batchWeight}")
+    private String batchWeightService;
+    //重量批次计算间隔
+    @Value("${weight.batch_interval}")
+    private Integer batchIntervalHours;
+    //重量批次计算间隔
+    @Value("${weight.batch.prefix}")
+    private String batchPrefix;
+    //符号
+    @Value("${weight.batchCode.spacemark}")
+    private String spacemark;
 
     @Override
     public void handleReceiveData(String receiveInfo) throws ParseException {
 //        dateUtil.fromLongToDate(new Date().getTime(),"yyyy-MM-dd HH:mm:ss");
-        System.out.println(dateUtil.fromLongToDate(new Date().getTime(),"yyyy-MM-dd HH:mm:ss")+",当前收到数据>>"+receiveInfo);
+        System.out.println(dateUtil.fromLongToDate(new Date().getTime(), "yyyy-MM-dd HH:mm:ss") + ",当前收到数据>>" + receiveInfo);
         //解析数据
         String data = receiveInfo.trim();
-        if(data.length() == 1){
-            if(Integer.parseInt(data) == 0){
+        if (data.length() == 1) {
+            if (Integer.parseInt(data) == 0) {
                 System.out.println("当前接收到数据为0,为无效数据。");
-            }else{
+            } else {
                 //电子秤输出的数据模板有问题。
-                System.out.println("电子秤输出模板>>"+data);
+                System.out.println("电子秤输出模板>>" + data);
             }
-        }else{
+        } else {
             //解析收到的质量毛重,皮重,净重
             Map weightMap = _parseReceiveWeight(receiveInfo);
 //            System.out.println("weightMap>>"+weightMap.toString());
-            if(weightMap.size()>0){
+            if (weightMap.size() > 0) {
                 //保存称重数据
 //                ProdWeightEntity addeightEntity = _saveWeight(farmID,weightMap);
-                _saveWeight(weightMap);
+                ProdWeightEntity newWeightEntity = _saveWeight(weightMap);
 
                 //计算每天总重
 //                _sendWeightToCenter(JSON.toJSON(addeightEntity).toString());
                 _countDayWeight();
 
-                //发数据到数据中心
+                //发送日总重数据到数据中心
                 sendDayWeightToCenter();
+
+                //计算批次重量
+                ProdBatchWeightEntity updateBatchWeightEntity = _countBatchWeight(newWeightEntity);
+
+                //发送批次数据到数据中心
+                sendBatchWeightToCenter(updateBatchWeightEntity);
+            }
+        }
+    }
+
+    //发送批次数据到数据中心
+    private void sendBatchWeightToCenter(ProdBatchWeightEntity updateBatchWeightEntity) {
+        System.out.println("发送批次数据到数据中心" + updateBatchWeightEntity);
+        //发送数据
+        _doPostSimple(batchWeightService, JSON.toJSONString(updateBatchWeightEntity));
+    }
+
+    @Override
+    public void sendBatchWeightToCenter() {
+//        //查找所有总重
+//        List<ProdDayWeightEntity> dayWeightEntityList = batchWeightRepo.findByFarmIdAndAddTime(dateUtil.fromLongToDate(new Date().getTime(),"yyyy-MM-dd"),farmID);
+//        if(dayWeightEntityList.size()>0){
+//            ProdDayWeightEntity dayWeightEntity = dayWeightEntityList.get(0);
+//            //发送数据
+//            _doPostSimple(weightService,JSON.toJSONString(dayWeightEntity));
+//        }
+    }
+
+
+    //计算批次重量
+    private ProdBatchWeightEntity _countBatchWeight(ProdWeightEntity newWightEntity) throws ParseException {
+        //先查询是否有批次记录,如果没有则新建批次记录;
+        //如果有批次记录,则获取最新一条批次记录,判断该批次入库时间和当前新记录时间比较;
+        //如果<=等于X(batchInterval),则更新当前批次的毛重和净重;
+        //如果>X,则新建一条批次记录。
+        System.out.println("newWightEntity>>" + newWightEntity.toString());
+        String todayText = dateUtil.getTodayDateText();
+        List<ProdBatchWeightEntity> ProdBatchWeightEntityList = batchWeightRepo.findByFarmIdAndToday(farmID, todayText);
+        if (ProdBatchWeightEntityList.size() == 0) {
+            //新建批次记录
+            ProdBatchWeightEntity newBatchWeightEntity = insertBatchWeight(newWightEntity, todayText, 1);
+            //更新每次称重batch_id
+            _updateWeightBatchId(newWightEntity, newBatchWeightEntity.getId());
+            return newBatchWeightEntity;
+        } else {
+            ProdBatchWeightEntity lastBatchWeightEntity = ProdBatchWeightEntityList.get(0);
+            Long intervalTime = newWightEntity.getAddTime().getTime() - lastBatchWeightEntity.getAddTime().getTime();
+            log.info("时间间隔>>" + intervalTime);
+            int hours = (int) ((intervalTime) / (1000 * 60 * 60));
+            log.info("hours>>" + hours);
+            if (hours <= batchIntervalHours) {
+                //更新上一批次数据
+                lastBatchWeightEntity.setGrossWeight(Float.parseFloat(new BigDecimal(lastBatchWeightEntity.getGrossWeight()).add(new BigDecimal(newWightEntity.getGrossWeight())).toString()));
+                lastBatchWeightEntity.setTareWeight(Float.parseFloat(new BigDecimal(lastBatchWeightEntity.getTareWeight()).add(new BigDecimal(newWightEntity.getTareWeight())).toString()));
+                lastBatchWeightEntity.setNetWeight(Float.parseFloat(new BigDecimal(lastBatchWeightEntity.getNetWeight()).add(new BigDecimal(newWightEntity.getNetWeight())).toString()));
+                lastBatchWeightEntity = batchWeightRepo.saveAndFlush(lastBatchWeightEntity);
+                //更新每次称重batch_id
+                _updateWeightBatchId(newWightEntity, lastBatchWeightEntity.getId());
+                return lastBatchWeightEntity;
+            } else {
+                //新增一批次数据
+                int newSort = lastBatchWeightEntity.getBatchSort() + 1;
+                ProdBatchWeightEntity newBatchWeightEntity = insertBatchWeight(newWightEntity, todayText, newSort);
+                //更新每次称重batch_id
+                _updateWeightBatchId(newWightEntity, newBatchWeightEntity.getId());
+                return newBatchWeightEntity;
             }
         }
+
+    }
+
+    //更新每次称重batch_id
+    public void _updateWeightBatchId(ProdWeightEntity newWightEntity, Integer batchId) {
+        newWightEntity.setBatchId(batchId);
+        weightRepo.saveAndFlush(newWightEntity);
+    }
+
+    //插入一条批次记录
+    public ProdBatchWeightEntity insertBatchWeight(ProdWeightEntity newWightEntity, String todayText, Integer sort) {
+        ProdBatchWeightEntity newBatchWeightEntity = new ProdBatchWeightEntity();
+        newBatchWeightEntity.setAddTime(new Timestamp(new Date().getTime()));
+        newBatchWeightEntity.setBatchCode(batchPrefix + spacemark + todayText + spacemark + sort);
+        newBatchWeightEntity.setBatchSort(sort);
+        newBatchWeightEntity.setFarmId(farmID);
+        newBatchWeightEntity.setGrossWeight(newWightEntity.getGrossWeight());
+        newBatchWeightEntity.setTareWeight(newWightEntity.getTareWeight());
+        newBatchWeightEntity.setNetWeight(newWightEntity.getNetWeight());
+        return batchWeightRepo.saveAndFlush(newBatchWeightEntity);
     }
 
     @Override
     public void sendDayWeightToCenter() {
-//        System.out.println("sendDayWeightToCenter 发送每日总重数据到数据中心。");
         //查找日总重
-        List<ProdDayWeightEntity> dayWeightEntityList = dayWeightRepo.findByFarmIdAndAddTime(dateUtil.fromLongToDate(new Date().getTime(),"yyyy-MM-dd"),farmID);
-//        System.out.println("每天总称重数据>>"+dayWeightEntityList.size());
-        if(dayWeightEntityList.size()>0){
+        List<ProdDayWeightEntity> dayWeightEntityList = dayWeightRepo.findByFarmIdAndAddTime(dateUtil.fromLongToDate(new Date().getTime(), "yyyy-MM-dd"), farmID);
+        if (dayWeightEntityList.size() > 0) {
             ProdDayWeightEntity dayWeightEntity = dayWeightEntityList.get(0);
-            System.out.println("dayWeightEntity.toString>>"+dayWeightEntity.toString());
             //发送数据
-            _doPostSimple(JSON.toJSONString(dayWeightEntity));
+            _doPostSimple(dayWeightService, JSON.toJSONString(dayWeightEntity));
         }
     }
 
+
     //发送简单数据
-    public void _doPostSimple(String data){
-        String url = ipAddr + port + weightService;
-        log.info("推送数据地址:"+url);
+    private void _doPostSimple(String apiService, String data) {
+        String url = ipAddr + port + apiService;
+        log.info("推送数据地址:" + url);
         //
-        Map<String,String> paramsMap = new HashMap<String,String>();
+        Map<String, String> paramsMap = new HashMap<String, String>();
         paramsMap.put("data", data);
         //
-        Map<String,Integer> timeoutMap = new HashMap<String,Integer>();
+        Map<String, Integer> timeoutMap = new HashMap<String, Integer>();
         timeoutMap.put("connectTimeout", 5000);
         timeoutMap.put("requestTimeout", 5000);
         timeoutMap.put("socketTimeout", 5000);
-        try{
+        try {
             // 用Post方法推送接口数据
-            httpTemplete.doPost(url,paramsMap,timeoutMap);
+            httpTemplete.doPost(url, paramsMap, timeoutMap);
         } catch (IOException e) {
-            System.out.println("###错误信息:"+e.getMessage());
+            System.out.println("###错误信息:" + e.getMessage());
         }
     }
 
     // 计算每天总重
-    private void _countDayWeight()  {
+    private void _countDayWeight() {
         //
-        String todayDateText = dateUtil.fromLongToDate(new Date().getTime(),"yyyy-MM-dd");
+        String todayDateText = dateUtil.fromLongToDate(new Date().getTime(), "yyyy-MM-dd");
         //计算当天总重
 //        List<Object[]> dayWeightList = weightRepo.countDayWeight(todayDateText,farmID);
 //        JSONObject weightJo = new JSONObject();
@@ -135,15 +235,15 @@ public class WeightImpl implements IWeight {
 //            weightJo.put("tareWeight",weihtObj[2]);
 //            weightJo.put("netWeight",weihtObj[3]);
 //        }
-        List<ProdWeightEntity> dayWeightEntityList = weightRepo.countDayWeight2(todayDateText,farmID);
-        if(dayWeightEntityList.size()>0){
+        List<ProdWeightEntity> dayWeightEntityList = weightRepo.countDayWeight2(todayDateText, farmID);
+        if (dayWeightEntityList.size() > 0) {
             //查找数据
-            ProdDayWeightEntity dayWeightEntity = dayWeightRepo.findByDateAndFarmId(todayDateText,farmID);
-            if(dayWeightEntity != null){
+            ProdDayWeightEntity dayWeightEntity = dayWeightRepo.findByDateAndFarmId(todayDateText, farmID);
+            if (dayWeightEntity != null) {
                 BigDecimal grossBd = new BigDecimal(0);
                 BigDecimal tareBd = new BigDecimal(0);
                 BigDecimal netBd = new BigDecimal(0);
-                for(ProdWeightEntity weightEntity:dayWeightEntityList){
+                for (ProdWeightEntity weightEntity : dayWeightEntityList) {
                     grossBd = new BigDecimal(weightEntity.getGrossWeight()).add(grossBd);
                     tareBd = new BigDecimal(weightEntity.getTareWeight()).add(tareBd);
                     netBd = new BigDecimal(weightEntity.getNetWeight()).add(netBd);
@@ -155,11 +255,11 @@ public class WeightImpl implements IWeight {
                 dayWeightEntity.setTareWeight(Float.parseFloat(tareBd.toString()));
                 dayWeightEntity.setNetWeight(Float.parseFloat(netBd.toString()));
                 dayWeightRepo.save(dayWeightEntity);
-            }else{
+            } else {
                 BigDecimal grossBd = new BigDecimal(0);
                 BigDecimal tareBd = new BigDecimal(0);
                 BigDecimal netBd = new BigDecimal(0);
-                for(ProdWeightEntity weightEntity:dayWeightEntityList){
+                for (ProdWeightEntity weightEntity : dayWeightEntityList) {
                     grossBd = new BigDecimal(weightEntity.getGrossWeight()).add(grossBd);
                     tareBd = new BigDecimal(weightEntity.getTareWeight()).add(tareBd);
                     netBd = new BigDecimal(weightEntity.getNetWeight()).add(netBd);
@@ -188,16 +288,16 @@ public class WeightImpl implements IWeight {
 
     //
     private Map _parseReceiveWeight(String receiveInfo) {
-        receiveInfo = receiveInfo.replace("T","").replace("N","").replace("\n","");
+        receiveInfo = receiveInfo.replace("T", "").replace("N", "").replace("\n", "");
         String[] weightArray = receiveInfo.split("kg");
 
 //        for(int a=0;a<weightArray.length;a++){
 //            System.out.println((a+1)+","+weightArray[a].trim());
 //        }
         Map weightMap = new HashMap();
-        weightMap.put("grossWeight",weightArray[0].trim());
-        weightMap.put("tareWeight",weightArray[1].trim());
-        weightMap.put("netWeight",weightArray[2].trim());
+        weightMap.put("grossWeight", weightArray[0].trim());
+        weightMap.put("tareWeight", weightArray[1].trim());
+        weightMap.put("netWeight", weightArray[2].trim());
         return weightMap;
     }
 }

+ 9 - 3
huimv-farm-device/huimv-farm-local-weight/src/main/resources/application.properties

@@ -8,7 +8,13 @@ spring.jackson.time-zone=GMT+8
 # 数据中心数据推送地址
 dataCenter.ipAddr=http://115.238.57.190
 dataCenter.port=:10091
-# 称重数据服务
-dataCenter.service.weight=/receiver/farm/weight/putDayWeight
+# 日总称重数据服务
+dataCenter.service.dayWeight=/receiver/farm/weight/putDayWeight
+# 批次称重数据服务
+dataCenter.service.batchWeight=/receiver/farm/weight/putBatchWeight
 # 部署牧场ID(base_farm.id)
-local.farmID=1
+local.farmID=1
+# 批次计算间隔
+weight.batch_interval=4
+weight.batch.prefix=W
+weight.batchCode.spacemark=-