|
@@ -75,42 +75,38 @@ public class EartagServiceImpl implements IEartagService {
|
|
|
@Override
|
|
|
public Integer countAct(String earmark, String nowAct,String farmId) {
|
|
|
|
|
|
- EartagData eartagData = eartagData2Mapper.selectOne(new QueryWrapper<EartagData>().eq("farm_id", farmId)
|
|
|
- .eq("earmark", earmark)
|
|
|
- .orderByDesc("add_time").orderByDesc("id").last("limit 1"));
|
|
|
- Integer lastAct = 0;
|
|
|
- if (ObjectUtil.isNotEmpty(eartagData)){
|
|
|
- lastAct = eartagData.getAct();
|
|
|
+// EartagData eartagData = eartagData2Mapper.selectOne(new QueryWrapper<EartagData>().eq("farm_id", farmId).eq("earmark", earmark).orderByDesc("ask_time").last("limit 1"));
|
|
|
+// Integer lastAct = 0;
|
|
|
+// if (ObjectUtil.isNotEmpty(eartagData)){
|
|
|
+// lastAct = eartagData.getAct();
|
|
|
+// }
|
|
|
+// int nowActInt = Integer.parseInt(nowAct);
|
|
|
+// return nowActInt < lastAct ? nowActInt: nowActInt - lastAct;
|
|
|
+
|
|
|
+
|
|
|
+ //读取hash
|
|
|
+ Object actObj = redisTemplate.opsForHash().get(earmark, "act");
|
|
|
+ if (actObj == null) {
|
|
|
+ //--初始化最新的redis记录
|
|
|
+ initRedisObj(earmark, farmId);
|
|
|
+ Object lastAct = redisTemplate.opsForHash().get(earmark, "act");
|
|
|
+ if (lastAct == null) {
|
|
|
+ log.error("#--- redis数据库有问题,请检查redis是否能正常连接 ---# ");
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
}
|
|
|
+ int lastAct = Integer.parseInt(redisTemplate.opsForHash().get(earmark, "act").toString());
|
|
|
int nowActInt = Integer.parseInt(nowAct);
|
|
|
return nowActInt < lastAct ? nowActInt: nowActInt - lastAct;
|
|
|
|
|
|
|
|
|
- //读取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 lastAct = redisTemplate.opsForHash().get(deviceCode, "act");
|
|
|
-// Integer act1 = Integer.parseInt(nowAct) - Integer.parseInt(lastAct.toString());
|
|
|
-//// redisTemplate.opsForHash().put(deviceCode, "act1", act1);
|
|
|
-// if(act1<0){
|
|
|
-// return Integer.parseInt(nowAct);
|
|
|
-// }
|
|
|
-// return act1;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 初始化redis最新记录
|
|
|
*/
|
|
|
- private void initRedisObj(String deviceCode) {
|
|
|
- EartagDataEntity lastEartagDataEntity = eartagDataRepo.getLastEartagData(deviceCode);
|
|
|
+ private void initRedisObj(String earmark,String farmId) {
|
|
|
+ EartagData lastEartagDataEntity = eartagData2Mapper.selectOne(new QueryWrapper<EartagData>().eq("farm_id", farmId).eq("earmark", earmark).orderByDesc("ask_time").last("limit 1"));
|
|
|
if (lastEartagDataEntity != null) {
|
|
|
System.out.println("## lastEartagDataEntity.toString>>" + lastEartagDataEntity.toString());
|
|
|
//--从数据库读取最新记录并覆盖redis
|
|
@@ -130,7 +126,7 @@ public class EartagServiceImpl implements IEartagService {
|
|
|
map.put("other", lastEartagDataEntity.getOther());
|
|
|
map.put("addTime", lastEartagDataEntity.getAddTime().toString());
|
|
|
//为hash结构设置多个键值对(hmset)
|
|
|
- redisTemplate.opsForHash().putAll(deviceCode, map);
|
|
|
+ redisTemplate.opsForHash().putAll(earmark, map);
|
|
|
}
|
|
|
}
|
|
|
}
|