Commit 9658ed1a0d93dd492e3551038c105f540ccda2aa

Authored by YunaiV
1 parent e4ef1c81

【代码评审】IOT:物模型的 review

yudao-module-iot/yudao-module-iot-api/src/main/java/cn/iocoder/yudao/module/iot/enums/ErrorCodeConstants.java
@@ -21,4 +21,5 @@ public interface ErrorCodeConstants { @@ -21,4 +21,5 @@ public interface ErrorCodeConstants {
21 21
22 // ========== IoT 设备 1-050-003-000 ============ 22 // ========== IoT 设备 1-050-003-000 ============
23 ErrorCode DEVICE_NOT_EXISTS = new ErrorCode(1_050_003_000, "设备不存在"); 23 ErrorCode DEVICE_NOT_EXISTS = new ErrorCode(1_050_003_000, "设备不存在");
  24 +
24 } 25 }
yudao-module-iot/yudao-module-iot-api/src/main/java/cn/iocoder/yudao/module/iot/enums/product/IotAccessModeEnum.java
@@ -10,7 +10,7 @@ import lombok.Getter; @@ -10,7 +10,7 @@ import lombok.Getter;
10 */ 10 */
11 @AllArgsConstructor 11 @AllArgsConstructor
12 @Getter 12 @Getter
13 -public enum IotAccessModeEnum { 13 +public enum IotAccessModeEnum {
14 14
15 READ("r"), 15 READ("r"),
16 WRITE("w"), 16 WRITE("w"),
@@ -18,8 +18,4 @@ public enum IotAccessModeEnum { @@ -18,8 +18,4 @@ public enum IotAccessModeEnum {
18 18
19 private final String mode; 19 private final String mode;
20 20
21 - public String getMode() {  
22 - return mode;  
23 - }  
24 -  
25 } 21 }
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/mysql/thinkmodelfunction/IotThinkModelFunctionMapper.java
@@ -29,10 +29,13 @@ public interface IotThinkModelFunctionMapper extends BaseMapperX<IotThinkModelFu @@ -29,10 +29,13 @@ public interface IotThinkModelFunctionMapper extends BaseMapperX<IotThinkModelFu
29 IotThinkModelFunctionDO::getType, type); 29 IotThinkModelFunctionDO::getType, type);
30 } 30 }
31 31
32 - default List<IotThinkModelFunctionDO> selectListByProductIdAndIdentifiersAndTypes(Long productId, List<String> list, List<Integer> list1){ 32 + default List<IotThinkModelFunctionDO> selectListByProductIdAndIdentifiersAndTypes(Long productId,
  33 + List<String> identifiers,
  34 + List<Integer> types){
33 return selectList(new LambdaQueryWrapperX<IotThinkModelFunctionDO>() 35 return selectList(new LambdaQueryWrapperX<IotThinkModelFunctionDO>()
34 .eq(IotThinkModelFunctionDO::getProductId, productId) 36 .eq(IotThinkModelFunctionDO::getProductId, productId)
35 - .in(IotThinkModelFunctionDO::getIdentifier, list)  
36 - .in(IotThinkModelFunctionDO::getType, list1)); 37 + .in(IotThinkModelFunctionDO::getIdentifier, identifiers)
  38 + .in(IotThinkModelFunctionDO::getType, types));
37 } 39 }
  40 +
