05463ecd5bb58f7877369b93deeee4653c0e311a.svn-base 10 KB

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