From 9295d610308d692caf698b27df053428e5e5b738 Mon Sep 17 00:00:00 2001 From: puhui999 Date: Mon, 19 May 2025 11:50:07 +0800 Subject: [PATCH] =?UTF-8?q?feat:=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?=E6=97=B6=EF=BC=8C=E5=8F=AF=E7=94=9F=E6=88=90=E6=89=B9=E9=87=8F?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../codegen/vo/table/CodegenTableRespVO.java | 4 ++ .../vo/table/CodegenTableSaveReqVO.java | 4 ++ .../demo/demo01/Demo01ContactController.java | 14 ++++++- .../demo01/vo/Demo01ContactPageReqVO.java | 8 ++-- .../demo/demo01/vo/Demo01ContactRespVO.java | 13 +++--- .../demo01/vo/Demo01ContactSaveReqVO.java | 4 +- .../dataobject/codegen/CodegenTableDO.java | 6 +++ .../demo/demo01/Demo01ContactMapper.java | 4 +- .../demo/demo01/Demo01ContactService.java | 17 +++++--- .../demo/demo01/Demo01ContactServiceImpl.java | 34 +++++++++++----- .../codegen/java/controller/controller.vm | 13 ++++++ .../resources/codegen/java/dal/mapper_sub.vm | 6 +++ .../resources/codegen/java/service/service.vm | 9 +++++ .../codegen/java/service/serviceImpl.vm | 40 +++++++++++++++++++ 14 files changed, 146 insertions(+), 30 deletions(-) diff --git a/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/codegen/vo/table/CodegenTableRespVO.java b/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/codegen/vo/table/CodegenTableRespVO.java index 8b438b2da6..a8a8fe108f 100644 --- a/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/codegen/vo/table/CodegenTableRespVO.java +++ b/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/codegen/vo/table/CodegenTableRespVO.java @@ -1,6 +1,7 @@ package cn.iocoder.yudao.module.infra.controller.admin.codegen.vo.table; import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.NotNull; import lombok.Data; import java.time.LocalDateTime; @@ -45,6 +46,9 @@ public class CodegenTableRespVO { @Schema(description = "前端类型,参见 CodegenFrontTypeEnum 枚举", requiredMode = Schema.RequiredMode.REQUIRED, example = "20") private Integer frontType; + @Schema(description = "是否生成批量删除接口", example = "true") + private Boolean deleteBatch; + @Schema(description = "父菜单编号", example = "1024") private Long parentMenuId; diff --git a/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/codegen/vo/table/CodegenTableSaveReqVO.java b/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/codegen/vo/table/CodegenTableSaveReqVO.java index 201d94d2e2..1fa2ba9278 100644 --- a/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/codegen/vo/table/CodegenTableSaveReqVO.java +++ b/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/codegen/vo/table/CodegenTableSaveReqVO.java @@ -60,6 +60,10 @@ public class CodegenTableSaveReqVO { @NotNull(message = "前端类型不能为空") private Integer frontType; + @Schema(description = "是否生成批量删除接口", example = "true") + @NotNull(message = "是否生成批量删除接口不能为空") + private Boolean deleteBatch; + @Schema(description = "父菜单编号", example = "1024") private Long parentMenuId; diff --git a/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/demo/demo01/Demo01ContactController.java b/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/demo/demo01/Demo01ContactController.java index cf6935e505..c3c518db9c 100644 --- a/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/demo/demo01/Demo01ContactController.java +++ b/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/demo/demo01/Demo01ContactController.java @@ -17,6 +17,7 @@ import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.annotation.Resource; import jakarta.servlet.http.HttpServletResponse; import jakarta.validation.Valid; +import jakarta.validation.constraints.Size; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; @@ -60,6 +61,15 @@ public class Demo01ContactController { return success(true); } + @DeleteMapping("/delete-batch") + @Parameter(name = "ids", description = "编号", required = true) + @Operation(summary = "批量删除示例联系人") + @PreAuthorize("@ss.hasPermission('infra:demo01-contact:delete')") + public CommonResult deleteDemo01Contact(@RequestParam("ids") @Size(max = 100, message = "最多允许100个") List ids) { + demo01ContactService.deleteDemo01ContactByIds(ids); + return success(true); + } + @GetMapping("/get") @Operation(summary = "获得示例联系人") @Parameter(name = "id", description = "编号", required = true, example = "1024") @@ -82,12 +92,12 @@ public class Demo01ContactController { @PreAuthorize("@ss.hasPermission('infra:demo01-contact:export')") @ApiAccessLog(operateType = EXPORT) public void exportDemo01ContactExcel(@Valid Demo01ContactPageReqVO pageReqVO, - HttpServletResponse response) throws IOException { + HttpServletResponse response) throws IOException { pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE); List list = demo01ContactService.getDemo01ContactPage(pageReqVO).getList(); // 导出 Excel ExcelUtils.write(response, "示例联系人.xls", "数据", Demo01ContactRespVO.class, - BeanUtils.toBean(list, Demo01ContactRespVO.class)); + BeanUtils.toBean(list, Demo01ContactRespVO.class)); } } \ No newline at end of file diff --git a/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/demo/demo01/vo/Demo01ContactPageReqVO.java b/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/demo/demo01/vo/Demo01ContactPageReqVO.java index d337d2d7db..151813965c 100644 --- a/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/demo/demo01/vo/Demo01ContactPageReqVO.java +++ b/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/demo/demo01/vo/Demo01ContactPageReqVO.java @@ -1,10 +1,12 @@ package cn.iocoder.yudao.module.infra.controller.admin.demo.demo01.vo; -import lombok.*; -import java.util.*; -import io.swagger.v3.oas.annotations.media.Schema; import cn.iocoder.yudao.framework.common.pojo.PageParam; +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.ToString; import org.springframework.format.annotation.DateTimeFormat; + import java.time.LocalDateTime; import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND; diff --git a/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/demo/demo01/vo/Demo01ContactRespVO.java b/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/demo/demo01/vo/Demo01ContactRespVO.java index 5d176c262a..17ee9fef84 100644 --- a/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/demo/demo01/vo/Demo01ContactRespVO.java +++ b/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/demo/demo01/vo/Demo01ContactRespVO.java @@ -1,14 +1,13 @@ package cn.iocoder.yudao.module.infra.controller.admin.demo.demo01.vo; -import io.swagger.v3.oas.annotations.media.Schema; -import lombok.*; -import java.util.*; -import java.util.*; -import org.springframework.format.annotation.DateTimeFormat; -import java.time.LocalDateTime; -import com.alibaba.excel.annotation.*; import cn.iocoder.yudao.framework.excel.core.annotations.DictFormat; import cn.iocoder.yudao.framework.excel.core.convert.DictConvert; +import com.alibaba.excel.annotation.ExcelIgnoreUnannotated; +import com.alibaba.excel.annotation.ExcelProperty; +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +import java.time.LocalDateTime; @Schema(description = "管理后台 - 示例联系人 Response VO") @Data diff --git a/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/demo/demo01/vo/Demo01ContactSaveReqVO.java b/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/demo/demo01/vo/Demo01ContactSaveReqVO.java index 352e43178a..15d2727663 100644 --- a/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/demo/demo01/vo/Demo01ContactSaveReqVO.java +++ b/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/demo/demo01/vo/Demo01ContactSaveReqVO.java @@ -1,10 +1,10 @@ package cn.iocoder.yudao.module.infra.controller.admin.demo.demo01.vo; import io.swagger.v3.oas.annotations.media.Schema; -import lombok.Data; - import jakarta.validation.constraints.NotEmpty; import jakarta.validation.constraints.NotNull; +import lombok.Data; + import java.time.LocalDateTime; @Schema(description = "管理后台 - 示例联系人新增/修改 Request VO") diff --git a/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/dal/dataobject/codegen/CodegenTableDO.java b/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/dal/dataobject/codegen/CodegenTableDO.java index b46cbe4fef..d8aa79b66a 100644 --- a/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/dal/dataobject/codegen/CodegenTableDO.java +++ b/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/dal/dataobject/codegen/CodegenTableDO.java @@ -108,6 +108,12 @@ public class CodegenTableDO extends BaseDO { * 枚举 {@link CodegenFrontTypeEnum} */ private Integer frontType; + /** + * 是否生成批量删除接口 + * -true 是 + * -false 否 + */ + private Boolean deleteBatch; // ========== 菜单相关字段 ========== diff --git a/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/dal/mysql/demo/demo01/Demo01ContactMapper.java b/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/dal/mysql/demo/demo01/Demo01ContactMapper.java index f5f3cdbbe8..800f662afb 100644 --- a/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/dal/mysql/demo/demo01/Demo01ContactMapper.java +++ b/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/dal/mysql/demo/demo01/Demo01ContactMapper.java @@ -1,9 +1,9 @@ package cn.iocoder.yudao.module.infra.dal.mysql.demo.demo01; import cn.iocoder.yudao.framework.common.pojo.PageResult; -import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; -import cn.iocoder.yudao.module.infra.controller.admin.demo.demo01.vo.Demo01ContactPageReqVO; +import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; +import cn.iocoder.yudao.module.infra.controller.admin.demo.demo01.vo.*; import cn.iocoder.yudao.module.infra.dal.dataobject.demo.demo01.Demo01ContactDO; import org.apache.ibatis.annotations.Mapper; diff --git a/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/service/demo/demo01/Demo01ContactService.java b/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/service/demo/demo01/Demo01ContactService.java index efcb6e5d04..5d1d611559 100644 --- a/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/service/demo/demo01/Demo01ContactService.java +++ b/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/service/demo/demo01/Demo01ContactService.java @@ -1,11 +1,11 @@ package cn.iocoder.yudao.module.infra.service.demo.demo01; -import jakarta.validation.*; - -import cn.iocoder.yudao.module.infra.controller.admin.demo.demo01.vo.Demo01ContactPageReqVO; -import cn.iocoder.yudao.module.infra.controller.admin.demo.demo01.vo.Demo01ContactSaveReqVO; -import cn.iocoder.yudao.module.infra.dal.dataobject.demo.demo01.Demo01ContactDO; import cn.iocoder.yudao.framework.common.pojo.PageResult; +import cn.iocoder.yudao.module.infra.controller.admin.demo.demo01.vo.*; +import cn.iocoder.yudao.module.infra.dal.dataobject.demo.demo01.Demo01ContactDO; +import jakarta.validation.Valid; + +import java.util.List; /** * 示例联系人 Service 接口 @@ -36,6 +36,13 @@ public interface Demo01ContactService { */ void deleteDemo01Contact(Long id); + /** + * 批量删除示例联系人 + * + * @param ids 编号 + */ + void deleteDemo01ContactByIds(List ids); + /** * 获得示例联系人 * diff --git a/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/service/demo/demo01/Demo01ContactServiceImpl.java b/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/service/demo/demo01/Demo01ContactServiceImpl.java index f695e3dcb7..5f74253102 100644 --- a/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/service/demo/demo01/Demo01ContactServiceImpl.java +++ b/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/service/demo/demo01/Demo01ContactServiceImpl.java @@ -1,19 +1,20 @@ package cn.iocoder.yudao.module.infra.service.demo.demo01; -import cn.iocoder.yudao.module.infra.controller.admin.demo.demo01.vo.Demo01ContactPageReqVO; -import cn.iocoder.yudao.module.infra.controller.admin.demo.demo01.vo.Demo01ContactSaveReqVO; -import org.springframework.stereotype.Service; -import jakarta.annotation.Resource; -import org.springframework.validation.annotation.Validated; - -import cn.iocoder.yudao.module.infra.dal.dataobject.demo.demo01.Demo01ContactDO; +import cn.hutool.core.collection.CollUtil; 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.demo01.vo.Demo01ContactPageReqVO; +import cn.iocoder.yudao.module.infra.controller.admin.demo.demo01.vo.Demo01ContactSaveReqVO; +import cn.iocoder.yudao.module.infra.dal.dataobject.demo.demo01.Demo01ContactDO; import cn.iocoder.yudao.module.infra.dal.mysql.demo.demo01.Demo01ContactMapper; +import jakarta.annotation.Resource; +import org.springframework.stereotype.Service; +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.module.infra.enums.ErrorCodeConstants.*; +import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.DEMO01_CONTACT_NOT_EXISTS; /** * 示例联系人 Service 实现类 @@ -53,6 +54,21 @@ public class Demo01ContactServiceImpl implements Demo01ContactService { demo01ContactMapper.deleteById(id); } + @Override + public void deleteDemo01ContactByIds(List ids) { + // 校验存在 + validateDemo01ContactExists(ids); + // 删除 + demo01ContactMapper.deleteByIds(ids); + } + + private void validateDemo01ContactExists(List ids) { + List list = demo01ContactMapper.selectByIds(ids); + if (CollUtil.isEmpty(list) || list.size() != ids.size()) { + throw exception(DEMO01_CONTACT_NOT_EXISTS); + } + } + private void validateDemo01ContactExists(Long id) { if (demo01ContactMapper.selectById(id) == null) { throw exception(DEMO01_CONTACT_NOT_EXISTS); diff --git a/yudao-module-infra/src/main/resources/codegen/java/controller/controller.vm b/yudao-module-infra/src/main/resources/codegen/java/controller/controller.vm index 5aa3baef83..0d607ced55 100644 --- a/yudao-module-infra/src/main/resources/codegen/java/controller/controller.vm +++ b/yudao-module-infra/src/main/resources/codegen/java/controller/controller.vm @@ -74,6 +74,19 @@ public class ${sceneEnum.prefixClass}${table.className}Controller { return success(true); } +#if ( $table.templateType != 2 && $table.deleteBatch) + @DeleteMapping("/delete-batch") + @Parameter(name = "ids", description = "编号", required = true) + @Operation(summary = "批量删除${table.classComment}") + #if ($sceneEnum.scene == 1) + @PreAuthorize("@ss.hasPermission('${permissionPrefix}:delete')") + #end + public CommonResult delete${simpleClassName}(@RequestParam("ids") @Size(max = 100, message = "最多允许100个") List<${primaryColumn.javaType}> ids) { + ${classNameVar}Service.delete${simpleClassName}ByIds(ids); + return success(true); + } +#end + @GetMapping("/get") @Operation(summary = "获得${table.classComment}") @Parameter(name = "id", description = "编号", required = true, example = "1024") diff --git a/yudao-module-infra/src/main/resources/codegen/java/dal/mapper_sub.vm b/yudao-module-infra/src/main/resources/codegen/java/dal/mapper_sub.vm index 6ccaea79ea..88d1cd0745 100644 --- a/yudao-module-infra/src/main/resources/codegen/java/dal/mapper_sub.vm +++ b/yudao-module-infra/src/main/resources/codegen/java/dal/mapper_sub.vm @@ -54,4 +54,10 @@ public interface ${subTable.className}Mapper extends BaseMapperX<${subTable.clas return delete(${subTable.className}DO::get${SubJoinColumnName}, ${subJoinColumn.javaField}); } +#if ( $table.templateType != 2 && $table.deleteBatch) + default int deleteBy${SubJoinColumnName}s(List<${subJoinColumn.javaType}> ${subJoinColumn.javaField}s) { + return delete(${subTable.className}DO::get${SubJoinColumnName}, ${subJoinColumn.javaField}s); + } +#end + } diff --git a/yudao-module-infra/src/main/resources/codegen/java/service/service.vm b/yudao-module-infra/src/main/resources/codegen/java/service/service.vm index c4ee4f0f64..f99817c6ce 100644 --- a/yudao-module-infra/src/main/resources/codegen/java/service/service.vm +++ b/yudao-module-infra/src/main/resources/codegen/java/service/service.vm @@ -40,6 +40,15 @@ public interface ${table.className}Service { */ void delete${simpleClassName}(${primaryColumn.javaType} id); +#if ( $table.templateType != 2 && $table.deleteBatch) + /** + * 批量删除${table.classComment} + * + * @param ids 编号 + */ + void delete${simpleClassName}ByIds(List<${primaryColumn.javaType}> ids); +#end + /** * 获得${table.classComment} * 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 80bc71b026..1680398909 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 @@ -1,5 +1,6 @@ package ${basePackage}.module.${table.moduleName}.service.${table.businessName}; +import cn.hutool.core.collection.CollUtil; import org.springframework.stereotype.Service; import ${jakartaPackage}.annotation.Resource; import org.springframework.validation.annotation.Validated; @@ -154,6 +155,39 @@ public class ${table.className}ServiceImpl implements ${table.className}Service #end } +#if ( $table.templateType != 2 && $table.deleteBatch) + @Override + ## 特殊:主子表专属逻辑 + #if ( $subTables && $subTables.size() > 0) + @Transactional(rollbackFor = Exception.class) + #end + public void delete${simpleClassName}ByIds(List<${primaryColumn.javaType}> ids) { + // 校验存在 + validate${simpleClassName}Exists(ids); + // 删除 + ${classNameVar}Mapper.deleteByIds(ids); + ## 特殊:主子表专属逻辑 + #if ( $subTables && $subTables.size() > 0) + + // 删除子表 + #foreach ($subTable in $subTables) + #set ($index = $foreach.count - 1) + #set ($subSimpleClassName = $subSimpleClassNames.get($index)) + #set ($subJoinColumn = $subJoinColumns.get($index))##当前 join 字段 + #set ($SubJoinColumnName = $subJoinColumn.javaField.substring(0,1).toUpperCase() + ${subJoinColumn.javaField.substring(1)})##首字母大写 + delete${subSimpleClassName}By${SubJoinColumnName}s(ids); + #end + #end + } + + private void validate${simpleClassName}Exists(List<${primaryColumn.javaType}> ids) { + List<${table.className}DO> list = ${classNameVar}Mapper.selectByIds(ids); + if (CollUtil.isEmpty(list) || list.size() != ids.size()) { + throw exception(${simpleClassName_underlineCase.toUpperCase()}_NOT_EXISTS); + } + } +#end + private void validate${simpleClassName}Exists(${primaryColumn.javaType} id) { if (${classNameVar}Mapper.selectById(id) == null) { throw exception(${simpleClassName_underlineCase.toUpperCase()}_NOT_EXISTS); @@ -347,5 +381,11 @@ public class ${table.className}ServiceImpl implements ${table.className}Service ${subClassNameVars.get($index)}Mapper.deleteBy${SubJoinColumnName}(${subJoinColumn.javaField}); } +#if ( $table.templateType != 2 && $table.deleteBatch) + private void delete${subSimpleClassName}By${SubJoinColumnName}s(List<${primaryColumn.javaType}> ${subJoinColumn.javaField}s) { + ${subClassNameVars.get($index)}Mapper.deleteBy${SubJoinColumnName}s(${subJoinColumn.javaField}s); + } +#end + #end } \ No newline at end of file