|
@@ -1,14 +1,8 @@
|
|
|
package com.huimv.eartag2.eartag.service.impl;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
-import com.huimv.eartag2.common.dao.entity.EartagDataEntity;
|
|
|
-import com.huimv.eartag2.common.dao.entity.EartagEartagOnlineEntity;
|
|
|
-import com.huimv.eartag2.common.dao.entity.EartagEartagRegisterEntity;
|
|
|
-import com.huimv.eartag2.common.dao.entity.EartarFarmAllStatusEntity;
|
|
|
-import com.huimv.eartag2.common.dao.repo.EartagDataRepo;
|
|
|
-import com.huimv.eartag2.common.dao.repo.EartagEartagOnlineRepo;
|
|
|
-import com.huimv.eartag2.common.dao.repo.EartagEartagRegisterRepo;
|
|
|
-import com.huimv.eartag2.common.dao.repo.EartarFarmAllStatusRepo;
|
|
|
+import com.huimv.eartag2.common.dao.entity.*;
|
|
|
+import com.huimv.eartag2.common.dao.repo.*;
|
|
|
import com.huimv.eartag2.common.utils.DateUtil;
|
|
|
import com.huimv.eartag2.eartag.service.ICacheService;
|
|
|
import com.huimv.eartag2.eartag.service.IDeviceService;
|
|
@@ -18,6 +12,8 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.sql.Time;
|
|
|
import java.sql.Timestamp;
|
|
|
import java.text.ParseException;
|
|
|
import java.util.Date;
|
|
@@ -50,49 +46,192 @@ public class EartagServiceImpl implements IEartagService {
|
|
|
private EartagEartagOnlineRepo eartagOnlineRepo;
|
|
|
@Autowired
|
|
|
private EartarFarmAllStatusRepo farmAllStatusRepo;
|
|
|
+ @Autowired
|
|
|
+ private EartarFarmAllStatusRepo eartagFarmAllStatusRepo;
|
|
|
+ @Autowired
|
|
|
+ private EartagDeviceEartagCountRepo deviceEartagCountRepo;
|
|
|
|
|
|
@Override
|
|
|
public void handleEartag(JSONObject dataJo) throws ParseException {
|
|
|
- //保存耳标流水
|
|
|
- saveEartag(dataJo);
|
|
|
-
|
|
|
//设备编码
|
|
|
String deviceCode = dataJo.getString("device");
|
|
|
-// System.out.println("444 设备编号>>" + deviceCode);
|
|
|
//耳标号
|
|
|
String earmark = dataJo.getString("earmark");
|
|
|
-// System.out.println("444 耳标号>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + earmark);
|
|
|
-
|
|
|
- //--保存设备注册数据(MySQL,Redis)
|
|
|
- //--保存设备状态数据(MySQL,Redis)
|
|
|
String todayDateText = new DateUtil().getTodayDateText();
|
|
|
java.sql.Date todayDate = new java.sql.Date(new java.util.Date().getTime());
|
|
|
Timestamp nowTimestamp = new Timestamp(new java.util.Date().getTime());
|
|
|
-
|
|
|
-// System.out.println("## eartagOnlineSetExpire>>"+eartagOnlineSetExpire);
|
|
|
//--设备在线统计表
|
|
|
//--设备状态每日统计表
|
|
|
//--设备和耳标总状态表
|
|
|
//获取牧场id
|
|
|
String farmId = deviceService.getFarmIdByDeviceCode(deviceCode);
|
|
|
+ //{保存耳标流水}
|
|
|
+ saveEartagFlow(dataJo,nowTimestamp);
|
|
|
|
|
|
- //Step1:更新耳标注册数据
|
|
|
- updateEartagRegister(earmark,deviceCode,nowTimestamp,farmId);
|
|
|
+ //{更新耳标注册数据}
|
|
|
+ updateEartagRegister(earmark,deviceCode,nowTimestamp,todayDate,farmId);
|
|
|
|
|
|
- //Step2:更新耳标在线数据
|
|
|
+ //{更新耳标在线数据}
|
|
|
updateEartagOnline(earmark,deviceCode,todayDateText,todayDate,nowTimestamp,dataJo,farmId);
|
|
|
|
|
|
+ //{更新耳标设备在线统计}
|
|
|
+ updateEartagDeviceOnlineCount(earmark,deviceCode,todayDateText,todayDate,farmId);
|
|
|
+
|
|
|
//判断该设备编号是否存在牧场缓存在线集合当中;
|
|
|
if (!cacheService.isExistEartagOnlineSet(farmId, earmark,todayDateText)) {
|
|
|
-
|
|
|
//更新总状态数据
|
|
|
- updateEartagOfAllStatus(earmark,deviceCode,todayDateText,farmId);
|
|
|
-
|
|
|
+ updateEartagPropertyOfAllStatus(farmId, todayDateText,todayDate,nowTimestamp);
|
|
|
//将耳标号加入到耳标在线集合当中
|
|
|
cacheService.putEartagToOnlineInSet(farmId, earmark,todayDateText);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ //更新耳标设备在线次数
|
|
|
+ private void updateEartagDeviceOnlineCount(String earmark, String deviceCode, String todayDateText, java.sql.Date todayDate, String farmId) {
|
|
|
+ //从缓存读取耳标关联数据
|
|
|
+// Map timesMap = cacheService.getEartagDeviceOnlineCountEntries(earmark,deviceCode,todayDateText);
|
|
|
+// System.out.println("###################### timesMap>>"+timesMap);
|
|
|
+// if(timesMap.size()==0){
|
|
|
+// System.out.println("==0");
|
|
|
+// }else{
|
|
|
+// System.out.println("!=0");
|
|
|
+// }
|
|
|
+ //{读取耳标设备关联统计}
|
|
|
+ List<EartagDeviceEartagCountEntity> deviceEartagCountEntityList = deviceEartagCountRepo.getEartagDeviceCount(earmark,deviceCode,todayDateText);
|
|
|
+ if(deviceEartagCountEntityList.size()==0){
|
|
|
+ Integer count = 1;
|
|
|
+ //{新建耳标设备关联统计}
|
|
|
+ newEartagDeviceCount(earmark,deviceCode,count,todayDateText,todayDate,farmId);
|
|
|
+ }else{
|
|
|
+ //更新次数
|
|
|
+ EartagDeviceEartagCountEntity deviceEartagCountEntity = deviceEartagCountEntityList.get(0);
|
|
|
+ int total = deviceEartagCountEntity.getTotal();
|
|
|
+ ++total;
|
|
|
+ deviceEartagCountEntity.setTotal(total);
|
|
|
+ deviceEartagCountRepo.saveAndFlush(deviceEartagCountEntity);
|
|
|
+ //缓存里计数+1
|
|
|
+ cacheService.countEartagDeviceOnlineTimes(earmark,deviceCode,total,deviceEartagCountEntity);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //新建耳标设备关联统计记录
|
|
|
+ private void newEartagDeviceCount(String earmark, String deviceCode, Integer count, String todayDateText, java.sql.Date todayDate, String farmId) {
|
|
|
+ EartagDeviceEartagCountEntity newDeviceEartagCountEntity = new EartagDeviceEartagCountEntity();
|
|
|
+ newDeviceEartagCountEntity.setDeviceCode(deviceCode);
|
|
|
+ newDeviceEartagCountEntity.setEarmark(earmark);
|
|
|
+ newDeviceEartagCountEntity.setTotal(count);
|
|
|
+ newDeviceEartagCountEntity.setCreateDate(todayDate);
|
|
|
+ newDeviceEartagCountEntity.setFarmId(farmId);
|
|
|
+ deviceEartagCountRepo.saveAndFlush(newDeviceEartagCountEntity);
|
|
|
+ //将数据保存到缓存中
|
|
|
+ cacheService.putEartagDeviceOnlineCount(earmark,deviceCode,todayDateText,newDeviceEartagCountEntity);
|
|
|
+ }
|
|
|
+
|
|
|
+ //更新设备环境信息
|
|
|
+ public void updateEartagPropertyOfAllStatus(String farmId, String todayDateText, java.sql.Date todayDate, Timestamp nowTimestamp) throws ParseException {
|
|
|
+ //{获取处于工作状态的设备数量}
|
|
|
+ Integer deviceTotal = deviceService.getDeviceTotalByFarmIdFromDeviceRegister(farmId);
|
|
|
+ //设备在线数量
|
|
|
+ Integer deviceOnlineSum = cacheService.getDeviceOnlineSum(farmId,todayDateText);
|
|
|
+ //设备离线数量
|
|
|
+ Integer deviceOfflineSum = 0;
|
|
|
+ if(deviceTotal > deviceOnlineSum){
|
|
|
+ deviceOfflineSum = deviceTotal - deviceOnlineSum;
|
|
|
+ }
|
|
|
+ //设备在线率
|
|
|
+ BigDecimal deviceRateBd= new BigDecimal(deviceOnlineSum).divide(new BigDecimal(deviceTotal),1,BigDecimal.ROUND_HALF_UP);
|
|
|
+ Float deviceOnlineRate = Float.parseFloat(deviceRateBd.toString());
|
|
|
+ //{获取注销设备数量}
|
|
|
+ Integer deviceCancelSum = deviceService.getDeviceCancelTotalFromDeviceRegister(farmId);
|
|
|
+ //耳标总数量 eartagService.
|
|
|
+ Integer eartagTotal = getEartagRegTotalByFarmId(farmId);
|
|
|
+ //耳标在线数量 eartagService.
|
|
|
+ Integer eartagOnlineSum = getEartagOnelineTotal(farmId,todayDateText);
|
|
|
+ eartagOnlineSum++;
|
|
|
+ //耳标在线率
|
|
|
+ BigDecimal eartagRateBd= new BigDecimal(eartagOnlineSum).divide(new BigDecimal(eartagTotal),1,BigDecimal.ROUND_HALF_UP);
|
|
|
+ Float eartagOnlineRate = Float.parseFloat(eartagRateBd.toString());
|
|
|
+ //
|
|
|
+ EartarFarmAllStatusEntity farmAllStatusEntity = eartagFarmAllStatusRepo.getOneByFarmIdAndCreateDate(farmId,todayDateText);
|
|
|
+ if(farmAllStatusEntity == null){
|
|
|
+ //{新建设备状态记录并更新缓存cache}
|
|
|
+ newFarmAllStatus(farmId,todayDate,nowTimestamp,deviceTotal,deviceOnlineSum,deviceOfflineSum,deviceCancelSum,deviceOnlineRate,eartagTotal,eartagOnlineSum,eartagOnlineRate);
|
|
|
+ }else{
|
|
|
+ //计算设备状态并更新缓存cache(在线数+1,离线数-1,计算设备在线率)
|
|
|
+ farmAllStatusEntity.setDeviceTotal(deviceTotal);
|
|
|
+ farmAllStatusEntity.setDeviceOnline(deviceOnlineSum);
|
|
|
+ farmAllStatusEntity.setDeviceOffline(deviceOfflineSum);
|
|
|
+ farmAllStatusEntity.setDeviceRate(deviceOnlineRate);
|
|
|
+ farmAllStatusEntity.setDeviceCancel(deviceCancelSum);
|
|
|
+ farmAllStatusEntity.setEartagTotal(eartagTotal);
|
|
|
+ farmAllStatusEntity.setEartagOnline(eartagOnlineSum);
|
|
|
+ farmAllStatusEntity.setEartagRate(eartagOnlineRate);
|
|
|
+ farmAllStatusEntity.setUpdateTime(nowTimestamp);
|
|
|
+ eartagFarmAllStatusRepo.saveAndFlush(farmAllStatusEntity);
|
|
|
+ //{同步更新牧场总状态缓存相关字段}
|
|
|
+ cacheService.flashAllStatusCache(farmId,farmAllStatusEntity);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //# 新建总状态记录 #
|
|
|
+ private void newFarmAllStatus(String farmId, java.sql.Date todayDate, Timestamp nowTimestamp, Integer deviceTotal, Integer deviceOnlineSum, Integer deviceOfflineSum, Integer deviceCancelSum, Float deviceOnlineRate, Integer eartagTotal, Integer eartagOnlineSum, Float eartagOnlineRate) {
|
|
|
+ Integer liveStatus = 1;
|
|
|
+ if(deviceService.getDeviceSum(farmId,liveStatus) == 0){
|
|
|
+ log.error("该牧场没有任何处于工作状态的采集器设备.");
|
|
|
+ }else{
|
|
|
+ //# 计算耳标总数(耳标注册表)
|
|
|
+ EartarFarmAllStatusEntity farmAllStatusEntity = new EartarFarmAllStatusEntity();
|
|
|
+ farmAllStatusEntity.setDeviceTotal(deviceTotal);
|
|
|
+ farmAllStatusEntity.setDeviceOnline(deviceOnlineSum);
|
|
|
+ farmAllStatusEntity.setDeviceOffline(deviceOfflineSum);
|
|
|
+ farmAllStatusEntity.setDeviceRate(deviceOnlineRate);
|
|
|
+ farmAllStatusEntity.setDeviceCancel(deviceCancelSum);
|
|
|
+ farmAllStatusEntity.setEartagTotal(eartagTotal);
|
|
|
+ farmAllStatusEntity.setEartagOnline(eartagOnlineSum);
|
|
|
+ farmAllStatusEntity.setEartagRate(eartagOnlineRate);
|
|
|
+ farmAllStatusEntity.setFarmId(farmId);
|
|
|
+ farmAllStatusEntity.setUpdateTime(nowTimestamp);
|
|
|
+ farmAllStatusEntity.setCreateDate(todayDate);
|
|
|
+ eartagFarmAllStatusRepo.saveAndFlush(farmAllStatusEntity);
|
|
|
+ //设置总状态缓存
|
|
|
+ cacheService.flashAllStatusCache(farmId,farmAllStatusEntity);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //
|
|
|
+ public Integer getEartagOnelineTotal(String farmId, String todayDateText) {
|
|
|
+ return cacheService.getEartagOnlineTotalFromCache(farmId,todayDateText);
|
|
|
+ }
|
|
|
+
|
|
|
+ //
|
|
|
+ public Integer getEartagRegTotalByFarmId(String farmId){
|
|
|
+ //从缓存读取耳标总数
|
|
|
+ Integer eartagTotal = cacheService.getEartagTotal(farmId);
|
|
|
+ if(eartagTotal != null){
|
|
|
+ return eartagTotal;
|
|
|
+ }else{
|
|
|
+// List<Object[]> eartagRegList = eartagRegisterRepo.getEartagCountByFarmId(farmId);
|
|
|
+// Object[] eartagRegObj = (Object[]) eartagRegList.get(0);
|
|
|
+// Integer total = Integer.parseInt(eartagRegObj[0].toString());
|
|
|
+ ///
|
|
|
+ Integer liveStatus = 1;
|
|
|
+ //{读取处于工作状态的耳标总数}
|
|
|
+ Integer total = getEartagSum(farmId,liveStatus);
|
|
|
+ //设置耳标总数缓存
|
|
|
+ cacheService.putEartagTotal(total,farmId);
|
|
|
+ //处于工作状态的耳标总数
|
|
|
+ return total;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //计算处于不同生存状态的耳标数量
|
|
|
+ private Integer getEartagSum(String farmId,Integer liveStatus){
|
|
|
+ List<Object[]> eartagRegList = eartagRegisterRepo.getEartagCountByFarmId(farmId,liveStatus);
|
|
|
+ Object[] eartagRegObj = (Object[]) eartagRegList.get(0);
|
|
|
+ //处于工作状态的耳标总数
|
|
|
+ return Integer.parseInt(eartagRegObj[0].toString());
|
|
|
+ }
|
|
|
+
|
|
|
//状态更新
|
|
|
public void updateEartagOfAllStatus(String earmark, String deviceCode,String todayDateText, String farmId) {
|
|
|
//# 判断设备和耳标总状态表是否存在
|
|
@@ -101,7 +240,7 @@ public class EartagServiceImpl implements IEartagService {
|
|
|
EartarFarmAllStatusEntity farmAllStatusEntity = farmAllStatusRepo.getOneByFarmIdAndCreateDate(farmId,todayDateText);
|
|
|
if(farmAllStatusEntity == null){
|
|
|
//#获取耳标注册数量#
|
|
|
- int eartagTotal = getEartagRegTotalByFarmId(farmId);
|
|
|
+ int eartagTotal = getEartagRegTotalByFarmId_1(farmId);
|
|
|
//#总状态记录不存在;
|
|
|
EartarFarmAllStatusEntity newFarmAllStatusEntity = new EartarFarmAllStatusEntity();
|
|
|
newFarmAllStatusEntity.setEartagTotal(eartagTotal);
|
|
@@ -116,7 +255,7 @@ public class EartagServiceImpl implements IEartagService {
|
|
|
}
|
|
|
|
|
|
//获取耳标注册数量
|
|
|
- public Integer getEartagRegTotalByFarmId(String farmId){
|
|
|
+ public Integer getEartagRegTotalByFarmId_1(String farmId){
|
|
|
int eartagTotal = 0;
|
|
|
List<Object[]> eartagRegList = eartagRegisterRepo.getEartagCountByFarmId(farmId);
|
|
|
Object[] eartagRegObj = (Object[]) eartagRegList.get(0);
|
|
@@ -129,6 +268,46 @@ public class EartagServiceImpl implements IEartagService {
|
|
|
//# 判断耳标在线统计数据是否存在
|
|
|
// 存在,则更新上传记录数、最后上传时间、环境温度,耳根温度,并将上面数据更新到缓存数据中;
|
|
|
// 不存在,则创建耳标在线记录,并更新到缓存数据中;
|
|
|
+ String envTemp = eartagJo.getString("envTemp1");
|
|
|
+ String earTemp = eartagJo.getString("earTemp1");
|
|
|
+ EartagEartagOnlineEntity eartagOnlineEntity = eartagOnlineRepo.getOneByEarmarkAndFarmId(earmark,todayDateText,farmId);
|
|
|
+ if(eartagOnlineEntity == null){
|
|
|
+ int total = 1;
|
|
|
+ //{新建耳标在线记录}
|
|
|
+ newEartagOnline(earmark,total,nowTimestamp,earTemp,envTemp,todayDate,farmId);
|
|
|
+ }else{
|
|
|
+ int total = eartagOnlineEntity.getTotal();
|
|
|
+ eartagOnlineEntity.setTotal(++total);
|
|
|
+ eartagOnlineEntity.setLastTime(nowTimestamp);
|
|
|
+ eartagOnlineEntity.setEnvTemp(envTemp);
|
|
|
+ eartagOnlineEntity.setEartagTemp(earTemp);
|
|
|
+ eartagOnlineRepo.saveAndFlush(eartagOnlineEntity);
|
|
|
+ //更新耳标在线统计
|
|
|
+ cacheService.putEartagOnlineCount(earmark,eartagOnlineEntity);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //新建耳标在线记录
|
|
|
+ private void newEartagOnline(String earmark, int total, Timestamp nowTimestamp, String earTemp, String envTemp, java.sql.Date todayDate, String farmId) {
|
|
|
+ EartagEartagOnlineEntity newEartagOnlineEntity = new EartagEartagOnlineEntity();
|
|
|
+ newEartagOnlineEntity.setEarmark(earmark);
|
|
|
+ newEartagOnlineEntity.setTotal(total);
|
|
|
+ newEartagOnlineEntity.setFirstTime(nowTimestamp);
|
|
|
+ newEartagOnlineEntity.setLastTime(nowTimestamp);
|
|
|
+ newEartagOnlineEntity.setEnvTemp(envTemp);
|
|
|
+ newEartagOnlineEntity.setEartagTemp(earTemp);
|
|
|
+ newEartagOnlineEntity.setAddDate(todayDate);
|
|
|
+ newEartagOnlineEntity.setFarmId(farmId);
|
|
|
+ eartagOnlineRepo.saveAndFlush(newEartagOnlineEntity);
|
|
|
+ //更新耳标在线统计
|
|
|
+ cacheService.putEartagOnlineCount(earmark,newEartagOnlineEntity);
|
|
|
+ }
|
|
|
+
|
|
|
+ //更新耳标在线数据
|
|
|
+ public void updateEartagOnline_old1(String earmark, String deviceCode, String todayDateText, java.sql.Date todayDate, Timestamp nowTimestamp, JSONObject eartagJo, String farmId) {
|
|
|
+ //# 判断耳标在线统计数据是否存在
|
|
|
+ // 存在,则更新上传记录数、最后上传时间、环境温度,耳根温度,并将上面数据更新到缓存数据中;
|
|
|
+ // 不存在,则创建耳标在线记录,并更新到缓存数据中;
|
|
|
//耳根温度
|
|
|
String earTemp = eartagJo.getString("earTemp1");
|
|
|
//环境温度
|
|
@@ -162,7 +341,52 @@ public class EartagServiceImpl implements IEartagService {
|
|
|
}
|
|
|
|
|
|
//更新耳标注册数据
|
|
|
- public void updateEartagRegister(String earmark, String deviceCode, Timestamp nowTimestamp, String farmId) {
|
|
|
+ public void updateEartagRegister(String earmark, String deviceCode, Timestamp nowTimestamp, java.sql.Date todayDate, String farmId) {
|
|
|
+ //# 判断耳标注册表是否存在;
|
|
|
+ // 如果不存在就注册耳标;
|
|
|
+ // 如果存在就更新活动状态(active_status)和活动状态更新时间(active_time)
|
|
|
+ Integer registerType = 1;
|
|
|
+ Integer activeStatus = 1;
|
|
|
+ Integer liveStatus = 1; //
|
|
|
+ EartagEartagRegisterEntity eartagRegisterEntity = eartagRegisterRepo.getOneByEarmark(earmark,farmId);
|
|
|
+ if(eartagRegisterEntity == null){
|
|
|
+ //{新建耳标注册信息}
|
|
|
+ newEartagRegister(earmark,nowTimestamp,deviceCode,registerType,activeStatus,liveStatus,todayDate,farmId);
|
|
|
+ }else{
|
|
|
+ eartagRegisterEntity.setLastTime(nowTimestamp);
|
|
|
+ eartagRegisterEntity.setLastDevice(deviceCode);
|
|
|
+ eartagRegisterEntity.setActiveStatus(activeStatus);
|
|
|
+ eartagRegisterEntity.setActiveTime(nowTimestamp);
|
|
|
+ eartagRegisterRepo.saveAndFlush(eartagRegisterEntity);
|
|
|
+ //更新耳标注册消息缓存
|
|
|
+ cacheService.putEartagRegister(earmark,eartagRegisterEntity);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //新建耳标注册信息
|
|
|
+ private void newEartagRegister(String earmark, Timestamp nowTimestamp, String deviceCode, Integer registerType, Integer activeStatus, Integer liveStatus, java.sql.Date todayDate, String farmId) {
|
|
|
+ //#创建耳标注册记录#
|
|
|
+ EartagEartagRegisterEntity newEartagRegisterEntity = new EartagEartagRegisterEntity();
|
|
|
+ newEartagRegisterEntity.setEarmark(earmark);
|
|
|
+ newEartagRegisterEntity.setFirstTime(nowTimestamp);
|
|
|
+ newEartagRegisterEntity.setLastTime(nowTimestamp);
|
|
|
+ newEartagRegisterEntity.setFirstDevice(deviceCode);
|
|
|
+ newEartagRegisterEntity.setBelongDevice(deviceCode);
|
|
|
+ newEartagRegisterEntity.setLastDevice(deviceCode);
|
|
|
+ newEartagRegisterEntity.setRegisterTime(nowTimestamp);
|
|
|
+ newEartagRegisterEntity.setRegisterType(registerType);
|
|
|
+ newEartagRegisterEntity.setFarmId(farmId);
|
|
|
+ newEartagRegisterEntity.setActiveStatus(activeStatus);
|
|
|
+ newEartagRegisterEntity.setActiveTime(nowTimestamp);
|
|
|
+ newEartagRegisterEntity.setLiveStatus(liveStatus);
|
|
|
+ newEartagRegisterEntity.setCreateDate(todayDate);
|
|
|
+ eartagRegisterRepo.saveAndFlush(newEartagRegisterEntity);
|
|
|
+ //{更新耳标注册消息缓存}
|
|
|
+ cacheService.putEartagRegister(earmark,newEartagRegisterEntity);
|
|
|
+ }
|
|
|
+
|
|
|
+ //更新耳标注册数据
|
|
|
+ public void updateEartagRegister_old(String earmark, String deviceCode, Timestamp nowTimestamp, String farmId) {
|
|
|
//# 判断耳标注册表是否存在;
|
|
|
// 如果不存在就注册耳标;
|
|
|
// 如果存在就更新活动状态(active_status)和活动状态更新时间(active_time)
|
|
@@ -205,31 +429,40 @@ public class EartagServiceImpl implements IEartagService {
|
|
|
* @Time : 15:25
|
|
|
*/
|
|
|
@Override
|
|
|
- public Integer countAct(String deviceCode, String nowAct) {
|
|
|
+ public Integer countAct(String earmark, String nowAct) {
|
|
|
//读取hash
|
|
|
- Object actObj = redisTemplate.opsForHash().get(deviceCode, "act");
|
|
|
- if (actObj == null) {
|
|
|
- //--初始化最新的redis记录
|
|
|
- initRedisObj(deviceCode);
|
|
|
- Object lastAct = redisTemplate.opsForHash().get(deviceCode, "act");
|
|
|
- if (lastAct == null) {
|
|
|
- log.error("#--- redis数据库有问题,请检查redis是否能正常连接 ---# ");
|
|
|
- return 0;
|
|
|
- }
|
|
|
+ Object lastActObj = cacheService.getEartagLastAct(earmark);
|
|
|
+ cacheService.putEartagAct(earmark,nowAct);
|
|
|
+ //
|
|
|
+// Object testActObj = cacheService.getEartagLastAct(earmark);
|
|
|
+// System.out.println("## TEST testActObj>>"+testActObj);
|
|
|
+ if (lastActObj != null) {
|
|
|
+ return Integer.parseInt(nowAct) - Integer.parseInt(lastActObj.toString());
|
|
|
+ }else{
|
|
|
+ //{初始化最新的redis记录}
|
|
|
+// cacheService.putEartagAct(earmark,nowAct);
|
|
|
+ //第一次的运动量计数
|
|
|
+// return Integer.parseInt(nowAct) - 0;
|
|
|
+ return 0;
|
|
|
+ //验证刚才初始化结果
|
|
|
+// Object lastAct = cacheService.getEartagLastAct(earmark);
|
|
|
+// if (lastAct == null) {
|
|
|
+// log.error("#--- redis数据库有问题,请检查redis是否能正常连接 ---# ");
|
|
|
+// return 0;
|
|
|
+// }else{
|
|
|
+// return Integer.parseInt(lastAct.toString());
|
|
|
+// }
|
|
|
}
|
|
|
- Object lastAct = redisTemplate.opsForHash().get(deviceCode, "act");
|
|
|
- Integer act1 = Integer.parseInt(nowAct) - Integer.parseInt(lastAct.toString());
|
|
|
-// redisTemplate.opsForHash().put(deviceCode, "act1", act1);
|
|
|
- return act1;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @Author : ZhuoNing
|
|
|
* @Date : 2022/2/9
|
|
|
* @Time : 14:33
|
|
|
+ * @param eartagJo
|
|
|
+ * @param nowTimestamp
|
|
|
*/
|
|
|
- @Override
|
|
|
- public void saveEartag(JSONObject eartagJo) {
|
|
|
+ public void saveEartagFlow(JSONObject eartagJo, Timestamp nowTimestamp) {
|
|
|
EartagDataEntity dataEntity = new EartagDataEntity();
|
|
|
dataEntity.setCmdHeader(eartagJo.getString("cmdHeader"));
|
|
|
dataEntity.setDevice(eartagJo.getString("device"));
|
|
@@ -238,33 +471,17 @@ public class EartagServiceImpl implements IEartagService {
|
|
|
dataEntity.setEarTemp(Integer.parseInt(eartagJo.getString("earTemp")));
|
|
|
dataEntity.setEarTemp1(eartagJo.getFloat("earTemp1"));
|
|
|
dataEntity.setEnvTemp(Integer.parseInt(eartagJo.getString("envTemp")));
|
|
|
- dataEntity.setEnvTemp1(Integer.parseInt(eartagJo.getString("envTemp")));
|
|
|
+ dataEntity.setEnvTemp1(eartagJo.getFloat("envTemp1"));
|
|
|
dataEntity.setAct(Integer.parseInt(eartagJo.getString("act")));
|
|
|
dataEntity.setAct1(Integer.parseInt(eartagJo.getString("act1")));
|
|
|
dataEntity.setSignal1(Integer.parseInt(eartagJo.getString("signal")));
|
|
|
dataEntity.setAskTime(getAskTime(eartagJo.getString("askTime")));
|
|
|
dataEntity.setOther(eartagJo.getString("other"));
|
|
|
- dataEntity.setAddTime(new Timestamp(new Date().getTime()));
|
|
|
+ dataEntity.setAddTime(nowTimestamp);
|
|
|
eartagDataRepo.saveAndFlush(dataEntity);
|
|
|
|
|
|
//最新的耳标数据覆盖老数据
|
|
|
- Map<String, String> map = new HashMap<>();
|
|
|
- map.put("cmdHeader", dataEntity.getCmdHeader());
|
|
|
- map.put("device", dataEntity.getDevice());
|
|
|
- map.put("earmark", dataEntity.getEarmark());
|
|
|
- map.put("bat", dataEntity.getBat().toString());
|
|
|
- map.put("earTemp", dataEntity.getEarTemp().toString());
|
|
|
- map.put("earTemp1", dataEntity.getEarTemp1().toString());
|
|
|
- map.put("envTemp", dataEntity.getEnvTemp().toString());
|
|
|
- map.put("envTemp1", dataEntity.getEnvTemp1().toString());
|
|
|
- map.put("act", dataEntity.getAct().toString());
|
|
|
- map.put("act1", dataEntity.getAct1().toString());
|
|
|
- map.put("signal1", dataEntity.getSignal1().toString());
|
|
|
- map.put("askTime", dataEntity.getAskTime());
|
|
|
- map.put("other", dataEntity.getOther());
|
|
|
- map.put("addTime", dataEntity.getAddTime().toString());
|
|
|
- //为hash结构设置多个键值对(hmset)
|
|
|
- redisTemplate.opsForHash().putAll(dataEntity.getDevice(), map);
|
|
|
+ cacheService.putEartagFlowToCache(dataEntity.getEarmark(),dataEntity);
|
|
|
}
|
|
|
|
|
|
//
|