SanYaController.java 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. package com.onemap.sanya.controller;
  2. import com.alibaba.cloud.commons.io.FileUtils;
  3. import com.alibaba.fastjson.util.IOUtils;
  4. import com.alibaba.fastjson2.JSON;
  5. import com.alibaba.fastjson2.JSONObject;
  6. import com.deepoove.poi.XWPFTemplate;
  7. import com.deepoove.poi.config.Configure;
  8. import com.deepoove.poi.data.PictureRenderData;
  9. import com.deepoove.poi.data.PictureType;
  10. import com.deepoove.poi.data.Pictures;
  11. import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
  12. import com.deepoove.poi.template.MetaTemplate;
  13. import com.deepoove.poi.template.run.RunTemplate;
  14. import com.onemap.common.core.utils.StringUtils;
  15. import com.onemap.common.core.web.controller.BaseController;
  16. import com.onemap.common.core.web.domain.AjaxResult;
  17. import com.onemap.sanya.domain.*;
  18. import com.onemap.sanya.domain.mergeCell.AnalyseDetailTablePolicy;
  19. import com.onemap.system.api.domain.SysUser;
  20. import com.onemap.system.api.factory.RemoteLogFallbackFactory;
  21. import org.apache.poi.xwpf.usermodel.*;
  22. import org.slf4j.Logger;
  23. import org.slf4j.LoggerFactory;
  24. import org.springframework.core.io.ClassPathResource;
  25. import org.springframework.util.ClassUtils;
  26. import org.springframework.util.ResourceUtils;
  27. import org.springframework.web.bind.annotation.*;
  28. import javax.servlet.http.HttpServletRequest;
  29. import javax.servlet.http.HttpServletResponse;
  30. import java.io.*;
  31. import java.net.URLEncoder;
  32. import java.nio.charset.StandardCharsets;
  33. import java.util.*;
  34. import java.util.stream.Collectors;
  35. /**
  36. * @author Zzz
  37. */
  38. @CrossOrigin(origins = "*")
  39. @RestController
  40. @RequestMapping("/sanya")
  41. public class SanYaController extends BaseController {
  42. private static final Logger log = LoggerFactory.getLogger(SanYaController.class);
  43. private static final String[] LvArray = {"Ⅰ", "Ⅱ", "Ⅲ", "Ⅳ", "Ⅴ", "Ⅵ", "Ⅶ", "Ⅷ", "Ⅸ", "Ⅹ"};
  44. /**
  45. * 导出选址报告word
  46. * @param request
  47. * @param response
  48. */
  49. @RequestMapping("/exportWord1")
  50. private void exportWord1(HttpServletRequest request, HttpServletResponse response) {
  51. try {
  52. SiteSelectionReport siteSelectionReport = setWordData1();
  53. if (siteSelectionReport == null) {
  54. log.info("选址报告数据为空,导出失败!");
  55. return;
  56. }
  57. ClassPathResource classPathResource = new ClassPathResource("template/word/"+"01-选址报告-02.docx");
  58. InputStream inputStream = classPathResource.getInputStream();
  59. Configure configure = Configure.builder()
  60. .bind("tableList", new LoopRowTableRenderPolicy())
  61. .bind("analyseList", new AnalyseDetailTablePolicy()).build();
  62. // 通过 XWPFTemplate 编译文件并渲染数据到模板中
  63. XWPFTemplate template = XWPFTemplate.compile(inputStream, configure).render(siteSelectionReport);
  64. //生成临时文件存放地址
  65. //String temDir = basePath;
  66. //生成文件名
  67. String wordName = "01-选址报告-02" + "-" + System.currentTimeMillis();
  68. writeWord(response, null, wordName, template);
  69. } catch (Exception e) {
  70. e.printStackTrace();
  71. log.error("选址报告导出异常,{}" + e);
  72. }
  73. }
  74. /**
  75. * 导出选址报告word
  76. * @param request
  77. * @param response
  78. */
  79. @RequestMapping("/exportWord2")
  80. private void exportWord2(HttpServletRequest request, HttpServletResponse response) {
  81. try {
  82. ComplianceAnalysisReport complianceAnalysisReport = setWordData2();
  83. if (complianceAnalysisReport == null) {
  84. log.info("合规性分析报告数据为空,导出失败!");
  85. return;
  86. }
  87. ClassPathResource classPathResource = new ClassPathResource("template/word/"+"02-合规性分析报告-02.docx");
  88. InputStream inputStream = classPathResource.getInputStream();
  89. Configure configure = Configure.builder()
  90. .bind("tableList01", new LoopRowTableRenderPolicy())
  91. .bind("tableList02", new LoopRowTableRenderPolicy())
  92. .bind("analyseList01", new AnalyseDetailTablePolicy())
  93. .bind("analyseList02", new AnalyseDetailTablePolicy()).build();
  94. // 通过 XWPFTemplate 编译文件并渲染数据到模板中
  95. XWPFTemplate template = XWPFTemplate.compile(inputStream, configure).render(complianceAnalysisReport);
  96. List<MetaTemplate> elementTemplates = template.getElementTemplates();
  97. for (MetaTemplate elementTemplate : elementTemplates) {
  98. String variable = elementTemplate.variable();
  99. if ("{{tableList01}}".equals(variable) || "{{tableList02}}".equals(variable)) {
  100. RunTemplate runTemplate = (RunTemplate) elementTemplate;
  101. XWPFRun run = runTemplate.getRun();
  102. XWPFTableCell cell = (XWPFTableCell) ((XWPFParagraph) run.getParent()).getBody();
  103. List<XWPFTableRow> rows = cell.getTableRow().getTable().getRows();
  104. int rowIndex = rows.indexOf(cell.getTableRow());
  105. XWPFTable table = cell.getTableRow().getTable();
  106. table.removeRow(rowIndex);
  107. }
  108. }
  109. //生成临时文件存放地址
  110. //String temDir = basePath;
  111. //生成文件名
  112. String wordName = "02-合规性分析报告-02" + "-" + System.currentTimeMillis();
  113. writeWord(response, null, wordName, template);
  114. } catch (Exception e) {
  115. e.printStackTrace();
  116. log.error("合规性分析报告导出异常,{}" + e);
  117. }
  118. }
  119. /**
  120. * 导出征收补偿预估报告word
  121. * @param request
  122. * @param response
  123. */
  124. @RequestMapping("/exportWord3")
  125. private void exportWord3(HttpServletRequest request, HttpServletResponse response) {
  126. try {
  127. CompensateEstimateReport compensateEstimateReport = setWordData3();
  128. if (compensateEstimateReport == null) {
  129. log.info("征收补偿预估报告数据为空,导出失败!");
  130. return;
  131. }
  132. ClassPathResource classPathResource = new ClassPathResource("template/word/"+"03-征收补偿预估报告-02.docx");
  133. InputStream inputStream = classPathResource.getInputStream();
  134. Configure configure = Configure.builder()
  135. .bind("tableList01", new LoopRowTableRenderPolicy()).build();
  136. // 通过 XWPFTemplate 编译文件并渲染数据到模板中
  137. XWPFTemplate template = XWPFTemplate.compile(inputStream, configure).render(compensateEstimateReport);
  138. //生成临时文件存放地址
  139. //String temDir = basePath;
  140. //生成文件名
  141. String wordName = "03-征收补偿预估报告-02" + "-" + System.currentTimeMillis();
  142. writeWord(response, null, wordName, template);
  143. } catch (Exception e) {
  144. e.printStackTrace();
  145. log.error("征收补偿预估报告导出异常,{}" + e);
  146. }
  147. }
  148. /**
  149. * 导出基准地价报告word
  150. * @param request
  151. * @param response
  152. */
  153. @RequestMapping("/exportWord4")
  154. private void exportWord4(@RequestBody BenchmarkLandPriceReport benchmarkLandPriceReport, HttpServletRequest request, HttpServletResponse response) {
  155. try {
  156. // BenchmarkLandPriceReport benchmarkLandPriceReport = setWordData4();
  157. if (benchmarkLandPriceReport == null) {
  158. log.info("基准地价报告数据为空,导出失败!");
  159. return ;
  160. }
  161. List<PicData> picList = new ArrayList<>();
  162. //将base64图片转为PictureRenderData
  163. for (String base64ImageData:benchmarkLandPriceReport.getPicBase64List()) {
  164. PictureRenderData pictureRenderData =Pictures.ofBase64(base64ImageData, PictureType.JPEG).size(300, 200).create();
  165. picList.add(new PicData(pictureRenderData));
  166. }
  167. benchmarkLandPriceReport.setPicList(picList);
  168. ClassPathResource classPathResource = new ClassPathResource("template/word/"+"04-基准地价报告-02.docx");
  169. InputStream inputStream = classPathResource.getInputStream();
  170. Configure configure = Configure.builder()
  171. .bind("tableList01", new LoopRowTableRenderPolicy())
  172. .bind("tableList02", new LoopRowTableRenderPolicy()).build();
  173. // 通过 XWPFTemplate 编译文件并渲染数据到模板中
  174. XWPFTemplate template = XWPFTemplate.compile(inputStream, configure).render(benchmarkLandPriceReport);
  175. List<MetaTemplate> elementTemplates = template.getElementTemplates();
  176. for (MetaTemplate elementTemplate : elementTemplates) {
  177. String variable = elementTemplate.variable();
  178. if ("{{tableList01}}".equals(variable) || "{{tableList02}}".equals(variable)) {
  179. RunTemplate runTemplate = (RunTemplate) elementTemplate;
  180. XWPFRun run = runTemplate.getRun();
  181. XWPFTableCell cell = (XWPFTableCell) ((XWPFParagraph) run.getParent()).getBody();
  182. List<XWPFTableRow> rows = cell.getTableRow().getTable().getRows();
  183. int rowIndex = rows.indexOf(cell.getTableRow());
  184. XWPFTable table = cell.getTableRow().getTable();
  185. table.removeRow(rowIndex);
  186. }
  187. }
  188. //生成文件存放地址
  189. // String temDir = this.getClass().getClassLoader().getResource("template/storageword/").getPath();
  190. //ClassUtils.getDefaultClassLoader().getResource("").getPath() + "template/storageword/";;
  191. //生成文件名
  192. String wordName = "04-基准地价报告-02" + "-" + System.currentTimeMillis();
  193. writeWord(response, null, wordName, template);
  194. } catch (Exception e) {
  195. e.printStackTrace();
  196. log.error("基准地价报告导出异常,{}" + e);
  197. }
  198. }
  199. /**
  200. * 下载分析文档
  201. * @param fileName
  202. * @param response
  203. */
  204. @RequestMapping("/getWord")
  205. private void getWord( String fileName,HttpServletResponse response) {
  206. String filePath = ClassUtils.getDefaultClassLoader().getResource("").getPath() + "template/storageword/";
  207. // 设置请求头
  208. response.setContentType("application/octet-stream");
  209. response.addHeader("Content-Disposition", "attachment;filename=\"" + new String(fileName.getBytes(), StandardCharsets.UTF_8) + "\"");
  210. response.setHeader("download-filename", fileName);
  211. // 开放请求头download-filename前端获取权限
  212. response.setHeader("Access-Control-Expose-Headers", "download-filename");
  213. FileInputStream fis = null;
  214. OutputStream os = null;
  215. try {
  216. // 输出
  217. os = response.getOutputStream();
  218. File file = new File(filePath+fileName);
  219. if (!file.exists()) {
  220. throw new FileNotFoundException(filePath+fileName);
  221. }
  222. fis = new FileInputStream(file);
  223. byte[] b = new byte[1024];
  224. int length;
  225. while ((length = fis.read(b)) > 0) {
  226. os.write(b, 0, length);
  227. }
  228. } catch (Exception e) {
  229. e.printStackTrace();
  230. } finally {
  231. IOUtils.close(os);
  232. IOUtils.close(fis);
  233. }
  234. }
  235. /**
  236. * 封装-选址报告导出数据
  237. *
  238. * @return
  239. */
  240. private SiteSelectionReport setWordData1() {
  241. try {
  242. SiteSelectionReport siteSelectionReport = new SiteSelectionReport();
  243. // // 模拟数据 读取静态JSON文件填充数据 根据resource文件路径,生成文件
  244. // File jsonFile = ResourceUtils.getFile("classpath:template/json/word.json");
  245. // // 解析文件为指定编码的字符串
  246. // String json = FileUtils.readFileToString(jsonFile, "UTF-8");
  247. ClassPathResource classPathResource = new ClassPathResource("template/json/word.json");
  248. InputStream inputStream = classPathResource.getInputStream();
  249. String json = org.apache.commons.io.IOUtils.toString(inputStream, StandardCharsets.UTF_8);
  250. // 转换格式
  251. JSONObject jsonObject = JSON.parseObject(json);
  252. // 读取模拟数据 并赋值给导出对象
  253. String wordDataJson = jsonObject.get("wordData1").toString();
  254. siteSelectionReport = JSON.parseObject(wordDataJson, SiteSelectionReport.class);
  255. //模拟 影响因子数据 赋值给导出对象
  256. List<FactorData> factorDataList = new ArrayList<>();
  257. for (int i = 0; i < 6; i++) {
  258. factorDataList.add(new FactorData().FactorDataForSiteSelectionReport("测试因子名称" + i, "测试计算方法" + i, "测试因子约束" + i));
  259. }
  260. siteSelectionReport.setTableList(factorDataList);
  261. //模拟读取静态图片 赋值给导出对象
  262. String imgPath = ClassUtils.getDefaultClassLoader().getResource("").getPath() + "template/img/test.png";
  263. PictureRenderData pictureRenderData = Pictures.ofLocal(imgPath).size(300, 200).create();
  264. List<PicData> picList = new ArrayList<>();
  265. picList.add(new PicData(pictureRenderData));
  266. picList.add(new PicData(pictureRenderData));
  267. picList.add(new PicData(pictureRenderData));
  268. siteSelectionReport.setPicList(picList);
  269. // 读取分析JSON 用于生成选址范围现状分析导出数据格式
  270. String analyseJson = jsonObject.get("analyseJson").toString();
  271. List<JsonConvert> analyseConList = JSON.parseArray(analyseJson, JsonConvert.class);
  272. List<AnalyseData> analyseList = new ArrayList<>();
  273. Double totalArea = 0.0;
  274. String detail = "";
  275. //遍历选址范围现状分析 封装数据 默认数据为0-100
  276. for (JsonConvert jsonConvert : analyseConList) {
  277. String key = jsonConvert.getKey();
  278. String value = jsonConvert.getValue();
  279. List<JsonConvert> children = jsonConvert.getChildren();
  280. AnalyseData analyseData = null;
  281. if (children != null && children.size() > 0) {
  282. for (JsonConvert child : children) {
  283. String cKey = child.getKey();
  284. String cValue = child.getValue();
  285. //封装一级,二级,模拟设置分析值
  286. analyseData = new AnalyseData(key, value, cKey, cValue, new Random().nextDouble()*100, new Random().nextDouble()*100);
  287. analyseList.add(analyseData);
  288. //计算总面积
  289. if (analyseData.getAreaValue() != null) {
  290. totalArea = totalArea + analyseData.getAreaValue();
  291. }
  292. //拼接范围说明字符
  293. if (analyseData.getAreaValue() != null && analyseData.getPercentValue() != null) {
  294. detail = detail + analyseData.getSecondName() + analyseData.getAreaValue() + "平方米,占比" + analyseData.getPercentValue() + "%;";
  295. }
  296. }
  297. } else {
  298. analyseData = new AnalyseData(key, value, "", "", null, null);
  299. analyseList.add(analyseData);
  300. }
  301. }
  302. // 赋值导出对象
  303. siteSelectionReport.setTotalArea(totalArea + "");
  304. siteSelectionReport.setDetail(detail);
  305. siteSelectionReport.setAnalyseList(analyseList);
  306. return siteSelectionReport;
  307. } catch (Exception e) {
  308. e.printStackTrace();
  309. log.error("选址报告数据封装异常,{}" + e);
  310. }
  311. return null;
  312. }
  313. /**
  314. * 封装-合规性分析报告导出数据
  315. *
  316. * @return
  317. */
  318. private ComplianceAnalysisReport setWordData2() {
  319. try {
  320. ComplianceAnalysisReport complianceAnalysisReport = new ComplianceAnalysisReport();
  321. // // 模拟数据 读取静态JSON文件填充数据 根据resource文件路径,生成文件
  322. // File jsonFile = ResourceUtils.getFile("classpath:template/json/word.json");
  323. // // 解析文件为指定编码的字符串
  324. // String json = FileUtils.readFileToString(jsonFile, "UTF-8");
  325. ClassPathResource classPathResource = new ClassPathResource("template/json/word.json");
  326. InputStream inputStream = classPathResource.getInputStream();
  327. String json = org.apache.commons.io.IOUtils.toString(inputStream, StandardCharsets.UTF_8);
  328. // 转换格式
  329. JSONObject jsonObject = JSON.parseObject(json);
  330. // 读取模拟数据 并赋值给导出对象
  331. String wordDataJson = jsonObject.get("wordData2").toString();
  332. complianceAnalysisReport = JSON.parseObject(wordDataJson, ComplianceAnalysisReport.class);
  333. // 读取分析JSON 用于生成选址范围现状分析导出数据格式
  334. String analyseJson = jsonObject.get("analyseJson").toString();
  335. List<JsonConvert> analyseConList = JSON.parseArray(analyseJson, JsonConvert.class);
  336. List<AnalyseData> analyseList01 = new ArrayList<>();
  337. List<AnalyseData> analyseList02 = new ArrayList<>();
  338. Double totalArea01 = 0.0;
  339. String detail01 = "";
  340. Double totalArea02 = 0.0;
  341. String detail02 = "";
  342. //遍历选址范围 生成现状分析和控制性详细规划分析 封装数据 默认数据为0-100
  343. for (JsonConvert jsonConvert : analyseConList) {
  344. String key = jsonConvert.getKey();
  345. String value = jsonConvert.getValue();
  346. List<JsonConvert> children = jsonConvert.getChildren();
  347. AnalyseData analyseData01 = null;
  348. AnalyseData analyseData02 = null;
  349. if (children != null && children.size() > 0) {
  350. for (JsonConvert child : children) {
  351. String cKey = child.getKey();
  352. String cValue = child.getValue();
  353. //封装一级,二级,模拟设置分析值
  354. analyseData01 = new AnalyseData(key, value, cKey, cValue, new Random().nextDouble()*100, new Random().nextDouble()*100);
  355. analyseData02 = new AnalyseData(key, value, cKey, cValue, new Random().nextDouble()*100, new Random().nextDouble()*100);
  356. analyseList01.add(analyseData01);
  357. analyseList02.add(analyseData02);
  358. //计算现状分析总面积
  359. if (analyseData01.getAreaValue() != null) {
  360. totalArea01 = totalArea01 + analyseData01.getAreaValue();
  361. }
  362. //拼接现状分析范围说明字符
  363. if (analyseData01.getAreaValue() != null && analyseData01.getPercentValue() != null) {
  364. detail01 = detail01 + analyseData01.getSecondName() + analyseData01.getAreaValue() + "平方米,占比" + analyseData01.getPercentValue() + "%;";
  365. }
  366. //计算控制性详细规划分析总面积
  367. if (analyseData02.getAreaValue() != null) {
  368. totalArea02 = totalArea02 + analyseData02.getAreaValue();
  369. }
  370. //拼接控制性详细规划分析范围说明字符
  371. if (analyseData02.getAreaValue() != null && analyseData02.getPercentValue() != null) {
  372. detail02 = detail02 + analyseData02.getSecondName() + analyseData02.getAreaValue() + "平方米,占比" + analyseData02.getPercentValue() + "%;";
  373. }
  374. }
  375. } else {
  376. analyseData01 = new AnalyseData(key, value, "", "", null, null);
  377. analyseData02 = new AnalyseData(key, value, "", "", null, null);
  378. analyseList01.add(analyseData01);
  379. analyseList02.add(analyseData02);
  380. }
  381. }
  382. //生成分析因子->现状分析 数据 根据现状上面的现状分析计算而来
  383. List<FactorData> tableList01 = new ArrayList<>();
  384. if (analyseList01 != null && analyseList01.size() > 0) {
  385. Map<String, List<AnalyseData>> analyseCollect01 = analyseList01.stream().collect(Collectors.groupingBy(AnalyseData::getFirstCode));
  386. if (analyseCollect01 != null && analyseCollect01.size() > 0) {
  387. analyseCollect01.entrySet()
  388. .stream()
  389. .sorted(Map.Entry.comparingByKey())
  390. .forEach(entry -> {
  391. List<AnalyseData> analyseDataList = entry.getValue();
  392. Double sum = analyseDataList.stream().mapToDouble(obj -> obj.getAreaValue() != null ? obj.getAreaValue() : 0).sum();
  393. AnalyseData analyseData = analyseDataList.get(0);
  394. String firstCode = analyseData.getFirstCode();
  395. String firstName = analyseData.getFirstName();
  396. tableList01.add(new FactorData().FactorDataForComplianceAnalysisReport01(firstCode + " " + firstName, sum + ""));
  397. });
  398. }
  399. }
  400. //生成分析因子->控制性详细规划分析 数据 根据现状上面的控制性详细规划分析计算而来
  401. List<FactorData> tableList02 = new ArrayList<>();
  402. if (analyseList02 != null && analyseList02.size() > 0) {
  403. Map<String, List<AnalyseData>> analyseCollect02 = analyseList02.stream().collect(Collectors.groupingBy(AnalyseData::getFirstCode));
  404. if (analyseCollect02 != null && analyseCollect02.size() > 0) {
  405. analyseCollect02.entrySet()
  406. .stream()
  407. .sorted(Map.Entry.comparingByKey())
  408. .forEach(entry -> {
  409. List<AnalyseData> analyseDataList = entry.getValue();
  410. Double sum = analyseDataList.stream().mapToDouble(obj -> obj.getAreaValue() != null ? obj.getAreaValue() : 0).sum();
  411. AnalyseData analyseData = analyseDataList.get(0);
  412. String firstCode = analyseData.getFirstCode();
  413. String firstName = analyseData.getFirstName();
  414. tableList02.add(new FactorData().FactorDataForComplianceAnalysisReport02(firstCode + " " + firstName, sum + ""));
  415. });
  416. }
  417. }
  418. // 赋值导出对象
  419. complianceAnalysisReport.setTableList01(tableList01);
  420. complianceAnalysisReport.setTableList02(tableList02);
  421. complianceAnalysisReport.setAnalyseList01(analyseList01);
  422. complianceAnalysisReport.setTotalArea01(totalArea01);
  423. complianceAnalysisReport.setDetail01(detail01);
  424. complianceAnalysisReport.setAnalyseList02(analyseList02);
  425. complianceAnalysisReport.setTotalArea02(totalArea02);
  426. complianceAnalysisReport.setDetail02(detail02);
  427. return complianceAnalysisReport;
  428. } catch (Exception e) {
  429. e.printStackTrace();
  430. log.error("合规性分析报告封装异常,{}" + e);
  431. }
  432. return null;
  433. }
  434. /**
  435. * 封装-征收补偿预估报告导出数据
  436. *
  437. * @return
  438. */
  439. private CompensateEstimateReport setWordData3() {
  440. try {
  441. CompensateEstimateReport compensateEstimateReport = new CompensateEstimateReport();
  442. // // 模拟数据 读取静态JSON文件填充数据 根据resource文件路径,生成文件
  443. // File jsonFile = ResourceUtils.getFile("classpath:template/json/word.json");
  444. // // 解析文件为指定编码的字符串
  445. // String json = FileUtils.readFileToString(jsonFile, "UTF-8");
  446. ClassPathResource classPathResource = new ClassPathResource("template/json/word.json");
  447. InputStream inputStream = classPathResource.getInputStream();
  448. String json = org.apache.commons.io.IOUtils.toString(inputStream, StandardCharsets.UTF_8);
  449. // 转换格式
  450. JSONObject jsonObject = JSON.parseObject(json);
  451. // 读取模拟数据 并赋值给导出对象
  452. String wordDataJson = jsonObject.get("wordData3").toString();
  453. compensateEstimateReport = JSON.parseObject(wordDataJson, CompensateEstimateReport.class);
  454. //模拟读取静态图片 赋值给导出对象
  455. String imgPath = ClassUtils.getDefaultClassLoader().getResource("").getPath() + "template/img/test.png";
  456. PictureRenderData pictureRenderData = Pictures.ofLocal(imgPath).size(300, 200).create();
  457. List<PicData> picList = new ArrayList<>();
  458. picList.add(new PicData(pictureRenderData));
  459. picList.add(new PicData(pictureRenderData));
  460. picList.add(new PicData(pictureRenderData));
  461. compensateEstimateReport.setPicList(picList);
  462. //模拟 3.2房屋补偿费预估->各个房屋补偿情况 数据
  463. List<HouseCompensate> tableList01 = new ArrayList<>();
  464. for (int i = 0; i < 10; i++) {
  465. tableList01.add(new HouseCompensate(i, "房屋编号" + i, "产权人" + i, new Random().nextInt(100), "房屋结构" + i, new Random().nextInt(100), "房屋地址" + i));
  466. }
  467. //根据各个房屋补偿情况统计面积,拆迁款等内容 初始为0
  468. int numberEHD = 0;
  469. int feeECHD = 0;
  470. int totalFA = 0;
  471. int averageFeeECHD = 0;
  472. if (tableList01 != null && tableList01.size() > 0) {
  473. numberEHD = tableList01.size();
  474. feeECHD = tableList01.stream().mapToInt(obj -> obj.getFeeCHD() != null ? obj.getFeeCHD() : 0).sum();
  475. totalFA = tableList01.stream().mapToInt(obj -> obj.getFloorArea() != null ? obj.getFloorArea() : 0).sum();
  476. averageFeeECHD = feeECHD / totalFA;
  477. }
  478. // 赋值导出对象
  479. compensateEstimateReport.setTableList01(tableList01);
  480. compensateEstimateReport.setNumberEHD(numberEHD);
  481. compensateEstimateReport.setFeeECHD(feeECHD);
  482. compensateEstimateReport.setTotalFA(totalFA);
  483. compensateEstimateReport.setAverageFeeECHD(averageFeeECHD);
  484. return compensateEstimateReport;
  485. } catch (Exception e) {
  486. e.printStackTrace();
  487. log.error("征收补偿预估报告封装异常,{}" + e);
  488. }
  489. return null;
  490. }
  491. /**
  492. * 封装-基准地价报告导出数据
  493. *
  494. * @return
  495. */
  496. // private BenchmarkLandPriceReport setWordData4() {
  497. // try {
  498. // BenchmarkLandPriceReport benchmarkLandPriceReport = new BenchmarkLandPriceReport();
  499. // // 模拟数据 读取静态JSON文件填充数据 根据resource文件路径,生成文件
  500. // File jsonFile = ResourceUtils.getFile("classpath:template/json/word.json");
  501. // // 解析文件为指定编码的字符串
  502. // String json = FileUtils.readFileToString(jsonFile, "UTF-8");
  503. // // 转换格式
  504. // JSONObject jsonObject = JSON.parseObject(json);
  505. // // 读取模拟数据 并赋值给导出对象
  506. // String wordDataJson = jsonObject.get("wordData4").toString();
  507. // benchmarkLandPriceReport = JSON.parseObject(wordDataJson, BenchmarkLandPriceReport.class);
  508. //
  509. // //模拟读取静态图片 赋值给导出对象
  510. // String imgPath = ClassUtils.getDefaultClassLoader().getResource("").getPath() + "template/img/test.png";
  511. // PictureRenderData pictureRenderData = Pictures.ofLocal(imgPath).size(300, 200).create();
  512. // List<PicData> picList = new ArrayList<>();
  513. // picList.add(new PicData(pictureRenderData));
  514. // picList.add(new PicData(pictureRenderData));
  515. // picList.add(new PicData(pictureRenderData));
  516. // benchmarkLandPriceReport.setPicList(picList);
  517. //
  518. // //模拟 用地地价预估情况 数据,分为 国有建设用地 集体建设用地
  519. // List<LandPriceEstimation> tableList01 = new ArrayList<>();
  520. // List<LandPriceEstimation> tableList02 = new ArrayList<>();
  521. // for (int i = 0; i < 6; i++) {
  522. // tableList01.add(new LandPriceEstimation(LvArray[i], new Random().nextDouble()*100, new Random().nextDouble()*100, new Random().nextDouble()*100));
  523. // tableList02.add(new LandPriceEstimation(LvArray[i], new Random().nextDouble()*100, new Random().nextDouble()*100, new Random().nextDouble()*100));
  524. // }
  525. //
  526. // //国有建设用地面积 国有土地地价预估 默认0
  527. // Double areaSCL = 0.0;
  528. // Double landPriceESCL = 0.0;
  529. // //集体建设用地面积 集体土地地价预估 默认0
  530. // Double areaCCL = 0.0;
  531. // Double landPriceECCL = 0.0;
  532. // //总面积 总地价预估 默认0
  533. // Double totalArea = 0.0;
  534. // Double totalLandPrice = 0.0;
  535. // if (tableList01 != null && tableList01.size() > 0) {
  536. // areaSCL = tableList01.stream().mapToDouble(obj -> obj.getArea() != null ? obj.getArea() : 0).sum();
  537. // landPriceESCL = tableList01.stream().mapToDouble(obj -> obj.getLandPriceES() != null ? obj.getLandPriceES() : 0).sum();
  538. // }
  539. // if (tableList02 != null && tableList02.size() > 0) {
  540. // areaCCL = tableList02.stream().mapToDouble(obj -> obj.getArea() != null ? obj.getArea() : 0).sum();
  541. // landPriceECCL = tableList02.stream().mapToDouble(obj -> obj.getLandPriceES() != null ? obj.getLandPriceES() : 0).sum();
  542. // }
  543. // if (areaSCL > 0 && areaCCL > 0) {
  544. // totalArea = areaSCL + areaCCL;
  545. // }
  546. // if (landPriceESCL > 0 && landPriceECCL > 0) {
  547. // totalLandPrice = landPriceESCL + landPriceECCL;
  548. // }
  549. // // 赋值导出对象
  550. // benchmarkLandPriceReport.setTableList01(tableList01);
  551. // benchmarkLandPriceReport.setTableList02(tableList02);
  552. // benchmarkLandPriceReport.setTotalArea(totalArea);
  553. // benchmarkLandPriceReport.setTotalLandPrice(totalLandPrice);
  554. // benchmarkLandPriceReport.setAreaSCL(areaSCL);
  555. // benchmarkLandPriceReport.setLandPriceESCL(landPriceESCL);
  556. // benchmarkLandPriceReport.setAreaCCL(areaCCL);
  557. // benchmarkLandPriceReport.setLandPriceECCL(landPriceECCL);
  558. // return benchmarkLandPriceReport;
  559. // } catch (Exception e) {
  560. // e.printStackTrace();
  561. // log.error("基准地价报告数据封装异常,{}" + e);
  562. // }
  563. // return null;
  564. // }
  565. /**
  566. * word写出
  567. *
  568. * @param response
  569. * @param temDir
  570. * @param wordName
  571. * @param template
  572. * @throws Exception
  573. */
  574. private void writeWord(HttpServletResponse response, String temDir, String wordName, XWPFTemplate template) throws Exception {
  575. // 生成的word格式
  576. String formatSuffix = ".docx";
  577. // 拼接后的文件名 文件名.带后缀
  578. String fileName = wordName + formatSuffix;
  579. //=================生成word到设置浏览默认下载地址=================
  580. // 设置强制下载不打开
  581. response.setContentType("application/force-download");
  582. response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
  583. // 设置文件名
  584. response.addHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8"));
  585. OutputStream out = response.getOutputStream();
  586. template.write(out);
  587. out.flush();
  588. out.close();
  589. template.close();
  590. //删除临时文件
  591. if(StringUtils.isNotEmpty(temDir)){
  592. File file = new File(temDir + fileName);
  593. file.setWritable(true);
  594. file.delete();
  595. }
  596. }
  597. /**
  598. * word存储
  599. *
  600. * @param response
  601. * @param temDir
  602. * @param wordName
  603. * @param template
  604. * @throws Exception
  605. */
  606. private String storageWord(HttpServletResponse response, String temDir, String wordName, XWPFTemplate template) throws Exception {
  607. // 生成的word格式
  608. String formatSuffix = ".docx";
  609. // 拼接后的文件名 文件名.带后缀
  610. String fileName = wordName + formatSuffix;
  611. File file = new File(temDir + fileName);
  612. FileOutputStream out = new FileOutputStream(file);
  613. template.write(out);
  614. out.close();
  615. return fileName;
  616. }
  617. }