From 9658ed1a0d93dd492e3551038c105f540ccda2aa Mon Sep 17 00:00:00 2001 From: YunaiV Date: Tue, 17 Sep 2024 10:27:33 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BB=A3=E7=A0=81=E8=AF=84=E5=AE=A1?= =?UTF-8?q?=E3=80=91IOT=EF=BC=9A=E7=89=A9=E6=A8=A1=E5=9E=8B=E7=9A=84=20rev?= =?UTF-8?q?iew?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../module/iot/enums/ErrorCodeConstants.java | 1 + .../iot/enums/product/IotAccessModeEnum.java | 6 +--- .../IotThinkModelFunctionMapper.java | 9 ++++-- .../IotThinkModelFunctionServiceImpl.java | 30 +++++++++---------- 4 files changed, 22 insertions(+), 24 deletions(-) diff --git a/yudao-module-iot/yudao-module-iot-api/src/main/java/cn/iocoder/yudao/module/iot/enums/ErrorCodeConstants.java b/yudao-module-iot/yudao-module-iot-api/src/main/java/cn/iocoder/yudao/module/iot/enums/ErrorCodeConstants.java index 7677ef89d..790bbb40c 100644 --- a/yudao-module-iot/yudao-module-iot-api/src/main/java/cn/iocoder/yudao/module/iot/enums/ErrorCodeConstants.java +++ b/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 { // ========== IoT 设备 1-050-003-000 ============ ErrorCode DEVICE_NOT_EXISTS = new ErrorCode(1_050_003_000, "设备不存在"); + } diff --git a/yudao-module-iot/yudao-module-iot-api/src/main/java/cn/iocoder/yudao/module/iot/enums/product/IotAccessModeEnum.java b/yudao-module-iot/yudao-module-iot-api/src/main/java/cn/iocoder/yudao/module/iot/enums/product/IotAccessModeEnum.java index 630240b86..64ece99ca 100644 --- a/yudao-module-iot/yudao-module-iot-api/src/main/java/cn/iocoder/yudao/module/iot/enums/product/IotAccessModeEnum.java +++ b/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; */ @AllArgsConstructor @Getter -public enum IotAccessModeEnum { +public enum IotAccessModeEnum { READ("r"), WRITE("w"), @@ -18,8 +18,4 @@ public enum IotAccessModeEnum { private final String mode; - public String getMode() { - return mode; - } - } diff --git a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/mysql/thinkmodelfunction/IotThinkModelFunctionMapper.java b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/mysql/thinkmodelfunction/IotThinkModelFunctionMapper.java index cd1327461..7227a0f26 100644 --- a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/mysql/thinkmodelfunction/IotThinkModelFunctionMapper.java +++ b/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 selectListByProductIdAndIdentifiersAndTypes(Long productId, List list, List list1){ + default List selectListByProductIdAndIdentifiersAndTypes(Long productId, + List identifiers, + List types){ return selectList(new LambdaQueryWrapperX() .eq(IotThinkModelFunctionDO::getProductId, productId) - .in(IotThinkModelFunctionDO::getIdentifier, list) - .in(IotThinkModelFunctionDO::getType, list1)); + .in(IotThinkModelFunctionDO::getIdentifier, identifiers) + .in(IotThinkModelFunctionDO::getType, types)); } + } \ No newline at end of file diff --git a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/thinkmodelfunction/IotThinkModelFunctionServiceImpl.java b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/thinkmodelfunction/IotThinkModelFunctionServiceImpl.java index 7d4c9ef04..0edb96db2 100644 --- a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/thinkmodelfunction/IotThinkModelFunctionServiceImpl.java +++ b/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 // 3. 如果创建的是属性,需要更新默认的事件和服务 if (Objects.equals(createReqVO.getType(), IotThingModelTypeEnum.PROPERTY.getType())) { // 获取当前属性列表,并添加新插入的属性 + // TODO @haohao:是不是插入后,查询已经包含了 function List propertyList = thinkModelFunctionMapper .selectListByProductIdAndType(createReqVO.getProductId(), IotThingModelTypeEnum.PROPERTY.getType()); propertyList.add(function); @@ -85,6 +86,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe // 4. 如果更新的是属性,需要更新默认的事件和服务 if (Objects.equals(updateReqVO.getType(), IotThingModelTypeEnum.PROPERTY.getType())) { // 获取当前属性列表,更新其中的属性 + // TODO @haohao:是不是更新后,查询出来的,已经是最新的 thinkModelFunction List propertyList = thinkModelFunctionMapper .selectListByProductIdAndType(updateReqVO.getProductId(), IotThingModelTypeEnum.PROPERTY.getType()); for (int i = 0; i < propertyList.size(); i++) { @@ -119,6 +121,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe // 3. 如果删除的是属性,需要更新默认的事件和服务 if (Objects.equals(functionDO.getType(), IotThingModelTypeEnum.PROPERTY.getType())) { // 获取当前属性列表,移除已删除的属性 + // TODO @haohao:是不是删除后,已经没有 id 对应的记录啦? List propertyList = thinkModelFunctionMapper .selectListByProductIdAndType(functionDO.getProductId(), IotThingModelTypeEnum.PROPERTY.getType()); propertyList.removeIf(property -> property.getId().equals(id)); @@ -153,21 +156,18 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe public void createDefaultEventsAndServices(Long productId, String productKey, List propertyList) { // 1. 生成新的事件和服务列表 List newFunctionList = new ArrayList<>(); - // 生成属性上报事件 ThingModelEvent propertyPostEvent = generatePropertyPostEvent(propertyList); if (propertyPostEvent != null) { IotThinkModelFunctionDO eventFunction = buildEventFunctionDO(productId, productKey, propertyPostEvent); newFunctionList.add(eventFunction); } - // 生成属性设置服务 ThingModelService propertySetService = generatePropertySetService(propertyList); if (propertySetService != null) { IotThinkModelFunctionDO setServiceFunction = buildServiceFunctionDO(productId, productKey, propertySetService); newFunctionList.add(setServiceFunction); } - // 生成属性获取服务 ThingModelService propertyGetService = generatePropertyGetService(propertyList); if (propertyGetService != null) { @@ -182,19 +182,15 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe Arrays.asList(IotThingModelTypeEnum.EVENT.getType(), IotThingModelTypeEnum.SERVICE.getType()) ); - // 3. 使用 diffList 方法比较新旧列表 - List> diffResult = diffList( - oldFunctionList, - newFunctionList, + // 3.1 使用 diffList 方法比较新旧列表 + List> diffResult = diffList(oldFunctionList, newFunctionList, + // TODO @haohao:是不是用 id 比较相同就 ok 哈。如果可以的化,下面的 update 可以更简单 (oldFunc, newFunc) -> Objects.equals(oldFunc.getIdentifier(), newFunc.getIdentifier()) - && Objects.equals(oldFunc.getType(), newFunc.getType()) - ); - + && Objects.equals(oldFunc.getType(), newFunc.getType())); List createList = diffResult.get(0); // 需要新增的 List updateList = diffResult.get(1); // 需要更新的 List deleteList = diffResult.get(2); // 需要删除的 - - // 4. 批量执行数据库操作 + // 3.2 批量执行数据库操作 if (CollUtil.isNotEmpty(createList)) { thinkModelFunctionMapper.insertBatch(createList); } @@ -202,8 +198,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe for (IotThinkModelFunctionDO updateFunc : updateList) { // 设置 ID,以便更新 IotThinkModelFunctionDO oldFunc = findFunctionByIdentifierAndType( - oldFunctionList, updateFunc.getIdentifier(), updateFunc.getType() - ); + oldFunctionList, updateFunc.getIdentifier(), updateFunc.getType()); if (oldFunc != null) { updateFunc.setId(oldFunc.getId()); thinkModelFunctionMapper.updateById(updateFunc); @@ -211,6 +206,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe } } if (CollUtil.isNotEmpty(deleteList)) { + // TODO @haohao:使用 convertSet 简化。 List idsToDelete = deleteList.stream().map(IotThinkModelFunctionDO::getId).collect(Collectors.toList()); thinkModelFunctionMapper.deleteByIds(idsToDelete); } @@ -221,6 +217,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe */ private IotThinkModelFunctionDO findFunctionByIdentifierAndType(List functionList, String identifier, Integer type) { + // TODO @haohao:这个可以使用 CollUtil.findOne 简化只有一行 return functionList.stream() .filter(func -> Objects.equals(func.getIdentifier(), identifier) && Objects.equals(func.getType(), type)) .findFirst() @@ -335,7 +332,9 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe List outputData = new ArrayList<>(); for (IotThinkModelFunctionDO functionDO : propertyList) { ThingModelProperty property = functionDO.getProperty(); - if (IotAccessModeEnum.READ.getMode().equals(property.getAccessMode()) || IotAccessModeEnum.READ_WRITE.getMode().equals(property.getAccessMode())) { + // TODO @haohao:ObjectUtils.equalsAny(),进一步简化判断 + if (IotAccessModeEnum.READ.getMode().equals(property.getAccessMode()) + || IotAccessModeEnum.READ_WRITE.getMode().equals(property.getAccessMode())) { ThingModelArgument arg = new ThingModelArgument() .setIdentifier(property.getIdentifier()) .setName(property.getName()) @@ -372,7 +371,6 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe textType.setType("text"); arraySpecs.setItem(textType); arrayType.setSpecs(arraySpecs); - inputArg.setDataType(arrayType); service.setInputData(Collections.singletonList(inputArg));