树形Controller.java 7.1 KB

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