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

This commit is contained in:
YunaiV 2024-09-17 10:27:33 +08:00
parent e4ef1c8190
commit 9658ed1a0d
4 changed files with 22 additions and 24 deletions

View File

@ -21,4 +21,5 @@ public interface ErrorCodeConstants {
// ========== IoT 设备 1-050-003-000 ============ // ========== IoT 设备 1-050-003-000 ============
ErrorCode DEVICE_NOT_EXISTS = new ErrorCode(1_050_003_000, "设备不存在"); ErrorCode DEVICE_NOT_EXISTS = new ErrorCode(1_050_003_000, "设备不存在");
} }

View File

@ -18,8 +18,4 @@ public enum IotAccessModeEnum {
private final String mode; private final String mode;
public String getMode() {
return mode;
}
} }

View File

@ -29,10 +29,13 @@ public interface IotThinkModelFunctionMapper extends BaseMapperX<IotThinkModelFu
IotThinkModelFunctionDO::getType, type); IotThinkModelFunctionDO::getType, type);
} }
default List<IotThinkModelFunctionDO> selectListByProductIdAndIdentifiersAndTypes(Long productId, List<String> list, List<Integer> list1){ default List<IotThinkModelFunctionDO> selectListByProductIdAndIdentifiersAndTypes(Long productId,
List<String> identifiers,
List<Integer> types){
return selectList(new LambdaQueryWrapperX<IotThinkModelFunctionDO>() return selectList(new LambdaQueryWrapperX<IotThinkModelFunctionDO>()
.eq(IotThinkModelFunctionDO::getProductId, productId) .eq(IotThinkModelFunctionDO::getProductId, productId)
.in(IotThinkModelFunctionDO::getIdentifier, list) .in(IotThinkModelFunctionDO::getIdentifier, identifiers)
.in(IotThinkModelFunctionDO::getType, list1)); .in(IotThinkModelFunctionDO::getType, types));
} }
} }

View File

