RelationReportController.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. package com.persagy.proxy.adm.controller;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.net.URLEncoder;
  5. import java.nio.charset.StandardCharsets;
  6. import java.util.List;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.util.ResourceUtils;
  11. import org.springframework.web.bind.annotation.RequestMapping;
  12. import org.springframework.web.bind.annotation.RequestParam;
  13. import org.springframework.web.bind.annotation.RestController;
  14. import org.springframework.web.multipart.MultipartFile;
  15. import com.alibaba.excel.EasyExcel;
  16. import com.alibaba.excel.ExcelWriter;
  17. import com.google.common.collect.Lists;
  18. import com.persagy.proxy.adm.constant.AdmCommonConstant;
  19. import com.persagy.proxy.adm.handler.RelationImportExcelDataHandler;
  20. import com.persagy.proxy.adm.model.AdmRelationObject;
  21. import com.persagy.proxy.adm.request.AdmResponse;
  22. import com.persagy.proxy.adm.service.IRelationReportService;
  23. import com.persagy.proxy.adm.strategy.QueryRelationObjectContext;
  24. import cn.hutool.core.util.StrUtil;
  25. import lombok.extern.slf4j.Slf4j;
  26. /**
  27. * 报表/模板下载
  28. *
  29. * @version 1.0.0
  30. * @company persagy
  31. * @author zhangqiankun
  32. * @date 2021年8月30日 下午7:41:56
  33. */
  34. @Slf4j
  35. @RestController
  36. public class RelationReportController {
  37. public static final String EXPORT_SHEET_NAME = "关系维护";
  38. @Autowired
  39. private IRelationReportService relationReportService;
  40. @Autowired
  41. private QueryRelationObjectContext queryRelationObjectContext;
  42. /**
  43. * 下载报告-设备静态信息维护模板
  44. *
  45. * @param request
  46. * @param response
  47. */
  48. @RequestMapping("/graphic/downloads/equip/template")
  49. public void downloadEquipTemplate(HttpServletRequest request, HttpServletResponse response) {
  50. ExcelWriter excelWriter = null;
  51. try {
  52. response.setContentType("application/vnd.ms-excel");
  53. String encode = StandardCharsets.UTF_8.name();
  54. response.setCharacterEncoding(encode);
  55. // 防止中文乱码
  56. String fileName = URLEncoder.encode("台账信息导入模板", encode);
  57. response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
  58. File templateFile = this.getTemplateFile("equip_template.xlsx");
  59. excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(templateFile).build();
  60. } catch (Exception e) {
  61. log.error("设备静态信息维护模板下载失败", e);
  62. } finally {
  63. if (excelWriter != null) {
  64. excelWriter.finish();
  65. }
  66. }
  67. }
  68. /**
  69. * 下载报告-点表
  70. *
  71. * @param request
  72. * @param response
  73. */
  74. @RequestMapping("/graphic/downloads/point")
  75. public void downloadPoint(@RequestParam(required = false) String key, HttpServletRequest request, HttpServletResponse response) {
  76. ExcelWriter excelWriter = null;
  77. try {
  78. response.setContentType("application/vnd.ms-excel");
  79. String encode = StandardCharsets.UTF_8.name();
  80. response.setCharacterEncoding(encode);
  81. response.setHeader("Content-disposition", "attachment;filename=" + key);
  82. File file = null;
  83. try {
  84. file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "template/upload/" + key);
  85. } catch (FileNotFoundException e) {
  86. file = ResourceUtils.getFile(AdmCommonConstant.SERVER_ROOT_PATH + "template/upload/" + key);
  87. }
  88. if (file == null || !file.exists()) {
  89. try {
  90. file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "/template/relation-template.xlsx");
  91. } catch (FileNotFoundException e) {
  92. file = ResourceUtils.getFile(AdmCommonConstant.SERVER_ROOT_PATH + "/template/relation-template.xlsx");
  93. }
  94. }
  95. excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(file).build();
  96. } catch (Exception e) {
  97. log.error("下载报告-点表失败", e);
  98. } finally {
  99. if (excelWriter != null) {
  100. excelWriter.finish();
  101. }
  102. }
  103. }
  104. /**
  105. * 下载模板
  106. *
  107. * @param request
  108. * @param response
  109. */
  110. @RequestMapping("/graphic/template-downloads")
  111. public void templateDownloads(HttpServletRequest request, HttpServletResponse response) {
  112. ExcelWriter excelWriter = null;
  113. try {
  114. response.setContentType("application/vnd.ms-excel");
  115. String encode = StandardCharsets.UTF_8.name();
  116. response.setCharacterEncoding(encode);
  117. response.setHeader("Content-disposition", "attachment;filename=relation-template.xlsx");
  118. File templateFile = this.getTemplateFile("relation-template.xlsx");
  119. excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(templateFile).build();
  120. } catch (Exception e) {
  121. log.error("模板下载失败", e);
  122. } finally {
  123. if (excelWriter != null) {
  124. excelWriter.finish();
  125. }
  126. }
  127. }
  128. /**
  129. * 下载报告
  130. *
  131. * @param request
  132. * @param response
  133. */
  134. @RequestMapping("/graphic/report-downloads")
  135. public void reportDownloads(@RequestParam String projectId, @RequestParam String relType, @RequestParam(required = false) String zoneType,
  136. HttpServletRequest request, HttpServletResponse response) {
  137. ExcelWriter excelWriter = null;
  138. try {
  139. response.setContentType("application/vnd.ms-excel");
  140. String encode = StandardCharsets.UTF_8.name();
  141. response.setCharacterEncoding(encode);
  142. String templateFile = projectId + AdmCommonConstant.LINE_THROUGH + relType + AdmCommonConstant.EXCEL_SUFFIX_XLSX;
  143. response.setHeader("Content-disposition", "attachment;filename=" + templateFile);
  144. File file = this.getTemplateOrDefaultFile(templateFile);
  145. excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(file).build();
  146. } catch (Exception e) {
  147. log.error("下载报告失败", e);
  148. } finally {
  149. if (excelWriter != null) {
  150. excelWriter.finish();
  151. }
  152. }
  153. }
  154. /**
  155. * 导出-点位模板
  156. *
  157. * @param request
  158. * @param response
  159. */
  160. @RequestMapping("/graphic/export/point")
  161. public void exportPoint(@RequestParam String buildingId, @RequestParam String floorId, @RequestParam String category,
  162. HttpServletRequest request, HttpServletResponse response) {
  163. ExcelWriter excelWriter = null;
  164. try {
  165. response.setContentType("application/vnd.ms-excel");
  166. String encode = StandardCharsets.UTF_8.name();
  167. response.setCharacterEncoding(encode);
  168. //excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(file).build();
  169. } catch (Exception e) {
  170. log.error("导出-点位模板失败", e);
  171. } finally {
  172. if (excelWriter != null) {
  173. excelWriter.finish();
  174. }
  175. }
  176. }
  177. /**
  178. * 导出关系
  179. *
  180. * @param request
  181. * @param response
  182. */
  183. @RequestMapping("/graphic/export")
  184. public void export(@RequestParam String projectId, @RequestParam String relType, @RequestParam(required = false) String code,
  185. @RequestParam(required = false) String zoneType, HttpServletRequest request, HttpServletResponse response) {
  186. ExcelWriter excelWriter = null;
  187. try {
  188. String groupCode = request.getHeader(AdmCommonConstant.GROUP_CODE_HEADER);
  189. code = StrUtil.isBlank(code) ? "对象ID" : code;
  190. response.setContentType("application/vnd.ms-excel");
  191. String encode = StandardCharsets.UTF_8.name();
  192. response.setCharacterEncoding(encode);
  193. response.setHeader("Content-disposition", "attachment;filename=relation-template.xlsx");
  194. List<AdmRelationObject> relationObjects = this.relationReportService.reportRelationObject(groupCode, projectId, relType, code, zoneType);
  195. File templateFile = this.getTemplateFile("relation-template.xlsx");
  196. EasyExcel.write(response.getOutputStream()).withTemplate(templateFile).sheet(EXPORT_SHEET_NAME).needHead(false).doWrite(relationObjects);
  197. } catch (Exception e) {
  198. log.error("导出关系失败", e);
  199. } finally {
  200. if (excelWriter != null) {
  201. excelWriter.finish();
  202. }
  203. }
  204. }
  205. /************************************************** 文件上传 ***********************************************/
  206. /**
  207. * 文件导入
  208. *
  209. * @param relType
  210. * @param code
  211. * @param zoneType
  212. * @param request
  213. * @param response
  214. */
  215. @RequestMapping("/graphic/import")
  216. public AdmResponse importFile(@RequestParam("file") MultipartFile file, @RequestParam String relType, @RequestParam(required = false) String zoneType,
  217. HttpServletRequest request, HttpServletResponse response) {
  218. String failure = null;
  219. try {
  220. if (file == null) {
  221. return AdmResponse.failure("未选择文件");
  222. }
  223. String groupCode = request.getHeader(AdmCommonConstant.GROUP_CODE_HEADER);
  224. String projectId = request.getHeader(AdmCommonConstant.PROJECT_ID_HEADER);
  225. RelationImportExcelDataHandler excelDataHandler = new RelationImportExcelDataHandler(groupCode, projectId, relType, queryRelationObjectContext);
  226. EasyExcel.read(file.getInputStream(), AdmRelationObject.class, excelDataHandler).sheet(EXPORT_SHEET_NAME).doRead();
  227. return AdmResponse.success(Lists.newArrayList(excelDataHandler.afterWriteObject()));
  228. } catch (Exception e) {
  229. log.error("文件导入失败", e);
  230. failure = e.getMessage();
  231. }
  232. return AdmResponse.failure(failure);
  233. }
  234. /************************************************** 本地文件获取 ***********************************************/
  235. /**
  236. * 获取对应的文件,或者默认文件
  237. * 不同路径,防止镜像部署时获取不到
  238. *
  239. * @param templateName 带后缀的文件名
  240. * @return
  241. * @throws FileNotFoundException
  242. */
  243. private File getTemplateOrDefaultFile(String templateName) throws FileNotFoundException {
  244. File file = null;
  245. try {
  246. file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "template/" + templateName);
  247. } catch (FileNotFoundException e) {
  248. file = ResourceUtils.getFile(AdmCommonConstant.SERVER_ROOT_PATH + "template/" + templateName);
  249. }
  250. if (file == null || !file.exists()) {
  251. try {
  252. file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "template/relation-template.xlsx");
  253. } catch (FileNotFoundException e) {
  254. file = ResourceUtils.getFile(AdmCommonConstant.SERVER_ROOT_PATH + "/template/relation-template.xlsx");
  255. }
  256. }
  257. return file;
  258. }
  259. /**
  260. * 获取对应的模板文件,不同路径,防止镜像内获取不到
  261. *
  262. * @param templateName 带后缀的文件名
  263. * @return
  264. * @throws FileNotFoundException
  265. */
  266. private File getTemplateFile(String templateName) throws FileNotFoundException {
  267. File file = null;
  268. try {
  269. file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "template/" + templateName);
  270. } catch (FileNotFoundException e) {}
  271. if (file == null || !file.exists()) {
  272. file = ResourceUtils.getFile(AdmCommonConstant.SERVER_ROOT_PATH + "/template/" + templateName);
  273. }
  274. return file;
  275. }
  276. }