主子Contoller.java 7.8 KB

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