perf:【INFRA 基础设施】代码生成主子表非 erp 模式时,当子表一对多时更新改为通过 diffList 实现对应的增删改
This commit is contained in:
parent
bc77af09e0
commit
cd341da674
|
@ -236,6 +236,7 @@ public class CodegenEngine {
|
||||||
+ '.' + "framework"); // 用于后续获取测试类的 package 地址
|
+ '.' + "framework"); // 用于后续获取测试类的 package 地址
|
||||||
globalBindingMap.put("jakartaPackage", jakartaEnable ? "jakarta" : "javax");
|
globalBindingMap.put("jakartaPackage", jakartaEnable ? "jakarta" : "javax");
|
||||||
globalBindingMap.put("voType", codegenProperties.getVoType());
|
globalBindingMap.put("voType", codegenProperties.getVoType());
|
||||||
|
globalBindingMap.put("deleteBatchEnable", codegenProperties.getDeleteBatchEnable());
|
||||||
// 全局 Java Bean
|
// 全局 Java Bean
|
||||||
globalBindingMap.put("CommonResultClassName", CommonResult.class.getName());
|
globalBindingMap.put("CommonResultClassName", CommonResult.class.getName());
|
||||||
globalBindingMap.put("PageResultClassName", PageResult.class.getName());
|
globalBindingMap.put("PageResultClassName", PageResult.class.getName());
|
||||||
|
@ -257,6 +258,7 @@ public class CodegenEngine {
|
||||||
globalBindingMap.put("ApiAccessLogClassName", ApiAccessLog.class.getName());
|
globalBindingMap.put("ApiAccessLogClassName", ApiAccessLog.class.getName());
|
||||||
globalBindingMap.put("OperateTypeEnumClassName", OperateTypeEnum.class.getName());
|
globalBindingMap.put("OperateTypeEnumClassName", OperateTypeEnum.class.getName());
|
||||||
globalBindingMap.put("BeanUtils", BeanUtils.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("columns", columns);
|
||||||
bindingMap.put("primaryColumn", CollectionUtils.findFirst(columns, CodegenColumnDO::getPrimaryKey)); // 主键字段
|
bindingMap.put("primaryColumn", CollectionUtils.findFirst(columns, CodegenColumnDO::getPrimaryKey)); // 主键字段
|
||||||
bindingMap.put("sceneEnum", CodegenSceneEnum.valueOf(table.getScene()));
|
bindingMap.put("sceneEnum", CodegenSceneEnum.valueOf(table.getScene()));
|
||||||
bindingMap.put("deleteBatchEnable", codegenProperties.getDeleteBatchEnable());
|
|
||||||
// className 相关
|
// className 相关
|
||||||
// 去掉指定前缀,将 TestDictType 转换成 DictType. 因为在 create 等方法后,不需要带上 Test 前缀
|
// 去掉指定前缀,将 TestDictType 转换成 DictType. 因为在 create 等方法后,不需要带上 Test 前缀
|
||||||
String className = table.getClassName();
|
String className = table.getClassName();
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package cn.iocoder.yudao.module.infra.service.demo.demo03.inner;
|
package cn.iocoder.yudao.module.infra.service.demo.demo03.inner;
|
||||||
|
|
||||||
import cn.hutool.core.collection.CollUtil;
|
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.pojo.PageResult;
|
||||||
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
||||||
import cn.iocoder.yudao.module.infra.controller.admin.demo.demo03.inner.vo.Demo03StudentInnerPageReqVO;
|
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 java.util.List;
|
||||||
|
|
||||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
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;
|
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<Demo03CourseDO> list) {
|
private void updateDemo03CourseList(Long studentId, List<Demo03CourseDO> list) {
|
||||||
deleteDemo03CourseByStudentId(studentId);
|
list.forEach(o -> o.setStudentId(studentId));
|
||||||
list.forEach(o -> o.setId(null).setUpdater(null).setUpdateTime(null)); // 解决更新情况下:1)id 冲突;2)updateTime 不更新
|
List<Demo03CourseDO> oldList = demo03CourseInnerMapper.selectListByStudentId(studentId);
|
||||||
createDemo03CourseList(studentId, list);
|
List<List<Demo03CourseDO>> 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) {
|
private void deleteDemo03CourseByStudentId(Long studentId) {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package cn.iocoder.yudao.module.infra.service.demo.demo03.normal;
|
package cn.iocoder.yudao.module.infra.service.demo.demo03.normal;
|
||||||
|
|
||||||
import cn.hutool.core.collection.CollUtil;
|
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.pojo.PageResult;
|
||||||
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
||||||
import cn.iocoder.yudao.module.infra.controller.admin.demo.demo03.normal.vo.Demo03StudentNormalPageReqVO;
|
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 java.util.List;
|
||||||
|
|
||||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
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;
|
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<Demo03CourseDO> list) {
|
private void updateDemo03CourseList(Long studentId, List<Demo03CourseDO> list) {
|
||||||
deleteDemo03CourseByStudentId(studentId);
|
list.forEach(o -> o.setStudentId(studentId));
|
||||||
list.forEach(o -> o.setId(null).setUpdater(null).setUpdateTime(null)); // 解决更新情况下:1)id 冲突;2)updateTime 不更新
|
List<Demo03CourseDO> oldList = demo03CourseNormalMapper.selectListByStudentId(studentId);
|
||||||
createDemo03CourseList(studentId, list);
|
List<List<Demo03CourseDO>> 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) {
|
private void deleteDemo03CourseByStudentId(Long studentId) {
|
||||||
|
|
|
@ -25,6 +25,8 @@ import ${basePackage}.module.${subTable.moduleName}.dal.mysql.${subTable.busines
|
||||||
#end
|
#end
|
||||||
|
|
||||||
import static ${ServiceExceptionUtilClassName}.exception;
|
import static ${ServiceExceptionUtilClassName}.exception;
|
||||||
|
import static ${CollectionUtilsClassName}.convertList;
|
||||||
|
import static ${CollectionUtilsClassName}.diffList;
|
||||||
import static ${basePackage}.module.${table.moduleName}.enums.ErrorCodeConstants.*;
|
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 ($TreeParentJavaField = $treeParentColumn.javaField.substring(0,1).toUpperCase() + ${treeParentColumn.javaField.substring(1)})##首字母大写
|
||||||
#set ($TreeNameJavaField = $treeNameColumn.javaField.substring(0,1).toUpperCase() + ${treeNameColumn.javaField.substring(1)})##首字母大写
|
#set ($TreeNameJavaField = $treeNameColumn.javaField.substring(0,1).toUpperCase() + ${treeNameColumn.javaField.substring(1)})##首字母大写
|
||||||
// 校验${treeParentColumn.columnComment}的有效性
|
// 校验${treeParentColumn.columnComment}的有效性
|
||||||
validateParent${simpleClassName}(null, ${createReqVOVar}.get${TreeParentJavaField}());
|
validateParent${simpleClassName}(null, ${saveReqVOVar}.get${TreeParentJavaField}());
|
||||||
// 校验${treeNameColumn.columnComment}的唯一性
|
// 校验${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
|
#end
|
||||||
// 插入
|
// 插入
|
||||||
|
@ -76,9 +78,9 @@ public class ${table.className}ServiceImpl implements ${table.className}Service
|
||||||
#set ($subJoinColumn = $subJoinColumns.get($index))##当前 join 字段
|
#set ($subJoinColumn = $subJoinColumns.get($index))##当前 join 字段
|
||||||
#set ($SubJoinColumnName = $subJoinColumn.javaField.substring(0,1).toUpperCase() + ${subJoinColumn.javaField.substring(1)})##首字母大写
|
#set ($SubJoinColumnName = $subJoinColumn.javaField.substring(0,1).toUpperCase() + ${subJoinColumn.javaField.substring(1)})##首字母大写
|
||||||
#if ( $subTable.subJoinMany)
|
#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
|
#else
|
||||||
create${subSimpleClassName}(${classNameVar}.getId(), ${createReqVOVar}.get${subSimpleClassNames.get($index)}());
|
create${subSimpleClassName}(${classNameVar}.getId(), ${saveReqVOVar}.get${subSimpleClassNames.get($index)}());
|
||||||
#end
|
#end
|
||||||
#end
|
#end
|
||||||
#end
|
#end
|
||||||
|
@ -372,14 +374,31 @@ public class ${table.className}ServiceImpl implements ${table.className}Service
|
||||||
#else
|
#else
|
||||||
#if ( $subTable.subJoinMany)
|
#if ( $subTable.subJoinMany)
|
||||||
private void create${subSimpleClassName}List(${primaryColumn.javaType} ${subJoinColumn.javaField}, List<${subTable.className}DO> list) {
|
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);
|
${subClassNameVars.get($index)}Mapper.insertBatch(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void update${subSimpleClassName}List(${primaryColumn.javaType} ${subJoinColumn.javaField}, List<${subTable.className}DO> 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.set${SubJoinColumnName}(${subJoinColumn.javaField}));
|
||||||
list.forEach(o -> o.setId(null).setUpdater(null).setUpdateTime(null)); // 解决更新情况下:1)id 冲突;2)updateTime 不更新
|
List<${subTable.className}DO> oldList = ${subClassNameVar}Mapper.selectListBy${SubJoinColumnName}(${subJoinColumn.javaField});
|
||||||
create${subSimpleClassName}List(${subJoinColumn.javaField}, list);
|
List<List<${subTable.className}DO>> 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
|
#else
|
||||||
|
|
Loading…
Reference in New Issue