Controller.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. package ${package}.${moduleName}.controller;
  2. #if($queryList)
  3. import cn.hutool.core.util.StrUtil;
  4. #end
  5. import cn.hutool.core.util.ArrayUtil;
  6. import cn.hutool.core.collection.CollUtil;
  7. import cn.hutool.core.date.DateUtil;
  8. import java.time.LocalDate;
  9. import java.time.LocalDateTime;
  10. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  11. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  12. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  13. import ${package}.common.core.util.R;
  14. import ${package}.common.log.annotation.SysLog;
  15. #if($opensource)
  16. import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
  17. import com.pig4cloud.plugin.excel.annotation.RequestExcel;
  18. #else
  19. import ${package}.common.excel.annotation.ResponseExcel;
  20. import ${package}.common.excel.annotation.RequestExcel;
  21. #end
  22. import ${package}.${moduleName}.entity.${ClassName}Entity;
  23. import ${package}.${moduleName}.service.${ClassName}Service;
  24. import io.swagger.v3.oas.annotations.security.SecurityRequirement;
  25. #if($isSpringBoot3)
  26. import ${package}.common.security.annotation.HasPermission;
  27. import org.springdoc.core.annotations.ParameterObject;
  28. #else
  29. import org.springframework.security.access.prepost.PreAuthorize;
  30. import org.springdoc.api.annotations.ParameterObject;
  31. #end
  32. import org.springframework.http.HttpHeaders;
  33. import io.swagger.v3.oas.annotations.tags.Tag;
  34. import io.swagger.v3.oas.annotations.Operation;
  35. import lombok.RequiredArgsConstructor;
  36. import org.springframework.validation.BindingResult;
  37. import org.springframework.web.bind.annotation.*;
  38. import java.util.List;
  39. import java.util.Objects;
  40. /**
  41. * ${tableComment}
  42. *
  43. * @author ${author}
  44. * @date ${datetime}
  45. */
  46. @RestController
  47. @RequiredArgsConstructor
  48. @RequestMapping("/${functionName}" )
  49. @Tag(description = "${functionName}" , name = "${tableComment}管理" )
  50. @SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
  51. public class ${ClassName}Controller {
  52. private final ${ClassName}Service ${className}Service;
  53. /**
  54. * 分页查询
  55. * @param page 分页对象
  56. * @param ${className} ${tableComment}
  57. * @return
  58. */
  59. @Operation(summary = "分页查询" , description = "分页查询" )
  60. @GetMapping("/page" )
  61. #if($isSpringBoot3)
  62. @HasPermission("${moduleName}_${functionName}_view")
  63. #else
  64. @PreAuthorize("@pms.hasPermission('${moduleName}_${functionName}_view')" )
  65. #end
  66. public R get${ClassName}Page(@ParameterObject Page page, @ParameterObject ${ClassName}Entity ${className}) {
  67. LambdaQueryWrapper<${ClassName}Entity> wrapper = Wrappers.lambdaQuery();
  68. #foreach ($field in $queryList)
  69. #set($getAttrName=$str.getProperty($field.attrName))
  70. #set($var="${className}.$getAttrName()")
  71. #if($field.attrType == 'String')
  72. #set($expression="StrUtil.isNotBlank")
  73. #else
  74. #set($expression="Objects.nonNull")
  75. #end
  76. #if($field.queryFormType == 'date-range')
  77. // ${field.fieldComment}范围查询
  78. if (ArrayUtil.isNotEmpty(${className}.get${field.attrName.substring(0,1).toUpperCase()}${field.attrName.substring(1)}Range())) {
  79. String[] range = ${className}.get${field.attrName.substring(0,1).toUpperCase()}${field.attrName.substring(1)}Range();
  80. if (StrUtil.isNotBlank(range[0])) {
  81. wrapper.ge(${ClassName}Entity::$getAttrName, LocalDate.parse(range[0]));
  82. }
  83. if (StrUtil.isNotBlank(range[1])) {
  84. wrapper.le(${ClassName}Entity::$getAttrName, LocalDate.parse(range[1]));
  85. }
  86. }
  87. #elseif($field.queryFormType == 'datetime-range')
  88. // ${field.fieldComment}范围查询
  89. if (ArrayUtil.isNotEmpty(${className}.get${field.attrName.substring(0,1).toUpperCase()}${field.attrName.substring(1)}Range())) {
  90. String[] range = ${className}.get${field.attrName.substring(0,1).toUpperCase()}${field.attrName.substring(1)}Range();
  91. if (StrUtil.isNotBlank(range[0])) {
  92. wrapper.ge(${ClassName}Entity::$getAttrName, LocalDateTime.parse(range[0]));
  93. }
  94. if (StrUtil.isNotBlank(range[1])) {
  95. wrapper.le(${ClassName}Entity::$getAttrName, LocalDateTime.parse(range[1]));
  96. }
  97. }
  98. #elseif($field.queryType == '=')
  99. wrapper.eq($expression($var),${ClassName}Entity::$getAttrName,$var);
  100. #elseif( $field.queryType == 'like' )
  101. wrapper.like($expression($var),${ClassName}Entity::$getAttrName,$var);
  102. #elseif( $field.queryType == '!-' )
  103. wrapper.ne($expression($var),${ClassName}Entity::$getAttrName,$var);
  104. #elseif( $field.queryType == '>' )
  105. wrapper.gt($expression($var),${ClassName}Entity::$getAttrName,$var);
  106. #elseif( $field.queryType == '<' )
  107. wrapper.lt($expression($var),${ClassName}Entity::$getAttrName,$var);
  108. #elseif( $field.queryType == '>=' )
  109. wrapper.ge($expression($var),${ClassName}Entity::$getAttrName,$var);
  110. #elseif( $field.queryType == '<=' )
  111. wrapper.le($expression($var),${ClassName}Entity::$getAttrName,$var);
  112. #elseif( $field.queryType == 'left like' )
  113. wrapper.likeLeft($expression($var),${ClassName}Entity::$getAttrName,$var);
  114. #elseif( $field.queryType == 'right like' )
  115. wrapper.likeRight($expression($var),${ClassName}Entity::$getAttrName,$var);
  116. #end
  117. #end
  118. return R.ok(${className}Service.page(page, wrapper));
  119. }
  120. /**
  121. * 通过条件查询${tableComment}
  122. * @param ${className} 查询条件
  123. * @return R 对象列表
  124. */
  125. @Operation(summary = "通过条件查询" , description = "通过条件查询对象" )
  126. @GetMapping("/details" )
  127. #if($isSpringBoot3)
  128. @HasPermission("${moduleName}_${functionName}_view")
  129. #else
  130. @PreAuthorize("@pms.hasPermission('${moduleName}_${functionName}_view')" )
  131. #end
  132. public R getDetails(@ParameterObject ${ClassName}Entity ${className}) {
  133. return R.ok(${className}Service.list(Wrappers.query(${className})));
  134. }
  135. /**
  136. * 新增${tableComment}
  137. * @param ${className} ${tableComment}
  138. * @return R
  139. */
  140. @Operation(summary = "新增${tableComment}" , description = "新增${tableComment}" )
  141. @SysLog("新增${tableComment}" )
  142. @PostMapping
  143. #if($isSpringBoot3)
  144. @HasPermission("${moduleName}_${functionName}_add")
  145. #else
  146. @PreAuthorize("@pms.hasPermission('${moduleName}_${functionName}_add')" )
  147. #end
  148. public R save(@RequestBody ${ClassName}Entity ${className}) {
  149. return R.ok(${className}Service.save(${className}));
  150. }
  151. /**
  152. * 修改${tableComment}
  153. * @param ${className} ${tableComment}
  154. * @return R
  155. */
  156. @Operation(summary = "修改${tableComment}" , description = "修改${tableComment}" )
  157. @SysLog("修改${tableComment}" )
  158. @PutMapping
  159. #if($isSpringBoot3)
  160. @HasPermission("${moduleName}_${functionName}_edit")
  161. #else
  162. @PreAuthorize("@pms.hasPermission('${moduleName}_${functionName}_edit')" )
  163. #end
  164. public R updateById(@RequestBody ${ClassName}Entity ${className}) {
  165. return R.ok(${className}Service.updateById(${className}));
  166. }
  167. /**
  168. * 通过id删除${tableComment}
  169. * @param ids ${pk.attrName}列表
  170. * @return R
  171. */
  172. @Operation(summary = "通过id删除${tableComment}" , description = "通过id删除${tableComment}" )
  173. @SysLog("通过id删除${tableComment}" )
  174. @DeleteMapping
  175. #if($isSpringBoot3)
  176. @HasPermission("${moduleName}_${functionName}_del")
  177. #else
  178. @PreAuthorize("@pms.hasPermission('${moduleName}_${functionName}_del')" )
  179. #end
  180. public R removeById(@RequestBody ${pk.attrType}[] ids) {
  181. return R.ok(${className}Service.removeBatchByIds(CollUtil.toList(ids)));
  182. }
  183. /**
  184. * 导出excel 表格
  185. * @param ${className} 查询条件
  186. * @param ids 导出指定ID
  187. * @return excel 文件流
  188. */
  189. @ResponseExcel
  190. @GetMapping("/export")
  191. #if($isSpringBoot3)
  192. @HasPermission("${moduleName}_${functionName}_export")
  193. #else
  194. @PreAuthorize("@pms.hasPermission('${moduleName}_${functionName}_export')" )
  195. #end
  196. public List<${ClassName}Entity> exportExcel(${ClassName}Entity ${className},${pk.attrType}[] ids) {
  197. return ${className}Service.list(Wrappers.lambdaQuery(${className}).in(ArrayUtil.isNotEmpty(ids), ${ClassName}Entity::$str.getProperty($pk.attrName), ids));
  198. }
  199. /**
  200. * 导入excel 表
  201. * @param ${className}List 对象实体列表
  202. * @param bindingResult 错误信息列表
  203. * @return ok fail
  204. */
  205. @PostMapping("/import")
  206. #if($isSpringBoot3)
  207. @HasPermission("${moduleName}_${functionName}_export")
  208. #else
  209. @PreAuthorize("@pms.hasPermission('${moduleName}_${functionName}_export')" )
  210. #end
  211. public R importExcel(@RequestExcel List<${ClassName}Entity> ${className}List, BindingResult bindingResult) {
  212. return R.ok(${className}Service.saveBatch(${className}List));
  213. }
  214. }