|
@@ -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;
|
|
|
}
|
|
|
}
|