Controller.java 7.8 KB

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