Controller.java 8.7 KB

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