|
@@ -0,0 +1,139 @@
|
|
|
+package com.huimv.eartag2.manage.service.impl;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.huimv.eartag2.common.dao.entity.EartarFarmAllStatusEntity;
|
|
|
+import com.huimv.eartag2.common.dao.repo.EartagDeviceRegisterRepo;
|
|
|
+import com.huimv.eartag2.common.dao.repo.EartagEartagRegisterRepo;
|
|
|
+import com.huimv.eartag2.common.dao.repo.EartarFarmAllStatusRepo;
|
|
|
+//import com.huimv.eartag2.common.service.IDevice;
|
|
|
+import com.huimv.eartag2.common.service.impl.DeviceImpl;
|
|
|
+import com.huimv.eartag2.common.utils.BizConst;
|
|
|
+import com.huimv.eartag2.common.utils.DateUtil;
|
|
|
+import com.huimv.eartag2.common.utils.Result;
|
|
|
+import com.huimv.eartag2.common.utils.ResultCode;
|
|
|
+import com.huimv.eartag2.manage.service.ICacheService;
|
|
|
+import com.huimv.eartag2.manage.service.IDeviceService;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.sql.Date;
|
|
|
+import java.sql.Timestamp;
|
|
|
+import java.text.ParseException;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Project : huimv.shiwan
|
|
|
+ * @Package : com.huimv.biosafety.uface.controller
|
|
|
+ * @Description : TODO
|
|
|
+ * @Version : 1.0
|
|
|
+ * @Author : ZhuoNing
|
|
|
+ * @Create : 2020-12-25
|
|
|
+ **/
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class DeviceServiceImpl implements IDeviceService {
|
|
|
+ @Autowired
|
|
|
+ private ICacheService cacheService;
|
|
|
+ @Autowired
|
|
|
+ private EartarFarmAllStatusRepo farmAllStatusRepo;
|
|
|
+ @Autowired
|
|
|
+ private EartagDeviceRegisterRepo deviceRegisterRepo;
|
|
|
+ @Autowired
|
|
|
+ private EartagEartagRegisterRepo eartagRegisterRepo;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result getNowAllStatus(String farmId) throws ParseException {
|
|
|
+ //#读取设备和耳标总状态缓存;
|
|
|
+ // 总状态缓存存在:从缓存读取数据返回;
|
|
|
+ // 总状态缓存不存在:从数据库读取记录:
|
|
|
+ // {
|
|
|
+ // 数据库记录存在:读取记录返回,并初始化缓存;
|
|
|
+ // 数据库记录不存在:则构建数据库记录,并初始化缓存,返回结果;
|
|
|
+ // }
|
|
|
+
|
|
|
+ //{从总状态缓存钟读取缓存数据}
|
|
|
+ DateUtil dateUtil = new DateUtil();
|
|
|
+ Map farmAllStatusMap = cacheService.getFarmAllStatusByFarmId(farmId);
|
|
|
+ if(farmAllStatusMap.size()>0){
|
|
|
+ log.info("返回缓存数据.");
|
|
|
+ return new Result(ResultCode.SUCCESS,JSONObject.toJSONString(farmAllStatusMap));
|
|
|
+ }else{
|
|
|
+ //#从数据库读取总状态表
|
|
|
+ String todayDateText = dateUtil.getTodayDateText();
|
|
|
+ Date todayDate = dateUtil.getTodayMySQLDate();
|
|
|
+ Timestamp timestamp = dateUtil.getTimestamp();
|
|
|
+ EartarFarmAllStatusEntity farmAllStatusEntity = farmAllStatusRepo.getOneByFarmIdAndCreateDate(farmId,todayDateText);
|
|
|
+ if(farmAllStatusEntity != null){
|
|
|
+ //#初始化总状态缓存
|
|
|
+ cacheService.putFarmAllStatusCache(farmId,farmAllStatusEntity);
|
|
|
+ return new Result(ResultCode.SUCCESS,JSONObject.toJSONString(farmAllStatusEntity));
|
|
|
+ }else{
|
|
|
+ //*构建总状态数据 */
|
|
|
+ int deviceTotal = getDeviceCancelTotalFromDeviceRegister(farmId);
|
|
|
+ int eartagTotal = getEartagRegTotalByFarmId(farmId);
|
|
|
+ int deviceOnlineTotal = cacheService.getDeviceOnlineTotal(farmId,todayDateText);
|
|
|
+ int eartagOnlineTotal = cacheService.getEartagOnlineTotal(farmId,todayDateText);
|
|
|
+ int deviceOffline = deviceTotal - deviceOnlineTotal;
|
|
|
+ BigDecimal rateBd= new BigDecimal(deviceOnlineTotal).divide(new BigDecimal(deviceTotal),1,BigDecimal.ROUND_HALF_UP);
|
|
|
+ EartarFarmAllStatusEntity newFarmAllStatusEntity = new EartarFarmAllStatusEntity();
|
|
|
+ newFarmAllStatusEntity.setDeviceTotal(deviceTotal);
|
|
|
+ newFarmAllStatusEntity.setDeviceOnline(deviceOnlineTotal);
|
|
|
+ newFarmAllStatusEntity.setDeviceOffline(deviceOffline);
|
|
|
+ newFarmAllStatusEntity.setDeviceRate(Float.parseFloat(rateBd.toString()));
|
|
|
+ newFarmAllStatusEntity.setEartagTotal(eartagTotal);
|
|
|
+ newFarmAllStatusEntity.setEartagOnline(eartagOnlineTotal);
|
|
|
+ newFarmAllStatusEntity.setFarmId(farmId);
|
|
|
+ newFarmAllStatusEntity.setUpdateTime(timestamp);
|
|
|
+ newFarmAllStatusEntity.setCreateDate(todayDate);
|
|
|
+ farmAllStatusRepo.saveAndFlush(newFarmAllStatusEntity);
|
|
|
+ //*初始化总状态缓存*/
|
|
|
+ cacheService.putFarmAllStatusCache(farmId,newFarmAllStatusEntity);
|
|
|
+ return new Result(ResultCode.SUCCESS,JSONObject.toJSONString(newFarmAllStatusEntity));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result listManyDaysAllStatus(String farmId, Integer days) throws ParseException {
|
|
|
+ DateUtil dateUtil = new DateUtil();
|
|
|
+ String pastDate = dateUtil.getPastDate(days);
|
|
|
+ String todayDate = dateUtil.getTodayDateText();
|
|
|
+ List<EartarFarmAllStatusEntity> FarmAllStatusEntityList = farmAllStatusRepo.getOneByFarmIdAndCreateDateAndPastDate(farmId,pastDate,todayDate);
|
|
|
+ JSONArray dataJa = new JSONArray();
|
|
|
+ for(EartarFarmAllStatusEntity farmAllStatusEntity:FarmAllStatusEntityList)
|
|
|
+ {
|
|
|
+ JSONObject newJo = new JSONObject();
|
|
|
+ dataJa.add(newJo);
|
|
|
+ newJo.put("time",dateUtil.formatDateText(farmAllStatusEntity.getCreateDate()));
|
|
|
+ newJo.put("value",farmAllStatusEntity.getDeviceOnline());
|
|
|
+ newJo.put("total", farmAllStatusEntity.getDeviceTotal());
|
|
|
+ newJo.put("rate", farmAllStatusEntity.getDeviceRate());
|
|
|
+ newJo.put("offline", farmAllStatusEntity.getDeviceOffline());
|
|
|
+ }
|
|
|
+ return new Result(ResultCode.SUCCESS,JSONObject.toJSONString(dataJa));
|
|
|
+ }
|
|
|
+
|
|
|
+ //计算注册设备数量
|
|
|
+ private int getDeviceCancelTotalFromDeviceRegister(String farmId) {
|
|
|
+ List<Object[]> deviceRegList = deviceRegisterRepo.getDeviceCountByFarmId(farmId);
|
|
|
+ Object[] deviceRegObj = (Object[]) deviceRegList.get(0);
|
|
|
+ //设备总数
|
|
|
+ return Integer.parseInt(deviceRegObj[0].toString());
|
|
|
+ }
|
|
|
+
|
|
|
+ //计算注册耳标数量
|
|
|
+ public Integer getEartagRegTotalByFarmId(String farmId){
|
|
|
+ int eartagTotal = 0;
|
|
|
+ List<Object[]> eartagRegList = eartagRegisterRepo.getEartagCountByFarmId(farmId);
|
|
|
+ Object[] eartagRegObj = (Object[]) eartagRegList.get(0);
|
|
|
+ //处于工作状态的耳标总数
|
|
|
+ return Integer.parseInt(eartagRegObj[0].toString());
|
|
|
+ }
|
|
|
+}
|