Controller.java.template 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package org.dromara.${module}.${submodule}.controller;
  2. import cn.dev33.satoken.annotation.SaCheckPermission;
  3. import jakarta.servlet.http.HttpServletResponse;
  4. import jakarta.validation.constraints.NotEmpty;
  5. import jakarta.validation.constraints.NotNull;
  6. import lombok.RequiredArgsConstructor;
  7. import org.dromara.${module}.${submodule}.domain.bo.${ClassName}Bo;
  8. import org.dromara.${module}.${submodule}.domain.vo.${ClassName}Vo;
  9. import org.dromara.${module}.${submodule}.listener.${ClassName}ImportListener;
  10. import org.dromara.${module}.${submodule}.service.I${ClassName}Service;
  11. import org.dromara.common.core.domain.R;
  12. import org.dromara.common.core.validate.AddGroup;
  13. import org.dromara.common.core.validate.EditGroup;
  14. import org.dromara.common.excel.core.ExcelResult;
  15. import org.dromara.common.excel.utils.ExcelUtil;
  16. import org.dromara.common.idempotent.annotation.RepeatSubmit;
  17. import org.dromara.common.log.annotation.Log;
  18. import org.dromara.common.log.enums.BusinessType;
  19. import org.dromara.common.mybatis.core.page.PageQuery;
  20. import org.dromara.common.mybatis.core.page.TableDataInfo;
  21. import org.dromara.common.web.core.BaseController;
  22. import org.springframework.http.MediaType;
  23. import org.springframework.validation.annotation.Validated;
  24. import org.springframework.web.bind.annotation.*;
  25. import org.springframework.web.multipart.MultipartFile;
  26. import java.util.ArrayList;
  27. import java.util.List;
  28. /**
  29. * ${tableComment}
  30. * 前端访问路由地址为:/${submodule}/${businessName}
  31. *
  32. * @author ${author}
  33. * @date ${date}
  34. */
  35. @Validated
  36. @RequiredArgsConstructor
  37. @RestController
  38. @RequestMapping("/${submodule}/${businessName}")
  39. public class ${ClassName}Controller extends BaseController {
  40. private final I${ClassName}Service ${className}Service;
  41. /**
  42. * 查询${tableComment}列表
  43. */
  44. @SaCheckPermission("${submodule}:${businessName}:list")
  45. @GetMapping("/list")
  46. public TableDataInfo<${ClassName}Vo> list(${ClassName}Bo bo, PageQuery pageQuery) {
  47. return ${className}Service.queryPageList(bo, pageQuery);
  48. }
  49. /**
  50. * 导出${tableComment}列表
  51. */
  52. @SaCheckPermission("${submodule}:${businessName}:export")
  53. @Log(title = "${tableComment}", businessType = BusinessType.EXPORT)
  54. @PostMapping("/export")
  55. public void export(${ClassName}Bo bo, HttpServletResponse response) {
  56. List<${ClassName}Vo> list = ${className}Service.queryList(bo);
  57. ExcelUtil.exportExcel(list, "${tableComment}", ${ClassName}Vo.class, response);
  58. }
  59. /**
  60. * 获取${tableComment}详细信息
  61. *
  62. * @param ${pkField} 主键
  63. */
  64. @SaCheckPermission("${submodule}:${businessName}:query")
  65. @GetMapping("/{${pkField}}")
  66. public R<${ClassName}Vo> getInfo(@NotNull(message = "主键不能为空")
  67. @PathVariable ${pkType} ${pkField}) {
  68. return R.ok(${className}Service.queryById(${pkField}));
  69. }
  70. /**
  71. * 新增${tableComment}
  72. */
  73. @SaCheckPermission("${submodule}:${businessName}:add")
  74. @Log(title = "${tableComment}", businessType = BusinessType.INSERT)
  75. @RepeatSubmit()
  76. @PostMapping()
  77. public R<Void> add(@Validated(AddGroup.class) @RequestBody ${ClassName}Bo bo) {
  78. return toAjax(${className}Service.insertByBo(bo));
  79. }
  80. /**
  81. * 修改${tableComment}
  82. */
  83. @SaCheckPermission("${submodule}:${businessName}:edit")
  84. @Log(title = "${tableComment}", businessType = BusinessType.UPDATE)
  85. @RepeatSubmit()
  86. @PutMapping()
  87. public R<Void> edit(@Validated(EditGroup.class) @RequestBody ${ClassName}Bo bo) {
  88. return toAjax(${className}Service.updateByBo(bo));
  89. }
  90. /**
  91. * 删除${tableComment}
  92. *
  93. * @param ${pkField}s 主键串
  94. */
  95. @SaCheckPermission("${submodule}:${businessName}:remove")
  96. @Log(title = "${tableComment}", businessType = BusinessType.DELETE)
  97. @DeleteMapping("/{${pkField}s}")
  98. public R<Void> remove(@NotEmpty(message = "主键不能为空")
  99. @PathVariable ${pkType}[] ${pkField}s) {
  100. return toAjax(${className}Service.deleteWithValidByIds(List.of(${pkField}s), true));
  101. }
  102. /**
  103. * 导出${tableComment}导入模板
  104. */
  105. @SaCheckPermission("${submodule}:${businessName}:export")
  106. @Log(title = "${tableComment}导入模板", businessType = BusinessType.EXPORT)
  107. @PostMapping("/exportTemplate")
  108. public void exportTemplate(HttpServletResponse response) {
  109. List<${ClassName}ImportVo> list = new ArrayList<>();
  110. ExcelUtil.exportExcel(list, "${tableComment}导入模板", ${ClassName}ImportVo.class, response);
  111. }
  112. /**
  113. * 导入${tableComment}数据
  114. *
  115. * @param file Excel文件
  116. * @param updateSupport 是否更新已存在数据
  117. */
  118. @SaCheckPermission("${submodule}:${businessName}:import")
  119. @Log(title = "${tableComment}导入", businessType = BusinessType.IMPORT)
  120. @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  121. public R<Void> importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception {
  122. ExcelResult<${ClassName}ImportVo> result = ExcelUtil.importExcel(
  123. file.getInputStream(),
  124. ${ClassName}ImportVo.class,
  125. new ${ClassName}ImportListener(updateSupport)
  126. );
  127. return R.ok(result.getAnalysis());
  128. }
  129. }