ReportDownloadController.java 8.7 KB

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