38 } 41 }
39 \ No newline at end of file 42 \ No newline at end of file
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/thinkmodelfunction/IotThinkModelFunctionServiceImpl.java
@@ -55,6 +55,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe @@ -55,6 +55,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
55 // 3. 如果创建的是属性,需要更新默认的事件和服务 55 // 3. 如果创建的是属性,需要更新默认的事件和服务
56 if (Objects.equals(createReqVO.getType(), IotThingModelTypeEnum.PROPERTY.getType())) { 56 if (Objects.equals(createReqVO.getType(), IotThingModelTypeEnum.PROPERTY.getType())) {
57 // 获取当前属性列表,并添加新插入的属性 57 // 获取当前属性列表,并添加新插入的属性
  58 + // TODO @haohao:是不是插入后,查询已经包含了 function
58 List<IotThinkModelFunctionDO> propertyList = thinkModelFunctionMapper 59 List<IotThinkModelFunctionDO> propertyList = thinkModelFunctionMapper
59 .selectListByProductIdAndType(createReqVO.getProductId(), IotThingModelTypeEnum.PROPERTY.getType()); 60 .selectListByProductIdAndType(createReqVO.getProductId(), IotThingModelTypeEnum.PROPERTY.getType());
60 propertyList.add(function); 61 propertyList.add(function);
@@ -85,6 +86,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe @@ -85,6 +86,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
85 // 4. 如果更新的是属性,需要更新默认的事件和服务 86 // 4. 如果更新的是属性,需要更新默认的事件和服务
86 if (Objects.equals(updateReqVO.getType(), IotThingModelTypeEnum.PROPERTY.getType())) { 87 if (Objects.equals(updateReqVO.getType(), IotThingModelTypeEnum.PROPERTY.getType())) {
87 // 获取当前属性列表,更新其中的属性 88 // 获取当前属性列表,更新其中的属性
  89 + // TODO @haohao:是不是更新后,查询出来的,已经是最新的 thinkModelFunction
88 List<IotThinkModelFunctionDO> propertyList = thinkModelFunctionMapper 90 List<IotThinkModelFunctionDO> propertyList = thinkModelFunctionMapper
89 .selectListByProductIdAndType(updateReqVO.getProductId(), IotThingModelTypeEnum.PROPERTY.getType()); 91 .selectListByProductIdAndType(updateReqVO.getProductId(), IotThingModelTypeEnum.PROPERTY.getType());
90 for (int i = 0; i < propertyList.size(); i++) { 92 for (int i = 0; i < propertyList.size(); i++) {
@@ -119,6 +121,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe @@ -119,6 +121,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
119 // 3. 如果删除的是属性,需要更新默认的事件和服务 121 // 3. 如果删除的是属性,需要更新默认的事件和服务
120 if (Objects.equals(functionDO.getType(), IotThingModelTypeEnum.PROPERTY.getType())) { 122 if (Objects.equals(functionDO.getType(), IotThingModelTypeEnum.PROPERTY.getType())) {
121 // 获取当前属性列表,移除已删除的属性 123 // 获取当前属性列表,移除已删除的属性
  124 + // TODO @haohao:是不是删除后,已经没有 id 对应的记录啦?
122 List<IotThinkModelFunctionDO> propertyList = thinkModelFunctionMapper 125 List<IotThinkModelFunctionDO> propertyList = thinkModelFunctionMapper
123 .selectListByProductIdAndType(functionDO.getProductId(), IotThingModelTypeEnum.PROPERTY.getType()); 126 .selectListByProductIdAndType(functionDO.getProductId(), IotThingModelTypeEnum.PROPERTY.getType());
124 propertyList.removeIf(property -> property.getId().equals(id)); 127 propertyList.removeIf(property -> property.getId().equals(id));
@@ -153,21 +156,18 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe @@ -153,21 +156,18 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
153 public void createDefaultEventsAndServices(Long productId, String productKey, List<IotThinkModelFunctionDO> propertyList) { 156 public void createDefaultEventsAndServices(Long productId, String productKey, List<IotThinkModelFunctionDO> propertyList) {
154 // 1. 生成新的事件和服务列表 157 // 1. 生成新的事件和服务列表
155 List<IotThinkModelFunctionDO> newFunctionList = new ArrayList<>(); 158 List<IotThinkModelFunctionDO> newFunctionList = new ArrayList<>();
156 -  
157 // 生成属性上报事件 159 // 生成属性上报事件
158 ThingModelEvent propertyPostEvent = generatePropertyPostEvent(propertyList); 160 ThingModelEvent propertyPostEvent = generatePropertyPostEvent(propertyList);
159 if (propertyPostEvent != null) { 161 if (propertyPostEvent != null) {
160 IotThinkModelFunctionDO eventFunction = buildEventFunctionDO(productId, productKey, propertyPostEvent); 162 IotThinkModelFunctionDO eventFunction = buildEventFunctionDO(productId, productKey, propertyPostEvent);
161 newFunctionList.add(eventFunction); 163 newFunctionList.add(eventFunction);
162 } 164 }
163 -  
164 // 生成属性设置服务 165 // 生成属性设置服务
165 ThingModelService propertySetService = generatePropertySetService(propertyList); 166 ThingModelService propertySetService = generatePropertySetService(propertyList);
166 if (propertySetService != null) { 167 if (propertySetService != null) {
167 IotThinkModelFunctionDO setServiceFunction = buildServiceFunctionDO(productId, productKey, propertySetService); 168 IotThinkModelFunctionDO setServiceFunction = buildServiceFunctionDO(productId, productKey, propertySetService);
168 newFunctionList.add(setServiceFunction); 169 newFunctionList.add(setServiceFunction);
169 } 170 }
170 -  
171 // 生成属性获取服务 171 // 生成属性获取服务
172 ThingModelService propertyGetService = generatePropertyGetService(propertyList); 172 ThingModelService propertyGetService = generatePropertyGetService(propertyList);
173 if (propertyGetService != null) { 173 if (propertyGetService != null) {
@@ -182,19 +182,15 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe @@ -182,19 +182,15 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
182 Arrays.asList(IotThingModelTypeEnum.EVENT.getType(), IotThingModelTypeEnum.SERVICE.getType()) 182 Arrays.asList(IotThingModelTypeEnum.EVENT.getType(), IotThingModelTypeEnum.SERVICE.getType())
183 ); 183 );
184 184
185 - // 3. 使用 diffList 方法比较新旧列表  
186 - List<List<IotThinkModelFunctionDO>> diffResult = diffList(  
187 - oldFunctionList,  
188 - newFunctionList, 185 + // 3.1 使用 diffList 方法比较新旧列表
  186 + List<List<IotThinkModelFunctionDO>> diffResult = diffList(oldFunctionList, newFunctionList,
  187 + // TODO @haohao:是不是用 id 比较相同就 ok 哈。如果可以的化,下面的 update 可以更简单
189 (oldFunc, newFunc) -> Objects.equals(oldFunc.getIdentifier(), newFunc.getIdentifier()) 188 (oldFunc, newFunc) -> Objects.equals(oldFunc.getIdentifier(), newFunc.getIdentifier())
190 - && Objects.equals(oldFunc.getType(), newFunc.getType())  
191 - );  
192 - 189 + && Objects.equals(oldFunc.getType(), newFunc.getType()));
193 List<IotThinkModelFunctionDO> createList = diffResult.get(0); // 需要新增的 190 List<IotThinkModelFunctionDO> createList = diffResult.get(0); // 需要新增的
194 List<IotThinkModelFunctionDO> updateList = diffResult.get(1); // 需要更新的 191 List<IotThinkModelFunctionDO> updateList = diffResult.get(1); // 需要更新的
195 List<IotThinkModelFunctionDO> deleteList = diffResult.get(2); // 需要删除的 192 List<IotThinkModelFunctionDO> deleteList = diffResult.get(2); // 需要删除的
196 -  
197 - // 4. 批量执行数据库操作 193 + // 3.2 批量执行数据库操作
198 if (CollUtil.isNotEmpty(createList)) { 194 if (CollUtil.isNotEmpty(createList)) {
199 thinkModelFunctionMapper.insertBatch(createList); 195 thinkModelFunctionMapper.insertBatch(createList);
200 } 196 }
@@ -202,8 +198,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe @@ -202,8 +198,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
202 for (IotThinkModelFunctionDO updateFunc : updateList) { 198 for (IotThinkModelFunctionDO updateFunc : updateList) {
203 // 设置 ID,以便更新 199 // 设置 ID,以便更新
204 IotThinkModelFunctionDO oldFunc = findFunctionByIdentifierAndType( 200 IotThinkModelFunctionDO oldFunc = findFunctionByIdentifierAndType(
205 - oldFunctionList, updateFunc.getIdentifier(), updateFunc.getType()  
206 - ); 201 + oldFunctionList, updateFunc.getIdentifier(), updateFunc.getType());
207 if (oldFunc != null) { 202 if (oldFunc != null) {
208 updateFunc.setId(oldFunc.getId()); 203 updateFunc.setId(oldFunc.getId());
209 thinkModelFunctionMapper.updateById(updateFunc); 204 thinkModelFunctionMapper.updateById(updateFunc);
@@ -211,6 +206,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe @@ -211,6 +206,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
211 } 206 }
212 } 207 }
213 if (CollUtil.isNotEmpty(deleteList)) { 208 if (CollUtil.isNotEmpty(deleteList)) {
  209 + // TODO @haohao:使用 convertSet 简化。
214 List<Long> idsToDelete = deleteList.stream().map(IotThinkModelFunctionDO::getId).collect(Collectors.toList()); 210 List<Long> idsToDelete = deleteList.stream().map(IotThinkModelFunctionDO::getId).collect(Collectors.toList());
215 thinkModelFunctionMapper.deleteByIds(idsToDelete); 211 thinkModelFunctionMapper.deleteByIds(idsToDelete);
216 } 212 }
@@ -221,6 +217,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe @@ -221,6 +217,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
221 */ 217 */
222 private IotThinkModelFunctionDO findFunctionByIdentifierAndType(List<IotThinkModelFunctionDO> functionList, 218 private IotThinkModelFunctionDO findFunctionByIdentifierAndType(List<IotThinkModelFunctionDO> functionList,
223 String identifier, Integer type) { 219 String identifier, Integer type) {
  220 + // TODO @haohao:这个可以使用 CollUtil.findOne 简化只有一行
224 return functionList.stream() 221 return functionList.stream()
225 .filter(func -> Objects.equals(func.getIdentifier(), identifier) && Objects.equals(func.getType(), type)) 222 .filter(func -> Objects.equals(func.getIdentifier(), identifier) && Objects.equals(func.getType(), type))
226 .findFirst() 223 .findFirst()
@@ -335,7 +332,9 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe @@ -335,7 +332,9 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
335 List<ThingModelArgument> outputData = new ArrayList<>(); 332 List<ThingModelArgument> outputData = new ArrayList<>();
336 for (IotThinkModelFunctionDO functionDO : propertyList) { 333 for (IotThinkModelFunctionDO functionDO : propertyList) {
337 ThingModelProperty property = functionDO.getProperty(); 334 ThingModelProperty property = functionDO.getProperty();
338 - if (IotAccessModeEnum.READ.getMode().equals(property.getAccessMode()) || IotAccessModeEnum.READ_WRITE.getMode().equals(property.getAccessMode())) { 335 + // TODO @haohao:ObjectUtils.equalsAny(),进一步简化判断
  336 + if (IotAccessModeEnum.READ.getMode().equals(property.getAccessMode())
  337 + || IotAccessModeEnum.READ_WRITE.getMode().equals(property.getAccessMode())) {
339 ThingModelArgument arg = new ThingModelArgument() 338 ThingModelArgument arg = new ThingModelArgument()
340 .setIdentifier(property.getIdentifier()) 339 .setIdentifier(property.getIdentifier())
341 .setName(property.getName()) 340 .setName(property.getName())
@@ -372,7 +371,6 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe @@ -372,7 +371,6 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
372 textType.setType("text"); 371 textType.setType("text");
373 arraySpecs.setItem(textType); 372 arraySpecs.setItem(textType);
374 arrayType.setSpecs(arraySpecs); 373 arrayType.setSpecs(arraySpecs);
375 -  
376 inputArg.setDataType(arrayType); 374 inputArg.setDataType(arrayType);
377 375
378 service.setInputData(Collections.singletonList(inputArg)); 376 service.setInputData(Collections.singletonList(inputArg));