@ -55,6 +55,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
// 3. 如果创建的是属性需要更新默认的事件和服务 // 3. 如果创建的是属性需要更新默认的事件和服务
if (Objects.equals(createReqVO.getType(), IotThingModelTypeEnum.PROPERTY.getType())) { if (Objects.equals(createReqVO.getType(), IotThingModelTypeEnum.PROPERTY.getType())) {
// 获取当前属性列表并添加新插入的属性 // 获取当前属性列表并添加新插入的属性
// TODO @haohao是不是插入后查询已经包含了 function
List<IotThinkModelFunctionDO> propertyList = thinkModelFunctionMapper List<IotThinkModelFunctionDO> propertyList = thinkModelFunctionMapper
.selectListByProductIdAndType(createReqVO.getProductId(), IotThingModelTypeEnum.PROPERTY.getType()); .selectListByProductIdAndType(createReqVO.getProductId(), IotThingModelTypeEnum.PROPERTY.getType());
propertyList.add(function); propertyList.add(function);
@ -85,6 +86,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
// 4. 如果更新的是属性需要更新默认的事件和服务 // 4. 如果更新的是属性需要更新默认的事件和服务
if (Objects.equals(updateReqVO.getType(), IotThingModelTypeEnum.PROPERTY.getType())) { if (Objects.equals(updateReqVO.getType(), IotThingModelTypeEnum.PROPERTY.getType())) {
// 获取当前属性列表更新其中的属性 // 获取当前属性列表更新其中的属性
// TODO @haohao是不是更新后查询出来的已经是最新的 thinkModelFunction
List<IotThinkModelFunctionDO> propertyList = thinkModelFunctionMapper List<IotThinkModelFunctionDO> propertyList = thinkModelFunctionMapper
.selectListByProductIdAndType(updateReqVO.getProductId(), IotThingModelTypeEnum.PROPERTY.getType()); .selectListByProductIdAndType(updateReqVO.getProductId(), IotThingModelTypeEnum.PROPERTY.getType());
for (int i = 0; i < propertyList.size(); i++) { for (int i = 0; i < propertyList.size(); i++) {
@ -119,6 +121,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
// 3. 如果删除的是属性需要更新默认的事件和服务 // 3. 如果删除的是属性需要更新默认的事件和服务
if (Objects.equals(functionDO.getType(), IotThingModelTypeEnum.PROPERTY.getType())) { if (Objects.equals(functionDO.getType(), IotThingModelTypeEnum.PROPERTY.getType())) {
// 获取当前属性列表移除已删除的属性 // 获取当前属性列表移除已删除的属性
// TODO @haohao是不是删除后已经没有 id 对应的记录啦
List<IotThinkModelFunctionDO> propertyList = thinkModelFunctionMapper List<IotThinkModelFunctionDO> propertyList = thinkModelFunctionMapper
.selectListByProductIdAndType(functionDO.getProductId(), IotThingModelTypeEnum.PROPERTY.getType()); .selectListByProductIdAndType(functionDO.getProductId(), IotThingModelTypeEnum.PROPERTY.getType());
propertyList.removeIf(property -> property.getId().equals(id)); propertyList.removeIf(property -> property.getId().equals(id));
@ -153,21 +156,18 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
public void createDefaultEventsAndServices(Long productId, String productKey, List<IotThinkModelFunctionDO> propertyList) { public void createDefaultEventsAndServices(Long productId, String productKey, List<IotThinkModelFunctionDO> propertyList) {
// 1. 生成新的事件和服务列表 // 1. 生成新的事件和服务列表
List<IotThinkModelFunctionDO> newFunctionList = new ArrayList<>(); List<IotThinkModelFunctionDO> newFunctionList = new ArrayList<>();
// 生成属性上报事件 // 生成属性上报事件
ThingModelEvent propertyPostEvent = generatePropertyPostEvent(propertyList); ThingModelEvent propertyPostEvent = generatePropertyPostEvent(propertyList);
if (propertyPostEvent != null) { if (propertyPostEvent != null) {
IotThinkModelFunctionDO eventFunction = buildEventFunctionDO(productId, productKey, propertyPostEvent); IotThinkModelFunctionDO eventFunction = buildEventFunctionDO(productId, productKey, propertyPostEvent);
newFunctionList.add(eventFunction); newFunctionList.add(eventFunction);
} }
// 生成属性设置服务 // 生成属性设置服务
ThingModelService propertySetService = generatePropertySetService(propertyList); ThingModelService propertySetService = generatePropertySetService(propertyList);
if (propertySetService != null) { if (propertySetService != null) {
IotThinkModelFunctionDO setServiceFunction = buildServiceFunctionDO(productId, productKey, propertySetService); IotThinkModelFunctionDO setServiceFunction = buildServiceFunctionDO(productId, productKey, propertySetService);
newFunctionList.add(setServiceFunction); newFunctionList.add(setServiceFunction);
} }
// 生成属性获取服务 // 生成属性获取服务
ThingModelService propertyGetService = generatePropertyGetService(propertyList); ThingModelService propertyGetService = generatePropertyGetService(propertyList);
if (propertyGetService != null) { if (propertyGetService != null) {
@ -182,19 +182,15 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
Arrays.asList(IotThingModelTypeEnum.EVENT.getType(), IotThingModelTypeEnum.SERVICE.getType()) Arrays.asList(IotThingModelTypeEnum.EVENT.getType(), IotThingModelTypeEnum.SERVICE.getType())
); );
// 3. 使用 diffList 方法比较新旧列表 // 3.1 使用 diffList 方法比较新旧列表
List<List<IotThinkModelFunctionDO>> diffResult = diffList( List<List<IotThinkModelFunctionDO>> diffResult = diffList(oldFunctionList, newFunctionList,
oldFunctionList, // TODO @haohao是不是用 id 比较相同就 ok 如果可以的化下面的 update 可以更简单
newFunctionList,
(oldFunc, newFunc) -> Objects.equals(oldFunc.getIdentifier(), newFunc.getIdentifier()) (oldFunc, newFunc) -> Objects.equals(oldFunc.getIdentifier(), newFunc.getIdentifier())
&& Objects.equals(oldFunc.getType(), newFunc.getType()) && Objects.equals(oldFunc.getType(), newFunc.getType()));
);
List<IotThinkModelFunctionDO> createList = diffResult.get(0); // 需要新增的 List<IotThinkModelFunctionDO> createList = diffResult.get(0); // 需要新增的
List<IotThinkModelFunctionDO> updateList = diffResult.get(1); // 需要更新的 List<IotThinkModelFunctionDO> updateList = diffResult.get(1); // 需要更新的
List<IotThinkModelFunctionDO> deleteList = diffResult.get(2); // 需要删除的 List<IotThinkModelFunctionDO> deleteList = diffResult.get(2); // 需要删除的
// 3.2 批量执行数据库操作
// 4. 批量执行数据库操作
if (CollUtil.isNotEmpty(createList)) { if (CollUtil.isNotEmpty(createList)) {
thinkModelFunctionMapper.insertBatch(createList); thinkModelFunctionMapper.insertBatch(createList);
} }
@ -202,8 +198,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
for (IotThinkModelFunctionDO updateFunc : updateList) { for (IotThinkModelFunctionDO updateFunc : updateList) {
// 设置 ID以便更新 // 设置 ID以便更新
IotThinkModelFunctionDO oldFunc = findFunctionByIdentifierAndType( IotThinkModelFunctionDO oldFunc = findFunctionByIdentifierAndType(
oldFunctionList, updateFunc.getIdentifier(), updateFunc.getType() oldFunctionList, updateFunc.getIdentifier(), updateFunc.getType());
);
if (oldFunc != null) { if (oldFunc != null) {
updateFunc.setId(oldFunc.getId()); updateFunc.setId(oldFunc.getId());
thinkModelFunctionMapper.updateById(updateFunc); thinkModelFunctionMapper.updateById(updateFunc);
@ -211,6 +206,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
} }
} }
if (CollUtil.isNotEmpty(deleteList)) { if (CollUtil.isNotEmpty(deleteList)) {
// TODO @haohao使用 convertSet 简化
List<Long> idsToDelete = deleteList.stream().map(IotThinkModelFunctionDO::getId).collect(Collectors.toList()); List<Long> idsToDelete = deleteList.stream().map(IotThinkModelFunctionDO::getId).collect(Collectors.toList());
thinkModelFunctionMapper.deleteByIds(idsToDelete); thinkModelFunctionMapper.deleteByIds(idsToDelete);
} }
@ -221,6 +217,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
*/ */
private IotThinkModelFunctionDO findFunctionByIdentifierAndType(List<IotThinkModelFunctionDO> functionList, private IotThinkModelFunctionDO findFunctionByIdentifierAndType(List<IotThinkModelFunctionDO> functionList,
String identifier, Integer type) { String identifier, Integer type) {
// TODO @haohao这个可以使用 CollUtil.findOne 简化只有一行
return functionList.stream() return functionList.stream()
.filter(func -> Objects.equals(func.getIdentifier(), identifier) && Objects.equals(func.getType(), type)) .filter(func -> Objects.equals(func.getIdentifier(), identifier) && Objects.equals(func.getType(), type))
.findFirst() .findFirst()
@ -335,7 +332,9 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
List<ThingModelArgument> outputData = new ArrayList<>(); List<ThingModelArgument> outputData = new ArrayList<>();
for (IotThinkModelFunctionDO functionDO : propertyList) { for (IotThinkModelFunctionDO functionDO : propertyList) {
ThingModelProperty property = functionDO.getProperty(); ThingModelProperty property = functionDO.getProperty();
if (IotAccessModeEnum.READ.getMode().equals(property.getAccessMode()) || IotAccessModeEnum.READ_WRITE.getMode().equals(property.getAccessMode())) { // TODO @haohaoObjectUtils.equalsAny()进一步简化判断
if (IotAccessModeEnum.READ.getMode().equals(property.getAccessMode())
|| IotAccessModeEnum.READ_WRITE.getMode().equals(property.getAccessMode())) {
ThingModelArgument arg = new ThingModelArgument() ThingModelArgument arg = new ThingModelArgument()
.setIdentifier(property.getIdentifier()) .setIdentifier(property.getIdentifier())
.setName(property.getName()) .setName(property.getName())
@ -372,7 +371,6 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
textType.setType("text"); textType.setType("text");
arraySpecs.setItem(textType); arraySpecs.setItem(textType);
arrayType.setSpecs(arraySpecs); arrayType.setSpecs(arraySpecs);
inputArg.setDataType(arrayType); inputArg.setDataType(arrayType);
service.setInputData(Collections.singletonList(inputArg)); service.setInputData(Collections.singletonList(inputArg));