d19f15fb4a7da3c74a31b06017fd2ea719abdffc.svn-base 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. package ${bussiPackage}.${entityPackage}.controller;
  2. import java.io.UnsupportedEncodingException;
  3. import java.io.IOException;
  4. import java.net.URLDecoder;
  5. import java.util.ArrayList;
  6. import java.util.Arrays;
  7. import java.util.List;
  8. import java.util.Map;
  9. import java.util.stream.Collectors;
  10. import javax.servlet.http.HttpServletRequest;
  11. import javax.servlet.http.HttpServletResponse;
  12. import org.jeecgframework.poi.excel.ExcelImportUtil;
  13. import org.jeecgframework.poi.excel.def.NormalExcelConstants;
  14. import org.jeecgframework.poi.excel.entity.ExportParams;
  15. import org.jeecgframework.poi.excel.entity.ImportParams;
  16. import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
  17. import org.jeecg.common.system.vo.LoginUser;
  18. import org.apache.shiro.SecurityUtils;
  19. import org.jeecg.common.api.vo.Result;
  20. import org.jeecg.common.system.query.QueryGenerator;
  21. import org.jeecg.common.util.oConvertUtils;
  22. <#list subTables as sub>
  23. import ${bussiPackage}.${entityPackage}.entity.${sub.entityName};
  24. </#list>
  25. import ${bussiPackage}.${entityPackage}.entity.${entityName};
  26. import ${bussiPackage}.${entityPackage}.vo.${entityName}Page;
  27. import ${bussiPackage}.${entityPackage}.service.I${entityName}Service;
  28. <#list subTables as sub>
  29. import ${bussiPackage}.${entityPackage}.service.I${sub.entityName}Service;
  30. </#list>
  31. import org.springframework.beans.BeanUtils;
  32. import org.springframework.beans.factory.annotation.Autowired;
  33. import org.springframework.web.bind.annotation.*;
  34. import org.springframework.web.servlet.ModelAndView;
  35. import org.springframework.web.multipart.MultipartFile;
  36. import org.springframework.web.multipart.MultipartHttpServletRequest;
  37. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  38. import com.baomidou.mybatisplus.core.metadata.IPage;
  39. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  40. import lombok.extern.slf4j.Slf4j;
  41. import com.alibaba.fastjson.JSON;
  42. import io.swagger.annotations.Api;
  43. import io.swagger.annotations.ApiOperation;
  44. import org.jeecg.common.aspect.annotation.AutoLog;
  45. /**
  46. * @Description: ${tableVo.ftlDescription}
  47. * @Author: jeecg-boot
  48. * @Date: ${.now?string["yyyy-MM-dd"]}
  49. * @Version: V1.0
  50. */
  51. @Api(tags="${tableVo.ftlDescription}")
  52. @RestController
  53. @RequestMapping("/${entityPackage}/${entityName?uncap_first}")
  54. @Slf4j
  55. public class ${entityName}Controller {
  56. @Autowired
  57. private I${entityName}Service ${entityName?uncap_first}Service;
  58. <#list subTables as sub>
  59. @Autowired
  60. private I${sub.entityName}Service ${sub.entityName?uncap_first}Service;
  61. </#list>
  62. /**
  63. * 分页列表查询
  64. *
  65. * @param ${entityName?uncap_first}
  66. * @param pageNo
  67. * @param pageSize
  68. * @param req
  69. * @return
  70. */
  71. @AutoLog(value = "${tableVo.ftlDescription}-分页列表查询")
  72. @ApiOperation(value="${tableVo.ftlDescription}-分页列表查询", notes="${tableVo.ftlDescription}-分页列表查询")
  73. @GetMapping(value = "/list")
  74. public Result<?> queryPageList(${entityName} ${entityName?uncap_first},
  75. @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
  76. @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
  77. HttpServletRequest req) {
  78. QueryWrapper<${entityName}> queryWrapper = QueryGenerator.initQueryWrapper(${entityName?uncap_first}, req.getParameterMap());
  79. Page<${entityName}> page = new Page<${entityName}>(pageNo, pageSize);
  80. IPage<${entityName}> pageList = ${entityName?uncap_first}Service.page(page, queryWrapper);
  81. return Result.OK(pageList);
  82. }
  83. /**
  84. * 添加
  85. *
  86. * @param ${entityName?uncap_first}Page
  87. * @return
  88. */
  89. @AutoLog(value = "${tableVo.ftlDescription}-添加")
  90. @ApiOperation(value="${tableVo.ftlDescription}-添加", notes="${tableVo.ftlDescription}-添加")
  91. @PostMapping(value = "/add")
  92. public Result<?> add(@RequestBody ${entityName}Page ${entityName?uncap_first}Page) {
  93. ${entityName} ${entityName?uncap_first} = new ${entityName}();
  94. BeanUtils.copyProperties(${entityName?uncap_first}Page, ${entityName?uncap_first});
  95. ${entityName?uncap_first}Service.saveMain(${entityName?uncap_first}, <#list subTables as sub>${entityName?uncap_first}Page.get${sub.entityName}List()<#if sub_has_next>,</#if></#list>);
  96. return Result.OK("添加成功!");
  97. }
  98. /**
  99. * 编辑
  100. *
  101. * @param ${entityName?uncap_first}Page
  102. * @return
  103. */
  104. @AutoLog(value = "${tableVo.ftlDescription}-编辑")
  105. @ApiOperation(value="${tableVo.ftlDescription}-编辑", notes="${tableVo.ftlDescription}-编辑")
  106. @PostMapping(value = "/edit")
  107. public Result<?> edit(@RequestBody ${entityName}Page ${entityName?uncap_first}Page) {
  108. ${entityName} ${entityName?uncap_first} = new ${entityName}();
  109. BeanUtils.copyProperties(${entityName?uncap_first}Page, ${entityName?uncap_first});
  110. ${entityName} ${entityName?uncap_first}Entity = ${entityName?uncap_first}Service.getById(${entityName?uncap_first}.getId());
  111. if(${entityName?uncap_first}Entity==null) {
  112. return Result.error("未找到对应数据");
  113. }
  114. ${entityName?uncap_first}Service.updateMain(${entityName?uncap_first}, <#list subTables as sub>${entityName?uncap_first}Page.get${sub.entityName}List()<#if sub_has_next>,</#if></#list>);
  115. return Result.OK("编辑成功!");
  116. }
  117. /**
  118. * 通过id删除
  119. *
  120. * @param id
  121. * @return
  122. */
  123. @AutoLog(value = "${tableVo.ftlDescription}-通过id删除")
  124. @ApiOperation(value="${tableVo.ftlDescription}-通过id删除", notes="${tableVo.ftlDescription}-通过id删除")
  125. @PostMapping(value = "/delete")
  126. public Result<?> delete(@RequestParam(name="id",required=true) String id) {
  127. ${entityName?uncap_first}Service.delMain(id);
  128. return Result.OK("删除成功!");
  129. }
  130. /**
  131. * 批量删除
  132. *
  133. * @param ids
  134. * @return
  135. */
  136. @AutoLog(value = "${tableVo.ftlDescription}-批量删除")
  137. @ApiOperation(value="${tableVo.ftlDescription}-批量删除", notes="${tableVo.ftlDescription}-批量删除")
  138. @PostMapping(value = "/deleteBatch")
  139. public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
  140. this.${entityName?uncap_first}Service.delBatchMain(Arrays.asList(ids.split(",")));
  141. return Result.OK("批量删除成功!");
  142. }
  143. /**
  144. * 通过id查询
  145. *
  146. * @param id
  147. * @return
  148. */
  149. @AutoLog(value = "${tableVo.ftlDescription}-通过id查询")
  150. @ApiOperation(value="${tableVo.ftlDescription}-通过id查询", notes="${tableVo.ftlDescription}-通过id查询")
  151. @GetMapping(value = "/queryById")
  152. public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
  153. ${entityName} ${entityName?uncap_first} = ${entityName?uncap_first}Service.getById(id);
  154. if(${entityName?uncap_first}==null) {
  155. return Result.error("未找到对应数据");
  156. }
  157. return Result.OK(${entityName?uncap_first});
  158. }
  159. <#list subTables as sub>
  160. /**
  161. * 通过id查询
  162. *
  163. * @param id
  164. * @return
  165. */
  166. @AutoLog(value = "${sub.ftlDescription}通过主表ID查询")
  167. @ApiOperation(value="${sub.ftlDescription}主表ID查询", notes="${sub.ftlDescription}-通主表ID查询")
  168. @GetMapping(value = "/query${sub.entityName}ByMainId")
  169. public Result<?> query${sub.entityName}ListByMainId(@RequestParam(name="id",required=true) String id) {
  170. List<${sub.entityName}> ${sub.entityName?uncap_first}List = ${sub.entityName?uncap_first}Service.selectByMainId(id);
  171. return Result.OK(${sub.entityName?uncap_first}List);
  172. }
  173. </#list>
  174. /**
  175. * 导出excel
  176. *
  177. * @param request
  178. * @param ${entityName?uncap_first}
  179. */
  180. @RequestMapping(value = "/exportXls")
  181. public ModelAndView exportXls(HttpServletRequest request, ${entityName} ${entityName?uncap_first}) {
  182. // Step.1 组装查询条件查询数据
  183. QueryWrapper<${entityName}> queryWrapper = QueryGenerator.initQueryWrapper(${entityName?uncap_first}, request.getParameterMap());
  184. LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
  185. //Step.2 获取导出数据
  186. List<${entityName}> queryList = ${entityName?uncap_first}Service.list(queryWrapper);
  187. // 过滤选中数据
  188. String selections = request.getParameter("selections");
  189. List<${entityName}> ${entityName?uncap_first}List = new ArrayList<${entityName}>();
  190. if(oConvertUtils.isEmpty(selections)) {
  191. ${entityName?uncap_first}List = queryList;
  192. }else {
  193. List<String> selectionList = Arrays.asList(selections.split(","));
  194. ${entityName?uncap_first}List = queryList.stream().filter(item -> selectionList.contains(item.getId())).collect(Collectors.toList());
  195. }
  196. // Step.3 组装pageList
  197. List<${entityName}Page> pageList = new ArrayList<${entityName}Page>();
  198. for (${entityName} main : ${entityName?uncap_first}List) {
  199. ${entityName}Page vo = new ${entityName}Page();
  200. BeanUtils.copyProperties(main, vo);
  201. <#list subTables as sub>
  202. List<${sub.entityName}> ${sub.entityName?uncap_first}List = ${sub.entityName?uncap_first}Service.selectByMainId(main.getId());
  203. vo.set${sub.entityName}List(${sub.entityName?uncap_first}List);
  204. </#list>
  205. pageList.add(vo);
  206. }
  207. // Step.4 AutoPoi 导出Excel
  208. ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
  209. mv.addObject(NormalExcelConstants.FILE_NAME, "${tableVo.ftlDescription}列表");
  210. mv.addObject(NormalExcelConstants.CLASS, ${entityName}Page.class);
  211. mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("${tableVo.ftlDescription}数据", "导出人:"+sysUser.getRealname(), "${tableVo.ftlDescription}"));
  212. mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
  213. return mv;
  214. }
  215. /**
  216. * 通过excel导入数据
  217. *
  218. * @param request
  219. * @param response
  220. * @return
  221. */
  222. @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  223. public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
  224. MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
  225. Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
  226. for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
  227. MultipartFile file = entity.getValue();// 获取上传文件对象
  228. ImportParams params = new ImportParams();
  229. params.setTitleRows(2);
  230. params.setHeadRows(1);
  231. params.setNeedSave(true);
  232. try {
  233. List<${entityName}Page> list = ExcelImportUtil.importExcel(file.getInputStream(), ${entityName}Page.class, params);
  234. for (${entityName}Page page : list) {
  235. ${entityName} po = new ${entityName}();
  236. BeanUtils.copyProperties(page, po);
  237. ${entityName?uncap_first}Service.saveMain(po, <#list subTables as sub>page.get${sub.entityName}List()<#if sub_has_next>,</#if></#list>);
  238. }
  239. return Result.OK("文件导入成功!数据行数:" + list.size());
  240. } catch (Exception e) {
  241. log.error(e.getMessage(),e);
  242. return Result.error("文件导入失败:"+e.getMessage());
  243. } finally {
  244. try {
  245. file.getInputStream().close();
  246. } catch (IOException e) {
  247. e.printStackTrace();
  248. }
  249. }
  250. }
  251. return Result.OK("文件导入失败!");
  252. }
  253. }