perf:【INFRA 基础设施】代码生成主子表非 erp 模式时,当子表一对多时更新改为通过 diffList 实现对应的增删改

This commit is contained in:
puhui999
2025-05-20 11:15:05 +08:00
parent bc77af09e0
commit cd341da674
4 changed files with 75 additions and 15 deletions

View File

@@ -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();

View File

@@ -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<Demo03CourseDO> list) {
deleteDemo03CourseByStudentId(studentId);
list.forEach(o -> o.setId(null).setUpdater(null).setUpdateTime(null)); // 解决更新情况下1id 冲突2updateTime 不更新
createDemo03CourseList(studentId, list);
list.forEach(o -> o.setStudentId(studentId));
List<Demo03CourseDO> oldList = demo03CourseInnerMapper.selectListByStudentId(studentId);
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) {

View File

@@ -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<Demo03CourseDO> list) {
deleteDemo03CourseByStudentId(studentId);
list.forEach(o -> o.setId(null).setUpdater(null).setUpdateTime(null)); // 解决更新情况下1id 冲突2updateTime 不更新
createDemo03CourseList(studentId, list);
list.forEach(o -> o.setStudentId(studentId));
List<Demo03CourseDO> oldList = demo03CourseNormalMapper.selectListByStudentId(studentId);
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) {