|
@@ -11,6 +11,7 @@ import com.huimv.eartag2.common.utils.DateUtil;
|
|
|
import com.huimv.eartag2.process2.service.IDeviceService;
|
|
|
//import com.huimv.eartag2.process2.utils.DateUtil;
|
|
|
import com.huimv.eartag2.process2.service.ICacheService;
|
|
|
+import com.huimv.eartag2.process2.service.IEartagService;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
@@ -55,6 +56,12 @@ public class DeviceServiceImpl implements IDeviceService {
|
|
|
private EartagEartagRegisterRepo eartagRegisterRepo;
|
|
|
// @Autowired
|
|
|
// private EartagDeviceRegisterRepo deviceRegisterRepo;
|
|
|
+ @Autowired
|
|
|
+ private EartagDeviceOnlineRepo deviceOnlineRepo;
|
|
|
+ @Autowired
|
|
|
+ private EartarFarmAllStatusRepo farmAllStatusRepo;
|
|
|
+ @Autowired
|
|
|
+ private IEartagService eartagService;
|
|
|
|
|
|
|
|
|
// 保存在线数据
|
|
@@ -288,15 +295,19 @@ public class DeviceServiceImpl implements IDeviceService {
|
|
|
*/
|
|
|
@Override
|
|
|
public void updateDeviceRegister(String farmId, String deviceCode,Timestamp nowTimestamp) {
|
|
|
-// System.out.println("## (2)/(B)");
|
|
|
+ System.out.println("## 心跳 1.1/2.1");
|
|
|
//--更新mysql
|
|
|
EartagDeviceRegisterEntity deviceRegisterEntity = deviceRegisterRepo.getDeviceRegister(deviceCode);
|
|
|
if (deviceRegisterEntity != null) {
|
|
|
+ System.out.println("## 心跳 1.1.1/2.1.1");
|
|
|
//设备注册数据存在
|
|
|
deviceRegisterEntity.setActiveStatus(1);
|
|
|
deviceRegisterEntity.setActiveTime(new Timestamp(new Date().getTime()));
|
|
|
deviceRegisterRepo.saveAndFlush(deviceRegisterEntity);
|
|
|
+ //更新缓存中设备注册信息
|
|
|
+ cacheService.putDeviceRegister(deviceCode,deviceRegisterEntity);
|
|
|
}else{
|
|
|
+ System.out.println("## 心跳 1.1.2/12.1.2");
|
|
|
//设备注册数据不存在
|
|
|
//新建设备注册记录
|
|
|
EartagDeviceRegisterEntity newDeviceRegisterEntity = new EartagDeviceRegisterEntity();
|
|
@@ -307,18 +318,44 @@ public class DeviceServiceImpl implements IDeviceService {
|
|
|
newDeviceRegisterEntity.setActiveTime(nowTimestamp);
|
|
|
newDeviceRegisterEntity.setDeviceStatus(1);
|
|
|
deviceRegisterRepo.saveAndFlush(newDeviceRegisterEntity);
|
|
|
+ //更新缓存中设备注册信息
|
|
|
+ cacheService.putDeviceRegister(deviceCode,newDeviceRegisterEntity);
|
|
|
}
|
|
|
- //更新缓存中设备注册信息
|
|
|
- cacheService.putDeviceRegister(deviceCode,deviceRegisterEntity);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void updateDeviceOnline(String farmId, String deviceCode) throws ParseException {
|
|
|
+ public void updateDeviceOnlineCount(String farmId, String deviceCode, String todayDateText, Timestamp nowTimestamp, java.sql.Date todayDate) throws ParseException {
|
|
|
//# 先判断今天在线记录是否存在 #
|
|
|
//# 更新数据表 #
|
|
|
//# 更新缓存 #
|
|
|
-
|
|
|
- //+++++++++++++++++++++++++++++++++++++++++++++++++++++ 3.3
|
|
|
+ System.out.println("## 心跳 1.2/2.2");
|
|
|
+ //
|
|
|
+ EartagDeviceOnlineEntity deviceOnlineEntity = deviceOnlineRepo.getDeviceOnline(deviceCode,todayDateText);
|
|
|
+ if(deviceOnlineEntity == null){
|
|
|
+ System.out.println("## 心跳 1.2.1/2.2.1");
|
|
|
+ //不存在;
|
|
|
+ EartagDeviceOnlineEntity newDeviceOnlineEntity = new EartagDeviceOnlineEntity();
|
|
|
+ newDeviceOnlineEntity.setDeviceCode(deviceCode);
|
|
|
+ newDeviceOnlineEntity.setHeartbeatTotal(1);
|
|
|
+ newDeviceOnlineEntity.setEnvtempTotal(0);
|
|
|
+ newDeviceOnlineEntity.setEartagTotal(0);
|
|
|
+ newDeviceOnlineEntity.setAddDate(todayDate);
|
|
|
+ newDeviceOnlineEntity.setHeartbeatFirstTime(nowTimestamp);
|
|
|
+ newDeviceOnlineEntity.setHeartbeatLastTime(nowTimestamp);
|
|
|
+ newDeviceOnlineEntity.setFarmId(farmId);
|
|
|
+ deviceOnlineRepo.saveAndFlush(newDeviceOnlineEntity);
|
|
|
+ //更新每天在线统计缓存
|
|
|
+ cacheService.putDeviceInOnline(deviceCode,newDeviceOnlineEntity);
|
|
|
+ }else{
|
|
|
+ System.out.println("## 心跳 1.2.2/2.2.2");
|
|
|
+ //存在;
|
|
|
+ int total = deviceOnlineEntity.getHeartbeatTotal();
|
|
|
+ deviceOnlineEntity.setHeartbeatTotal(++total);
|
|
|
+ deviceOnlineEntity.setHeartbeatLastTime(nowTimestamp);
|
|
|
+ deviceOnlineRepo.saveAndFlush(deviceOnlineEntity);
|
|
|
+ //更新每天在线统计缓存
|
|
|
+ cacheService.putDeviceInOnline(deviceCode,deviceOnlineEntity);
|
|
|
+ }
|
|
|
|
|
|
//--(优化思路) --//
|
|
|
// updateDeviceOnlineCount(deviceCode);
|
|
@@ -336,9 +373,75 @@ public class DeviceServiceImpl implements IDeviceService {
|
|
|
* @Time : 20:51
|
|
|
*/
|
|
|
@Override
|
|
|
- public void updateDeviceAllStatus(String farmId, String deviceCode) throws ParseException {
|
|
|
+ public void updateDeviceAllStatus(String farmId, String deviceCode, String todayDateText, Timestamp nowTimestamp, java.sql.Date todayDate) throws ParseException {
|
|
|
+ System.out.println("## 心跳 2.3");
|
|
|
+ //读取一条总状态记录;
|
|
|
+ EartarFarmAllStatusEntity farmAllStatusEntity = farmAllStatusRepo.getOneByFarmIdAndCreateDate(farmId,todayDateText);
|
|
|
+ if(farmAllStatusEntity == null){
|
|
|
+ System.out.println("## 心跳 2.3.1");
|
|
|
+ //总状态记录不存在;
|
|
|
+ //#创建一条新总状态记录;
|
|
|
+ //*计算设备在线数量、离线数量、在线率*
|
|
|
+ int deviceTotal = getDeviceTotalByFarmIdFromDeviceRegister(farmId);
|
|
|
+ int onlineNum = 1;
|
|
|
+ int offlineNum = deviceTotal-1;
|
|
|
+ BigDecimal reateBd= new BigDecimal(onlineNum).divide(new BigDecimal(deviceTotal),1,BigDecimal.ROUND_HALF_UP);
|
|
|
+ //*计算设备注销数量*
|
|
|
+ int cancelNum = getDeviceCancelTotalFromDeviceRegister(farmId);
|
|
|
+ //#计算耳标总数
|
|
|
+ int eartagTotal = eartagService.getEartagRegTotalByFarmId(farmId);
|
|
|
+ //计算耳标在线数量
|
|
|
+ int eartagOnlineTotal = eartagService.getEartagOnelineTotal(farmId,todayDateText);
|
|
|
+ //
|
|
|
+ EartarFarmAllStatusEntity newFarmAllStatusEntity = new EartarFarmAllStatusEntity();
|
|
|
+ newFarmAllStatusEntity.setDeviceTotal(deviceTotal);
|
|
|
+ newFarmAllStatusEntity.setDeviceOnline(onlineNum);
|
|
|
+ newFarmAllStatusEntity.setDeviceOffline(offlineNum);
|
|
|
+ newFarmAllStatusEntity.setDeviceRate(Float.parseFloat(reateBd.toString()));
|
|
|
+ newFarmAllStatusEntity.setDeviceCancel(cancelNum);
|
|
|
+ newFarmAllStatusEntity.setEartagTotal(eartagTotal);
|
|
|
+ newFarmAllStatusEntity.setEartagOnline(eartagOnlineTotal);
|
|
|
+ newFarmAllStatusEntity.setFarmId(farmId);
|
|
|
+ newFarmAllStatusEntity.setUpdateTime(nowTimestamp);
|
|
|
+ newFarmAllStatusEntity.setCreateDate(todayDate);
|
|
|
+ farmAllStatusRepo.saveAndFlush(newFarmAllStatusEntity);
|
|
|
+ //更新总状态表缓存
|
|
|
+ cacheService.putAllFarmAllStatusCache(farmId,newFarmAllStatusEntity);
|
|
|
+ }else{
|
|
|
+ System.out.println("## 心跳 2.3.2");
|
|
|
+ //总状态记录存在;
|
|
|
+ //#计算设备的在线、离线、在线率,更新时间
|
|
|
+ int onlineNum = farmAllStatusEntity.getDeviceOnline();
|
|
|
+ int offlineNum = farmAllStatusEntity.getDeviceOffline();
|
|
|
+ int total = farmAllStatusEntity.getDeviceTotal();
|
|
|
+ onlineNum++;
|
|
|
+ offlineNum--;
|
|
|
+ BigDecimal reateBd= new BigDecimal(onlineNum).divide(new BigDecimal(total),1,BigDecimal.ROUND_HALF_UP);
|
|
|
+ farmAllStatusEntity.setDeviceOnline(onlineNum);
|
|
|
+ farmAllStatusEntity.setDeviceOffline(offlineNum);
|
|
|
+ farmAllStatusEntity.setDeviceRate(Float.parseFloat(reateBd.toString()));
|
|
|
+ farmAllStatusEntity.setUpdateTime(nowTimestamp);
|
|
|
+ System.out.println("## farmAllStatusEntity>>"+farmAllStatusEntity);
|
|
|
+ eartagFarmAllStatusRepo.saveAndFlush(farmAllStatusEntity);
|
|
|
+ //更新总状态表缓存
|
|
|
+ cacheService.putAllFarmAllStatusCache(farmId,farmAllStatusEntity);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- //+++++++++++++++++++++++++++++++++++++++++++++++++++++ 3.3
|
|
|
+ //计算注销设备数量
|
|
|
+ private int getDeviceCancelTotalFromDeviceRegister(String farmId) {
|
|
|
+ List<Object[]> deviceRegList = deviceRegisterRepo.getDeviceCountByFarmId(farmId);
|
|
|
+ Object[] deviceRegObj = (Object[]) deviceRegList.get(0);
|
|
|
+ //设备总数
|
|
|
+ return Integer.parseInt(deviceRegObj[0].toString());
|
|
|
+ }
|
|
|
+
|
|
|
+ //计算设备总数
|
|
|
+ public int getDeviceTotalByFarmIdFromDeviceRegister(String farmId){
|
|
|
+ List<Object[]> deviceRegList = deviceRegisterRepo.getDeviceCountByFarmId(farmId);
|
|
|
+ Object[] deviceRegObj = (Object[]) deviceRegList.get(0);
|
|
|
+ //设备总数
|
|
|
+ return Integer.parseInt(deviceRegObj[0].toString());
|
|
|
}
|
|
|
|
|
|
// @Override
|