123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- package com.persagy.proxy.adm.controller;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.net.URLEncoder;
- import java.nio.charset.StandardCharsets;
- import java.util.List;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.util.ResourceUtils;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.bind.annotation.RestController;
- import org.springframework.web.multipart.MultipartFile;
- import com.alibaba.excel.EasyExcel;
- import com.alibaba.excel.ExcelWriter;
- import com.google.common.collect.Lists;
- import com.persagy.proxy.adm.constant.AdmCommonConstant;
- import com.persagy.proxy.adm.handler.RelationImportExcelDataHandler;
- import com.persagy.proxy.adm.model.AdmRelationObject;
- import com.persagy.proxy.adm.request.AdmResponse;
- import com.persagy.proxy.adm.service.IRelationReportService;
- import com.persagy.proxy.adm.strategy.QueryRelationObjectContext;
- import cn.hutool.core.util.StrUtil;
- import lombok.extern.slf4j.Slf4j;
- /**
- * 报表/模板下载
- *
- * @version 1.0.0
- * @company persagy
- * @author zhangqiankun
- * @date 2021年8月30日 下午7:41:56
- */
- @Slf4j
- @RestController
- public class RelationReportController {
- public static final String EXPORT_SHEET_NAME = "关系维护";
-
- @Autowired
- private IRelationReportService relationReportService;
-
- @Autowired
- private QueryRelationObjectContext queryRelationObjectContext;
-
- /**
- * 下载报告-设备静态信息维护模板
- *
- * @param request
- * @param response
- */
- @RequestMapping("/graphic/downloads/equip/template")
- public void downloadEquipTemplate(HttpServletRequest request, HttpServletResponse response) {
- ExcelWriter excelWriter = null;
- try {
- response.setContentType("application/vnd.ms-excel");
- String encode = StandardCharsets.UTF_8.name();
- response.setCharacterEncoding(encode);
- // 防止中文乱码
- String fileName = URLEncoder.encode("台账信息导入模板", encode);
- response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
-
- File templateFile = this.getTemplateFile("equip_template.xlsx");
- excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(templateFile).build();
- } catch (Exception e) {
- log.error("设备静态信息维护模板下载失败", e);
- } finally {
- if (excelWriter != null) {
- excelWriter.finish();
- }
- }
- }
- /**
- * 下载报告-点表
- *
- * @param request
- * @param response
- */
- @RequestMapping("/graphic/downloads/point")
- public void downloadPoint(@RequestParam(required = false) String key, HttpServletRequest request, HttpServletResponse response) {
- ExcelWriter excelWriter = null;
- try {
- response.setContentType("application/vnd.ms-excel");
- String encode = StandardCharsets.UTF_8.name();
- response.setCharacterEncoding(encode);
- response.setHeader("Content-disposition", "attachment;filename=" + key);
-
- File file = null;
- try {
- file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "template/upload/" + key);
- } catch (FileNotFoundException e) {
- file = ResourceUtils.getFile(AdmCommonConstant.SERVER_ROOT_PATH + "template/upload/" + key);
- }
- if (file == null || !file.exists()) {
- try {
- file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "/template/relation-template.xlsx");
- } catch (FileNotFoundException e) {
- file = ResourceUtils.getFile(AdmCommonConstant.SERVER_ROOT_PATH + "/template/relation-template.xlsx");
- }
- }
-
- excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(file).build();
- } catch (Exception e) {
- log.error("下载报告-点表失败", e);
- } finally {
- if (excelWriter != null) {
- excelWriter.finish();
- }
- }
- }
-
- /**
- * 下载模板
- *
- * @param request
- * @param response
- */
- @RequestMapping("/graphic/template-downloads")
- public void templateDownloads(HttpServletRequest request, HttpServletResponse response) {
- ExcelWriter excelWriter = null;
- try {
- response.setContentType("application/vnd.ms-excel");
- String encode = StandardCharsets.UTF_8.name();
- response.setCharacterEncoding(encode);
- response.setHeader("Content-disposition", "attachment;filename=relation-template.xlsx");
-
- File templateFile = this.getTemplateFile("relation-template.xlsx");
- excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(templateFile).build();
- } catch (Exception e) {
- log.error("模板下载失败", e);
- } finally {
- if (excelWriter != null) {
- excelWriter.finish();
- }
- }
- }
-
- /**
- * 下载报告
- *
- * @param request
- * @param response
- */
- @RequestMapping("/graphic/report-downloads")
- public void reportDownloads(@RequestParam String projectId, @RequestParam String relType, @RequestParam(required = false) String zoneType,
- HttpServletRequest request, HttpServletResponse response) {
- ExcelWriter excelWriter = null;
- try {
- response.setContentType("application/vnd.ms-excel");
- String encode = StandardCharsets.UTF_8.name();
- response.setCharacterEncoding(encode);
-
- String templateFile = projectId + AdmCommonConstant.LINE_THROUGH + relType + AdmCommonConstant.EXCEL_SUFFIX_XLSX;
- response.setHeader("Content-disposition", "attachment;filename=" + templateFile);
-
- File file = this.getTemplateOrDefaultFile(templateFile);
- excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(file).build();
- } catch (Exception e) {
- log.error("下载报告失败", e);
- } finally {
- if (excelWriter != null) {
- excelWriter.finish();
- }
- }
- }
-
- /**
- * 导出-点位模板
- *
- * @param request
- * @param response
- */
- @RequestMapping("/graphic/export/point")
- public void exportPoint(@RequestParam String buildingId, @RequestParam String floorId, @RequestParam String category,
- HttpServletRequest request, HttpServletResponse response) {
- ExcelWriter excelWriter = null;
- try {
- response.setContentType("application/vnd.ms-excel");
- String encode = StandardCharsets.UTF_8.name();
- response.setCharacterEncoding(encode);
-
- //excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(file).build();
- } catch (Exception e) {
- log.error("导出-点位模板失败", e);
- } finally {
- if (excelWriter != null) {
- excelWriter.finish();
- }
- }
- }
-
- /**
- * 导出关系
- *
- * @param request
- * @param response
- */
- @RequestMapping("/graphic/export")
- public void export(@RequestParam String projectId, @RequestParam String relType, @RequestParam(required = false) String code,
- @RequestParam(required = false) String zoneType, HttpServletRequest request, HttpServletResponse response) {
- ExcelWriter excelWriter = null;
- try {
- String groupCode = request.getHeader(AdmCommonConstant.GROUP_CODE_HEADER);
- code = StrUtil.isBlank(code) ? "对象ID" : code;
-
- response.setContentType("application/vnd.ms-excel");
- String encode = StandardCharsets.UTF_8.name();
- response.setCharacterEncoding(encode);
- response.setHeader("Content-disposition", "attachment;filename=relation-template.xlsx");
-
- List<AdmRelationObject> relationObjects = this.relationReportService.reportRelationObject(groupCode, projectId, relType, code, zoneType);
-
- File templateFile = this.getTemplateFile("relation-template.xlsx");
- EasyExcel.write(response.getOutputStream()).withTemplate(templateFile).sheet(EXPORT_SHEET_NAME).needHead(false).doWrite(relationObjects);
- } catch (Exception e) {
- log.error("导出关系失败", e);
- } finally {
- if (excelWriter != null) {
- excelWriter.finish();
- }
- }
- }
-
-
- /************************************************** 文件上传 ***********************************************/
-
- /**
- * 文件导入
- *
- * @param relType
- * @param code
- * @param zoneType
- * @param request
- * @param response
- */
- @RequestMapping("/graphic/import")
- public AdmResponse importFile(@RequestParam("file") MultipartFile file, @RequestParam String relType, @RequestParam(required = false) String zoneType,
- HttpServletRequest request, HttpServletResponse response) {
- String failure = null;
- try {
- if (file == null) {
- return AdmResponse.failure("未选择文件");
- }
- String groupCode = request.getHeader(AdmCommonConstant.GROUP_CODE_HEADER);
- String projectId = request.getHeader(AdmCommonConstant.PROJECT_ID_HEADER);
-
- RelationImportExcelDataHandler excelDataHandler = new RelationImportExcelDataHandler(groupCode, projectId, relType, queryRelationObjectContext);
- EasyExcel.read(file.getInputStream(), AdmRelationObject.class, excelDataHandler).sheet(EXPORT_SHEET_NAME).doRead();
-
- return AdmResponse.success(Lists.newArrayList(excelDataHandler.afterWriteObject()));
- } catch (Exception e) {
- log.error("文件导入失败", e);
- failure = e.getMessage();
- }
- return AdmResponse.failure(failure);
- }
-
-
-
-
- /************************************************** 本地文件获取 ***********************************************/
-
-
- /**
- * 获取对应的文件,或者默认文件
- * 不同路径,防止镜像部署时获取不到
- *
- * @param templateName 带后缀的文件名
- * @return
- * @throws FileNotFoundException
- */
- private File getTemplateOrDefaultFile(String templateName) throws FileNotFoundException {
- File file = null;
- try {
- file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "template/" + templateName);
- } catch (FileNotFoundException e) {
- file = ResourceUtils.getFile(AdmCommonConstant.SERVER_ROOT_PATH + "template/" + templateName);
- }
- if (file == null || !file.exists()) {
- try {
- file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "template/relation-template.xlsx");
- } catch (FileNotFoundException e) {
- file = ResourceUtils.getFile(AdmCommonConstant.SERVER_ROOT_PATH + "/template/relation-template.xlsx");
- }
- }
- return file;
- }
-
- /**
- * 获取对应的模板文件,不同路径,防止镜像内获取不到
- *
- * @param templateName 带后缀的文件名
- * @return
- * @throws FileNotFoundException
- */
- private File getTemplateFile(String templateName) throws FileNotFoundException {
- File file = null;
- try {
- file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "template/" + templateName);
- } catch (FileNotFoundException e) {}
- if (file == null || !file.exists()) {
- file = ResourceUtils.getFile(AdmCommonConstant.SERVER_ROOT_PATH + "/template/" + templateName);
- }
- return file;
- }
- }
|