df5d3321ccf3353f96deb7b55f610c05ad5f27d2.svn-base 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. <#-- 包裹分页对象,用于翻译注解 -->
  172. IPage <${sub.entityName}> page = new Page<>();
  173. page.setRecords(${sub.entityName?uncap_first}List);
  174. page.setTotal(${sub.entityName?uncap_first}List.size());
  175. return Result.OK(page);
  176. }
  177. </#list>
  178. /**
  179. * 导出excel
  180. *
  181. * @param request
  182. * @param ${entityName?uncap_first}
  183. */
  184. @RequestMapping(value = "/exportXls")
  185. public ModelAndView exportXls(HttpServletRequest request, ${entityName} ${entityName?uncap_first}) {
  186. // Step.1 组装查询条件查询数据
  187. QueryWrapper<${entityName}> queryWrapper = QueryGenerator.initQueryWrapper(${entityName?uncap_first}, request.getParameterMap());
  188. LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
  189. //Step.2 获取导出数据
  190. List<${entityName}> queryList = ${entityName?uncap_first}Service.list(queryWrapper);
  191. // 过滤选中数据
  192. String selections = request.getParameter("selections");
  193. List<${entityName}> ${entityName?uncap_first}List = new ArrayList<${entityName}>();
  194. if(oConvertUtils.isEmpty(selections)) {
  195. ${entityName?uncap_first}List = queryList;
  196. }else {
  197. List<String> selectionList = Arrays.asList(selections.split(","));
  198. ${entityName?uncap_first}List = queryList.stream().filter(item -> selectionList.contains(item.getId())).collect(Collectors.toList());
  199. }
  200. // Step.3 组装pageList
  201. List<${entityName}Page> pageList = new ArrayList<${entityName}Page>();
  202. for (${entityName} main : ${entityName?uncap_first}List) {
  203. ${entityName}Page vo = new ${entityName}Page();
  204. BeanUtils.copyProperties(main, vo);
  205. <#list subTables as sub>
  206. List<${sub.entityName}> ${sub.entityName?uncap_first}List = ${sub.entityName?uncap_first}Service.selectByMainId(main.getId());
  207. vo.set${sub.entityName}List(${sub.entityName?uncap_first}List);
  208. </#list>
  209. pageList.add(vo);
  210. }
  211. // Step.4 AutoPoi 导出Excel
  212. ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
  213. mv.addObject(NormalExcelConstants.FILE_NAME, "${tableVo.ftlDescription}列表");
  214. mv.addObject(NormalExcelConstants.CLASS, ${entityName}Page.class);
  215. mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("${tableVo.ftlDescription}数据", "导出人:"+sysUser.getRealname(), "${tableVo.ftlDescription}"));
  216. mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
  217. return mv;
  218. }
  219. /**
  220. * 通过excel导入数据
  221. *
  222. * @param request
  223. * @param response
  224. * @return
  225. */
  226. @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  227. public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
  228. MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
  229. Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
  230. for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
  231. MultipartFile file = entity.getValue();// 获取上传文件对象
  232. ImportParams params = new ImportParams();
  233. params.setTitleRows(2);
  234. params.setHeadRows(1);
  235. params.setNeedSave(true);
  236. try {
  237. List<${entityName}Page> list = ExcelImportUtil.importExcel(file.getInputStream(), ${entityName}Page.class, params);
  238. for (${entityName}Page page : list) {
  239. ${entityName} po = new ${entityName}();
  240. BeanUtils.copyProperties(page, po);
  241. ${entityName?uncap_first}Service.saveMain(po, <#list subTables as sub>page.get${sub.entityName}List()<#if sub_has_next>,</#if></#list>);
  242. }
  243. return Result.OK("文件导入成功!数据行数:" + list.size());
  244. } catch (Exception e) {
  245. log.error(e.getMessage(),e);
  246. return Result.error("文件导入失败:"+e.getMessage());
  247. } finally {
  248. try {
  249. file.getInputStream().close();
  250. } catch (IOException e) {
  251. e.printStackTrace();
  252. }
  253. }
  254. }
  255. return Result.OK("文件导入失败!");
  256. }
  257. }