主子Contoller.java 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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.queryFormType == 'daterange' || $field.queryFormType == 'datetimerange')
  74. wrapper.between(ArrayUtil.isNotEmpty(${className}.${getAttrName}Range()),${ClassName}Entity::$getAttrName,${className}.${getAttrName}Range()[0],${className}.${getAttrName}Range()[1]);
  75. #elseif($field.queryType == '=')
  76. wrapper.eq($expression($var),${ClassName}Entity::$getAttrName,$var);
  77. #elseif( $field.queryType == 'like' )
  78. wrapper.like($expression($var),${ClassName}Entity::$getAttrName,$var);
  79. #elseif( $field.queryType == '!-' )
  80. wrapper.ne($expression($var),${ClassName}Entity::$getAttrName,$var);
  81. #elseif( $field.queryType == '>' )
  82. wrapper.gt($expression($var),${ClassName}Entity::$getAttrName,$var);
  83. #elseif( $field.queryType == '<' )
  84. wrapper.lt($expression($var),${ClassName}Entity::$getAttrName,$var);
  85. #elseif( $field.queryType == '>=' )
  86. wrapper.ge($expression($var),${ClassName}Entity::$getAttrName,$var);
  87. #elseif( $field.queryType == '<=' )
  88. wrapper.le($expression($var),${ClassName}Entity::$getAttrName,$var);
  89. #elseif( $field.queryType == 'left like' )
  90. wrapper.likeLeft($expression($var),${ClassName}Entity::$getAttrName,$var);
  91. #elseif( $field.queryType == 'right like' )
  92. wrapper.likeRight($expression($var),${ClassName}Entity::$getAttrName,$var);
  93. #end
  94. #end
  95. return R.ok(${className}Service.page(page, wrapper));
  96. }
  97. /**
  98. * 通过条件查询${tableComment}
  99. * @param ${className} 查询条件
  100. * @return R 对象列表
  101. */
  102. @Operation(summary = "通过条件查询" , description = "通过条件查询对象" )
  103. @GetMapping("/details" )
  104. #if($isSpringBoot3)
  105. @HasPermission("${moduleName}_${functionName}_view")
  106. #else
  107. @PreAuthorize("@pms.hasPermission('${moduleName}_${functionName}_view')" )
  108. #end
  109. public R getDetails(@ParameterObject ${ClassName}Entity ${className}) {
  110. return R.ok(${className}Service.listDeep(Wrappers.query(${className})));
  111. }
  112. /**
  113. * 新增${tableComment}
  114. * @param ${className} ${tableComment}
  115. * @return R
  116. */
  117. @Operation(summary = "新增${tableComment}" , description = "新增${tableComment}" )
  118. @SysLog("新增${tableComment}" )
  119. @PostMapping
  120. #if($isSpringBoot3)
  121. @HasPermission("${moduleName}_${functionName}_add")
  122. #else
  123. @PreAuthorize("@pms.hasPermission('${moduleName}_${functionName}_add')" )
  124. #end
  125. public R save(@RequestBody ${ClassName}Entity ${className}) {
  126. return R.ok(${className}Service.saveDeep(${className}));
  127. }
  128. /**
  129. * 修改${tableComment}
  130. * @param ${className} ${tableComment}
  131. * @return R
  132. */
  133. @Operation(summary = "修改${tableComment}" , description = "修改${tableComment}" )
  134. @SysLog("修改${tableComment}" )
  135. @PutMapping
  136. #if($isSpringBoot3)
  137. @HasPermission("${moduleName}_${functionName}_edit")
  138. #else
  139. @PreAuthorize("@pms.hasPermission('${moduleName}_${functionName}_edit')" )
  140. #end
  141. public R updateById(@RequestBody ${ClassName}Entity ${className}) {
  142. return R.ok(${className}Service.updateDeep(${className}));
  143. }
  144. /**
  145. * 通过id删除${tableComment}
  146. * @param ids ${pk.attrName}列表
  147. * @return R
  148. */
  149. @Operation(summary = "通过id删除${tableComment}" , description = "通过id删除${tableComment}" )
  150. @SysLog("通过id删除${tableComment}" )
  151. @DeleteMapping
  152. #if($isSpringBoot3)
  153. @HasPermission("${moduleName}_${functionName}_del")
  154. #else
  155. @PreAuthorize("@pms.hasPermission('${moduleName}_${functionName}_del')" )
  156. #end
  157. public R removeById(@RequestBody ${pk.attrType}[] ids) {
  158. return R.ok(${className}Service.removeDeep(ids));
  159. }
  160. /**
  161. * 通过id删除${tableComment}子表数据
  162. * @param ids ${pk.attrName}列表
  163. * @return R
  164. */
  165. @Operation(summary = "通过id删除${tableComment}子表数据" , description = "通过id删除${tableComment}子表数据" )
  166. @SysLog("通过id删除${tableComment}子表数据" )
  167. @DeleteMapping("/child")
  168. #if($isSpringBoot3)
  169. @HasPermission("${moduleName}_${functionName}_del")
  170. #else
  171. @PreAuthorize("@pms.hasPermission('${moduleName}_${functionName}_del')" )
  172. #end
  173. public R removeChild(@RequestBody ${pk.attrType}[] ids) {
  174. return R.ok(${className}Service.removeChild(ids));
  175. }
  176. /**
  177. * 导出excel 表格
  178. * @param ${className} 查询条件
  179. * @param ids 导出指定ID
  180. * @return excel 文件流
  181. */
  182. @ResponseExcel
  183. @GetMapping("/export")
  184. #if($isSpringBoot3)
  185. @HasPermission("${moduleName}_${functionName}_export")
  186. #else
  187. @PreAuthorize("@pms.hasPermission('${moduleName}_${functionName}_export')" )
  188. #end
  189. public List<${ClassName}Entity> exportExcel(${ClassName}Entity ${className},${pk.attrType}[] ids) {
  190. return ${className}Service.list(Wrappers.lambdaQuery(${className}).in(ArrayUtil.isNotEmpty(ids), ${ClassName}Entity::$str.getProperty($pk.attrName), ids));
  191. }
  192. /**
  193. * 导入excel 表
  194. * @param ${className}List 对象实体列表
  195. * @param bindingResult 错误信息列表
  196. * @return ok fail
  197. */
  198. @PostMapping("/import")
  199. #if($isSpringBoot3)
  200. @HasPermission("${moduleName}_${functionName}_export")
  201. #else
  202. @PreAuthorize("@pms.hasPermission('${moduleName}_${functionName}_export')" )
  203. #end
  204. public R importExcel(@RequestExcel List<${ClassName}Entity> ${className}List, BindingResult bindingResult) {
  205. return R.ok(${className}Service.saveBatch(${className}List));
  206. }
  207. }