Commit 795c47bf102ff353e09bf34c6b2739ca7e1f5b36

Authored by 云中非
1 parent b24f8e16

fix: 组态相关逻辑优化

1、更新或新增,同时刷新组件数据。
2、删除时,同时清空组件数据。
... ... @@ -8,6 +8,7 @@ import org.thingsboard.server.common.data.yunteng.common.UpdateGroup;
8 8
9 9 import javax.validation.constraints.NotEmpty;
10 10 import javax.validation.constraints.NotNull;
  11 +import java.util.List;
11 12
12 13 @EqualsAndHashCode(callSuper = true)
13 14 @Data
... ... @@ -38,4 +39,6 @@ public class ConfigurationContentDTO extends TenantDTO {
38 39 message = "类型不能为空",
39 40 groups = {UpdateGroup.class, AddGroup.class})
40 41 private Integer type;
  42 +
  43 + private List<String> nodeIds;
41 44 }
... ...
... ... @@ -37,10 +37,55 @@ public class YtConfigurationContentServiceImpl
37 37 @Transactional
38 38 public ConfigurationContentDTO saveConfigurationContent(
39 39 ConfigurationContentDTO configurationContentDTO) {
  40 + freshNodeData(configurationContentDTO.getTenantId(), configurationContentDTO.getId(), configurationContentDTO.getNodeIds());
40 41 baseMapper.insert(configurationContentDTO.getEntity(ConfigurationContent.class));
41 42 return configurationContentDTO;
42 43 }
43 44
  45 + /**
  46 + * 刷新组态的结点数据
  47 + * @param tenantId 租户ID
  48 + * @param contentId 页签ID
  49 + * @param nodeIds 页面内组件集合
  50 + */
  51 + private void freshNodeData(String tenantId,String contentId,List<String> nodeIds){
  52 + LambdaQueryWrapper<ConfigurationDatasource> datasourceFilter = new QueryWrapper<ConfigurationDatasource>().lambda()
  53 + .eq(ConfigurationDatasource::getTenantId,tenantId)
  54 + .eq(ConfigurationDatasource::getContentId,contentId);
  55 + List<ConfigurationDatasource> datasources =datasourceMapper.selectList(datasourceFilter);
  56 + datasources.forEach(item ->{
  57 + if(nodeIds == null
  58 + || nodeIds.isEmpty()
  59 + || !nodeIds.contains(item.getNodeId())){
  60 + datasourceMapper.deleteById(item.getId());
  61 + }
  62 + });
  63 +
  64 + LambdaQueryWrapper<ConfigurationEvent> eventFilter = new QueryWrapper<ConfigurationEvent>().lambda()
  65 + .eq(ConfigurationEvent::getTenantId,tenantId)
  66 + .eq(ConfigurationEvent::getContentId,contentId);
  67 + List<ConfigurationEvent> events =eventMapper.selectList(eventFilter);
  68 + events.forEach(item ->{
  69 + if(nodeIds == null
  70 + || nodeIds.isEmpty()
  71 + || !nodeIds.contains(item.getId())){
  72 + eventMapper.deleteById(item.getId());
  73 + }
  74 + });
  75 +
  76 + LambdaQueryWrapper<ConfigurationAct> actFilter = new QueryWrapper<ConfigurationAct>().lambda()
  77 + .eq(ConfigurationAct::getTenantId,tenantId)
  78 + .eq(ConfigurationAct::getContentId,contentId);
  79 + List<ConfigurationAct> acts =actMapper.selectList(actFilter);
  80 + acts.forEach(item ->{
  81 + if(nodeIds == null
  82 + || nodeIds.isEmpty()
  83 + || !nodeIds.contains(item.getId())){
  84 + actMapper.deleteById(item.getId());
  85 + }
  86 + });
  87 + }
  88 +
44 89 @Override
45 90 @Transactional
46 91 public ConfigurationContentDTO updateConfigurationContent(
... ... @@ -50,6 +95,7 @@ public class YtConfigurationContentServiceImpl
50 95 if (!configurationContent.getTenantId().equals(configurationContentDTO.getTenantId())) {
51 96 throw new YtDataValidationException(ErrorMessage.TENANT_MISMATCHING.getMessage());
52 97 }
  98 + freshNodeData(configurationContentDTO.getTenantId(), configurationContentDTO.getId(), configurationContentDTO.getNodeIds());
53 99 baseMapper.updateById(configurationContentDTO.getEntity(ConfigurationContent.class));
54 100 return configurationContentDTO;
55 101 }
... ... @@ -66,27 +112,39 @@ public class YtConfigurationContentServiceImpl
66 112 throw new YtDataValidationException(ErrorMessage.TENANT_MISMATCHING.getMessage());
67 113 }
68 114 }
  115 + deleteNodeDatas(null,deleteDTO.getIds());
69 116 return baseMapper.deleteBatchIds(deleteDTO.getIds()) > FastIotConstants.MagicNumber.ZERO;
70 117 }
71 118
72 119 @Override
73 120 @Transactional
74 121 public boolean deleteConfigurationContentByCenterId(Set<String> configurationCenterIds) {
  122 + deleteNodeDatas(configurationCenterIds,null);
  123 + return baseMapper.delete(
  124 + new LambdaQueryWrapper<ConfigurationContent>()
  125 + .in(ConfigurationContent::getConfigurationId, configurationCenterIds))
  126 + > FastIotConstants.MagicNumber.ZERO;
  127 + }
  128 +
  129 + /**
  130 + * 清除组件数据
  131 + * @param configIds
  132 + * @param contentIds
  133 + */
  134 + private void deleteNodeDatas(Set<String> configIds,Set<String> contentIds) {
75 135 LambdaQueryWrapper<ConfigurationDatasource> dataFilter = new QueryWrapper<ConfigurationDatasource>().lambda()
76   - .in(ConfigurationDatasource::getConfigurationId,configurationCenterIds);
  136 + .in(configIds!=null,ConfigurationDatasource::getConfigurationId, configIds)
  137 + .in(contentIds!=null,ConfigurationDatasource::getContentId, contentIds);
77 138 datasourceMapper.delete(dataFilter);
78 139
79 140 LambdaQueryWrapper<ConfigurationEvent> eventFilter = new QueryWrapper<ConfigurationEvent>().lambda()
80   - .in(ConfigurationEvent::getConfigurationId,configurationCenterIds);
  141 + .in(contentIds!=null,ConfigurationEvent::getContentId, contentIds)
  142 + .in(configIds!=null,ConfigurationEvent::getConfigurationId, configIds);
81 143 eventMapper.delete(eventFilter);
82 144
83 145 LambdaQueryWrapper<ConfigurationAct> actFilter = new QueryWrapper<ConfigurationAct>().lambda()
84   - .in(ConfigurationAct::getConfigurationId,configurationCenterIds);
  146 + .in(contentIds!=null,ConfigurationAct::getContentId, contentIds)
  147 + .in(configIds!=null,ConfigurationAct::getConfigurationId, configIds);
85 148 actMapper.delete(actFilter);
86   -
87   - return baseMapper.delete(
88   - new LambdaQueryWrapper<ConfigurationContent>()
89   - .in(ConfigurationContent::getConfigurationId, configurationCenterIds))
90   - > FastIotConstants.MagicNumber.ZERO;
91 149 }
92 150 }
... ...