controller.java.ftl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. package ${package};
  2. import cn.hutool.core.bean.BeanUtil;
  3. import cn.hutool.core.util.ObjectUtil;
  4. import cn.hutool.core.util.StrUtil;
  5. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  6. import com.baomidou.mybatisplus.core.metadata.IPage;
  7. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  8. import com.xjrsoft.common.constant.GlobalConstant;
  9. import com.baomidou.mybatisplus.core.toolkit.StringPool;
  10. import com.xjrsoft.common.page.ConventPage;
  11. import com.xjrsoft.common.page.PageOutput;
  12. import com.xjrsoft.common.model.result.R;
  13. import com.xjrsoft.common.utils.VoToColumnUtil;
  14. import com.xjrsoft.module.${outputArea}.dto.Add${entityClass}Dto;
  15. import com.xjrsoft.module.${outputArea}.dto.Update${entityClass}Dto;
  16. import cn.dev33.satoken.annotation.SaCheckPermission;
  17. <#if codeRules != "">
  18. import com.xjrsoft.module.system.service.ICodeRuleService;
  19. </#if>
  20. <#if isImport || isExport>
  21. import com.alibaba.excel.EasyExcel;
  22. </#if>
  23. <#if isImport>
  24. import org.springframework.web.multipart.MultipartFile;
  25. import java.io.IOException;
  26. </#if>
  27. <#if isExport>
  28. import com.alibaba.excel.support.ExcelTypeEnum;
  29. import org.springframework.http.ResponseEntity;
  30. import java.io.ByteArrayOutputStream;
  31. import java.util.ArrayList;
  32. </#if>
  33. <#if !isPage>
  34. import com.xjrsoft.module.${outputArea}.dto.${entityClass}ListDto;
  35. <#else>
  36. import com.xjrsoft.module.${outputArea}.dto.${entityClass}PageDto;
  37. </#if>
  38. import com.xjrsoft.module.${outputArea}.entity.${entityClass};
  39. import com.xjrsoft.module.${outputArea}.service.I${className}Service;
  40. <#if !isPage>
  41. import com.xjrsoft.module.${outputArea}.vo.${entityClass}ListVo;
  42. <#else>
  43. import com.xjrsoft.module.${outputArea}.vo.${entityClass}PageVo;
  44. </#if>
  45. <#if isDataAuth>
  46. import com.xjrsoft.module.authority.dto.BatchSetDataAuthDto;
  47. import java.util.stream.Collectors;
  48. import java.util.ArrayList;
  49. </#if>
  50. import com.xjrsoft.module.${outputArea}.vo.${entityClass}Vo;
  51. import io.swagger.annotations.Api;
  52. import io.swagger.annotations.ApiOperation;
  53. import lombok.AllArgsConstructor;
  54. import org.springframework.web.bind.annotation.*;
  55. import javax.validation.Valid;
  56. import javax.validation.constraints.NotNull;
  57. import java.util.List;
  58. /**
  59. * @title: ${tableComment}
  60. * @Author ${author}
  61. * @Date: ${date}
  62. * @Version 1.0
  63. */
  64. @RestController
  65. @RequestMapping("/${outputArea}" + "/${className?lower_case}")
  66. @Api(value = "/${outputArea}" + "/${className?lower_case}",tags = "${tableComment}代码")
  67. @AllArgsConstructor
  68. public class ${className}Controller {
  69. private final I${className}Service ${className?uncap_first}Service;
  70. <#if codeRules != "">
  71. private final ICodeRuleService codeRuleService;
  72. </#if>
  73. <#if !isPage>
  74. @GetMapping(value = "/list")
  75. @ApiOperation(value="${entityClass}列表(不分页)")
  76. @SaCheckPermission("${className?lower_case}:detail")
  77. public R list(@Valid ${entityClass}ListDto dto){
  78. LambdaQueryWrapper<${entityClass}> queryWrapper = new LambdaQueryWrapper<>();
  79. queryWrapper
  80. <#--属性遍历-->
  81. <#list fields as field>
  82. <#if field.fieldType == "String">
  83. .like(StrUtil.isNotBlank(dto.get${field.fieldName?cap_first}()),${entityClass}::get${field.fieldName?cap_first},dto.get${field.fieldName?cap_first}())
  84. <#elseif field.fieldType == "Integer" || field.fieldType == "Long"|| field.fieldType == "Double"|| field.fieldType == "Float"|| field.fieldType == "Boolean"|| field.fieldType == "BigDecimal" >
  85. .eq(ObjectUtil.isNotNull(dto.get${field.fieldName?cap_first}()),${entityClass}::get${field.fieldName?cap_first},dto.get${field.fieldName?cap_first}())
  86. <#elseif field.fieldType == "LocalDateTime" || field.fieldType == "LocalTime">
  87. .between(ObjectUtil.isNotNull(dto.get${field.fieldName?cap_first}Start()) && ObjectUtil.isNotNull(dto.get${field.fieldName?cap_first}End()),${entityClass}::get${field.fieldName?cap_first},dto.get${field.fieldName?cap_first}Start(),dto.get${field.fieldName?cap_first}End())
  88. <#else>
  89. .like(StrUtil.isNotBlank(dto.get${field.fieldName?cap_first}()),${entityClass}::get${field.fieldName?cap_first},dto.get${field.fieldName?cap_first}())
  90. </#if>
  91. </#list>
  92. <#--默认排序-->
  93. <#if orderField?? && orderField != "">
  94. <#if orderType>
  95. .orderByDesc(${entityClass}::get${orderField?cap_first})
  96. <#else>
  97. .orderByAsc(${entityClass}::get${orderField?cap_first})
  98. </#if>
  99. <#-- 如果需要根据前端所传字段排序 屏蔽上面 放开下面-->
  100. <#-- .orderBy(StrUtil.isBlank(dto.getOrderField()),${orderType?string("false","true")},${entityClass}::get${orderField?cap_first})-->
  101. <#-- .last(StrUtil.isNotBlank(dto.getOrderField()) && StrUtil.isNotBlank(dto.getOrder()),"order by " + dto.getOrderField() + StringPool.SPACE + dto.getOrder())-->
  102. </#if>
  103. .select(${entityClass}.class,x -> VoToColumnUtil.fieldsToColumns(${entityClass}ListVo.class).contains(x.getProperty()));
  104. List<${entityClass}> list = ${className?uncap_first}Service.list(queryWrapper);
  105. List<${entityClass}ListVo> listVos = BeanUtil.copyToList(list, ${entityClass}ListVo.class);
  106. return R.ok(listVos);
  107. }
  108. <#else>
  109. @GetMapping(value = "/page")
  110. @ApiOperation(value="${entityClass}列表(分页)")
  111. @SaCheckPermission("${className?lower_case}:detail")
  112. public R page(@Valid ${entityClass}PageDto dto){
  113. LambdaQueryWrapper<${entityClass}> queryWrapper = new LambdaQueryWrapper<>();
  114. queryWrapper
  115. <#--属性遍历-->
  116. <#list fields as field>
  117. <#if field.fieldType == "String">
  118. .like(StrUtil.isNotBlank(dto.get${field.fieldName?cap_first}()),${entityClass}::get${field.fieldName?cap_first},dto.get${field.fieldName?cap_first}())
  119. <#elseif field.fieldType == "Integer" || field.fieldType == "Long"|| field.fieldType == "Double"|| field.fieldType == "Float"|| field.fieldType == "Boolean" || field.fieldType == "BigDecimal" >
  120. .eq(ObjectUtil.isNotNull(dto.get${field.fieldName?cap_first}()),${entityClass}::get${field.fieldName?cap_first},dto.get${field.fieldName?cap_first}())
  121. <#elseif field.fieldType == "LocalDateTime" || field.fieldType == "LocalTime">
  122. .between(ObjectUtil.isNotNull(dto.get${field.fieldName?cap_first}Start()) && ObjectUtil.isNotNull(dto.get${field.fieldName?cap_first}End()),${entityClass}::get${field.fieldName?cap_first},dto.get${field.fieldName?cap_first}Start(),dto.get${field.fieldName?cap_first}End())
  123. <#else>
  124. .like(StrUtil.isNotBlank(dto.get${field.fieldName?cap_first}()),${entityClass}::get${field.fieldName?cap_first},dto.get${field.fieldName?cap_first}())
  125. </#if>
  126. </#list>
  127. <#--默认排序-->
  128. <#if orderField?? && orderField != "">
  129. <#if orderType>
  130. .orderByDesc(${entityClass}::get${orderField?cap_first})
  131. <#else>
  132. .orderByAsc(${entityClass}::get${orderField?cap_first})
  133. </#if>
  134. <#-- .orderBy(StrUtil.isBlank(dto.getOrderField()),${orderType?string("false","true")},${entityClass}::get${orderField?cap_first})-->
  135. </#if>
  136. .select(${entityClass}.class,x -> VoToColumnUtil.fieldsToColumns(${entityClass}PageVo.class).contains(x.getProperty()));
  137. IPage<${entityClass}> page = ${className?uncap_first}Service.page(ConventPage.getPage(dto), queryWrapper);
  138. PageOutput<${entityClass}PageVo> pageOutput = ConventPage.getPageOutput(page, ${entityClass}PageVo.class);
  139. return R.ok(pageOutput);
  140. }
  141. </#if>
  142. @GetMapping(value = "/info")
  143. @ApiOperation(value="根据id查询${entityClass}信息")
  144. @SaCheckPermission("${className?lower_case}:detail")
  145. public R info(@RequestParam ${pkType} id){
  146. ${entityClass} ${entityClass?uncap_first} = ${className?uncap_first}Service.${isMulti?string("getByIdDeep","getById")}(id);
  147. if (${entityClass?uncap_first} == null) {
  148. return R.error("找不到此数据!");
  149. }
  150. return R.ok(BeanUtil.toBean(${entityClass?uncap_first}, ${entityClass}Vo.class));
  151. }
  152. @PostMapping
  153. @ApiOperation(value = "新增${entityClass}")
  154. @SaCheckPermission("${className?lower_case}:add")
  155. public R add(@Valid @RequestBody Add${entityClass}Dto dto){
  156. <#if isMulti>
  157. ${entityClass} ${entityClass?uncap_first} = BeanUtil.toBean(dto, ${entityClass}.class);
  158. boolean isSuccess = ${className?uncap_first}Service.add(${entityClass?uncap_first});
  159. <#else >
  160. ${entityClass} ${entityClass?uncap_first} = BeanUtil.toBean(dto, ${entityClass}.class);
  161. boolean isSuccess = ${className?uncap_first}Service.save(${entityClass?uncap_first});
  162. </#if>
  163. <#if codeRules != "">
  164. codeRuleService.useEncode("${codeRules}");
  165. </#if>
  166. return R.ok(isSuccess);
  167. }
  168. @PutMapping
  169. @ApiOperation(value = "修改${entityClass}")
  170. @SaCheckPermission("${className?lower_case}:edit")
  171. public R update(@Valid @RequestBody Update${entityClass}Dto dto){
  172. <#if isMulti>
  173. ${entityClass} ${entityClass?uncap_first} = BeanUtil.toBean(dto, ${entityClass}.class);
  174. return R.ok(${className?uncap_first}Service.update(${entityClass?uncap_first}));
  175. <#else >
  176. ${entityClass} ${entityClass?uncap_first} = BeanUtil.toBean(dto, ${entityClass}.class);
  177. return R.ok(${className?uncap_first}Service.updateById(${entityClass?uncap_first}));
  178. </#if>
  179. }
  180. @DeleteMapping
  181. @ApiOperation(value = "删除")
  182. @SaCheckPermission("${className?lower_case}:delete")
  183. public R delete(@Valid @RequestBody List<${pkType}> ids){
  184. <#if isMulti>
  185. return R.ok(${className?uncap_first}Service.delete(ids));
  186. <#else >
  187. return R.ok(${className?uncap_first}Service.removeBatchByIds(ids));
  188. </#if>
  189. }
  190. <#if isPage>
  191. <#assign importClass = "${entityClass}Page">
  192. <#else>
  193. <#assign importClass = "${entityClass}List">
  194. </#if>
  195. <#if isImport>
  196. @PostMapping("/import")
  197. @ApiOperation(value = "导入")
  198. public R importData(@RequestParam MultipartFile file) throws IOException {
  199. List<${importClass}Vo> savedDataList = EasyExcel.read(file.getInputStream()).head(${importClass}Vo.class).sheet().doReadSync();
  200. ${className?uncap_first}Service.saveBatch(BeanUtil.copyToList(savedDataList, ${entityClass}.class));
  201. return R.ok();
  202. }
  203. </#if>
  204. <#if isExport>
  205. @GetMapping("/export")
  206. @ApiOperation(value = "导出")
  207. public ResponseEntity<byte[]> exportData(@Valid ${importClass}Dto dto, @RequestParam(defaultValue = "false") Boolean isTemplate) {
  208. <#if isPage>
  209. List<${importClass}Vo> customerList = isTemplate != null && isTemplate ? new ArrayList<>() : ((PageOutput<${importClass}Vo>) page(dto).getData()).getList();
  210. <#else>
  211. List<${importClass}Vo> customerList = (List<${importClass}Vo>) page(dto).getData());
  212. </#if>
  213. ByteArrayOutputStream bot = new ByteArrayOutputStream();
  214. EasyExcel.write(bot, ${importClass}Vo.class).automaticMergeHead(false).excelType(ExcelTypeEnum.XLSX).sheet().doWrite(customerList);
  215. return R.fileStream(bot.toByteArray(), "${className}" + ExcelTypeEnum.XLSX.getValue());
  216. }
  217. </#if>
  218. <#if isDataAuth>
  219. @PutMapping("/data-auth")
  220. @ApiOperation(value = "批量设置权限所属人")
  221. public R setDataAUth(@RequestBody BatchSetDataAuthDto dto){
  222. List<${entityClass}> toUpdateList = new ArrayList<>();
  223. <#-- String userIdStr = dto.getUserIdList().stream().collect(Collectors.joining(StringPool.COMMA));-->
  224. for (Long dataId : dto.getDataIdList()) {
  225. ${entityClass} ${entityClass?uncap_first} = new ${entityClass}();
  226. ${entityClass?uncap_first}.set${pkField?cap_first}(dataId);
  227. ${entityClass?uncap_first}.setRuleUserId(dto.getUserIdList().get(0));
  228. toUpdateList.add(${entityClass?uncap_first});
  229. }
  230. return R.ok(${className?uncap_first}Service.updateBatchById(toUpdateList));
  231. }
  232. </#if>
  233. }