From cd341da674be599301a8fffa3cadc664505c58ad Mon Sep 17 00:00:00 2001 From: puhui999 Date: Tue, 20 May 2025 11:15:05 +0800 Subject: [PATCH] =?UTF-8?q?perf:=E3=80=90INFRA=20=E5=9F=BA=E7=A1=80?= =?UTF-8?q?=E8=AE=BE=E6=96=BD=E3=80=91=E4=BB=A3=E7=A0=81=E7=94=9F=E6=88=90?= =?UTF-8?q?=E4=B8=BB=E5=AD=90=E8=A1=A8=E9=9D=9E=20erp=20=E6=A8=A1=E5=BC=8F?= =?UTF-8?q?=E6=97=B6=EF=BC=8C=E5=BD=93=E5=AD=90=E8=A1=A8=E4=B8=80=E5=AF=B9?= =?UTF-8?q?=E5=A4=9A=E6=97=B6=E6=9B=B4=E6=96=B0=E6=94=B9=E4=B8=BA=E9=80=9A?= =?UTF-8?q?=E8=BF=87=20diffList=20=E5=AE=9E=E7=8E=B0=E5=AF=B9=E5=BA=94?= =?UTF-8?q?=E7=9A=84=E5=A2=9E=E5=88=A0=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/codegen/inner/CodegenEngine.java | 3 +- .../inner/Demo03StudentInnerServiceImpl.java | 26 ++++++++++++-- .../Demo03StudentNormalServiceImpl.java | 26 ++++++++++++-- .../codegen/java/service/serviceImpl.vm | 35 ++++++++++++++----- 4 files changed, 75 insertions(+), 15 deletions(-) diff --git a/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/service/codegen/inner/CodegenEngine.java b/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/service/codegen/inner/CodegenEngine.java index e09767386b..40fbee99fd 100644 --- a/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/service/codegen/inner/CodegenEngine.java +++ b/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/service/codegen/inner/CodegenEngine.java @@ -236,6 +236,7 @@ public class CodegenEngine { + '.' + "framework"); // 用于后续获取测试类的 package 地址 globalBindingMap.put("jakartaPackage", jakartaEnable ? "jakarta" : "javax"); globalBindingMap.put("voType", codegenProperties.getVoType()); + globalBindingMap.put("deleteBatchEnable", codegenProperties.getDeleteBatchEnable()); // 全局 Java Bean globalBindingMap.put("CommonResultClassName", CommonResult.class.getName()); globalBindingMap.put("PageResultClassName", PageResult.class.getName()); @@ -257,6 +258,7 @@ public class CodegenEngine { globalBindingMap.put("ApiAccessLogClassName", ApiAccessLog.class.getName()); globalBindingMap.put("OperateTypeEnumClassName", OperateTypeEnum.class.getName()); globalBindingMap.put("BeanUtils", BeanUtils.class.getName()); + globalBindingMap.put("CollectionUtilsClassName", CollectionUtils.class.getName()); } /** @@ -382,7 +384,6 @@ public class CodegenEngine { bindingMap.put("columns", columns); bindingMap.put("primaryColumn", CollectionUtils.findFirst(columns, CodegenColumnDO::getPrimaryKey)); // 主键字段 bindingMap.put("sceneEnum", CodegenSceneEnum.valueOf(table.getScene())); - bindingMap.put("deleteBatchEnable", codegenProperties.getDeleteBatchEnable()); // className 相关 // 去掉指定前缀,将 TestDictType 转换成 DictType. 因为在 create 等方法后,不需要带上 Test 前缀 String className = table.getClassName(); diff --git a/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/service/demo/demo03/inner/Demo03StudentInnerServiceImpl.java b/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/service/demo/demo03/inner/Demo03StudentInnerServiceImpl.java index 52298e934a..de053d55ef 100644 --- a/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/service/demo/demo03/inner/Demo03StudentInnerServiceImpl.java +++ b/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/service/demo/demo03/inner/Demo03StudentInnerServiceImpl.java @@ -1,6 +1,7 @@ package cn.iocoder.yudao.module.infra.service.demo.demo03.inner; import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.util.ObjectUtil; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.util.object.BeanUtils; import cn.iocoder.yudao.module.infra.controller.admin.demo.demo03.inner.vo.Demo03StudentInnerPageReqVO; @@ -19,6 +20,8 @@ import org.springframework.validation.annotation.Validated; import java.util.List; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList; import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.DEMO03_STUDENT_NOT_EXISTS; /** @@ -127,9 +130,26 @@ public class Demo03StudentInnerServiceImpl implements Demo03StudentInnerService } private void updateDemo03CourseList(Long studentId, List list) { - deleteDemo03CourseByStudentId(studentId); - list.forEach(o -> o.setId(null).setUpdater(null).setUpdateTime(null)); // 解决更新情况下:1)id 冲突;2)updateTime 不更新 - createDemo03CourseList(studentId, list); + list.forEach(o -> o.setStudentId(studentId)); + List oldList = demo03CourseInnerMapper.selectListByStudentId(studentId); + List> diffList = diffList(oldList, list, (oldVal, newVal) -> { + boolean same = ObjectUtil.equal(oldVal.getId(), newVal.getId()); + if (same) { + newVal.setId(oldVal.getId()).setUpdater(null).setUpdateTime(null); // 解决更新情况下:updateTime 不更新 + } + return same; + }); + + // 第二步,批量添加、修改、删除 + if (CollUtil.isNotEmpty(diffList.get(0))) { + demo03CourseInnerMapper.insertBatch(diffList.get(0)); + } + if (CollUtil.isNotEmpty(diffList.get(1))) { + demo03CourseInnerMapper.updateBatch(diffList.get(1)); + } + if (CollUtil.isNotEmpty(diffList.get(2))) { + demo03CourseInnerMapper.deleteByIds(convertList(diffList.get(2), Demo03CourseDO::getId)); + } } private void deleteDemo03CourseByStudentId(Long studentId) { diff --git a/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/service/demo/demo03/normal/Demo03StudentNormalServiceImpl.java b/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/service/demo/demo03/normal/Demo03StudentNormalServiceImpl.java index cac883d4c7..b4fb7ca250 100644 --- a/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/service/demo/demo03/normal/Demo03StudentNormalServiceImpl.java +++ b/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/service/demo/demo03/normal/Demo03StudentNormalServiceImpl.java @@ -1,6 +1,7 @@ package cn.iocoder.yudao.module.infra.service.demo.demo03.normal; import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.util.ObjectUtil; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.util.object.BeanUtils; import cn.iocoder.yudao.module.infra.controller.admin.demo.demo03.normal.vo.Demo03StudentNormalPageReqVO; @@ -19,6 +20,8 @@ import org.springframework.validation.annotation.Validated; import java.util.List; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList; import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.DEMO03_STUDENT_NOT_EXISTS; /** @@ -127,9 +130,26 @@ public class Demo03StudentNormalServiceImpl implements Demo03StudentNormalServic } private void updateDemo03CourseList(Long studentId, List list) { - deleteDemo03CourseByStudentId(studentId); - list.forEach(o -> o.setId(null).setUpdater(null).setUpdateTime(null)); // 解决更新情况下:1)id 冲突;2)updateTime 不更新 - createDemo03CourseList(studentId, list); + list.forEach(o -> o.setStudentId(studentId)); + List oldList = demo03CourseNormalMapper.selectListByStudentId(studentId); + List> diffList = diffList(oldList, list, (oldVal, newVal) -> { + boolean same = ObjectUtil.equal(oldVal.getId(), newVal.getId()); + if (same) { + newVal.setId(oldVal.getId()).setUpdater(null).setUpdateTime(null); // 解决更新情况下:updateTime 不更新 + } + return same; + }); + + // 第二步,批量添加、修改、删除 + if (CollUtil.isNotEmpty(diffList.get(0))) { + demo03CourseNormalMapper.insertBatch(diffList.get(0)); + } + if (CollUtil.isNotEmpty(diffList.get(1))) { + demo03CourseNormalMapper.updateBatch(diffList.get(1)); + } + if (CollUtil.isNotEmpty(diffList.get(2))) { + demo03CourseNormalMapper.deleteByIds(convertList(diffList.get(2), Demo03CourseDO::getId)); + } } private void deleteDemo03CourseByStudentId(Long studentId) { diff --git a/yudao-module-infra/src/main/resources/codegen/java/service/serviceImpl.vm b/yudao-module-infra/src/main/resources/codegen/java/service/serviceImpl.vm index bf2163cc19..098f503c2c 100644 --- a/yudao-module-infra/src/main/resources/codegen/java/service/serviceImpl.vm +++ b/yudao-module-infra/src/main/resources/codegen/java/service/serviceImpl.vm @@ -25,6 +25,8 @@ import ${basePackage}.module.${subTable.moduleName}.dal.mysql.${subTable.busines #end import static ${ServiceExceptionUtilClassName}.exception; +import static ${CollectionUtilsClassName}.convertList; +import static ${CollectionUtilsClassName}.diffList; import static ${basePackage}.module.${table.moduleName}.enums.ErrorCodeConstants.*; /** @@ -56,9 +58,9 @@ public class ${table.className}ServiceImpl implements ${table.className}Service #set ($TreeParentJavaField = $treeParentColumn.javaField.substring(0,1).toUpperCase() + ${treeParentColumn.javaField.substring(1)})##首字母大写 #set ($TreeNameJavaField = $treeNameColumn.javaField.substring(0,1).toUpperCase() + ${treeNameColumn.javaField.substring(1)})##首字母大写 // 校验${treeParentColumn.columnComment}的有效性 - validateParent${simpleClassName}(null, ${createReqVOVar}.get${TreeParentJavaField}()); + validateParent${simpleClassName}(null, ${saveReqVOVar}.get${TreeParentJavaField}()); // 校验${treeNameColumn.columnComment}的唯一性 - validate${simpleClassName}${TreeNameJavaField}Unique(null, ${createReqVOVar}.get${TreeParentJavaField}(), ${createReqVOVar}.get${TreeNameJavaField}()); + validate${simpleClassName}${TreeNameJavaField}Unique(null, ${saveReqVOVar}.get${TreeParentJavaField}(), ${saveReqVOVar}.get${TreeNameJavaField}()); #end // 插入 @@ -76,9 +78,9 @@ public class ${table.className}ServiceImpl implements ${table.className}Service #set ($subJoinColumn = $subJoinColumns.get($index))##当前 join 字段 #set ($SubJoinColumnName = $subJoinColumn.javaField.substring(0,1).toUpperCase() + ${subJoinColumn.javaField.substring(1)})##首字母大写 #if ( $subTable.subJoinMany) - create${subSimpleClassName}List(${classNameVar}.getId(), ${createReqVOVar}.get${subSimpleClassNames.get($index)}s()); + create${subSimpleClassName}List(${classNameVar}.getId(), ${saveReqVOVar}.get${subSimpleClassNames.get($index)}s()); #else - create${subSimpleClassName}(${classNameVar}.getId(), ${createReqVOVar}.get${subSimpleClassNames.get($index)}()); + create${subSimpleClassName}(${classNameVar}.getId(), ${saveReqVOVar}.get${subSimpleClassNames.get($index)}()); #end #end #end @@ -372,14 +374,31 @@ public class ${table.className}ServiceImpl implements ${table.className}Service #else #if ( $subTable.subJoinMany) private void create${subSimpleClassName}List(${primaryColumn.javaType} ${subJoinColumn.javaField}, List<${subTable.className}DO> list) { - list.forEach(o -> o.set$SubJoinColumnName(${subJoinColumn.javaField})); + list.forEach(o -> o.set${SubJoinColumnName}(${subJoinColumn.javaField})); ${subClassNameVars.get($index)}Mapper.insertBatch(list); } private void update${subSimpleClassName}List(${primaryColumn.javaType} ${subJoinColumn.javaField}, List<${subTable.className}DO> list) { - delete${subSimpleClassName}By${SubJoinColumnName}(${subJoinColumn.javaField}); - list.forEach(o -> o.setId(null).setUpdater(null).setUpdateTime(null)); // 解决更新情况下:1)id 冲突;2)updateTime 不更新 - create${subSimpleClassName}List(${subJoinColumn.javaField}, list); + list.forEach(o -> o.set${SubJoinColumnName}(${subJoinColumn.javaField})); + List<${subTable.className}DO> oldList = ${subClassNameVar}Mapper.selectListBy${SubJoinColumnName}(${subJoinColumn.javaField}); + List> diffList = diffList(oldList, list, (oldVal, newVal) -> { + boolean same = ObjectUtil.equal(oldVal.getId(), newVal.getId()); + if (same) { + newVal.setId(oldVal.getId()).setUpdater(null).setUpdateTime(null); // 解决更新情况下:updateTime 不更新 + } + return same; + }); + + // 第二步,批量添加、修改、删除 + if (CollUtil.isNotEmpty(diffList.get(0))) { + ${subClassNameVar}Mapper.insertBatch(diffList.get(0)); + } + if (CollUtil.isNotEmpty(diffList.get(1))) { + ${subClassNameVar}Mapper.updateBatch(diffList.get(1)); + } + if (CollUtil.isNotEmpty(diffList.get(2))) { + ${subClassNameVar}Mapper.deleteByIds(convertList(diffList.get(2), ${subTable.className}DO::getId)); + } } #else