Commit 786ec9f882072da87848f87b955fbf5a56be17f6

Authored by xp.Huang
1 parent f6c52a01

fix: 任务中心,执行后返回的最近执行时间

@@ -69,11 +69,13 @@ public class RpcCommandTask { @@ -69,11 +69,13 @@ public class RpcCommandTask {
69 SecurityUser securityUser = new SecurityUser(); 69 SecurityUser securityUser = new SecurityUser();
70 String taskCenterId = tkTaskCenterDTO.getId(); 70 String taskCenterId = tkTaskCenterDTO.getId();
71 71
72 - String immediateExecuteKey = FastIotConstants.CacheConfigKey.TASK_IMMEDIATE_EXECUTE + "_" + taskCenterId;  
73 - Optional<TaskImmediateExecuteDTO> immediateExecuteDTO = cacheUtils.get(cacheName,immediateExecuteKey); 72 + String immediateExecuteKey =
  73 + FastIotConstants.CacheConfigKey.TASK_IMMEDIATE_EXECUTE + "_" + taskCenterId;
  74 + Optional<TaskImmediateExecuteDTO> immediateExecuteDTO =
  75 + cacheUtils.get(cacheName, immediateExecuteKey);
74 List<String> data = targetContent.getData(); 76 List<String> data = targetContent.getData();
75 - //如果从缓存里面获取了任务的立即执行,则以缓存里面的数据作为执行依据  
76 - if(immediateExecuteDTO.isPresent()){ 77 + // 如果从缓存里面获取了任务的立即执行,则以缓存里面的数据作为执行依据
  78 + if (immediateExecuteDTO.isPresent()) {
77 TaskImmediateExecuteDTO immediateExecute = immediateExecuteDTO.get(); 79 TaskImmediateExecuteDTO immediateExecute = immediateExecuteDTO.get();
78 data = immediateExecute.getTargetIds(); 80 data = immediateExecute.getTargetIds();
79 targetType = immediateExecute.getExecuteTarget(); 81 targetType = immediateExecute.getExecuteTarget();
@@ -186,7 +188,11 @@ public class RpcCommandTask { @@ -186,7 +188,11 @@ public class RpcCommandTask {
186 tkDeviceTaskCenterService.saveOrUpdate(tkDeviceTaskCenterDTO); 188 tkDeviceTaskCenterService.saveOrUpdate(tkDeviceTaskCenterDTO);
187 // 将设备执行时间存入缓存 189 // 将设备执行时间存入缓存
188 String key = 190 String key =
189 - FastIotConstants.CacheConfigKey.TASK_CENTER_DEVICE_EXECUTE_TIME + "_" + originateId; 191 + FastIotConstants.CacheConfigKey.TASK_CENTER_DEVICE_EXECUTE_TIME
  192 + + "_"
  193 + + taskCenterId
  194 + + "_"
  195 + + originateId;
190 cacheUtils.put(cacheName, key, executeTime); 196 cacheUtils.put(cacheName, key, executeTime);
191 } 197 }
192 } 198 }
@@ -39,6 +39,9 @@ public class TkTaskCenterDTO extends TenantDTO { @@ -39,6 +39,9 @@ public class TkTaskCenterDTO extends TenantDTO {
39 @ApiModelProperty(value = "最后执行时间") 39 @ApiModelProperty(value = "最后执行时间")
40 private Long lastExecuteTime; 40 private Long lastExecuteTime;
41 41
  42 + @ApiModelProperty(value = "最后执行时间:刚刚 1秒前")
  43 + private String lastExecuteStr;
  44 +
42 private TkDeviceTaskCenterDTO tkDeviceTaskCenter; 45 private TkDeviceTaskCenterDTO tkDeviceTaskCenter;
43 46
44 @ApiModelProperty(value = "备注") 47 @ApiModelProperty(value = "备注")
@@ -70,6 +70,8 @@ public class TkDeviceTaskCenterServiceImpl @@ -70,6 +70,8 @@ public class TkDeviceTaskCenterServiceImpl
70 String key = 70 String key =
71 FastIotConstants.CacheConfigKey.TASK_CENTER_DEVICE_EXECUTE_TIME 71 FastIotConstants.CacheConfigKey.TASK_CENTER_DEVICE_EXECUTE_TIME
72 + "_" 72 + "_"
  73 + + tkDeviceTaskCenterDTO.getId()
  74 + + "_"
73 + tkDeviceTaskCenterDTO.getTbDeviceId(); 75 + tkDeviceTaskCenterDTO.getTbDeviceId();
74 cacheUtils.invalidate(cacheName, key); 76 cacheUtils.invalidate(cacheName, key);
75 } 77 }
@@ -23,13 +23,18 @@ import org.thingsboard.server.common.data.yunteng.enums.StatusEnum; @@ -23,13 +23,18 @@ import org.thingsboard.server.common.data.yunteng.enums.StatusEnum;
23 import org.thingsboard.server.common.data.yunteng.enums.TargetTypeEnum; 23 import org.thingsboard.server.common.data.yunteng.enums.TargetTypeEnum;
24 import org.thingsboard.server.common.data.yunteng.utils.JacksonUtil; 24 import org.thingsboard.server.common.data.yunteng.utils.JacksonUtil;
25 import org.thingsboard.server.common.data.yunteng.utils.tools.TkPageData; 25 import org.thingsboard.server.common.data.yunteng.utils.tools.TkPageData;
  26 +import org.thingsboard.server.dao.util.yunteng.task.RelativeDateHandler;
26 import org.thingsboard.server.dao.yunteng.entities.TkDeviceTaskCenterEntity; 27 import org.thingsboard.server.dao.yunteng.entities.TkDeviceTaskCenterEntity;
27 import org.thingsboard.server.dao.yunteng.entities.TkTaskCenterEntity; 28 import org.thingsboard.server.dao.yunteng.entities.TkTaskCenterEntity;
28 import org.thingsboard.server.dao.yunteng.mapper.TkTaskCenterMapper; 29 import org.thingsboard.server.dao.yunteng.mapper.TkTaskCenterMapper;
29 import org.thingsboard.server.dao.yunteng.service.*; 30 import org.thingsboard.server.dao.yunteng.service.*;
30 31
31 import javax.transaction.Transactional; 32 import javax.transaction.Transactional;
  33 +import java.time.Instant;
  34 +import java.time.LocalDateTime;
  35 +import java.time.ZoneOffset;
32 import java.util.*; 36 import java.util.*;
  37 +import java.util.stream.Collectors;
33 38
34 @Service 39 @Service
35 @RequiredArgsConstructor 40 @RequiredArgsConstructor
@@ -50,24 +55,39 @@ public class TkTaskCenterServiceImpl @@ -50,24 +55,39 @@ public class TkTaskCenterServiceImpl
50 getPage(queryMap, FastIotConstants.DefaultOrder.CREATE_TIME, false); 55 getPage(queryMap, FastIotConstants.DefaultOrder.CREATE_TIME, false);
51 IPage<TkTaskCenterDTO> iPage = baseMapper.getPageData(page, queryMap); 56 IPage<TkTaskCenterDTO> iPage = baseMapper.getPageData(page, queryMap);
52 if (!iPage.getRecords().isEmpty()) { 57 if (!iPage.getRecords().isEmpty()) {
53 - iPage.getRecords().stream()  
54 - .peek(  
55 - obj -> {  
56 - // 任务执行时间  
57 - String key =  
58 - FastIotConstants.CacheConfigKey.TASK_CENTER_EXECUTE_TIME + "_" + obj.getId(); 58 + iPage.setRecords(
  59 + iPage.getRecords().stream()
  60 + .map(
  61 + obj -> {
  62 + // 任务执行时间
  63 + String key =
  64 + FastIotConstants.CacheConfigKey.TASK_CENTER_EXECUTE_TIME
  65 + + "_"
  66 + + obj.getId();
59 67
60 - // 如果通过设备查询分页,以设备执行时间为准  
61 - String tbDeviceId = (String) queryMap.get("tbDeviceId");  
62 - if (null != tbDeviceId) {  
63 - key =  
64 - FastIotConstants.CacheConfigKey.TASK_CENTER_DEVICE_EXECUTE_TIME  
65 - + "_"  
66 - + tbDeviceId;  
67 - }  
68 - Optional<Long> lastExecuteTime = cacheUtils.get(cacheName, key);  
69 - lastExecuteTime.ifPresent(obj::setLastExecuteTime);  
70 - }); 68 + // 如果通过设备查询分页,以设备执行时间为准
  69 + String tbDeviceId = (String) queryMap.get("tbDeviceId");
  70 + if (null != tbDeviceId) {
  71 + key =
  72 + FastIotConstants.CacheConfigKey.TASK_CENTER_DEVICE_EXECUTE_TIME
  73 + + "_"
  74 + + obj.getId()
  75 + + "_"
  76 + + tbDeviceId;
  77 + }
  78 + Optional<Long> lastExecuteTime = cacheUtils.get(cacheName, key);
  79 + if (lastExecuteTime.isPresent()) {
  80 + Long executeTime = lastExecuteTime.get();
  81 + obj.setLastExecuteTime(executeTime);
  82 + LocalDateTime localDateTime =
  83 + Instant.ofEpochMilli(executeTime)
  84 + .atZone(ZoneOffset.ofHours(8))
  85 + .toLocalDateTime();
  86 + obj.setLastExecuteStr(RelativeDateHandler.format(localDateTime));
  87 + }
  88 + return obj;
  89 + })
  90 + .collect(Collectors.toList()));
71 } 91 }
72 return new TkPageData<>(iPage.getRecords(), iPage.getTotal()); 92 return new TkPageData<>(iPage.getRecords(), iPage.getTotal());
73 } 93 }