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 21  
22 22 // ========== IoT 设备 1-050-003-000 ============
23 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 10 */
11 11 @AllArgsConstructor
12 12 @Getter
13   -public enum IotAccessModeEnum {
  13 +public enum IotAccessModeEnum {
14 14  
15 15 READ("r"),
16 16 WRITE("w"),
... ... @@ -18,8 +18,4 @@ public enum IotAccessModeEnum {
18 18  
19 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 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 35 return selectList(new LambdaQueryWrapperX<IotThinkModelFunctionDO>()
34 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 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 55 // 3. 如果创建的是属性,需要更新默认的事件和服务
56 56 if (Objects.equals(createReqVO.getType(), IotThingModelTypeEnum.PROPERTY.getType())) {
57 57 // 获取当前属性列表,并添加新插入的属性
  58 + // TODO @haohao:是不是插入后,查询已经包含了 function
58 59 List<IotThinkModelFunctionDO> propertyList = thinkModelFunctionMapper
59 60 .selectListByProductIdAndType(createReqVO.getProductId(), IotThingModelTypeEnum.PROPERTY.getType());
60 61 propertyList.add(function);
... ... @@ -85,6 +86,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
85 86 // 4. 如果更新的是属性,需要更新默认的事件和服务
86 87 if (Objects.equals(updateReqVO.getType(), IotThingModelTypeEnum.PROPERTY.getType())) {
87 88 // 获取当前属性列表,更新其中的属性
  89 + // TODO @haohao:是不是更新后,查询出来的,已经是最新的 thinkModelFunction
88 90 List<IotThinkModelFunctionDO> propertyList = thinkModelFunctionMapper
89 91 .selectListByProductIdAndType(updateReqVO.getProductId(), IotThingModelTypeEnum.PROPERTY.getType());
90 92 for (int i = 0; i < propertyList.size(); i++) {
... ... @@ -119,6 +121,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
119 121 // 3. 如果删除的是属性,需要更新默认的事件和服务
120 122 if (Objects.equals(functionDO.getType(), IotThingModelTypeEnum.PROPERTY.getType())) {
121 123 // 获取当前属性列表,移除已删除的属性
  124 + // TODO @haohao:是不是删除后,已经没有 id 对应的记录啦?
122 125 List<IotThinkModelFunctionDO> propertyList = thinkModelFunctionMapper
123 126 .selectListByProductIdAndType(functionDO.getProductId(), IotThingModelTypeEnum.PROPERTY.getType());
124 127 propertyList.removeIf(property -> property.getId().equals(id));
... ... @@ -153,21 +156,18 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
153 156 public void createDefaultEventsAndServices(Long productId, String productKey, List<IotThinkModelFunctionDO> propertyList) {
154 157 // 1. 生成新的事件和服务列表
155 158 List<IotThinkModelFunctionDO> newFunctionList = new ArrayList<>();
156   -
157 159 // 生成属性上报事件
158 160 ThingModelEvent propertyPostEvent = generatePropertyPostEvent(propertyList);
159 161 if (propertyPostEvent != null) {
160 162 IotThinkModelFunctionDO eventFunction = buildEventFunctionDO(productId, productKey, propertyPostEvent);
161 163 newFunctionList.add(eventFunction);
162 164 }
163   -
164 165 // 生成属性设置服务
165 166 ThingModelService propertySetService = generatePropertySetService(propertyList);
166 167 if (propertySetService != null) {
167 168 IotThinkModelFunctionDO setServiceFunction = buildServiceFunctionDO(productId, productKey, propertySetService);
168 169 newFunctionList.add(setServiceFunction);
169 170 }
170   -
171 171 // 生成属性获取服务
172 172 ThingModelService propertyGetService = generatePropertyGetService(propertyList);
173 173 if (propertyGetService != null) {
... ... @@ -182,19 +182,15 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
182 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 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 190 List<IotThinkModelFunctionDO> createList = diffResult.get(0); // 需要新增的
194 191 List<IotThinkModelFunctionDO> updateList = diffResult.get(1); // 需要更新的
195 192 List<IotThinkModelFunctionDO> deleteList = diffResult.get(2); // 需要删除的
196   -
197   - // 4. 批量执行数据库操作
  193 + // 3.2 批量执行数据库操作
198 194 if (CollUtil.isNotEmpty(createList)) {
199 195 thinkModelFunctionMapper.insertBatch(createList);
200 196 }
... ... @@ -202,8 +198,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
202 198 for (IotThinkModelFunctionDO updateFunc : updateList) {
203 199 // 设置 ID,以便更新
204 200 IotThinkModelFunctionDO oldFunc = findFunctionByIdentifierAndType(
205   - oldFunctionList, updateFunc.getIdentifier(), updateFunc.getType()
206   - );
  201 + oldFunctionList, updateFunc.getIdentifier(), updateFunc.getType());
207 202 if (oldFunc != null) {
208 203 updateFunc.setId(oldFunc.getId());
209 204 thinkModelFunctionMapper.updateById(updateFunc);
... ... @@ -211,6 +206,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
211 206 }
212 207 }
213 208 if (CollUtil.isNotEmpty(deleteList)) {
  209 + // TODO @haohao:使用 convertSet 简化。
214 210 List<Long> idsToDelete = deleteList.stream().map(IotThinkModelFunctionDO::getId).collect(Collectors.toList());
215 211 thinkModelFunctionMapper.deleteByIds(idsToDelete);
216 212 }
... ... @@ -221,6 +217,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
221 217 */
222 218 private IotThinkModelFunctionDO findFunctionByIdentifierAndType(List<IotThinkModelFunctionDO> functionList,
223 219 String identifier, Integer type) {
  220 + // TODO @haohao:这个可以使用 CollUtil.findOne 简化只有一行
224 221 return functionList.stream()
225 222 .filter(func -> Objects.equals(func.getIdentifier(), identifier) && Objects.equals(func.getType(), type))
226 223 .findFirst()
... ... @@ -335,7 +332,9 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
335 332 List<ThingModelArgument> outputData = new ArrayList<>();
336 333 for (IotThinkModelFunctionDO functionDO : propertyList) {
337 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 338 ThingModelArgument arg = new ThingModelArgument()
340 339 .setIdentifier(property.getIdentifier())
341 340 .setName(property.getName())
... ... @@ -372,7 +371,6 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
372 371 textType.setType("text");
373 372 arraySpecs.setItem(textType);
374 373 arrayType.setSpecs(arraySpecs);
375   -
376 374 inputArg.setDataType(arrayType);
377 375  
378 376 service.setInputData(Collections.singletonList(inputArg));
... ...