|
@@ -0,0 +1,361 @@
|
|
|
+package com.huimv.eartag2.eartag.service.impl;
|
|
|
+
|
|
|
+import com.huimv.eartag2.common.dao.entity.*;
|
|
|
+import com.huimv.eartag2.common.dao.repo.EartagDeviceRegisterRepo;
|
|
|
+import com.huimv.eartag2.common.utils.BizConst;
|
|
|
+import com.huimv.eartag2.common.utils.DateUtil;
|
|
|
+import com.huimv.eartag2.eartag.service.ICacheService;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.text.ParseException;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Project : huimv.shiwan
|
|
|
+ * @Package : com.huimv.biosafety.uface.controller
|
|
|
+ * @Description : TODO
|
|
|
+ * @Version : 1.0
|
|
|
+ * @Author : ZhuoNing
|
|
|
+ * @Create : 2020-12-25
|
|
|
+ **/
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class CacheServiceImpl implements ICacheService {
|
|
|
+ @Autowired
|
|
|
+ private RedisTemplate redisTemplate;
|
|
|
+ @Autowired
|
|
|
+ private EartagDeviceRegisterRepo eartagDeviceRegisterRepo;
|
|
|
+ @Value("${redis.expire.eartag_online_set}")
|
|
|
+ private Integer eartagOnlineSetExpire;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Boolean isExistTodayFarmAllStatus(String deviceCode) throws ParseException {
|
|
|
+ System.out.println("isExistTodayFarmAllStatus 2.deviceCode>>"+deviceCode);
|
|
|
+
|
|
|
+ //(1)从注册表缓存中(device_register_牧场id)取出牧场id,(2)如果缓存没有则从注册表(eartag_device_register)中查询00
|
|
|
+ //(1)
|
|
|
+ Object farmIdObj = redisTemplate.opsForHash().get(BizConst.DEVICE_REGISTER_PREFIX + deviceCode,"farmId");
|
|
|
+ System.out.println("2.farmIdObj>>"+farmIdObj);
|
|
|
+ String farmId = "";
|
|
|
+ if(farmIdObj != null){
|
|
|
+ farmId = farmIdObj.toString();
|
|
|
+ }else{
|
|
|
+ //(2)
|
|
|
+ EartagDeviceRegisterEntity eartagDeviceRegisterEntity = eartagDeviceRegisterRepo.getDeviceRegister(deviceCode);
|
|
|
+ System.out.println("2.eartagDeviceRegisterEntity>>"+eartagDeviceRegisterEntity.toString());
|
|
|
+ if(eartagDeviceRegisterEntity == null){
|
|
|
+ log.error("设备编号:"+deviceCode+",不存在.");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ farmId = eartagDeviceRegisterEntity.getFarmId();
|
|
|
+ }
|
|
|
+ System.out.println("2.farmId>>"+farmId);
|
|
|
+ //从牧场总状态缓存中获取最近一次日期
|
|
|
+ Object createDateObj = redisTemplate.opsForHash().get(BizConst.FARM_ALL_STATUS_PREFIX + farmId,"createDate");
|
|
|
+ System.out.println("2.createDateObj>>"+createDateObj);
|
|
|
+ if(createDateObj == null){
|
|
|
+ //--总状态记录记录不存在
|
|
|
+ System.out.println("A.");
|
|
|
+ return false;
|
|
|
+ }else{
|
|
|
+ //
|
|
|
+ String createDate = createDateObj.toString();
|
|
|
+ System.out.println("缓存中上次操作日期 createDate>>"+createDate);
|
|
|
+ if(new DateUtil().getTodayDateText().trim().equalsIgnoreCase(createDate)){
|
|
|
+ //--当天记录存在,就返回true;
|
|
|
+ System.out.println("B.");
|
|
|
+ return true;
|
|
|
+ }else{
|
|
|
+ //--当天记录不存在,就返回false;
|
|
|
+ System.out.println("C.");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //# 读根据设备编码读取牧场id
|
|
|
+ @Override
|
|
|
+ public Object getFarmIdByDeviceCode(String deviceCode) {
|
|
|
+ Object farmIdObj = redisTemplate.opsForHash().get(BizConst.DEVICE_REGISTER_PREFIX + deviceCode,"farmId");
|
|
|
+ return farmIdObj;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object getCreateDateByFarmId(String farmId) {
|
|
|
+ return redisTemplate.opsForHash().get(BizConst.FARM_ALL_STATUS_PREFIX + farmId,"createDate");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void putFarmAllStatusCache(String farmId, EartarFarmAllStatusEntity farmAllStatusEntity) throws ParseException {
|
|
|
+ DateUtil duObj = new DateUtil();
|
|
|
+ redisTemplate.opsForHash().put(BizConst.FARM_ALL_STATUS_PREFIX+farmId,"deviceOnline",farmAllStatusEntity.getDeviceOnline());
|
|
|
+ redisTemplate.opsForHash().put(BizConst.FARM_ALL_STATUS_PREFIX+farmId,"deviceOffline",farmAllStatusEntity.getDeviceOffline());
|
|
|
+ redisTemplate.opsForHash().put(BizConst.FARM_ALL_STATUS_PREFIX+farmId,"deviceRate",farmAllStatusEntity.getDeviceRate());
|
|
|
+ redisTemplate.opsForHash().put(BizConst.FARM_ALL_STATUS_PREFIX+farmId,"updateTime",duObj.formatDateTime(duObj.getTodayText()));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void putAllFarmAllStatusCache(String farmId, EartarFarmAllStatusEntity farmAllStatusEntity) throws ParseException {
|
|
|
+ Map allStatusMap = new HashMap();
|
|
|
+// allStatusMap.put("id",farmAllStatusEntity.getId());
|
|
|
+ allStatusMap.put("deviceTotal",farmAllStatusEntity.getDeviceTotal());
|
|
|
+ allStatusMap.put("deviceOnline",farmAllStatusEntity.getDeviceOnline());
|
|
|
+ allStatusMap.put("deviceOffline",farmAllStatusEntity.getDeviceOffline());
|
|
|
+ allStatusMap.put("deviceRate",farmAllStatusEntity.getDeviceRate());
|
|
|
+ allStatusMap.put("deviceCancel",farmAllStatusEntity.getDeviceCancel());
|
|
|
+ allStatusMap.put("eartagTotal",farmAllStatusEntity.getEartagTotal());
|
|
|
+ allStatusMap.put("eartagOnline",farmAllStatusEntity.getEartagOnline());
|
|
|
+ allStatusMap.put("farmId",farmId);
|
|
|
+// allStatusMap.put("updateTime",duObj.formatDateTime(duObj.getTodayText()));
|
|
|
+// allStatusMap.put("createDate",duObj.getTodayDate());
|
|
|
+ allStatusMap.put("updateTime",farmAllStatusEntity.getUpdateTime());
|
|
|
+ allStatusMap.put("createDate",farmAllStatusEntity.getCreateDate());
|
|
|
+
|
|
|
+ System.out.println("allStatusMap>>"+allStatusMap.toString());
|
|
|
+ redisTemplate.opsForHash().putAll(BizConst.FARM_ALL_STATUS_PREFIX+farmId, allStatusMap);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void flashAllStatusCache(String farmId, EartarFarmAllStatusEntity farmAllStatusEntity) {
|
|
|
+ Map allStatusMap = new HashMap();
|
|
|
+// allStatusMap.put("id",farmAllStatusEntity.getId());
|
|
|
+ allStatusMap.put("deviceTotal",farmAllStatusEntity.getDeviceTotal());
|
|
|
+ allStatusMap.put("deviceOnline",farmAllStatusEntity.getDeviceOnline());
|
|
|
+ allStatusMap.put("deviceOffline",farmAllStatusEntity.getDeviceOffline());
|
|
|
+ allStatusMap.put("deviceRate",farmAllStatusEntity.getDeviceRate());
|
|
|
+ allStatusMap.put("deviceCancel",farmAllStatusEntity.getDeviceCancel());
|
|
|
+ allStatusMap.put("eartagTotal",farmAllStatusEntity.getEartagTotal());
|
|
|
+ allStatusMap.put("eartagOnline",farmAllStatusEntity.getEartagOnline());
|
|
|
+ allStatusMap.put("farmId",farmId);
|
|
|
+// allStatusMap.put("updateTime",duObj.formatDateTime(duObj.getTodayText()));
|
|
|
+// allStatusMap.put("createDate",duObj.getTodayDate());
|
|
|
+ allStatusMap.put("updateTime",farmAllStatusEntity.getUpdateTime());
|
|
|
+ allStatusMap.put("createDate",farmAllStatusEntity.getCreateDate());
|
|
|
+
|
|
|
+ System.out.println("allStatusMap>>"+allStatusMap.toString());
|
|
|
+ redisTemplate.opsForHash().putAll(BizConst.FARM_ALL_STATUS_PREFIX+farmId, allStatusMap);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean isExistDeviceOnlineSet(String farmId, String deviceCode, String todayDateText) {
|
|
|
+ return redisTemplate.opsForSet().isMember(BizConst.SET_FARM_DEVICE_ONLINE_PREFIX + farmId+"_"+todayDateText, deviceCode);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Method : putDeviceCode
|
|
|
+ * @Description : 将新的设备编号加入到缓存当中;
|
|
|
+ * @Params : [farmId, deviceCode]
|
|
|
+ * @Return : void
|
|
|
+ *
|
|
|
+ * @Author : ZhuoNing
|
|
|
+ * @Date : 2022/3/1
|
|
|
+ * @Time : 13:42
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void putDeviceToOnline(String farmId, String deviceCode, String todayDateText) {
|
|
|
+ System.out.println("## 心跳 2.4");
|
|
|
+ String setKey = BizConst.SET_FARM_DEVICE_ONLINE_PREFIX + farmId+"_"+todayDateText;
|
|
|
+ redisTemplate.opsForSet().add(setKey, deviceCode);
|
|
|
+ redisTemplate.expire(setKey,eartagOnlineSetExpire , TimeUnit.HOURS);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void putDeviceRegister(String deviceCode, EartagDeviceRegisterEntity deviceRegisterEntity) {
|
|
|
+ //--更新redis中注册表数据
|
|
|
+ String deviceRegHashKey = BizConst.DEVICE_REGISTER_PREFIX + deviceCode;
|
|
|
+ //将Entity转为Map
|
|
|
+ Map registerMap = RegisterEntityToMap(deviceRegisterEntity);
|
|
|
+ redisTemplate.opsForHash().putAll(deviceRegHashKey, registerMap);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Method : putDeviceInOnline
|
|
|
+ * @Description : 将数据库中的在线数据更新对应的缓存数据中
|
|
|
+ * @Params : [deviceOnlineHashKey, eartagDeviceOnlineEntity]
|
|
|
+ * @Return : void
|
|
|
+ *
|
|
|
+ * @Author : ZhuoNing
|
|
|
+ * @Date : 2022/3/1
|
|
|
+ * @Time : 16:07
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void putDeviceInOnline(String deviceCode, EartagDeviceOnlineEntity eartagDeviceOnlineEntity) {
|
|
|
+ Map map = new HashMap();
|
|
|
+// map.put("id", eartagDeviceOnlineEntity.getId());
|
|
|
+ map.put("deviceCode", eartagDeviceOnlineEntity.getDeviceCode());
|
|
|
+ map.put("alias", eartagDeviceOnlineEntity.getAlias());
|
|
|
+ map.put("heartbeatTotal", eartagDeviceOnlineEntity.getHeartbeatTotal());
|
|
|
+ map.put("envtempTotal", eartagDeviceOnlineEntity.getEnvtempTotal());
|
|
|
+ map.put("eartagTotal", eartagDeviceOnlineEntity.getEartagTotal());
|
|
|
+ map.put("firstTime", eartagDeviceOnlineEntity.getFirstTime());
|
|
|
+ map.put("lastTime", eartagDeviceOnlineEntity.getLastTime());
|
|
|
+ map.put("lastEnvtemp", eartagDeviceOnlineEntity.getLastEnvtemp());
|
|
|
+ map.put("lastEartag", eartagDeviceOnlineEntity.getLastEartag());
|
|
|
+ map.put("addDate", eartagDeviceOnlineEntity.getAddDate());
|
|
|
+ map.put("farmId", eartagDeviceOnlineEntity.getFarmId());
|
|
|
+ //更新缓存
|
|
|
+ String deviceOnlineKey = BizConst.DEVICE_ONLINE_PREFIX + deviceCode;
|
|
|
+ redisTemplate.opsForHash().putAll(deviceOnlineKey, map);
|
|
|
+// Map onlineMap = fromOnlineEntityToMap(newDeviceOnlineEntity);
|
|
|
+// redisTemplate.opsForHash().putAll(deviceOnlineHashKey, onlineMap);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Method : isExistEartagOnlineStatus
|
|
|
+ * @Description :
|
|
|
+ * @Params : [farmId, earmark]
|
|
|
+ * @Return : boolean
|
|
|
+ *
|
|
|
+ * @Author : ZhuoNing
|
|
|
+ * @Date : 2022/3/1
|
|
|
+ * @Time : 21:18
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public boolean isExistEartagOnlineSet(String farmId, String earmark, String todayDateText) {
|
|
|
+// redisTemplate.opsForSet().add(BizConst.SET_FARM_DEVICE_ONLINE_PREFIX + farmId+"_"+todayDateText, earmark);
|
|
|
+ String setKey = BizConst.SET_FARM_EARMARK_ONLINE_PREFIX + farmId+"_"+todayDateText;
|
|
|
+ System.out.println("## eartag.setKey .isExistEartagOnlineSet >>"+setKey+",earmark>>"+earmark);
|
|
|
+ return redisTemplate.opsForSet().isMember(setKey, earmark);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void putEartagToOnlineInSet(String farmId, String earmark, String todayDateText) throws ParseException {
|
|
|
+ String setKey = BizConst.SET_FARM_EARMARK_ONLINE_PREFIX + farmId+"_"+todayDateText;
|
|
|
+ System.out.println("## eartag.setKey .putEartagToOnlineInSet>>"+setKey+",earmark>>"+earmark);
|
|
|
+ redisTemplate.opsForSet().add(setKey, earmark);
|
|
|
+ redisTemplate.expire(setKey,eartagOnlineSetExpire , TimeUnit.HOURS);
|
|
|
+// Set<String> resultSet = redisTemplate.opsForSet().members(setKey);
|
|
|
+// System.out.println("## "+setKey+">>"+resultSet.size()+" >>"+resultSet);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Method : getEartagOnlineTotalFromCache
|
|
|
+ * @Description : 返回集合缓存的数量;
|
|
|
+ * @Params : [farmId, todayDateText]
|
|
|
+ * @Return : void
|
|
|
+ *
|
|
|
+ * @Author : ZhuoNing
|
|
|
+ * @Date : 2022/3/3
|
|
|
+ * @Time : 10:55
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void getEartagOnlineTotalFromCache(String farmId, String todayDateText) {
|
|
|
+ String setKey = BizConst.SET_FARM_EARMARK_ONLINE_PREFIX + farmId+"_"+todayDateText;
|
|
|
+ redisTemplate.opsForSet().members(setKey).size();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Method : putEartagRegister
|
|
|
+ * @Description :
|
|
|
+ * @Params : [earmark, eartagRegisterEntity]
|
|
|
+ * @Return : void
|
|
|
+ *
|
|
|
+ * @Author : ZhuoNing
|
|
|
+ * @Date : 2022/3/3
|
|
|
+ * @Time : 15:29
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void putEartagRegister(String earmark, EartagEartagRegisterEntity 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 :
|
|
|
+ * @Params : [earmark, eartagOnlineEntity]
|
|
|
+ * @Return : void
|
|
|
+ *
|
|
|
+ * @Author : ZhuoNing
|
|
|
+ * @Date : 2022/3/3
|
|
|
+ * @Time : 15:29
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void putEartagOnlineCount(String earmark, EartagEartagOnlineEntity eartagOnlineEntity) {
|
|
|
+ Map map = new HashMap();
|
|
|
+// map.put("id", eartagRegisterEntity.getId());
|
|
|
+ map.put("earmark",eartagOnlineEntity.getEarmark());
|
|
|
+ map.put("total",eartagOnlineEntity.getTotal());
|
|
|
+ map.put("firstTime",eartagOnlineEntity.getFirstTime());
|
|
|
+ map.put("lastTime",eartagOnlineEntity.getLastTime());
|
|
|
+ map.put("envTemp",eartagOnlineEntity.getEnvTemp());
|
|
|
+ map.put("eartagTemp",eartagOnlineEntity.getEartagTemp());
|
|
|
+ map.put("addDate",eartagOnlineEntity.getAddDate());
|
|
|
+ map.put("farmId",eartagOnlineEntity.getFarmId());
|
|
|
+ //更新缓存
|
|
|
+ String earmarkOnlineKey = BizConst.EARTAG_ONLINE_PREFIX + earmark;
|
|
|
+ redisTemplate.opsForHash().putAll(earmarkOnlineKey, map);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Integer getDeviceTotal(String farmId) {
|
|
|
+ String valueKey = BizConst.FARM_DEVICE_TOTAL_PREFIX+farmId;
|
|
|
+// String value = (String) redisTemplate.opsForValue().get(valueKey);
|
|
|
+// System.out.println("## value>>"+value);
|
|
|
+// redisTemplate.opsForValue().set(valueKey,"50");
|
|
|
+ Object obj = redisTemplate.opsForValue().get(valueKey);
|
|
|
+ if(obj == null){
|
|
|
+ return null;
|
|
|
+ }else{
|
|
|
+ return Integer.parseInt(String.valueOf(obj));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Method : RegisterEntityToMap
|
|
|
+ * @Description :
|
|
|
+ * @Params : [deviceRegisterEntity]
|
|
|
+ * @Return : java.util.Map
|
|
|
+ * @Author : ZhuoNing
|
|
|
+ * @Date : 2022/2/21
|
|
|
+ * @Time : 14:03
|
|
|
+ */
|
|
|
+ private Map RegisterEntityToMap(EartagDeviceRegisterEntity deviceRegisterEntity) {
|
|
|
+ Map map = new HashMap();
|
|
|
+ map.put("Id", deviceRegisterEntity.getId());
|
|
|
+ map.put("deviceCode", deviceRegisterEntity.getDeviceCode());
|
|
|
+ map.put("farmId", deviceRegisterEntity.getFarmId());
|
|
|
+ map.put("farmName", deviceRegisterEntity.getFarmName());
|
|
|
+ map.put("typeF", deviceRegisterEntity.getTypeF());
|
|
|
+ map.put("stage", deviceRegisterEntity.getStage());
|
|
|
+ map.put("pigpenId", deviceRegisterEntity.getPigpenId());
|
|
|
+ map.put("unitId", deviceRegisterEntity.getUnitId());
|
|
|
+ map.put("registerTime", deviceRegisterEntity.getRegisterTime());
|
|
|
+ map.put("lastTime", deviceRegisterEntity.getLastTime());
|
|
|
+ map.put("activeStatus", deviceRegisterEntity.getActiveStatus());
|
|
|
+ map.put("activeTime", deviceRegisterEntity.getActiveTime());
|
|
|
+ map.put("deviceStatus", deviceRegisterEntity.getDeviceStatus());
|
|
|
+ map.put("deviceAlias", deviceRegisterEntity.getDeviceAlias());
|
|
|
+ map.put("location", deviceRegisterEntity.getLocation());
|
|
|
+ map.put("remark", deviceRegisterEntity.getRemark());
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+ //#
|
|
|
+ public Object getValByKey(String hashKey,String key){
|
|
|
+ Object valueObj = redisTemplate.opsForHash().get(hashKey,key);
|
|
|
+ return valueObj;
|
|
|
+ }
|
|
|
+}
|