Commit 9295d610308d692caf698b27df053428e5e5b738

Authored by puhui999
1 parent 7a3634d8

feat:【INFRA 基础设施】代码生成时,可生成批量删除接口

Showing 14 changed files with 144 additions and 28 deletions
yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/codegen/vo/table/CodegenTableRespVO.java
1 1 package cn.iocoder.yudao.module.infra.controller.admin.codegen.vo.table;
2 2  
3 3 import io.swagger.v3.oas.annotations.media.Schema;
  4 +import jakarta.validation.constraints.NotNull;
4 5 import lombok.Data;
5 6  
6 7 import java.time.LocalDateTime;
... ... @@ -45,6 +46,9 @@ public class CodegenTableRespVO {
45 46 @Schema(description = "前端类型,参见 CodegenFrontTypeEnum 枚举", requiredMode = Schema.RequiredMode.REQUIRED, example = "20")
46 47 private Integer frontType;
47 48  
  49 + @Schema(description = "是否生成批量删除接口", example = "true")
  50 + private Boolean deleteBatch;
  51 +
48 52 @Schema(description = "父菜单编号", example = "1024")
49 53 private Long parentMenuId;
50 54  
... ...
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 {
60 60 @NotNull(message = "前端类型不能为空")
61 61 private Integer frontType;
62 62  
  63 + @Schema(description = "是否生成批量删除接口", example = "true")
  64 + @NotNull(message = "是否生成批量删除接口不能为空")
  65 + private Boolean deleteBatch;
  66 +
63 67 @Schema(description = "父菜单编号", example = "1024")
64 68 private Long parentMenuId;
65 69  
... ...
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;
17 17 import jakarta.annotation.Resource;
18 18 import jakarta.servlet.http.HttpServletResponse;
19 19 import jakarta.validation.Valid;
  20 +import jakarta.validation.constraints.Size;
20 21 import org.springframework.security.access.prepost.PreAuthorize;
21 22 import org.springframework.validation.annotation.Validated;
22 23 import org.springframework.web.bind.annotation.*;
... ... @@ -60,6 +61,15 @@ public class Demo01ContactController {
60 61 return success(true);
61 62 }
62 63  
  64 + @DeleteMapping("/delete-batch")
  65 + @Parameter(name = "ids", description = "编号", required = true)
  66 + @Operation(summary = "批量删除示例联系人")
  67 + @PreAuthorize("@ss.hasPermission('infra:demo01-contact:delete')")
  68 + public CommonResult<Boolean> deleteDemo01Contact(@RequestParam("ids") @Size(max = 100, message = "最多允许100个") List<Long> ids) {
  69 + demo01ContactService.deleteDemo01ContactByIds(ids);
  70 + return success(true);
  71 + }
  72 +
63 73 @GetMapping("/get")
64 74 @Operation(summary = "获得示例联系人")
65 75 @Parameter(name = "id", description = "编号", required = true, example = "1024")
... ... @@ -82,12 +92,12 @@ public class Demo01ContactController {
82 92 @PreAuthorize("@ss.hasPermission('infra:demo01-contact:export')")
83 93 @ApiAccessLog(operateType = EXPORT)
84 94 public void exportDemo01ContactExcel(@Valid Demo01ContactPageReqVO pageReqVO,
85   - HttpServletResponse response) throws IOException {
  95 + HttpServletResponse response) throws IOException {
86 96 pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
87 97 List<Demo01ContactDO> list = demo01ContactService.getDemo01ContactPage(pageReqVO).getList();
88 98 // 导出 Excel
89 99 ExcelUtils.write(response, "示例联系人.xls", "数据", Demo01ContactRespVO.class,
90   - BeanUtils.toBean(list, Demo01ContactRespVO.class));
  100 + BeanUtils.toBean(list, Demo01ContactRespVO.class));
91 101 }
92 102  
93 103 }
94 104 \ No newline at end of file
... ...
yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/demo/demo01/vo/Demo01ContactPageReqVO.java
1 1 package cn.iocoder.yudao.module.infra.controller.admin.demo.demo01.vo;
2 2  
3   -import lombok.*;
4   -import java.util.*;
5   -import io.swagger.v3.oas.annotations.media.Schema;
6 3 import cn.iocoder.yudao.framework.common.pojo.PageParam;
  4 +import io.swagger.v3.oas.annotations.media.Schema;
  5 +import lombok.Data;
  6 +import lombok.EqualsAndHashCode;
  7 +import lombok.ToString;
7 8 import org.springframework.format.annotation.DateTimeFormat;
  9 +
8 10 import java.time.LocalDateTime;
9 11  
10 12 import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
... ...
yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/demo/demo01/vo/Demo01ContactRespVO.java
1 1 package cn.iocoder.yudao.module.infra.controller.admin.demo.demo01.vo;
2 2  
3   -import io.swagger.v3.oas.annotations.media.Schema;
4   -import lombok.*;
5   -import java.util.*;
6   -import java.util.*;
7   -import org.springframework.format.annotation.DateTimeFormat;
8   -import java.time.LocalDateTime;
9   -import com.alibaba.excel.annotation.*;
10 3 import cn.iocoder.yudao.framework.excel.core.annotations.DictFormat;
11 4 import cn.iocoder.yudao.framework.excel.core.convert.DictConvert;
  5 +import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
  6 +import com.alibaba.excel.annotation.ExcelProperty;
  7 +import io.swagger.v3.oas.annotations.media.Schema;
  8 +import lombok.Data;
  9 +
  10 +import java.time.LocalDateTime;
12 11  
13 12 @Schema(description = "管理后台 - 示例联系人 Response VO")
14 13 @Data
... ...
yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/demo/demo01/vo/Demo01ContactSaveReqVO.java
1 1 package cn.iocoder.yudao.module.infra.controller.admin.demo.demo01.vo;
2 2  
3 3 import io.swagger.v3.oas.annotations.media.Schema;
4   -import lombok.Data;
5   -
6 4 import jakarta.validation.constraints.NotEmpty;
7 5 import jakarta.validation.constraints.NotNull;
  6 +import lombok.Data;
  7 +
8 8 import java.time.LocalDateTime;
9 9  
10 10 @Schema(description = "管理后台 - 示例联系人新增/修改 Request VO")
... ...
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 {
108 108 * 枚举 {@link CodegenFrontTypeEnum}
109 109 */
110 110 private Integer frontType;
  111 + /**
  112 + * 是否生成批量删除接口
  113 + * -true 是
  114 + * -false 否
  115 + */
  116 + private Boolean deleteBatch;
111 117  
112 118 // ========== 菜单相关字段 ==========
113 119  
... ...
yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/dal/mysql/demo/demo01/Demo01ContactMapper.java
1 1 package cn.iocoder.yudao.module.infra.dal.mysql.demo.demo01;
2 2  
3 3 import cn.iocoder.yudao.framework.common.pojo.PageResult;
4   -import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
5 4 import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
6   -import cn.iocoder.yudao.module.infra.controller.admin.demo.demo01.vo.Demo01ContactPageReqVO;
  5 +import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
  6 +import cn.iocoder.yudao.module.infra.controller.admin.demo.demo01.vo.*;
7 7 import cn.iocoder.yudao.module.infra.dal.dataobject.demo.demo01.Demo01ContactDO;
8 8 import org.apache.ibatis.annotations.Mapper;
9 9  
... ...
yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/service/demo/demo01/Demo01ContactService.java
1 1 package cn.iocoder.yudao.module.infra.service.demo.demo01;
2 2  
3   -import jakarta.validation.*;
4   -
5   -import cn.iocoder.yudao.module.infra.controller.admin.demo.demo01.vo.Demo01ContactPageReqVO;
6   -import cn.iocoder.yudao.module.infra.controller.admin.demo.demo01.vo.Demo01ContactSaveReqVO;
7   -import cn.iocoder.yudao.module.infra.dal.dataobject.demo.demo01.Demo01ContactDO;
8 3 import cn.iocoder.yudao.framework.common.pojo.PageResult;
  4 +import cn.iocoder.yudao.module.infra.controller.admin.demo.demo01.vo.*;
  5 +import cn.iocoder.yudao.module.infra.dal.dataobject.demo.demo01.Demo01ContactDO;
  6 +import jakarta.validation.Valid;
  7 +
  8 +import java.util.List;
9 9  
10 10 /**
11 11 * 示例联系人 Service 接口
... ... @@ -37,6 +37,13 @@ public interface Demo01ContactService {
37 37 void deleteDemo01Contact(Long id);
38 38  
39 39 /**
  40 + * 批量删除示例联系人
  41 + *
  42 + * @param ids 编号
  43 + */
  44 + void deleteDemo01ContactByIds(List<Long> ids);
  45 +
  46 + /**
40 47 * 获得示例联系人
41 48 *
42 49 * @param id 编号
... ...
yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/service/demo/demo01/Demo01ContactServiceImpl.java
1 1 package cn.iocoder.yudao.module.infra.service.demo.demo01;
2 2  
  3 +import cn.hutool.core.collection.CollUtil;
  4 +import cn.iocoder.yudao.framework.common.pojo.PageResult;
  5 +import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
3 6 import cn.iocoder.yudao.module.infra.controller.admin.demo.demo01.vo.Demo01ContactPageReqVO;
4 7 import cn.iocoder.yudao.module.infra.controller.admin.demo.demo01.vo.Demo01ContactSaveReqVO;
5   -import org.springframework.stereotype.Service;
  8 +import cn.iocoder.yudao.module.infra.dal.dataobject.demo.demo01.Demo01ContactDO;
  9 +import cn.iocoder.yudao.module.infra.dal.mysql.demo.demo01.Demo01ContactMapper;
6 10 import jakarta.annotation.Resource;
  11 +import org.springframework.stereotype.Service;
7 12 import org.springframework.validation.annotation.Validated;
8 13  
9   -import cn.iocoder.yudao.module.infra.dal.dataobject.demo.demo01.Demo01ContactDO;
10   -import cn.iocoder.yudao.framework.common.pojo.PageResult;
11   -import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
12   -
13   -import cn.iocoder.yudao.module.infra.dal.mysql.demo.demo01.Demo01ContactMapper;
  14 +import java.util.List;
14 15  
15 16 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
16   -import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.*;
  17 +import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.DEMO01_CONTACT_NOT_EXISTS;
17 18  
18 19 /**
19 20 * 示例联系人 Service 实现类
... ... @@ -53,6 +54,21 @@ public class Demo01ContactServiceImpl implements Demo01ContactService {
53 54 demo01ContactMapper.deleteById(id);
54 55 }
55 56  
  57 + @Override
  58 + public void deleteDemo01ContactByIds(List<Long> ids) {
  59 + // 校验存在
  60 + validateDemo01ContactExists(ids);
  61 + // 删除
  62 + demo01ContactMapper.deleteByIds(ids);
  63 + }
  64 +
  65 + private void validateDemo01ContactExists(List<Long> ids) {
  66 + List<Demo01ContactDO> list = demo01ContactMapper.selectByIds(ids);
  67 + if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
  68 + throw exception(DEMO01_CONTACT_NOT_EXISTS);
  69 + }
  70 + }
  71 +
56 72 private void validateDemo01ContactExists(Long id) {
57 73 if (demo01ContactMapper.selectById(id) == null) {
58 74 throw exception(DEMO01_CONTACT_NOT_EXISTS);
... ...
yudao-module-infra/src/main/resources/codegen/java/controller/controller.vm
... ... @@ -74,6 +74,19 @@ public class ${sceneEnum.prefixClass}${table.className}Controller {
74 74 return success(true);
75 75 }
76 76  
  77 +#if ( $table.templateType != 2 && $table.deleteBatch)
  78 + @DeleteMapping("/delete-batch")
  79 + @Parameter(name = "ids", description = "编号", required = true)
  80 + @Operation(summary = "批量删除${table.classComment}")
  81 + #if ($sceneEnum.scene == 1)
  82 + @PreAuthorize("@ss.hasPermission('${permissionPrefix}:delete')")
  83 + #end
  84 + public CommonResult<Boolean> delete${simpleClassName}(@RequestParam("ids") @Size(max = 100, message = "最多允许100个") List<${primaryColumn.javaType}> ids) {
  85 + ${classNameVar}Service.delete${simpleClassName}ByIds(ids);
  86 + return success(true);
  87 + }
  88 +#end
  89 +
77 90 @GetMapping("/get")
78 91 @Operation(summary = "获得${table.classComment}")
79 92 @Parameter(name = "id", description = "编号", required = true, example = "1024")
... ...
yudao-module-infra/src/main/resources/codegen/java/dal/mapper_sub.vm
... ... @@ -54,4 +54,10 @@ public interface ${subTable.className}Mapper extends BaseMapperX&lt;${subTable.clas
54 54 return delete(${subTable.className}DO::get${SubJoinColumnName}, ${subJoinColumn.javaField});
55 55 }
56 56  
  57 +#if ( $table.templateType != 2 && $table.deleteBatch)
  58 + default int deleteBy${SubJoinColumnName}s(List<${subJoinColumn.javaType}> ${subJoinColumn.javaField}s) {
  59 + return delete(${subTable.className}DO::get${SubJoinColumnName}, ${subJoinColumn.javaField}s);
  60 + }
  61 +#end
  62 +
57 63 }
... ...
yudao-module-infra/src/main/resources/codegen/java/service/service.vm
... ... @@ -40,6 +40,15 @@ public interface ${table.className}Service {
40 40 */
41 41 void delete${simpleClassName}(${primaryColumn.javaType} id);
42 42  
  43 +#if ( $table.templateType != 2 && $table.deleteBatch)
  44 + /**
  45 + * 批量删除${table.classComment}
  46 + *
  47 + * @param ids 编号
  48 + */
  49 + void delete${simpleClassName}ByIds(List<${primaryColumn.javaType}> ids);
  50 +#end
  51 +
43 52 /**
44 53 * 获得${table.classComment}
45 54 *
... ...
yudao-module-infra/src/main/resources/codegen/java/service/serviceImpl.vm
1 1 package ${basePackage}.module.${table.moduleName}.service.${table.businessName};
2 2  
  3 +import cn.hutool.core.collection.CollUtil;
3 4 import org.springframework.stereotype.Service;
4 5 import ${jakartaPackage}.annotation.Resource;
5 6 import org.springframework.validation.annotation.Validated;
... ... @@ -154,6 +155,39 @@ public class ${table.className}ServiceImpl implements ${table.className}Service
154 155 #end
155 156 }
156 157  
  158 +#if ( $table.templateType != 2 && $table.deleteBatch)
  159 + @Override
  160 + ## 特殊:主子表专属逻辑
  161 + #if ( $subTables && $subTables.size() > 0)
  162 + @Transactional(rollbackFor = Exception.class)
  163 + #end
  164 + public void delete${simpleClassName}ByIds(List<${primaryColumn.javaType}> ids) {
  165 + // 校验存在
  166 + validate${simpleClassName}Exists(ids);
  167 + // 删除
  168 + ${classNameVar}Mapper.deleteByIds(ids);
  169 + ## 特殊:主子表专属逻辑
  170 + #if ( $subTables && $subTables.size() > 0)
  171 +
  172 + // 删除子表
  173 + #foreach ($subTable in $subTables)
  174 + #set ($index = $foreach.count - 1)
  175 + #set ($subSimpleClassName = $subSimpleClassNames.get($index))
  176 + #set ($subJoinColumn = $subJoinColumns.get($index))##当前 join 字段
  177 + #set ($SubJoinColumnName = $subJoinColumn.javaField.substring(0,1).toUpperCase() + ${subJoinColumn.javaField.substring(1)})##首字母大写
  178 + delete${subSimpleClassName}By${SubJoinColumnName}s(ids);
  179 + #end
  180 + #end
  181 + }
  182 +
  183 + private void validate${simpleClassName}Exists(List<${primaryColumn.javaType}> ids) {
  184 + List<${table.className}DO> list = ${classNameVar}Mapper.selectByIds(ids);
  185 + if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
  186 + throw exception(${simpleClassName_underlineCase.toUpperCase()}_NOT_EXISTS);
  187 + }
  188 + }
  189 +#end
  190 +
157 191 private void validate${simpleClassName}Exists(${primaryColumn.javaType} id) {
158 192 if (${classNameVar}Mapper.selectById(id) == null) {
159 193 throw exception(${simpleClassName_underlineCase.toUpperCase()}_NOT_EXISTS);
... ... @@ -347,5 +381,11 @@ public class ${table.className}ServiceImpl implements ${table.className}Service
347 381 ${subClassNameVars.get($index)}Mapper.deleteBy${SubJoinColumnName}(${subJoinColumn.javaField});
348 382 }
349 383  
  384 +#if ( $table.templateType != 2 && $table.deleteBatch)
  385 + private void delete${subSimpleClassName}By${SubJoinColumnName}s(List<${primaryColumn.javaType}> ${subJoinColumn.javaField}s) {
  386 + ${subClassNameVars.get($index)}Mapper.deleteBy${SubJoinColumnName}s(${subJoinColumn.javaField}s);
  387 + }
  388 +#end
  389 +
350 390 #end
351 391 }
352 392 \ No newline at end of file
... ...