Kaynağa Gözat

添加eartag_eartag_register2数据

zhuoning 2 yıl önce
ebeveyn
işleme
52628aa9c5

+ 2 - 0
huimv-eartag2-platform/huimv-eartag2-eartag/src/main/java/com/huimv/eartag2/eartag/service/ICacheService.java

@@ -65,6 +65,8 @@ public interface ICacheService {
     //更新耳标注册消息缓存
     void putEartagRegister(String earmark, EartagEartagRegisterEntity eartagRegisterEntity);
 
+    void putEartagRegister(String earmark, EartagEartagRegister2Entity eartagRegisterEntity);
+
     //更新耳标在线统计
     void putEartagOnlineCount(String earmark, EartagEartagOnlineEntity eartagOnlineEntity);
 

+ 22 - 0
huimv-eartag2-platform/huimv-eartag2-eartag/src/main/java/com/huimv/eartag2/eartag/service/impl/CacheServiceImpl.java

@@ -424,6 +424,28 @@ public class CacheServiceImpl implements ICacheService {
         redisTemplate.opsForHash().putAll(earmarkOnlineKey, map);
     }
 
+    @Override
+    public void putEartagRegister(String earmark, EartagEartagRegister2Entity eartagRegisterEntity) {
+        Map map = new HashMap();
+//        map.put("id", eartagRegisterEntity.getId());
+        map.put("earmark",eartagRegisterEntity.getEarmark());
+        map.put("firstTime",eartagRegisterEntity.getFirstTime());
+        map.put("lastTime",eartagRegisterEntity.getLastTime());
+        map.put("firstDevice",eartagRegisterEntity.getFirstDevice());
+        map.put("belongDevice",eartagRegisterEntity.getBelongDevice());
+        map.put("lastDevice",eartagRegisterEntity.getLastDevice());
+        map.put("registerTime",eartagRegisterEntity.getRegisterTime());
+        map.put("registerType",eartagRegisterEntity.getRegisterType());
+        map.put("remark",eartagRegisterEntity.getRemark());
+        map.put("farmId",eartagRegisterEntity.getFarmId());
+        map.put("activeStatus",eartagRegisterEntity.getActiveStatus());
+        map.put("activeTime",eartagRegisterEntity.getActiveTime());
+        map.put("liveStatus",eartagRegisterEntity.getLiveStatus());
+        //更新缓存
+        String earmarkOnlineKey = BizConst.EARTAG_REGISTER_PREFIX + earmark;
+        redisTemplate.opsForHash().putAll(earmarkOnlineKey, map);
+    }
+
     /**
      * @Method      : putEartagOnlineCount
      * @Description : 

+ 286 - 117
huimv-eartag2-platform/huimv-eartag2-eartag/src/main/java/com/huimv/eartag2/eartag/service/impl/EartagServiceImpl.java

@@ -23,10 +23,7 @@ import java.math.BigDecimal;
 import java.sql.Time;
 import java.sql.Timestamp;
 import java.text.ParseException;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
 /**
  * @Project : huimv.shiwan
@@ -39,7 +36,7 @@ import java.util.Map;
 @Service
 @Slf4j
 public class EartagServiceImpl implements IEartagService {
-//    @Resource
+    //    @Resource
 //    private IEartagData2Service eartagData2Service;
 //    @Resource
 //    private  EartagData2Mapper eartagData2Mapper;
@@ -82,27 +79,30 @@ public class EartagServiceImpl implements IEartagService {
         //--设备和耳标总状态表
         //获取牧场id
         String farmId = deviceService.getFarmIdByDeviceCode(deviceCode);
-        if(farmId != null) {
+        if (farmId != null) {
             //{保存耳标流水}
             saveEartagFlow(dataJo, nowTimestamp, todayDate, farmId);
 
             saveEartagFlow2(dataJo, nowTimestamp, todayDate, farmId);
-            System.out.println("deviceCode >>>>>>>>>>>>>>>>>"+deviceCode);
+            System.out.println("deviceCode >>>>>>>>>>>>>>>>>" + deviceCode);
 
             //{更新设备注册信息}
             updateDeviceRegister(deviceCode, nowTimestamp, todayDate, farmId);
 
             //{更新耳标注册信息}
-            updateEartagRegister(earmark, deviceCode,bat, nowTimestamp, todayDate, farmId);
+//            updateEartagRegister(earmark, deviceCode, bat, nowTimestamp, todayDate, farmId);
+
+            //{更新耳标注册信息}
+            updateEartagRegister2(earmark, deviceCode, bat, nowTimestamp, todayDate, farmId, dataJo);
 
             //{更新设备在线统计}
-            updateDeviceOnline(earmark, deviceCode, todayDateText,nowTimestamp, todayDate, dataJo,farmId);
+            updateDeviceOnline(earmark, deviceCode, todayDateText, nowTimestamp, todayDate, dataJo, farmId);
 
             //{更新耳标在线统计}
             updateEartagOnline(earmark, deviceCode, todayDateText, todayDate, nowTimestamp, dataJo, farmId);
 
             //{更新耳标设备在线关联统计}
-            updateEartagDeviceOnlineCount(earmark, deviceCode, todayDateText, todayDate,nowTimestamp, farmId);
+            updateEartagDeviceOnlineCount(earmark, deviceCode, todayDateText, todayDate, nowTimestamp, farmId);
 
             //判断该设备编号是否存在牧场缓存在线集合当中;
             if (!cacheService.isExistEartagOnlineSet(farmId, earmark, todayDateText)) {
@@ -111,30 +111,29 @@ public class EartagServiceImpl implements IEartagService {
                 //将耳标号加入到耳标在线集合当中
                 cacheService.putEartagToOnlineInSet(farmId, earmark, todayDateText);
             }
-            System.out.println("## END 耳标数据。 "+new DateUtil().getTodayMissionText());
-        }else{
-            log.error("出错:该请求的FarmId为空.请检查该设备编码["+deviceCode+"]是否存在FarmId.");
+            System.out.println("## END 耳标数据。 " + new DateUtil().getTodayMissionText());
+        } else {
+            log.error("出错:该请求的FarmId为空.请检查该设备编码[" + deviceCode + "]是否存在FarmId.");
         }
     }
 
     /**
-     * @Method      : updateDeviceOnline
+     * @Method : updateDeviceOnline
      * @Description : 更新设备在线统计
-     * @Params      : [earmark, deviceCode, nowTimestamp, todayDate, farmId]
-     * @Return      : void
-     * 
-     * @Author      : ZhuoNing
-     * @Date        : 2022/3/29       
-     * @Time        : 9:13
+     * @Params : [earmark, deviceCode, nowTimestamp, todayDate, farmId]
+     * @Return : void
+     * @Author : ZhuoNing
+     * @Date : 2022/3/29
+     * @Time : 9:13
      */
     private void updateDeviceOnline(String earmark, String deviceCode, String todayDateText, Timestamp nowTimestamp, java.sql.Date todayDate, JSONObject dataJo, String farmId) {
         // 查询在线统计;
         // 不存在,则新建设备在线统计
         //存在,则直接更新耳标数量;
-        String hashKey = BizConst.DEVICE_REGISTER_PREFIX+deviceCode;
+        String hashKey = BizConst.DEVICE_REGISTER_PREFIX + deviceCode;
         //计算设备别名
-        String alias = (String)cacheService.getHashProperty(hashKey,"deviceAlias");
-        String earTemp1 =  dataJo.getString("earTemp1");
+        String alias = (String) cacheService.getHashProperty(hashKey, "deviceAlias");
+        String earTemp1 = dataJo.getString("earTemp1");
         String envTemp1 = dataJo.getString("envTemp1");
 
         //读取在线日期
@@ -153,8 +152,8 @@ public class EartagServiceImpl implements IEartagService {
             Integer eartagTotal = 1;
             Integer liveStatus = 1;
             //{新建设备在线统计并创建缓存}
-            newDeviceOnline(deviceCode,alias,heartbeatTotal,envtempTotal,eartagTotal,todayDate,farmId,heartbeatFirstTime,heartbeatLastTime,envtempFirstTime,envtempLastTime,firstTime,lastTime,eartagFirstTime,eartagLastTime,earTemp1,envTemp1,liveStatus);
-        }else{
+            newDeviceOnline(deviceCode, alias, heartbeatTotal, envtempTotal, eartagTotal, todayDate, farmId, heartbeatFirstTime, heartbeatLastTime, envtempFirstTime, envtempLastTime, firstTime, lastTime, eartagFirstTime, eartagLastTime, earTemp1, envTemp1, liveStatus);
+        } else {
 
             eartagDeviceOnlineEntity.setAlias(alias);
             int eartagTotal = eartagDeviceOnlineEntity.getEartagTotal();
@@ -163,32 +162,32 @@ public class EartagServiceImpl implements IEartagService {
             eartagDeviceOnlineEntity.setEartagLastTime(nowTimestamp);
             eartagDeviceOnlineEntity.setLastEartag(earTemp1);
             eartagDeviceOnlineEntity.setLastEnvtemp(envTemp1);
-            if(eartagDeviceOnlineEntity.getFirstTime() == null){
+            if (eartagDeviceOnlineEntity.getFirstTime() == null) {
                 eartagDeviceOnlineEntity.setFirstTime(nowTimestamp);
             }
             eartagDeviceOnlineEntity.setLastTime(nowTimestamp);
             eartagDeviceOnlineRepo.saveAndFlush(eartagDeviceOnlineEntity);
             //更新设备在线缓存数据
-            cacheService.putDeviceInOnline(deviceCode,eartagDeviceOnlineEntity);
+            cacheService.putDeviceInOnline(deviceCode, eartagDeviceOnlineEntity);
         }
 
     }
-    
+
     /**
-     * @Method      : newDeviceOnline
+     * @Method : newDeviceOnline
      * @Description : 新建设备在线
-     * @Params      : [deviceCode, alias, heartbeatTotal, envtempTotal, eartagTotal, todayDate, farmId, heartbeatFirstTime, heartbeatLastTime, envtempFirstTime, envtempLastTime, firstTime, lastTime, eartagFirstTime, eartagLastTime]
-     * @Return      : void
-     * 
-     * @Author      : ZhuoNing
-     * @Date        : 2022/3/29       
-     * @Time        : 9:44
+     * @Params : [deviceCode, alias, heartbeatTotal, envtempTotal, eartagTotal, todayDate, farmId, heartbeatFirstTime, heartbeatLastTime, envtempFirstTime, envtempLastTime, firstTime, lastTime, eartagFirstTime, eartagLastTime]
+     * @Return : void
+     * @Author : ZhuoNing
+     * @Date : 2022/3/29
+     * @Time : 9:44
      */
     private void newDeviceOnline(String deviceCode, String alias, Integer heartbeatTotal, Integer envtempTotal, Integer eartagTotal, java.sql.Date todayDate, String farmId, Timestamp heartbeatFirstTime, Timestamp heartbeatLastTime, Timestamp envtempFirstTime, Timestamp envtempLastTime, Timestamp firstTime, Timestamp lastTime, Timestamp eartagFirstTime, Timestamp eartagLastTime, String earTemp1, String envTemp1, Integer liveStatus) {
         EartagDeviceOnlineEntity newEartagDeviceOnlineEntity = new EartagDeviceOnlineEntity();
         newEartagDeviceOnlineEntity.setDeviceCode(deviceCode);
         newEartagDeviceOnlineEntity.setAlias(alias);
-        newEartagDeviceOnlineEntity.setHeartbeatTotal(heartbeatTotal);;
+        newEartagDeviceOnlineEntity.setHeartbeatTotal(heartbeatTotal);
+        ;
         newEartagDeviceOnlineEntity.setEnvtempTotal(envtempTotal);
         newEartagDeviceOnlineEntity.setEartagTotal(eartagTotal);
         newEartagDeviceOnlineEntity.setFirstTime(firstTime);
@@ -206,25 +205,24 @@ public class EartagServiceImpl implements IEartagService {
         newEartagDeviceOnlineEntity.setLiveStatus(liveStatus);
         eartagDeviceOnlineRepo.saveAndFlush(newEartagDeviceOnlineEntity);
         //将新建的设备在线数据更新到设备在线缓存
-        cacheService.putDeviceInOnline(deviceCode,newEartagDeviceOnlineEntity);
+        cacheService.putDeviceInOnline(deviceCode, newEartagDeviceOnlineEntity);
     }
 
     /**
-     * @Method      : updateDeviceRegister
+     * @Method : updateDeviceRegister
      * @Description : 更新设备注册状态信息
-     * @Params      : [deviceCode, nowTimestamp, todayDate, farmId]
-     * @Return      : void
-     * 
-     * @Author      : ZhuoNing
-     * @Date        : 2022/3/22       
-     * @Time        : 14:06
+     * @Params : [deviceCode, nowTimestamp, todayDate, farmId]
+     * @Return : void
+     * @Author : ZhuoNing
+     * @Date : 2022/3/22
+     * @Time : 14:06
      */
     private void updateDeviceRegister(String deviceCode, Timestamp nowTimestamp, java.sql.Date todayDate, String farmId) {
-        System.out.println("deviceCode>>"+deviceCode);
+        System.out.println("deviceCode>>" + deviceCode);
         EartagDeviceRegisterEntity deviceRegisterEntity = deviceRegisterRepo.getDeviceRegister(deviceCode);
-        System.out.println("deviceRegisterEntity>>"+deviceRegisterEntity);
-        if(deviceRegisterEntity == null){
-            log.error("(updateDeviceRegister).deviceRegisterEntity=null <-- deviceCode>>"+deviceCode);
+        System.out.println("deviceRegisterEntity>>" + deviceRegisterEntity);
+        if (deviceRegisterEntity == null) {
+            log.error("(updateDeviceRegister).deviceRegisterEntity=null <-- deviceCode>>" + deviceCode);
         }
         deviceRegisterEntity.setLastTime(nowTimestamp);
         deviceRegisterEntity.setActiveStatus(1);
@@ -245,12 +243,12 @@ public class EartagServiceImpl implements IEartagService {
 //            System.out.println("!=0");
 //        }
         //{读取耳标设备关联统计}
-        List<EartagDeviceEartagCountEntity> deviceEartagCountEntityList = deviceEartagCountRepo.getEartagDeviceCount(earmark,deviceCode,todayDateText);
-        if(deviceEartagCountEntityList.size()==0){
+        List<EartagDeviceEartagCountEntity> deviceEartagCountEntityList = deviceEartagCountRepo.getEartagDeviceCount(earmark, deviceCode, todayDateText);
+        if (deviceEartagCountEntityList.size() == 0) {
             Integer count = 1;
             //{新建耳标设备关联统计}
-            newEartagDeviceCount(earmark,deviceCode,count,todayDateText,todayDate,nowTimestamp,farmId);
-        }else{
+            newEartagDeviceCount(earmark, deviceCode, count, todayDateText, todayDate, nowTimestamp, farmId);
+        } else {
             //更新次数
             EartagDeviceEartagCountEntity deviceEartagCountEntity = deviceEartagCountEntityList.get(0);
             int total = deviceEartagCountEntity.getTotal();
@@ -259,7 +257,7 @@ public class EartagServiceImpl implements IEartagService {
             deviceEartagCountEntity.setUpdateTime(nowTimestamp);
             deviceEartagCountRepo.saveAndFlush(deviceEartagCountEntity);
             //缓存里计数+1
-            cacheService.countEartagDeviceOnlineTimes(earmark,deviceCode,total,deviceEartagCountEntity);
+            cacheService.countEartagDeviceOnlineTimes(earmark, deviceCode, total, deviceEartagCountEntity);
         }
     }
 
@@ -274,7 +272,7 @@ public class EartagServiceImpl implements IEartagService {
         newDeviceEartagCountEntity.setUpdateTime(nowTimestamp);
         deviceEartagCountRepo.saveAndFlush(newDeviceEartagCountEntity);
         //将数据保存到缓存中
-        cacheService.putEartagDeviceOnlineCount(earmark,deviceCode,todayDateText,newDeviceEartagCountEntity);
+        cacheService.putEartagDeviceOnlineCount(earmark, deviceCode, todayDateText, newDeviceEartagCountEntity);
     }
 
     //更新设备环境信息
@@ -282,36 +280,36 @@ public class EartagServiceImpl implements IEartagService {
         //{获取处于工作状态的设备数量}
         Integer deviceTotal = deviceService.getDeviceTotalByFarmIdFromDeviceRegister(farmId);
         //设备在线数量
-        Integer deviceOnlineSum = cacheService.getDeviceOnlineSum(farmId,todayDateText);
+        Integer deviceOnlineSum = cacheService.getDeviceOnlineSum(farmId, todayDateText);
         //设备离线数量
         Integer deviceOfflineSum = 0;
-        if(deviceTotal > deviceOnlineSum){
+        if (deviceTotal > deviceOnlineSum) {
             deviceOfflineSum = deviceTotal - deviceOnlineSum;
         }
         //设备在线率
-        BigDecimal deviceRateBd= new BigDecimal(deviceOnlineSum).divide(new BigDecimal(deviceTotal),1,BigDecimal.ROUND_HALF_UP);
+        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);
-        System.out.println("## eartagTotal>>"+eartagTotal);
+        System.out.println("## eartagTotal>>" + eartagTotal);
         Integer eartagOnlineSum = 0;
         Float eartagOnlineRate = 0F;
-        if(eartagTotal != 0){
+        if (eartagTotal != 0) {
             //耳标在线数量 eartagService.
-            eartagOnlineSum = getEartagOnelineTotal(farmId,todayDateText);
+            eartagOnlineSum = getEartagOnelineTotal(farmId, todayDateText);
             eartagOnlineSum++;
             //耳标在线率
-            BigDecimal eartagRateBd= new BigDecimal(eartagOnlineSum).divide(new BigDecimal(eartagTotal),1,BigDecimal.ROUND_HALF_UP);
+            BigDecimal eartagRateBd = new BigDecimal(eartagOnlineSum).divide(new BigDecimal(eartagTotal), 1, BigDecimal.ROUND_HALF_UP);
             eartagOnlineRate = Float.parseFloat(eartagRateBd.toString());
         }
         //
-        EartarFarmAllStatusEntity farmAllStatusEntity = eartagFarmAllStatusRepo.getOneByFarmIdAndCreateDate(farmId,todayDateText);
-        if(farmAllStatusEntity == null){
+        EartarFarmAllStatusEntity farmAllStatusEntity = eartagFarmAllStatusRepo.getOneByFarmIdAndCreateDate(farmId, todayDateText);
+        if (farmAllStatusEntity == null) {
             //{新建设备状态记录并更新缓存cache}
-            newFarmAllStatus(farmId,todayDate,nowTimestamp,deviceTotal,deviceOnlineSum,deviceOfflineSum,deviceCancelSum,deviceOnlineRate,eartagTotal,eartagOnlineSum,eartagOnlineRate);
-        }else{
+            newFarmAllStatus(farmId, todayDate, nowTimestamp, deviceTotal, deviceOnlineSum, deviceOfflineSum, deviceCancelSum, deviceOnlineRate, eartagTotal, eartagOnlineSum, eartagOnlineRate);
+        } else {
             //计算设备状态并更新缓存cache(在线数+1,离线数-1,计算设备在线率)
             farmAllStatusEntity.setDeviceTotal(deviceTotal);
             farmAllStatusEntity.setDeviceOnline(deviceOnlineSum);
@@ -324,16 +322,16 @@ public class EartagServiceImpl implements IEartagService {
             farmAllStatusEntity.setUpdateTime(nowTimestamp);
             eartagFarmAllStatusRepo.saveAndFlush(farmAllStatusEntity);
             //{同步更新牧场总状态缓存相关字段}
-            cacheService.flashAllStatusCache(farmId,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){
+        if (deviceService.getDeviceSum(farmId, liveStatus) == 0) {
             log.error("该牧场没有任何处于工作状态的采集器设备.");
-        }else{
+        } else {
             //# 计算耳标总数(耳标注册表)
             EartarFarmAllStatusEntity farmAllStatusEntity = new EartarFarmAllStatusEntity();
             farmAllStatusEntity.setDeviceTotal(deviceTotal);
@@ -349,59 +347,59 @@ public class EartagServiceImpl implements IEartagService {
             farmAllStatusEntity.setCreateDate(todayDate);
             eartagFarmAllStatusRepo.saveAndFlush(farmAllStatusEntity);
             //设置总状态缓存
-            cacheService.flashAllStatusCache(farmId,farmAllStatusEntity);
+            cacheService.flashAllStatusCache(farmId, farmAllStatusEntity);
         }
     }
 
     //
     public Integer getEartagOnelineTotal(String farmId, String todayDateText) {
-        return cacheService.getEartagOnlineTotalFromCache(farmId,todayDateText);
+        return cacheService.getEartagOnlineTotalFromCache(farmId, todayDateText);
     }
 
     //
-    public Integer getEartagRegTotalByFarmId(String farmId){
+    public Integer getEartagRegTotalByFarmId(String farmId) {
         //从缓存读取耳标总数
         Integer eartagTotal = cacheService.getEartagTotal(farmId);
-        if(eartagTotal != null && eartagTotal != 0){
+        if (eartagTotal != null && eartagTotal != 0) {
             return eartagTotal;
-        }else{
+        } 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);
+            Integer total = getEartagSum(farmId, liveStatus);
             //设置耳标总数缓存
-            cacheService.putEartagTotal(total,farmId);
+            cacheService.putEartagTotal(total, farmId);
             //处于工作状态的耳标总数
             return total;
         }
     }
 
     //计算处于不同生存状态的耳标数量
-    private Integer getEartagSum(String farmId,Integer liveStatus){
-        List<Object[]> eartagRegList = eartagRegisterRepo.getEartagCountByFarmId(farmId,liveStatus);
+    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) {
+    public void updateEartagOfAllStatus(String earmark, String deviceCode, String todayDateText, String farmId) {
         //# 判断设备和耳标总状态表是否存在
         //      存在:更新在线耳标数量;
         //      不存在:创建状态
-        EartarFarmAllStatusEntity farmAllStatusEntity = farmAllStatusRepo.getOneByFarmIdAndCreateDate(farmId,todayDateText);
-        if(farmAllStatusEntity == null){
+        EartarFarmAllStatusEntity farmAllStatusEntity = farmAllStatusRepo.getOneByFarmIdAndCreateDate(farmId, todayDateText);
+        if (farmAllStatusEntity == null) {
             //#获取耳标注册数量#
-            int eartagTotal =  getEartagRegTotalByFarmId_1(farmId);
+            int eartagTotal = getEartagRegTotalByFarmId_1(farmId);
             //#总状态记录不存在;
             EartarFarmAllStatusEntity newFarmAllStatusEntity = new EartarFarmAllStatusEntity();
             newFarmAllStatusEntity.setEartagTotal(eartagTotal);
             newFarmAllStatusEntity.setEartagOnline(1);
             farmAllStatusRepo.saveAndFlush(newFarmAllStatusEntity);
-        }else{
+        } else {
             //#存在;
             int total = farmAllStatusEntity.getEartagOnline();
             farmAllStatusEntity.setEartagOnline(++total);
@@ -410,7 +408,7 @@ public class EartagServiceImpl implements IEartagService {
     }
 
     //获取耳标注册数量
-    public Integer getEartagRegTotalByFarmId_1(String farmId){
+    public Integer getEartagRegTotalByFarmId_1(String farmId) {
         int eartagTotal = 0;
         List<Object[]> eartagRegList = eartagRegisterRepo.getEartagCountByFarmId(farmId);
         Object[] eartagRegObj = (Object[]) eartagRegList.get(0);
@@ -425,12 +423,12 @@ 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){
+        EartagEartagOnlineEntity eartagOnlineEntity = eartagOnlineRepo.getOneByEarmarkAndFarmId(earmark, todayDateText, farmId);
+        if (eartagOnlineEntity == null) {
             int total = 1;
             //{新建耳标在线记录}
-            newEartagOnline(earmark,total,nowTimestamp,earTemp,envTemp,todayDate,farmId);
-        }else{
+            newEartagOnline(earmark, total, nowTimestamp, earTemp, envTemp, todayDate, farmId);
+        } else {
             int total = eartagOnlineEntity.getTotal();
             eartagOnlineEntity.setTotal(++total);
             eartagOnlineEntity.setLastTime(nowTimestamp);
@@ -438,7 +436,7 @@ public class EartagServiceImpl implements IEartagService {
             eartagOnlineEntity.setEartagTemp(earTemp);
             eartagOnlineRepo.saveAndFlush(eartagOnlineEntity);
             //更新耳标在线统计
-            cacheService.putEartagOnlineCount(earmark,eartagOnlineEntity);
+            cacheService.putEartagOnlineCount(earmark, eartagOnlineEntity);
         }
     }
 
@@ -455,7 +453,7 @@ public class EartagServiceImpl implements IEartagService {
         newEartagOnlineEntity.setFarmId(farmId);
         eartagOnlineRepo.saveAndFlush(newEartagOnlineEntity);
         //更新耳标在线统计
-        cacheService.putEartagOnlineCount(earmark,newEartagOnlineEntity);
+        cacheService.putEartagOnlineCount(earmark, newEartagOnlineEntity);
     }
 
     //更新耳标在线数据
@@ -464,11 +462,11 @@ public class EartagServiceImpl implements IEartagService {
         //      存在,则更新上传记录数、最后上传时间、环境温度,耳根温度,并将上面数据更新到缓存数据中;
         //      不存在,则创建耳标在线记录,并更新到缓存数据中;
         //耳根温度
-        String earTemp =  eartagJo.getString("earTemp1");
+        String earTemp = eartagJo.getString("earTemp1");
         //环境温度
-        String envTemp =  eartagJo.getString("envTemp");
-        EartagEartagOnlineEntity eartagOnlineEntity = eartagOnlineRepo.getOneByEarmarkAndFarmId(earmark,todayDateText,farmId);
-        if(eartagOnlineEntity == null){
+        String envTemp = eartagJo.getString("envTemp");
+        EartagEartagOnlineEntity eartagOnlineEntity = eartagOnlineRepo.getOneByEarmarkAndFarmId(earmark, todayDateText, farmId);
+        if (eartagOnlineEntity == null) {
             //# 不存在
             EartagEartagOnlineEntity newEartagOnlineEntity = new EartagEartagOnlineEntity();
             newEartagOnlineEntity.setEarmark(earmark);
@@ -481,8 +479,8 @@ public class EartagServiceImpl implements IEartagService {
             newEartagOnlineEntity.setFarmId(farmId);
             eartagOnlineRepo.saveAndFlush(newEartagOnlineEntity);
             //更新耳标在线统计
-            cacheService.putEartagOnlineCount(earmark,newEartagOnlineEntity);
-        }else{
+            cacheService.putEartagOnlineCount(earmark, newEartagOnlineEntity);
+        } else {
             //# 存在
             int total = eartagOnlineEntity.getTotal();
             eartagOnlineEntity.setTotal(++total);
@@ -491,36 +489,159 @@ public class EartagServiceImpl implements IEartagService {
             eartagOnlineEntity.setEartagTemp(earTemp);
             eartagOnlineRepo.saveAndFlush(eartagOnlineEntity);
             //更新耳标在线统计
-            cacheService.putEartagOnlineCount(earmark,eartagOnlineEntity);
+            cacheService.putEartagOnlineCount(earmark, eartagOnlineEntity);
         }
     }
 
+    @Autowired
+    private BasePigpenRepo basePigpenRepo;
+    @Autowired
+    private BizBaseStageRepo bizBaseStageRepo;
+
     //更新耳标注册数据
     public void updateEartagRegister(String earmark, String deviceCode, String bat, Timestamp nowTimestamp, java.sql.Date todayDate, String farmId) {
+        EartagDeviceRegisterEntity deviceRegisterEntity = deviceRegisterRepo.getByDeviceCode(deviceCode);
+        Integer unitId = deviceRegisterEntity.getUnitId();
+        Integer pigpenId = deviceRegisterEntity.getPigpenId();
+        Map pigpenMap = new HashMap();
+        List<BasePigpenEntity> basePigpenEntityList = basePigpenRepo.getPigpenByPigpenIdAndUnitId(pigpenId, unitId);
+        if (basePigpenEntityList.size() == 0) {
+            log.error("栋舍不存在[" + pigpenId + "," + unitId + "].");
+            return;
+        }
+        //-- 获取栋舍名称--//
+        String pigpenName = "";
+        String unitName = "";
+        String stageCode = "";
+        for (BasePigpenEntity basePigpenEntity : basePigpenEntityList) {
+            if (basePigpenEntity.getId().equals(pigpenId)) {
+                pigpenName = basePigpenEntity.getPigpenName();
+                continue;
+            }
+            if (basePigpenEntity.getId().equals(unitId)) {
+                unitName = basePigpenEntity.getPigpenName();
+                stageCode = basePigpenEntity.getStageCode();
+            }
+        }
+        System.out.println("## pigpenName=" + pigpenName);
+        System.out.println("## unitName=" + unitName);
+        System.out.println("## stageCode=" + stageCode);
+        //-- 获取阶段名称 --//
+        Optional<BizBaseStageEntity> optionalBizBaseStageEntity = bizBaseStageRepo.getStageByStageCode(stageCode);
+        if (!optionalBizBaseStageEntity.isPresent()) {
+            log.error("该阶段不存在.");
+            return;
+        }
+        BizBaseStageEntity bizBaseStageEntity = optionalBizBaseStageEntity.get();
+        String stageName = bizBaseStageEntity.getStageName();
+        System.out.println("## stageName=" + stageName);
+
         //# 判断耳标注册表是否存在;
         //      如果不存在就注册耳标;
         //      如果存在就更新活动状态(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,bat,todayDate,farmId);
-        }else{
+        EartagEartagRegisterEntity eartagRegisterEntity = eartagRegisterRepo.getOneByEarmark(earmark, farmId);
+        if (eartagRegisterEntity == null) {
+            //{新建耳标注册信息}
+            newEartagRegister(earmark, nowTimestamp, deviceCode, registerType, activeStatus, liveStatus, bat, todayDate, farmId, deviceRegisterEntity);
+        } else {
             eartagRegisterEntity.setLastTime(nowTimestamp);
             eartagRegisterEntity.setLastDevice(deviceCode);
             eartagRegisterEntity.setActiveStatus(activeStatus);
             eartagRegisterEntity.setActiveTime(nowTimestamp);
             eartagRegisterEntity.setBat(Integer.parseInt(bat));
+            eartagRegisterEntity.setUnitId(deviceRegisterEntity.getUnitId());
+            eartagRegisterEntity.setUnitName(unitName);
+            eartagRegisterEntity.setPigpenId(deviceRegisterEntity.getPigpenId());
+            eartagRegisterEntity.setPigpenName(pigpenName);
             eartagRegisterRepo.saveAndFlush(eartagRegisterEntity);
             //更新耳标注册消息缓存
-            cacheService.putEartagRegister(earmark,eartagRegisterEntity);
+            cacheService.putEartagRegister(earmark, eartagRegisterEntity);
+        }
+    }
+
+    @Autowired
+    private EartagEartagRegister2EntityRepo eartagEartagRegister2EntityRepo;
+
+    /**
+     * @Method : updateEartagRegister2
+     * @Description :
+     * @Params : [earmark, deviceCode, bat, nowTimestamp, todayDate, farmId]
+     * @Return : void
+     * @Author : ZhuoNing
+     * @Date : 2022/8/4
+     * @Time : 9:39
+     */
+    public void updateEartagRegister2(String earmark, String deviceCode, String bat, Timestamp nowTimestamp, java.sql.Date todayDate, String farmId, JSONObject eartagJo) {
+        EartagDeviceRegisterEntity deviceRegisterEntity = deviceRegisterRepo.getByDeviceCode(deviceCode);
+        Integer unitId = deviceRegisterEntity.getUnitId();
+        Integer pigpenId = deviceRegisterEntity.getPigpenId();
+        List<BasePigpenEntity> basePigpenEntityList = basePigpenRepo.getPigpenByPigpenIdAndUnitId(pigpenId, unitId);
+        if (basePigpenEntityList.size() == 0) {
+            log.error("栋舍不存在[" + pigpenId + "," + unitId + "].");
+            return;
+        }
+        //-- 获取栋舍名称--//
+        String pigpenName = "";
+        String unitName = "";
+        String stageCode = "";
+        for (BasePigpenEntity basePigpenEntity : basePigpenEntityList) {
+            if (basePigpenEntity.getId().equals(pigpenId)) {
+                pigpenName = basePigpenEntity.getPigpenName();
+                continue;
+            }
+            if (basePigpenEntity.getId().equals(unitId)) {
+                unitName = basePigpenEntity.getPigpenName();
+                stageCode = basePigpenEntity.getStageCode();
+            }
+        }
+        //-- 获取阶段名称 --//
+        Optional<BizBaseStageEntity> optionalBizBaseStageEntity = bizBaseStageRepo.getStageByStageCode(stageCode);
+        if (!optionalBizBaseStageEntity.isPresent()) {
+            log.error("该阶段不存在["+stageCode+"].");
+            return;
+        }
+        BizBaseStageEntity bizBaseStageEntity = optionalBizBaseStageEntity.get();
+        String stageName = bizBaseStageEntity.getStageName();
+        //# 判断耳标注册表是否存在;
+        //      如果不存在就注册耳标;
+        //      如果存在就更新活动状态(active_status)和活动状态更新时间(active_time)
+        Integer registerType = 1;
+        Integer activeStatus = 1;
+        Integer liveStatus = 1;        //
+        EartagEartagRegister2Entity eartagRegisterEntity = eartagEartagRegister2EntityRepo.getOneByEarmark(earmark, farmId);
+        if (eartagRegisterEntity == null) {
+            //{新建耳标注册信息}
+            newEartagRegister2(earmark, nowTimestamp, deviceCode, registerType, activeStatus, liveStatus, bat, todayDate, farmId, eartagJo, stageCode, stageName, pigpenId, pigpenName, unitId, unitName, deviceRegisterEntity);
+        } else {
+            eartagRegisterEntity.setLastTime(nowTimestamp);
+            eartagRegisterEntity.setLastDevice(deviceCode);
+            eartagRegisterEntity.setActiveStatus(activeStatus);
+            eartagRegisterEntity.setActiveTime(nowTimestamp);
+            eartagRegisterEntity.setBat(Integer.parseInt(bat));
+            eartagRegisterEntity.setUnitId(deviceRegisterEntity.getUnitId());
+            eartagRegisterEntity.setUnitName(unitName);
+            eartagRegisterEntity.setPigpenId(deviceRegisterEntity.getPigpenId());
+            eartagRegisterEntity.setPigpenName(pigpenName);
+            eartagRegisterEntity.setStageCode(stageCode);
+            eartagRegisterEntity.setStageName(stageName);
+            eartagRegisterEntity.setEarTemp1(eartagJo.getFloat("earTemp1"));
+            eartagRegisterEntity.setEnvTemp1(eartagJo.getFloat("envTemp1"));
+            eartagRegisterEntity.setAct(Integer.parseInt(eartagJo.getString("act1")));
+            eartagRegisterEntity.setSignal1(Integer.parseInt(eartagJo.getString("signal")));
+            eartagRegisterEntity.setAskTime(getAskTime(eartagJo.getString("askTime")));
+            eartagRegisterEntity.setOther(eartagJo.getString("other"));
+
+            eartagEartagRegister2EntityRepo.saveAndFlush(eartagRegisterEntity);
+            //更新耳标注册消息缓存
+            cacheService.putEartagRegister(earmark, eartagRegisterEntity);
         }
     }
 
     //新建耳标注册信息
-    private void newEartagRegister(String earmark, Timestamp nowTimestamp, String deviceCode, Integer registerType, Integer activeStatus, Integer liveStatus, String bat, java.sql.Date todayDate, String farmId) {
+    private void newEartagRegister(String earmark, Timestamp nowTimestamp, String deviceCode, Integer registerType, Integer activeStatus, Integer liveStatus, String bat, java.sql.Date todayDate, String farmId, EartagDeviceRegisterEntity deviceRegisterEntity) {
         //#创建耳标注册记录#
         EartagEartagRegisterEntity newEartagRegisterEntity = new EartagEartagRegisterEntity();
         newEartagRegisterEntity.setEarmark(earmark);
@@ -537,11 +658,57 @@ public class EartagServiceImpl implements IEartagService {
         newEartagRegisterEntity.setLiveStatus(liveStatus);
         newEartagRegisterEntity.setCreateDate(todayDate);
         newEartagRegisterEntity.setBat(Integer.parseInt(bat));
+        newEartagRegisterEntity.setUnitId(deviceRegisterEntity.getUnitId());
+//            newEartagRegisterEntity.setUnitName();
+        newEartagRegisterEntity.setPigpenId(deviceRegisterEntity.getPigpenId());
+//            newEartagRegisterEntity.setPigpenName();
         eartagRegisterRepo.saveAndFlush(newEartagRegisterEntity);
         //{更新耳标注册消息缓存}
-        cacheService.putEartagRegister(earmark,newEartagRegisterEntity);
+        cacheService.putEartagRegister(earmark, newEartagRegisterEntity);
         //{更新耳标数量}
-        cacheService.putEartagTotal(getEartagSum(farmId,liveStatus),farmId);
+        cacheService.putEartagTotal(getEartagSum(farmId, liveStatus), farmId);
+    }
+
+    //新建耳标注册信息
+    private void newEartagRegister2(String earmark, Timestamp nowTimestamp, String deviceCode, Integer registerType, Integer activeStatus, Integer liveStatus, String bat, java.sql.Date todayDate, String farmId, JSONObject eartagJo, String stageCode, String stageName, Integer pigpenId, String pigpenName, Integer unitId, String unitName, EartagDeviceRegisterEntity deviceRegisterEntity) {
+        //#创建耳标注册记录#
+        EartagEartagRegister2Entity newEartagRegisterEntity = new EartagEartagRegister2Entity();
+        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);
+        newEartagRegisterEntity.setBat(Integer.parseInt(bat));
+        newEartagRegisterEntity.setUnitId(deviceRegisterEntity.getUnitId());
+        newEartagRegisterEntity.setUnitName(unitName);
+        newEartagRegisterEntity.setPigpenId(deviceRegisterEntity.getPigpenId());
+        newEartagRegisterEntity.setPigpenName(pigpenName);
+        newEartagRegisterEntity.setStageName(stageName);
+        newEartagRegisterEntity.setStageCode(stageCode);
+
+        newEartagRegisterEntity.setEarTemp1(eartagJo.getFloat("earTemp1"));
+        newEartagRegisterEntity.setEnvTemp1(eartagJo.getFloat("envTemp1"));
+        newEartagRegisterEntity.setAct(Integer.parseInt(eartagJo.getString("act1")));
+        newEartagRegisterEntity.setSignal1(Integer.parseInt(eartagJo.getString("signal")));
+        newEartagRegisterEntity.setAskTime(getAskTime(eartagJo.getString("askTime")));
+        newEartagRegisterEntity.setOther(eartagJo.getString("other"));
+
+        System.out.println("newEartagRegisterEntity>>"+newEartagRegisterEntity.toString());
+
+        eartagEartagRegister2EntityRepo.saveAndFlush(newEartagRegisterEntity);
+        //{更新耳标注册消息缓存}
+        cacheService.putEartagRegister(earmark, newEartagRegisterEntity);
+        //{更新耳标数量}
+        cacheService.putEartagTotal(getEartagSum(farmId, liveStatus), farmId);
     }
 
     //更新耳标注册数据
@@ -550,8 +717,8 @@ public class EartagServiceImpl implements IEartagService {
         //      如果不存在就注册耳标;
         //      如果存在就更新活动状态(active_status)和活动状态更新时间(active_time)
         //
-        EartagEartagRegisterEntity eartagRegisterEntity = eartagRegisterRepo.getOneByEarmark(earmark,farmId);
-        if(eartagRegisterEntity == null){
+        EartagEartagRegisterEntity eartagRegisterEntity = eartagRegisterRepo.getOneByEarmark(earmark, farmId);
+        if (eartagRegisterEntity == null) {
             //#创建耳标注册记录#
             EartagEartagRegisterEntity newEartagRegisterEntity = new EartagEartagRegisterEntity();
             newEartagRegisterEntity.setEarmark(earmark);
@@ -568,13 +735,13 @@ public class EartagServiceImpl implements IEartagService {
             newEartagRegisterEntity.setLiveStatus(1);
             eartagRegisterRepo.saveAndFlush(newEartagRegisterEntity);
             //更新耳标注册消息缓存
-            cacheService.putEartagRegister(earmark,newEartagRegisterEntity);
-        }else{
+            cacheService.putEartagRegister(earmark, newEartagRegisterEntity);
+        } else {
             eartagRegisterEntity.setActiveStatus(1);
             eartagRegisterEntity.setActiveTime(nowTimestamp);
             eartagRegisterRepo.saveAndFlush(eartagRegisterEntity);
             //更新耳标注册消息缓存
-            cacheService.putEartagRegister(earmark,eartagRegisterEntity);
+            cacheService.putEartagRegister(earmark, eartagRegisterEntity);
         }
     }
 
@@ -591,13 +758,13 @@ public class EartagServiceImpl implements IEartagService {
     public Integer countAct(String earmark, String nowAct) {
         //读取hash
         Object lastActObj = cacheService.getEartagLastAct(earmark);
-        cacheService.putEartagAct(earmark,nowAct);
+        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{
+        } else {
             //{初始化最新的redis记录}
 //            cacheService.putEartagAct(earmark,nowAct);
             //第一次的运动量计数
@@ -615,13 +782,13 @@ public class EartagServiceImpl implements IEartagService {
     }
 
     /**
-     * @Author      : ZhuoNing
-     * @Date        : 2022/2/9
-     * @Time        : 14:33
      * @param eartagJo
      * @param nowTimestamp
      * @param todayDate
      * @param farmId
+     * @Author : ZhuoNing
+     * @Date : 2022/2/9
+     * @Time : 14:33
      */
     public void saveEartagFlow(JSONObject eartagJo, Timestamp nowTimestamp, java.sql.Date todayDate, String farmId) throws ParseException {
         DateUtil dateUtil = new DateUtil();
@@ -643,10 +810,12 @@ public class EartagServiceImpl implements IEartagService {
         dataEntity.setCreateDate(todayDate);
         dataEntity.setFarmId(farmId);
         dataEntity.setAskDate(new java.sql.Date(dateUtil.parseDate(getAskDate(eartagJo.getString("askTime"))).getTime()));
+
+
         eartagDataRepo.saveAndFlush(dataEntity);
 
         //最新的耳标数据覆盖老数据
-        cacheService.putEartagFlowToCache(dataEntity.getEarmark(),dataEntity);
+        cacheService.putEartagFlowToCache(dataEntity.getEarmark(), dataEntity);
     }
 
     public void saveEartagFlow2(JSONObject eartagJo, Timestamp nowTimestamp, java.sql.Date todayDate, String farmId) throws ParseException {
@@ -709,7 +878,7 @@ public class EartagServiceImpl implements IEartagService {
     }
 
     public String getAskDate(String askTime) {
-        return askTime.substring(0, 4) + "-" + askTime.substring(4, 6) + "-" + askTime.substring(6, 8) ;
+        return askTime.substring(0, 4) + "-" + askTime.substring(4, 6) + "-" + askTime.substring(6, 8);
     }
 
     /**