42b09a78892aba142370c90a2457b9356f71cedd.svn-base 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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.apache.shiro.SecurityUtils;
  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.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.save(${entityName?uncap_first});
  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.updateById(${entityName?uncap_first});
  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.removeByIds(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. //===========================以下是子表信息操作相关API====================================
  152. <#list subTables as sub>
  153. /**
  154. * 通过主表id查询${sub.ftlDescription}
  155. *
  156. * @param ${sub.entityName?uncap_first}
  157. * @return
  158. */
  159. @AutoLog(value = "${sub.ftlDescription}-通过主表id查询")
  160. @ApiOperation(value="${sub.ftlDescription}-通过主表id查询", notes="${sub.ftlDescription}-通过主表id查询")
  161. <#-- update-begin--Author:kangxiaolin Date:20190905 for:[442]主子表分开维护,生成的代码子表的分页改为真实的分页-------------------- -->
  162. @GetMapping(value = "/list${sub.entityName}ByMainId")
  163. public Result<?> list${sub.entityName}ByMainId(${sub.entityName} ${sub.entityName?uncap_first},
  164. @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
  165. @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
  166. HttpServletRequest req) {
  167. if(<#list sub.foreignKeys as key><#rt/>
  168. <#if key?lower_case?index_of("${primaryKeyField}")!=-1><#rt/>
  169. <#if key_index == 0><#rt/>
  170. ${sub.entityName?uncap_first}.get${key?cap_first}()!=null<#rt/>
  171. <#else><#rt/>
  172. || ${sub.entityName?uncap_first}.get${key?cap_first}()!=null<#rt/>
  173. </#if><#rt/>
  174. <#else><#rt/>
  175. <#if key_index == 0><#rt/>
  176. ${sub.entityName?uncap_first}.get${key}()!=null<#rt/>
  177. <#else><#rt/>
  178. || ${sub.entityName?uncap_first}.get${key}()!=null<#rt/>
  179. </#if><#rt/>
  180. </#if>
  181. </#list><#rt/>
  182. ) {
  183. QueryWrapper<${sub.entityName}> queryWrapper = QueryGenerator.initQueryWrapper(${sub.entityName?uncap_first}, req.getParameterMap());
  184. Page<${sub.entityName}> page = new Page<${sub.entityName}>(pageNo, pageSize);
  185. IPage<${sub.entityName}> pageList = ${sub.entityName?uncap_first}Service.page(page, queryWrapper);
  186. return Result.OK(pageList);
  187. }else{
  188. return Result.OK();
  189. }
  190. }
  191. <#-- update-end--Author:kangxiaolin Date:20190905 for:[442]主子表分开维护,生成的代码子表的分页改为真实的分页-------------------- -->
  192. /**
  193. * 添加${sub.ftlDescription}
  194. *
  195. * @param ${sub.entityName?uncap_first}
  196. * @return
  197. */
  198. @AutoLog(value = "${sub.ftlDescription}-添加")
  199. @ApiOperation(value="${sub.ftlDescription}-添加", notes="${sub.ftlDescription}-添加")
  200. @PostMapping(value = "/add${sub.entityName}")
  201. public Result<?> add${sub.entityName}(@RequestBody ${sub.entityName} ${sub.entityName?uncap_first}) {
  202. ${sub.entityName?uncap_first}Service.save(${sub.entityName?uncap_first});
  203. return Result.OK("添加${sub.ftlDescription}成功!");
  204. }
  205. /**
  206. * 编辑${sub.ftlDescription}
  207. *
  208. * @param ${sub.entityName?uncap_first}
  209. * @return
  210. */
  211. @AutoLog(value = "${sub.ftlDescription}-编辑")
  212. @ApiOperation(value="${sub.ftlDescription}-编辑", notes="${sub.ftlDescription}-编辑")
  213. @PostMapping("/edit${sub.entityName}")
  214. public Result<?> edit${sub.entityName}(@RequestBody ${sub.entityName} ${sub.entityName?uncap_first}) {
  215. ${sub.entityName?uncap_first}Service.updateById(${sub.entityName?uncap_first});
  216. return Result.OK("编辑${sub.ftlDescription}成功!");
  217. }
  218. /**
  219. * 通过id删除${sub.ftlDescription}
  220. *
  221. * @param id
  222. * @return
  223. */
  224. @AutoLog(value = "${sub.ftlDescription}-通过id删除")
  225. @ApiOperation(value="${sub.ftlDescription}-通过id删除", notes="${sub.ftlDescription}-通过id删除")
  226. @PostMapping(value = "/delete${sub.entityName}")
  227. public Result<?> delete${sub.entityName}(@RequestParam(name = "id", required = true) String id) {
  228. ${sub.entityName?uncap_first}Service.removeById(id);
  229. return Result.OK("删除${sub.ftlDescription}成功!");
  230. }
  231. /**
  232. * 批量删除${sub.ftlDescription}
  233. *
  234. * @param ids
  235. * @return
  236. */
  237. @AutoLog(value = "${sub.ftlDescription}-批量删除")
  238. @ApiOperation(value="${sub.ftlDescription}-批量删除", notes="${sub.ftlDescription}-批量删除")
  239. @PostMapping(value = "/deleteBatch${sub.entityName}")
  240. public Result<?> deleteBatch${sub.entityName}(@RequestParam(name = "ids", required = true) String ids) {
  241. if (ids == null || "".equals(ids.trim())) {
  242. return Result.error("参数不识别!");
  243. }
  244. this.${sub.entityName?uncap_first}Service.removeByIds(Arrays.asList(ids.split(",")));
  245. return Result.OK("批量删除成功!");
  246. }
  247. </#list>
  248. /**
  249. * 导出excel
  250. *
  251. * @param request
  252. * @param ${entityName?uncap_first}
  253. */
  254. @RequestMapping(value = "/exportXls")
  255. public ModelAndView exportXls(HttpServletRequest request, ${entityName} ${entityName?uncap_first}) {
  256. // Step.1 组装查询条件
  257. QueryWrapper<${entityName}> queryWrapper = QueryGenerator.initQueryWrapper(${entityName?uncap_first}, request.getParameterMap());
  258. LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
  259. //Step.2 获取导出数据
  260. List<${entityName}Page> pageList = new ArrayList<${entityName}Page>();
  261. List<${entityName}> ${entityName?uncap_first}List = ${entityName?uncap_first}Service.list(queryWrapper);
  262. for (${entityName} temp : ${entityName?uncap_first}List) {
  263. ${entityName}Page vo = new ${entityName}Page();
  264. BeanUtils.copyProperties(temp, vo);
  265. <#list subTables as sub>
  266. List<${sub.entityName}> ${sub.entityName?uncap_first}List = ${sub.entityName?uncap_first}Service.selectByMainId(temp.getId());
  267. vo.set${sub.entityName}List(${sub.entityName?uncap_first}List);
  268. </#list>
  269. pageList.add(vo);
  270. }
  271. //Step.3 调用AutoPoi导出Excel
  272. ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
  273. mv.addObject(NormalExcelConstants.FILE_NAME, "${tableVo.ftlDescription}");
  274. mv.addObject(NormalExcelConstants.CLASS, ${entityName}Page.class);
  275. mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("${tableVo.ftlDescription}数据", "导出人:"+sysUser.getRealname(), "${tableVo.ftlDescription}"));
  276. mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
  277. return mv;
  278. }
  279. /**
  280. * 通过excel导入数据
  281. *
  282. * @param request
  283. * @param response
  284. * @return
  285. */
  286. @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  287. public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
  288. MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
  289. Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
  290. for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
  291. MultipartFile file = entity.getValue();// 获取上传文件对象
  292. ImportParams params = new ImportParams();
  293. params.setTitleRows(2);
  294. params.setHeadRows(1);
  295. params.setNeedSave(true);
  296. try {
  297. List<${entityName}Page> list = ExcelImportUtil.importExcel(file.getInputStream(), ${entityName}Page.class, params);
  298. for (${entityName}Page page : list) {
  299. ${entityName} po = new ${entityName}();
  300. BeanUtils.copyProperties(page, po);
  301. ${entityName?uncap_first}Service.saveMain(po, <#list subTables as sub>page.get${sub.entityName}List()<#if sub_has_next>,</#if></#list>);
  302. }
  303. return Result.OK("文件导入成功!数据行数:" + list.size());
  304. } catch (Exception e) {
  305. log.error(e.getMessage(),e);
  306. return Result.error("文件导入失败:"+e.getMessage());
  307. } finally {
  308. try {
  309. file.getInputStream().close();
  310. } catch (IOException e) {
  311. e.printStackTrace();
  312. }
  313. }
  314. }
  315. return Result.OK("文件导入失败!");
  316. }
  317. }