01c01a42ead36ee814232e72f116ad62a55a1987.svn-base 11 KB

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