|
@@ -1,7 +1,5 @@
|
|
package com.huimv.eartag2.eartag.service.impl;
|
|
package com.huimv.eartag2.eartag.service.impl;
|
|
|
|
|
|
-import cn.hutool.core.convert.Convert;
|
|
|
|
-import cn.hutool.core.date.DateTime;
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.huimv.eartag2.common.dao.entity.*;
|
|
import com.huimv.eartag2.common.dao.entity.*;
|
|
import com.huimv.eartag2.common.dao.repo.*;
|
|
import com.huimv.eartag2.common.dao.repo.*;
|
|
@@ -18,9 +16,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import javax.annotation.Resource;
|
|
|
|
import java.math.BigDecimal;
|
|
import java.math.BigDecimal;
|
|
-import java.sql.Time;
|
|
|
|
import java.sql.Timestamp;
|
|
import java.sql.Timestamp;
|
|
import java.text.ParseException;
|
|
import java.text.ParseException;
|
|
import java.util.*;
|
|
import java.util.*;
|
|
@@ -45,6 +41,8 @@ public class EartagServiceImpl implements IEartagService {
|
|
@Autowired
|
|
@Autowired
|
|
private EartagDataRepo eartagDataRepo;
|
|
private EartagDataRepo eartagDataRepo;
|
|
@Autowired
|
|
@Autowired
|
|
|
|
+ private EartagData2EntityRepo eartagData2Repo;
|
|
|
|
+ @Autowired
|
|
private ICacheService cacheService;
|
|
private ICacheService cacheService;
|
|
@Autowired
|
|
@Autowired
|
|
private IDeviceService deviceService;
|
|
private IDeviceService deviceService;
|
|
@@ -62,6 +60,12 @@ public class EartagServiceImpl implements IEartagService {
|
|
private EartagDeviceRegisterRepo deviceRegisterRepo;
|
|
private EartagDeviceRegisterRepo deviceRegisterRepo;
|
|
@Autowired
|
|
@Autowired
|
|
private EartagDeviceOnlineRepo eartagDeviceOnlineRepo;
|
|
private EartagDeviceOnlineRepo eartagDeviceOnlineRepo;
|
|
|
|
+ @Autowired
|
|
|
|
+ private EartagData2EntityRepo eartagData2EntityRepo;
|
|
|
|
+ @Autowired
|
|
|
|
+ private SysBaseConfigEntityRepo sysBaseConfigEntityRepo;
|
|
|
|
+ @Autowired
|
|
|
|
+ private EartagEartagRegister2EntityRepo eartagEartagRegister2EntityRepo;
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public void handleEartag(JSONObject dataJo) throws ParseException {
|
|
public void handleEartag(JSONObject dataJo) throws ParseException {
|
|
@@ -80,11 +84,11 @@ public class EartagServiceImpl implements IEartagService {
|
|
//获取牧场id
|
|
//获取牧场id
|
|
String farmId = deviceService.getFarmIdByDeviceCode(deviceCode);
|
|
String farmId = deviceService.getFarmIdByDeviceCode(deviceCode);
|
|
if (farmId != null) {
|
|
if (farmId != null) {
|
|
- //{保存耳标流水}
|
|
|
|
|
|
+ //{保存耳标流水(所有耳标数据,可能重复上传)}
|
|
saveEartagFlow(dataJo, nowTimestamp, todayDate, farmId);
|
|
saveEartagFlow(dataJo, nowTimestamp, todayDate, farmId);
|
|
|
|
|
|
|
|
+ //{保存耳标流水,过滤设定时间范围重复耳标上传数据}
|
|
saveEartagFlow2(dataJo, nowTimestamp, todayDate, farmId);
|
|
saveEartagFlow2(dataJo, nowTimestamp, todayDate, farmId);
|
|
- System.out.println("deviceCode >>>>>>>>>>>>>>>>>" + deviceCode);
|
|
|
|
|
|
|
|
//{更新设备注册信息}
|
|
//{更新设备注册信息}
|
|
updateDeviceRegister(deviceCode, nowTimestamp, todayDate, farmId);
|
|
updateDeviceRegister(deviceCode, nowTimestamp, todayDate, farmId);
|
|
@@ -117,6 +121,52 @@ public class EartagServiceImpl implements IEartagService {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * @Method : countTimesByInTime
|
|
|
|
+ * @Description :
|
|
|
|
+ * @Params : [earmark, nowTimestamp]
|
|
|
|
+ * @Return : java.lang.Boolean
|
|
|
|
+ *
|
|
|
|
+ * @Author : ZhuoNing
|
|
|
|
+ * @Date : 2022/8/5
|
|
|
|
+ * @Time : 14:16
|
|
|
|
+ */
|
|
|
|
+ public Map countTimesByInTime(String earmark, Timestamp nowTimestamp) {
|
|
|
|
+ Map countMap = new HashMap();
|
|
|
|
+ //
|
|
|
|
+ Optional<EartagData2Entity> optionEartagData = eartagData2Repo.getLastByEarmark(earmark);
|
|
|
|
+ if (!optionEartagData.isPresent()) {
|
|
|
|
+ log.info("该耳标号无数据["+earmark+"].");
|
|
|
|
+ countMap.put("countTimes",true);
|
|
|
|
+ countMap.put("act",0);
|
|
|
|
+ return countMap;
|
|
|
|
+ } else {
|
|
|
|
+ EartagData2Entity eartagData2Entity = optionEartagData.get();
|
|
|
|
+ Timestamp addTime = eartagData2Entity.getAddTime();
|
|
|
|
+ Integer act = eartagData2Entity.getAct();
|
|
|
|
+ long timeDifference = nowTimestamp.getTime() - addTime.getTime();
|
|
|
|
+ int setTimeDiff = 5;
|
|
|
|
+ Optional<SysBaseConfigEntity> optionConfig = sysBaseConfigEntityRepo.getConfigValue("timeDifference");
|
|
|
|
+ if (optionConfig.isPresent()) {
|
|
|
|
+ setTimeDiff = Integer.parseInt(optionConfig.get().getConfigValue());
|
|
|
|
+ log.info("耳标数据过滤时间差="+setTimeDiff);
|
|
|
|
+ }else{
|
|
|
|
+ log.error("耳标数据过滤时间差属性未配置.");
|
|
|
|
+ }
|
|
|
|
+ if (timeDifference / 1000 < setTimeDiff) {
|
|
|
|
+ // 同一耳标数据距上一次上传数据时间<5秒,视为被其他基站上传。
|
|
|
|
+ countMap.put("countTimes",false);
|
|
|
|
+ return countMap;
|
|
|
|
+ } else {
|
|
|
|
+ // 同一耳标数据距上一次上传数据时间>5
|
|
|
|
+ countMap.put("countTimes",true);
|
|
|
|
+ countMap.put("act",act);
|
|
|
|
+ return countMap;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* @Method : updateDeviceOnline
|
|
* @Method : updateDeviceOnline
|
|
* @Description : 更新设备在线统计
|
|
* @Description : 更新设备在线统计
|
|
@@ -494,9 +544,9 @@ public class EartagServiceImpl implements IEartagService {
|
|
}
|
|
}
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
- private BasePigpenRepo basePigpenRepo;
|
|
|
|
|
|
+ private BasePigpenEntityRepo basePigpenRepo;
|
|
@Autowired
|
|
@Autowired
|
|
- private BizBaseStageRepo bizBaseStageRepo;
|
|
|
|
|
|
+ private BizBaseStageEntityRepo bizBaseStageRepo;
|
|
|
|
|
|
//更新耳标注册数据
|
|
//更新耳标注册数据
|
|
public void updateEartagRegister(String earmark, String deviceCode, String bat, Timestamp nowTimestamp, java.sql.Date todayDate, String farmId) {
|
|
public void updateEartagRegister(String earmark, String deviceCode, String bat, Timestamp nowTimestamp, java.sql.Date todayDate, String farmId) {
|
|
@@ -562,9 +612,6 @@ public class EartagServiceImpl implements IEartagService {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- @Autowired
|
|
|
|
- private EartagEartagRegister2EntityRepo eartagEartagRegister2EntityRepo;
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* @Method : updateEartagRegister2
|
|
* @Method : updateEartagRegister2
|
|
* @Description :
|
|
* @Description :
|
|
@@ -600,7 +647,7 @@ public class EartagServiceImpl implements IEartagService {
|
|
//-- 获取阶段名称 --//
|
|
//-- 获取阶段名称 --//
|
|
Optional<BizBaseStageEntity> optionalBizBaseStageEntity = bizBaseStageRepo.getStageByStageCode(stageCode);
|
|
Optional<BizBaseStageEntity> optionalBizBaseStageEntity = bizBaseStageRepo.getStageByStageCode(stageCode);
|
|
if (!optionalBizBaseStageEntity.isPresent()) {
|
|
if (!optionalBizBaseStageEntity.isPresent()) {
|
|
- log.error("该阶段不存在["+stageCode+"].");
|
|
|
|
|
|
+ log.error("该阶段不存在[" + stageCode + "].");
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
BizBaseStageEntity bizBaseStageEntity = optionalBizBaseStageEntity.get();
|
|
BizBaseStageEntity bizBaseStageEntity = optionalBizBaseStageEntity.get();
|
|
@@ -702,7 +749,7 @@ public class EartagServiceImpl implements IEartagService {
|
|
newEartagRegisterEntity.setAskTime(getAskTime(eartagJo.getString("askTime")));
|
|
newEartagRegisterEntity.setAskTime(getAskTime(eartagJo.getString("askTime")));
|
|
newEartagRegisterEntity.setOther(eartagJo.getString("other"));
|
|
newEartagRegisterEntity.setOther(eartagJo.getString("other"));
|
|
|
|
|
|
- System.out.println("newEartagRegisterEntity>>"+newEartagRegisterEntity.toString());
|
|
|
|
|
|
+ System.out.println("newEartagRegisterEntity>>" + newEartagRegisterEntity.toString());
|
|
|
|
|
|
eartagEartagRegister2EntityRepo.saveAndFlush(newEartagRegisterEntity);
|
|
eartagEartagRegister2EntityRepo.saveAndFlush(newEartagRegisterEntity);
|
|
//{更新耳标注册消息缓存}
|
|
//{更新耳标注册消息缓存}
|
|
@@ -810,65 +857,69 @@ public class EartagServiceImpl implements IEartagService {
|
|
dataEntity.setCreateDate(todayDate);
|
|
dataEntity.setCreateDate(todayDate);
|
|
dataEntity.setFarmId(farmId);
|
|
dataEntity.setFarmId(farmId);
|
|
dataEntity.setAskDate(new java.sql.Date(dateUtil.parseDate(getAskDate(eartagJo.getString("askTime"))).getTime()));
|
|
dataEntity.setAskDate(new java.sql.Date(dateUtil.parseDate(getAskDate(eartagJo.getString("askTime"))).getTime()));
|
|
-
|
|
|
|
-
|
|
|
|
eartagDataRepo.saveAndFlush(dataEntity);
|
|
eartagDataRepo.saveAndFlush(dataEntity);
|
|
|
|
|
|
//最新的耳标数据覆盖老数据
|
|
//最新的耳标数据覆盖老数据
|
|
- cacheService.putEartagFlowToCache(dataEntity.getEarmark(), dataEntity);
|
|
|
|
|
|
+// cacheService.putEartagFlowToCache(dataEntity.getEarmark(), dataEntity);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * @Method : saveEartagFlow2
|
|
|
|
+ * @Description :
|
|
|
|
+ * @Params : [eartagJo, nowTimestamp, todayDate, farmId]
|
|
|
|
+ * @Return : void
|
|
|
|
+ * @Author : ZhuoNing
|
|
|
|
+ * @Date : 2022/8/5
|
|
|
|
+ * @Time : 10:53
|
|
|
|
+ */
|
|
public void saveEartagFlow2(JSONObject eartagJo, Timestamp nowTimestamp, java.sql.Date todayDate, String farmId) throws ParseException {
|
|
public void saveEartagFlow2(JSONObject eartagJo, Timestamp nowTimestamp, java.sql.Date todayDate, String farmId) throws ParseException {
|
|
- DateUtil dateUtil = new DateUtil();
|
|
|
|
- //判断近五分钟内是否有耳标数据 先获取耳标号
|
|
|
|
|
|
+ // 判断近五分钟内是否有耳标数据 先获取耳标号
|
|
String earmark = eartagJo.getString("earmark");
|
|
String earmark = eartagJo.getString("earmark");
|
|
-// List<EartagData2> eartagFlowByEarmark = eartagData2Service.getEartagData(earmark);
|
|
|
|
-// if (null == eartagFlowByEarmark ||eartagFlowByEarmark.size() == 0 ){
|
|
|
|
-// //插数据到数据库里
|
|
|
|
-// EartagData2 dataEntity2 = new EartagData2();
|
|
|
|
-// dataEntity2.setCmdHeader(eartagJo.getString("cmdHeader"));
|
|
|
|
-// dataEntity2.setDevice(eartagJo.getString("device"));
|
|
|
|
-// dataEntity2.setEarmark(eartagJo.getString("earmark"));
|
|
|
|
-// dataEntity2.setBat(Integer.parseInt(eartagJo.getString("bat")));
|
|
|
|
-// dataEntity2.setEarTemp(Integer.parseInt(eartagJo.getString("earTemp")));
|
|
|
|
-// dataEntity2.setEarTemp1(eartagJo.getFloat("earTemp1"));
|
|
|
|
-// dataEntity2.setEnvTemp(Integer.parseInt(eartagJo.getString("envTemp")));
|
|
|
|
-// dataEntity2.setEnvTemp1(eartagJo.getFloat("envTemp1"));
|
|
|
|
-// dataEntity2.setAct(Integer.parseInt(eartagJo.getString("act")));
|
|
|
|
-// dataEntity2.setAct1(Integer.parseInt(eartagJo.getString("act1")));
|
|
|
|
-// dataEntity2.setSignal1(Integer.parseInt(eartagJo.getString("signal")));
|
|
|
|
-// Date date = Convert.toDate(getAskTime(eartagJo.getString("askTime")));
|
|
|
|
-// Date date1 = new Date();
|
|
|
|
-// Timestamp timestamp = new Timestamp(date1.getTime());
|
|
|
|
-// dataEntity2.setAskTime(timestamp);
|
|
|
|
-// dataEntity2.setOther(eartagJo.getString("other"));
|
|
|
|
-// dataEntity2.setAddTime(nowTimestamp);
|
|
|
|
-// dataEntity2.setCreateDate(todayDate);
|
|
|
|
-// dataEntity2.setFarmId(farmId);
|
|
|
|
-// dataEntity2.setAskDate(new java.sql.Date(dateUtil.parseDate(getAskDate(eartagJo.getString("askTime"))).getTime()));
|
|
|
|
-// eartagData2Mapper.insert(dataEntity2);
|
|
|
|
-// }else {
|
|
|
|
-// EartagData2 eartagData2 = eartagFlowByEarmark.get(0);
|
|
|
|
-// eartagData2.setCmdHeader(eartagJo.getString("cmdHeader"));
|
|
|
|
-// eartagData2.setDevice(eartagJo.getString("device"));
|
|
|
|
-// eartagData2.setEarmark(eartagJo.getString("earmark"));
|
|
|
|
-// eartagData2.setBat(Integer.parseInt(eartagJo.getString("bat")));
|
|
|
|
-// eartagData2.setEarTemp(Integer.parseInt(eartagJo.getString("earTemp")));
|
|
|
|
-// eartagData2.setEarTemp1(eartagJo.getFloat("earTemp1"));
|
|
|
|
-// eartagData2.setEnvTemp(Integer.parseInt(eartagJo.getString("envTemp")));
|
|
|
|
-// eartagData2.setEnvTemp1(eartagJo.getFloat("envTemp1"));
|
|
|
|
-// eartagData2.setAct(Integer.parseInt(eartagJo.getString("act")));
|
|
|
|
-// eartagData2.setAct1(Integer.parseInt(eartagJo.getString("act1")));
|
|
|
|
-// eartagData2.setSignal1(Integer.parseInt(eartagJo.getString("signal")));
|
|
|
|
-// Date date = Convert.toDate(getAskTime(eartagJo.getString("askTime")));
|
|
|
|
-// Timestamp timestamp = new Timestamp(date.getTime());
|
|
|
|
-// eartagData2.setAskTime(timestamp);
|
|
|
|
-// eartagData2.setOther(eartagJo.getString("other"));
|
|
|
|
-// eartagData2.setCreateDate(todayDate);
|
|
|
|
-// eartagData2.setFarmId(farmId);
|
|
|
|
-// eartagData2.setAskDate(new java.sql.Date(dateUtil.parseDate(getAskDate(eartagJo.getString("askTime"))).getTime()));
|
|
|
|
-// eartagData2Mapper.updateById(eartagData2);
|
|
|
|
-// }
|
|
|
|
|
|
+ System.out.println("earmark ====================================="+earmark);
|
|
|
|
+ //
|
|
|
|
+ Map countMap = countTimesByInTime(earmark, nowTimestamp);
|
|
|
|
+ Boolean countTimes = Boolean.parseBoolean(countMap.get("countTimes").toString());
|
|
|
|
+ //true:设定过滤时间内无耳标数据上传入库;false:有数据入库;
|
|
|
|
+ if(countTimes){
|
|
|
|
+ Integer act1 = 0;
|
|
|
|
+ Integer act = Integer.parseInt(eartagJo.getString("act"));
|
|
|
|
+ Integer lastAct = Integer.parseInt(countMap.get("act").toString());
|
|
|
|
+ // 耳标重启之后运动量可能为0
|
|
|
|
+ if(act > 0){
|
|
|
|
+ act1 = act - lastAct;
|
|
|
|
+ }
|
|
|
|
+ System.out.println("act ====================="+act);
|
|
|
|
+ System.out.println("lastAct ====================="+lastAct);
|
|
|
|
+ DateUtil dateUtil = new DateUtil();
|
|
|
|
+ EartagData2Entity dataEntity = new EartagData2Entity();
|
|
|
|
+ dataEntity.setCmdHeader(eartagJo.getString("cmdHeader"));
|
|
|
|
+ dataEntity.setDevice(eartagJo.getString("device"));
|
|
|
|
+ dataEntity.setEarmark(eartagJo.getString("earmark"));
|
|
|
|
+ dataEntity.setBat(Integer.parseInt(eartagJo.getString("bat")));
|
|
|
|
+ dataEntity.setEarTemp(Integer.parseInt(eartagJo.getString("earTemp")));
|
|
|
|
+ dataEntity.setEarTemp1(eartagJo.getFloat("earTemp1"));
|
|
|
|
+ dataEntity.setEnvTemp(Integer.parseInt(eartagJo.getString("envTemp")));
|
|
|
|
+ dataEntity.setEnvTemp1(eartagJo.getFloat("envTemp1"));
|
|
|
|
+ dataEntity.setAct(act);
|
|
|
|
+ dataEntity.setAct1(act1);
|
|
|
|
+ dataEntity.setSignal1(Integer.parseInt(eartagJo.getString("signal")));
|
|
|
|
+ dataEntity.setAskTime(getAskTime(eartagJo.getString("askTime")));
|
|
|
|
+ dataEntity.setOther(eartagJo.getString("other"));
|
|
|
|
+ dataEntity.setAddTime(nowTimestamp);
|
|
|
|
+ dataEntity.setCreateDate(todayDate);
|
|
|
|
+ dataEntity.setFarmId(farmId);
|
|
|
|
+ dataEntity.setAskDate(new java.sql.Date(dateUtil.parseDate(getAskDate(eartagJo.getString("askTime"))).getTime()));
|
|
|
|
+ eartagData2EntityRepo.saveAndFlush(dataEntity);
|
|
|
|
+ //更新耳标注册表中的运动量数据
|
|
|
|
+ EartagEartagRegister2Entity eartagRegister2Entity = eartagEartagRegister2EntityRepo.getOneByEarmark(earmark, farmId);
|
|
|
|
+ if(eartagRegister2Entity != null){
|
|
|
|
+ System.out.println("更新耳标="+earmark+","+act1);
|
|
|
|
+ eartagRegister2Entity.setAct(act1);
|
|
|
|
+ eartagEartagRegister2EntityRepo.saveAndFlush(eartagRegister2Entity);
|
|
|
|
+ }
|
|
|
|
+ //最新的耳标数据覆盖老数据
|
|
|
|
+ cacheService.putEartagFlowToCache(dataEntity.getEarmark(), dataEntity);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
//
|
|
//
|