FzxzSchedule.java 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  1. package com.onemap.analyse.task;
  2. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  3. import com.google.common.base.Joiner;
  4. import com.onemap.analyse.domain.*;
  5. import com.onemap.analyse.mapper.*;
  6. import com.onemap.analyse.utils.NpoiHelper;
  7. import com.onemap.analyse.utils.NumberUtil;
  8. import com.onemap.common.core.utils.StringUtils;
  9. import net.lingala.zip4j.core.ZipFile;
  10. import net.lingala.zip4j.exception.ZipException;
  11. import net.lingala.zip4j.model.ZipParameters;
  12. import net.lingala.zip4j.util.Zip4jConstants;
  13. import org.apache.commons.io.FileUtils;
  14. import org.apache.poi.xwpf.usermodel.*;
  15. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
  16. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
  17. import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
  18. import org.springframework.beans.factory.annotation.Value;
  19. import org.springframework.scheduling.annotation.Async;
  20. import org.springframework.scheduling.annotation.Scheduled;
  21. import org.springframework.stereotype.Component;
  22. import javax.annotation.Resource;
  23. import java.io.*;
  24. import java.math.BigInteger;
  25. import java.text.SimpleDateFormat;
  26. import java.util.*;
  27. import java.util.stream.Collectors;
  28. /**
  29. * 辅助选址模块定时刷新任务
  30. */
  31. @Component
  32. public class FzxzSchedule {
  33. //间隔时间 单位ms
  34. public static final long TIME_INTERVAL = 1 * 15000;
  35. //任务状态标识码
  36. private RwztDTO Rwzt = new RwztDTO();
  37. @Resource
  38. private FzxzMapper fzxzMapper;
  39. @Resource
  40. private FxrwrzMapper fxrwrzMapper;
  41. @Resource
  42. private FzxzJgGISMapper fzxzJgGISMapper;
  43. @Resource
  44. private FzxzXzyzMapper fzxzXzyzMapper;
  45. @Resource
  46. private FzxzTbMapper fzxzTbMapper;
  47. @Resource
  48. private FzxzJgyzMapper fzxzJgyzMapper;
  49. //辅助选址python方法名标识
  50. @Value("${Fzxz.functionId}")
  51. private String functionId;
  52. @Value("${Fzxz.mapFunctionId}")
  53. private String mapFunctionId;
  54. @Value("${Fzxz.fzxzShp}")
  55. private String fzxzShp;
  56. @Value("${Hgxfx.temp}")
  57. private String temp;
  58. /**
  59. * Scheduled 定时器参数
  60. * cron表达式:指定任务在特定时间执行
  61. * fixedDelay:表示上一次任务执行完成后多久再执行,参数类型long,单位:ms
  62. * fixedDelayString:与fixedDelay一样,只是参数类型是String
  63. * fixedRate:表示按一定的频率执行任务,参数类型long,单位:ms 如: fixedRate(5000),表示这个定时器任务每5秒执行一次
  64. * fixedRateString:与fixedRate一样,只是参数类型变为String
  65. * initialDelay:表示延迟多久再第一次执行任务,参数类型为long ,单位:ms
  66. * initialDelayString:与initialDelay一样,只是参数类型String
  67. */
  68. /**
  69. * @Async("name") 开启多线程
  70. */
  71. /**
  72. * 辅助选址执行入口
  73. */
  74. @Async("taskExecutor")
  75. @Scheduled(fixedDelay = TIME_INTERVAL)
  76. public void FzxzStatus() {
  77. try {
  78. QueryWrapper<FzxzDTO> wrapper = new QueryWrapper<FzxzDTO>();
  79. wrapper.eq("rwzt", Rwzt.getCreate());
  80. //以创建时间正序排列
  81. wrapper.orderByAsc("cjsj");
  82. //每次只操作一条记录
  83. List<FzxzDTO> ress = fzxzMapper.selectList(wrapper);
  84. if (ress.size() > 0) {
  85. FzxzDTO res = ress.get(0);
  86. //插入执行状态
  87. res.setRwzt(Rwzt.getRun());
  88. res.setRwkssj(new Date());
  89. QueryWrapper<FzxzDTO> query = new QueryWrapper<FzxzDTO>();
  90. query.eq("bsm", res.getBsm());
  91. fzxzMapper.update(res, query);
  92. //插入任务日志
  93. FxrwrzDTO rzDto = new FxrwrzDTO();
  94. rzDto.setRwbsm(res.getBsm());
  95. rzDto.setRwlx("辅助选址");
  96. rzDto.setRzlr("开始分析");
  97. rzDto.setRzlx("info");
  98. rzDto.setRzsj(new Date());
  99. fxrwrzMapper.insert(rzDto);
  100. //执行python
  101. Map<String, String> params = new HashMap<>();
  102. params.put("bsm", res.getBsm());
  103. String result = PythonExecute.Run(functionId, params);
  104. if (!StringUtils.isEmpty(result) && result.contains("OK")) {//成功
  105. res.setRwjssj(new Date());
  106. res.setRwzt(Rwzt.getComplete());
  107. //TODO 生成辅助选址结果报告 返回 FxReport实体
  108. FzxzReport report = createReport(res.getBsm(), null);
  109. if (report != null) {
  110. res.setFxbg(report.getReportfile().replace(".docx", ".pdf"));
  111. res.setZip(report.getZipfile());
  112. }
  113. fzxzMapper.update(res, query);
  114. //插入成功日志
  115. FxrwrzDTO successDto = new FxrwrzDTO();
  116. successDto.setRwbsm(res.getBsm());
  117. successDto.setRwlx("辅助选址");
  118. successDto.setRzlr("计算完成");
  119. successDto.setRzlx("info");
  120. successDto.setRzsj(new Date());
  121. fxrwrzMapper.insert(successDto);
  122. } else {//失败
  123. res.setRwjssj(new Date());
  124. res.setRwzt(Rwzt.getError());
  125. fzxzMapper.update(res, query);
  126. //插入任务日志
  127. FxrwrzDTO errorDto = new FxrwrzDTO();
  128. errorDto.setRwbsm(res.getBsm());
  129. errorDto.setRwlx("辅助选址");
  130. errorDto.setRzlr("计算错误");
  131. errorDto.setRzsj(new Date());
  132. errorDto.setRzlx("error");
  133. errorDto.setFxjg(result);
  134. fxrwrzMapper.insert(errorDto);
  135. }
  136. System.out.println("辅助选址模块:" + res.getBsm() + "完成!!!!!!!! " + new Date().toString());
  137. }
  138. } catch (Exception e) {
  139. e.printStackTrace();
  140. }
  141. }
  142. //根据选址任务生成报告
  143. public FzxzReport createReport(String bsm, List<String> xzbsmList) {
  144. //插入开始生成报告日志
  145. FxrwrzDTO successDto = new FxrwrzDTO();
  146. successDto.setRwbsm(bsm);
  147. successDto.setRwlx("辅助选址");
  148. successDto.setRzlr("开始生成选址报告");
  149. successDto.setRzlx("info");
  150. successDto.setRzsj(new Date());
  151. fxrwrzMapper.insert(successDto);
  152. FzxzEntityDTO res = fzxzMapper.GetFzxzByBsm(bsm);
  153. FzxzReport fzxzReport = new FzxzReport();
  154. if (res != null) {
  155. //文件硬盘真实路径
  156. String timeStamp = new Date().getTime() + "";
  157. String rootPath = temp + File.separator + "辅助选址报告" + File.separator + res.getBsm();
  158. File f = new File(rootPath);
  159. if (!f.exists()) {
  160. f.mkdirs();
  161. }
  162. String reportPath = res.getXmmc() + "(选址报告)_" + timeStamp + ".docx";
  163. String simplePath = res.getXmmc() + "(选址简报)_" + timeStamp + ".docx";
  164. String zipPath = res.getXmmc() + "_" + timeStamp + ".zip";
  165. //选址报告
  166. createReport(res, rootPath + File.separator + reportPath, xzbsmList);
  167. //选址简报
  168. createSimpleReport(res, rootPath + File.separator + simplePath, xzbsmList);
  169. //zip打包
  170. createZipReport(res, rootPath + File.separator + zipPath
  171. , rootPath + File.separator + reportPath
  172. , rootPath + File.separator + simplePath);
  173. //生成返回结果
  174. fzxzReport.setRootPath(StringUtils.getFileStaticPath(rootPath));
  175. fzxzReport.setReportfile(StringUtils.getFileStaticPath(rootPath + File.separator + reportPath));
  176. fzxzReport.setSimplefile(StringUtils.getFileStaticPath(rootPath + File.separator + simplePath));
  177. fzxzReport.setZipfile(StringUtils.getFileStaticPath(rootPath + File.separator + zipPath));
  178. fzxzReport.setBsm(bsm);
  179. fzxzReport.setFxbg(fzxzReport.getReportfile());
  180. fzxzReport.setJsdw(res.getJsdw());
  181. fzxzReport.setXmmc(res.getXmmc());
  182. //查询分析结果图斑信息
  183. QueryWrapper<FzxzJgGisDTO> giswrapper = new QueryWrapper<FzxzJgGisDTO>();
  184. giswrapper.eq("rwbsm", res.getBsm());
  185. List<FzxzJgGisDTO> gisinfo = fzxzJgGISMapper.selectList(giswrapper);
  186. fzxzReport.setItems(gisinfo);
  187. }
  188. //插入开始生成报告日志
  189. FxrwrzDTO successDto2 = new FxrwrzDTO();
  190. successDto2.setRwbsm(bsm);
  191. successDto2.setRwlx("辅助选址");
  192. successDto2.setRzlr("选址报告生成结束");
  193. successDto2.setRzlx("info");
  194. successDto2.setRzsj(new Date());
  195. fxrwrzMapper.insert(successDto2);
  196. return fzxzReport;
  197. }
  198. /**
  199. * 选址报告
  200. */
  201. private void createReport(FzxzEntityDTO res, String reportPath, List<String> xzbsmList) {
  202. List<String> imgList = ReportImg(res, xzbsmList);
  203. //创建document文档对象对象实例
  204. XWPFDocument document = null;
  205. OutputStream outputStream = null;//把doc输出到输出流
  206. try {
  207. document = new XWPFDocument();
  208. int pos = 0;
  209. //文本标题
  210. NpoiHelper.title(document, res.getXmmc() + "选址报告", pos++);
  211. SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
  212. NpoiHelper.Option optionCENTER = new NpoiHelper.Option();
  213. optionCENTER.setAlign(ParagraphAlignment.CENTER);
  214. document.setParagraph(NpoiHelper.newParagraph(document, sdf.format(new Date()), optionCENTER), pos++);
  215. NpoiHelper.catalog(document, "一、项目选址信息", pos++);
  216. String ydmj = res.getYdmjbegin() + " - " + res.getYdmjend();
  217. if (StringUtils.isEmpty(res.getYdmjbegin()) || Double.parseDouble(res.getYdmjbegin()) == 0)
  218. ydmj = "≤" + res.getYdmjend();
  219. else if (StringUtils.isEmpty(res.getYdmjend()) || Double.parseDouble(res.getYdmjend()) == 0)
  220. ydmj = "≥" + res.getYdmjbegin();
  221. List<String> listTemp = new ArrayList<String>();
  222. listTemp.add("\t项目名称:" + res.getXmmc());
  223. listTemp.add("\t建设单位:" + res.getJsdw());
  224. listTemp.add("\t用地面积:" + ydmj + "平方米");
  225. listTemp.add("\t用地性质:" + res.getYdmc());
  226. listTemp.add("\t影响因子:");
  227. NpoiHelper.content(document, listTemp, pos++);
  228. List tabYzTitles = new ArrayList<NpoiHelper.TableTitle>();
  229. NpoiHelper.TableTitle tableTitle = new NpoiHelper.TableTitle();
  230. tableTitle.setKey("YXYZMC");
  231. tableTitle.setName("因子名称");
  232. tableTitle.setWidth(2880);
  233. tabYzTitles.add(tableTitle);
  234. tableTitle = new NpoiHelper.TableTitle();
  235. tableTitle.setKey("YZTJ_TEXT");
  236. tableTitle.setName("影响条件");
  237. tableTitle.setAlign(ParagraphAlignment.CENTER);
  238. tableTitle.setWidth(2880);
  239. tabYzTitles.add(tableTitle);
  240. tableTitle = new NpoiHelper.TableTitle();
  241. tableTitle.setKey("YXZ");
  242. tableTitle.setName("约束范围值(M)");
  243. tableTitle.setAlign(ParagraphAlignment.CENTER);
  244. tableTitle.setWidth(2880);
  245. tabYzTitles.add(tableTitle);
  246. //查询选址因子,并且转换为List<Map> 因子条件(包含:C、不包含:N、分析:A)
  247. QueryWrapper<FzxzXzyzDTO> wrapper = new QueryWrapper<FzxzXzyzDTO>();
  248. wrapper.eq("rwbsm", res.getBsm());
  249. List<FzxzXzyzDTO> fzxzXzyzDTOList = fzxzXzyzMapper.selectList(wrapper);
  250. List<Map<String, Object>> dataTablelist = new ArrayList<>();
  251. for (int i = 0; i < fzxzXzyzDTOList.size(); i++) {
  252. Map<String, Object> map = new HashMap<>();
  253. FzxzXzyzDTO fzxzXzyzDTO = fzxzXzyzDTOList.get(i);
  254. map.put("YXYZMC", fzxzXzyzDTO.getYxyzmc());
  255. String yztj = "";
  256. if (fzxzXzyzDTO.getYztj() != null) {
  257. if ("C".equals(fzxzXzyzDTO.getYztj().toUpperCase())) {
  258. yztj = "包含";
  259. }
  260. if ("N".equals(fzxzXzyzDTO.getYztj().toUpperCase())) {
  261. yztj = "不包含";
  262. }
  263. if ("A".equals(fzxzXzyzDTO.getYztj().toUpperCase()) || "F".equals(fzxzXzyzDTO.getYztj().toUpperCase())) {
  264. yztj = "分析";
  265. }
  266. }
  267. map.put("YZTJ_TEXT", yztj);
  268. String yxz = "";
  269. if (StringUtils.isNotEmpty(fzxzXzyzDTO.getYxz())) {
  270. yxz = fzxzXzyzDTO.getYxz();
  271. }
  272. map.put("YXZ", yxz);
  273. dataTablelist.add(map);
  274. }
  275. XWPFTable tableYz = NpoiHelper.setComTable(document, tabYzTitles, dataTablelist, "表1:选址影响因子", pos++);
  276. NpoiHelper.catalog(document, "二、选址范围分析", pos++);
  277. //查询辅助选址分析图斑
  278. QueryWrapper<FzxzTbEntityDTO> wrapperTb = new QueryWrapper<FzxzTbEntityDTO>();
  279. wrapperTb.eq("rwbsm", res.getBsm());
  280. wrapperTb.groupBy("objectid", "rwbsm", "ydyhfldm", "ydyhflmc", "frequency", "sum_mj");
  281. List<FzxzTbEntityDTO> fzxzTbEntityDTOList = fzxzTbMapper.selectList(wrapperTb);
  282. //计算面积总和
  283. double sumcount = 0;
  284. for (int i = 0; i < fzxzTbEntityDTOList.size(); i++) {
  285. sumcount = sumcount + fzxzTbEntityDTOList.get(i).getSumMj();
  286. }
  287. List<FzxzTbEntityDTO> list = fzxzTbEntityDTOList.stream().sorted(
  288. Comparator.comparing(FzxzTbEntityDTO::getSumMj).reversed()
  289. ).collect(Collectors.toList());
  290. String fwfx = "\t根据指定选址分析得出,所选范围共" + Math.round(sumcount) + "平方米,其中";
  291. List<Map<String, Object>> fztable = new ArrayList<>();
  292. //生成地类分析统计表
  293. for (int i = 0; i < list.size(); i++) {
  294. FzxzTbEntityDTO item = list.get(i);
  295. String zb = String.format("%.1f", item.getSumMj() / sumcount * 100);
  296. if (i < 5) {
  297. //面积前五的占比地类计算
  298. fwfx += item.getYdyhflmc()
  299. + Math.round(item.getSumMj()) + "平方米,占比" + zb + "%;";
  300. }
  301. //List<FzxzTbEntityDTO>转List<Map<String, Object>> 地类编码 地类名称 面积(平方千米) 占比(%)
  302. Map map = new HashMap();
  303. map.put("ZB", zb);
  304. map.put("YDYHFLDM", item.getYdyhfldm());
  305. map.put("YDYHFLMC", item.getYdyhflmc());
  306. map.put("SUM_MJ", Math.round(item.getSumMj()));
  307. fztable.add(map);
  308. }
  309. fwfx = fwfx.substring(0, fwfx.length() - 1) + "。详情如下:";
  310. NpoiHelper.content(document, fwfx, pos++);
  311. //地类编码 地类名称 面积(平方千米) 占比(%)
  312. List<NpoiHelper.TableTitle> tabYztbTitles = new ArrayList();
  313. NpoiHelper.TableTitle tableTitleTb = new NpoiHelper.TableTitle();
  314. tableTitleTb.setKey("YDYHFLDM");
  315. tableTitleTb.setName("地类编码");
  316. tableTitleTb.setAlign(ParagraphAlignment.CENTER);
  317. tableTitleTb.setWidth(2010);
  318. tabYztbTitles.add(tableTitleTb);
  319. tableTitleTb = new NpoiHelper.TableTitle();
  320. tableTitleTb.setKey("YDYHFLMC");
  321. tableTitleTb.setName("地类名称");
  322. tableTitleTb.setWidth(2910);
  323. tabYztbTitles.add(tableTitleTb);
  324. tableTitleTb = new NpoiHelper.TableTitle();
  325. tableTitleTb.setKey("SUM_MJ");
  326. tableTitleTb.setName("面积(平方千米)");
  327. tableTitleTb.setAlign(ParagraphAlignment.CENTER);
  328. tableTitleTb.setWidth(1870);
  329. tabYztbTitles.add(tableTitleTb);
  330. tableTitleTb = new NpoiHelper.TableTitle();
  331. tableTitleTb.setKey("ZB");
  332. tableTitleTb.setName("占比(%)");
  333. tableTitleTb.setAlign(ParagraphAlignment.CENTER);
  334. tableTitleTb.setWidth(1700);
  335. tabYztbTitles.add(tableTitleTb);
  336. XWPFTable tableYztb = NpoiHelper.setComTable(document, tabYztbTitles, fztable, "表2:地类分析统计表", pos++);
  337. //查询结果GIS FzxzJgGisDTO
  338. QueryWrapper<FzxzJgGisDTO> giswrapper = new QueryWrapper<FzxzJgGisDTO>();
  339. giswrapper.eq("rwbsm", res.getBsm());
  340. //筛选方案,根据传参或者预选方案
  341. // giswrapper.ne("yxfa", "0");
  342. if (xzbsmList != null) {
  343. giswrapper.in("bsm", xzbsmList);
  344. }
  345. List<FzxzJgGisDTO> fzxzJgGisDTOList = fzxzJgGISMapper.selectList(giswrapper);
  346. NpoiHelper.catalog(document, "三、选址分析", pos++);
  347. String ydmc = res.getYdmc();
  348. if(!StringUtils.isEmpty(ydmc)){
  349. if (ydmc.lastIndexOf("用地") == ydmc.length() - 2) {
  350. ydmc = res.getYdmc().substring(0, ydmc.length() - 2);
  351. }
  352. NpoiHelper.content(document, "\t按照项目选址要求和影响因子,经对所选范围内的所有"
  353. + ydmc + "用地进行分析,筛选出符合要求的方案共" + fzxzJgGisDTOList.size() + "个。", pos++);
  354. }else{
  355. NpoiHelper.content(document, "\t按照项目选址要求和影响因子,经对所选范围内的所有用地进行分析,筛选出符合要求的方案共" + fzxzJgGisDTOList.size() + "个。", pos++);
  356. }
  357. int index = 1;
  358. NpoiHelper.Option optionIsBold = new NpoiHelper.Option();
  359. optionIsBold.setBold(true);
  360. for (int i = 0; i < fzxzJgGisDTOList.size(); i++) {
  361. FzxzJgGisDTO item = fzxzJgGisDTOList.get(i);
  362. document.setParagraph(NpoiHelper.newParagraph(document, index + "、方案" + NumberUtil.int2chineseNum(index)
  363. , optionIsBold), pos++);
  364. XWPFTable table = document.createTable(7, 4);
  365. table.setWidth(9000);//总宽度
  366. for (int k = 0; k < 4; k++) {
  367. XWPFTableCell cell = table.getRow(0).getCell(k);
  368. CTTcPr ctTcPr = cell.getCTTc().isSetTcPr() ? cell.getCTTc().getTcPr() : cell.getCTTc().addNewTcPr();
  369. CTTblWidth ctTblWidth = ctTcPr.addNewTcW();
  370. ctTblWidth.setW(BigInteger.valueOf(2250));
  371. ctTblWidth.setType(STTblWidth.DXA);
  372. }
  373. //Table 表格第一行
  374. table.getRow(0).getCell(0).setColor("DBE5F1");
  375. NpoiHelper.mergeHorizontal(table, 0, 0, 3);
  376. table.getRow(0).getCell(0).setParagraph(NpoiHelper.setCellText(table, "符合用地情况", optionIsBold));
  377. //Table 表格第二行
  378. table.getRow(1).getCell(0).setParagraph(NpoiHelper.setCellText(table, "地类编码", optionIsBold));
  379. table.getRow(1).getCell(1).setParagraph(NpoiHelper.setCellText(table, item.getDlbm(), null));
  380. table.getRow(1).getCell(2).setParagraph(NpoiHelper.setCellText(table, "地类名称", optionIsBold));
  381. table.getRow(1).getCell(3).setParagraph(NpoiHelper.setCellText(table, item.getDlmc(), null));
  382. //Table 表格第三行
  383. table.getRow(2).getCell(0).setParagraph(NpoiHelper.setCellText(table, "地块面积", optionIsBold));
  384. table.getRow(2).getCell(1).setParagraph(NpoiHelper.setCellText(table, Math.round(item.getTbmj()) + "平方米", null));
  385. table.getRow(2).getCell(2).setParagraph(NpoiHelper.setCellText(table, "土地位置", optionIsBold));
  386. table.getRow(2).getCell(3).setParagraph(NpoiHelper.setCellText(table, item.getXzqmc(), null));
  387. //Table 表格第四行
  388. table.getRow(3).getCell(0).setColor("DBE5F1");
  389. NpoiHelper.mergeHorizontal(table, 3, 0, 3);
  390. table.getRow(3).getCell(0).setParagraph(NpoiHelper.setCellText(table, "地块位置信息", optionIsBold));
  391. //Table 表格第五行 -专题图
  392. NpoiHelper.mergeHorizontal(table, 4, 0, 1);
  393. NpoiHelper.mergeHorizontal(table, 4, 2, 3);
  394. String mapF = "";
  395. String mapM = "";
  396. if (imgList != null && imgList.size() > 0) {
  397. for (int j = 0; j < imgList.size(); j++) {
  398. String s = imgList.get(j);
  399. if (s.indexOf(item.getBsm() + "_F") >= 0) {
  400. mapF = s;//全局
  401. }
  402. if (s.indexOf(item.getBsm() + "_M") >= 0) {
  403. mapM = s;//局部
  404. }
  405. }
  406. }
  407. if (StringUtils.isNotEmpty(mapF)) {
  408. FileInputStream fis = null;
  409. try {
  410. fis = new FileInputStream(new File(mapF));
  411. XWPFParagraph paragraph = table.getRow(4).getCell(0).addParagraph();
  412. paragraph.setAlignment(ParagraphAlignment.CENTER);
  413. XWPFRun run = paragraph.createRun();
  414. run.addPicture(
  415. fis, // 条形码图片的位置
  416. Document.PICTURE_TYPE_JPEG, // 图片类型
  417. item.getBsm() + "_F.jpeg", // 图片名称
  418. 2200000, // 图片的长
  419. 1700000 // 图片的宽
  420. );
  421. run.addBreak(BreakType.TEXT_WRAPPING);
  422. run.setText("(宏观位置)");
  423. } catch (Exception e) {
  424. System.out.println(e.toString());
  425. } finally {
  426. if (fis != null) {
  427. try {
  428. fis.close();
  429. } catch (IOException e) {
  430. throw e;
  431. }
  432. }
  433. }
  434. }
  435. if (StringUtils.isNotEmpty(mapM)) {
  436. FileInputStream fis = null;
  437. try {
  438. fis = new FileInputStream(new File(mapM));
  439. XWPFParagraph paragraph = table.getRow(4).getCell(2).addParagraph();
  440. paragraph.setAlignment(ParagraphAlignment.CENTER);
  441. XWPFRun run = paragraph.createRun();
  442. run.addPicture(
  443. fis, // 条形码图片的位置
  444. Document.PICTURE_TYPE_JPEG, // 图片类型
  445. item.getBsm() + "_M.jpeg", // 图片名称
  446. 2200000, // 图片的长
  447. 1700000 // 图片的宽
  448. );
  449. run.addBreak(BreakType.TEXT_WRAPPING);
  450. run.setText("(具体位置)");
  451. } catch (Exception e) {
  452. System.out.println(e.toString());
  453. throw e;
  454. } finally {
  455. if (fis != null) {
  456. try {
  457. fis.close();
  458. } catch (IOException e) {
  459. throw e;
  460. }
  461. }
  462. }
  463. }
  464. //Table 表格第六行
  465. table.getRow(5).getCell(0).setColor("DBE5F1");
  466. NpoiHelper.mergeHorizontal(table, 5, 0, 3);
  467. table.getRow(5).getCell(0).setParagraph(NpoiHelper.setCellText(table, "分析结论", optionIsBold));
  468. //Table 表格第七行
  469. NpoiHelper.mergeHorizontal(table, 6, 0, 3);
  470. XWPFParagraph para6 = table.getRow(6).getCell(0).addParagraph();
  471. para6.setAlignment(ParagraphAlignment.LEFT);
  472. //查询辅助选址因子
  473. QueryWrapper<FzxzJgyzDTO> jgyzWrapper = new QueryWrapper<FzxzJgyzDTO>();
  474. jgyzWrapper.eq("jbbsm", item.getBsm());
  475. jgyzWrapper.ne("yztj", "F");
  476. List<FzxzJgyzDTO> jgyzList = fzxzJgyzMapper.selectList(jgyzWrapper);
  477. int jgyzid = 1;
  478. for (int j = 0; j < jgyzList.size(); j++) {
  479. FzxzJgyzDTO fzxzJgyzDTO = jgyzList.get(j);
  480. XWPFRun run = para6.createRun();
  481. run.setText(jgyzid + "、" + fzxzJgyzDTO.getFxjg());
  482. run.addBreak(BreakType.TEXT_WRAPPING);
  483. jgyzid++;
  484. }
  485. //查询辅助选址因子
  486. jgyzWrapper = new QueryWrapper<FzxzJgyzDTO>();
  487. jgyzWrapper.eq("jbbsm", item.getBsm());
  488. jgyzWrapper.eq("yztj", "F");
  489. jgyzList = fzxzJgyzMapper.selectList(jgyzWrapper);
  490. for (int j = 0; j < jgyzList.size(); j++) {
  491. FzxzJgyzDTO jgyz = jgyzList.get(j);
  492. XWPFRun run = para6.createRun();
  493. String jg = jgyz.getFxjg();
  494. Double jgDouble = 0.0;
  495. try {
  496. jgDouble = Double.parseDouble(jg);
  497. run.setText(jgyzid + "、压占" + jgyz.getYxyzmc() + "面积" + jg + "平方米");
  498. } catch (Exception e) {
  499. run.setText(jg);
  500. }
  501. run.addBreak(BreakType.TEXT_WRAPPING);
  502. jgyzid++;
  503. }
  504. index++;
  505. }
  506. File fileDoc = new File(reportPath);
  507. if (fileDoc.exists()) {
  508. FileUtils.forceDelete(fileDoc);
  509. }
  510. //word文件输出流
  511. outputStream = new FileOutputStream(reportPath);
  512. document.write(outputStream);
  513. } catch (Exception e) {
  514. e.printStackTrace();
  515. //插入任务日志
  516. FxrwrzDTO errorDto = new FxrwrzDTO();
  517. errorDto.setRwbsm(res.getBsm());
  518. errorDto.setRwlx("辅助选址");
  519. errorDto.setRzlr("生成选址报告错误:" + e.getMessage());
  520. errorDto.setRzsj(new Date());
  521. errorDto.setRzlx("error");
  522. fxrwrzMapper.insert(errorDto);
  523. } finally {
  524. if (document != null) {
  525. try {
  526. document.close();
  527. } catch (Exception ex) {
  528. System.out.println(ex.toString());
  529. }
  530. }
  531. if (outputStream != null) {
  532. try {
  533. outputStream.close();
  534. } catch (IOException e) {
  535. e.printStackTrace();
  536. }
  537. }
  538. }
  539. //转PDF
  540. String outfilepath = reportPath.replace(".docx", ".pdf");
  541. NpoiHelper.doc2pdf(reportPath, outfilepath);
  542. System.out.println(outfilepath);
  543. }
  544. /**
  545. * 选址简报
  546. */
  547. private void createSimpleReport(FzxzEntityDTO res, String reportPath, List<String> xzbsmList) {
  548. //创建document文档对象对象实例
  549. XWPFDocument document = null;
  550. OutputStream outputStream = null;//把doc输出到输出流
  551. try {
  552. //创建document文档对象对象实例
  553. document = new XWPFDocument();
  554. int pos = 0;
  555. NpoiHelper.Option optionCENTER = new NpoiHelper.Option();
  556. optionCENTER.setAlign(ParagraphAlignment.CENTER);
  557. //文本标题
  558. NpoiHelper.title(document, res.getXmmc() + "选址简报", pos++);
  559. SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
  560. document.setParagraph(NpoiHelper.newParagraph(document, sdf.format(new Date()), optionCENTER), pos++);
  561. NpoiHelper.catalog(document, "一、项目选址信息", pos++);
  562. XWPFTable table = document.createTable(4, 4);
  563. table.setWidth(8500);//总宽度
  564. for (int k = 0; k < 4; k++) {
  565. XWPFTableCell cell = table.getRow(0).getCell(k);
  566. CTTcPr ctTcPr = cell.getCTTc().isSetTcPr() ? cell.getCTTc().getTcPr() : cell.getCTTc().addNewTcPr();
  567. CTTblWidth ctTblWidth = ctTcPr.addNewTcW();
  568. ctTblWidth.setW(BigInteger.valueOf(2125));
  569. ctTblWidth.setType(STTblWidth.DXA);
  570. }
  571. //Table 表格第一行
  572. NpoiHelper.Option optionIsBoldCENTER = new NpoiHelper.Option();
  573. optionIsBoldCENTER.setAlign(ParagraphAlignment.CENTER);
  574. optionIsBoldCENTER.setBold(true);
  575. table.getRow(0).getCell(0).setColor("DBE5F1");
  576. table.getRow(0).getCell(0).setParagraph(NpoiHelper.setCellText(table, "项目名称", optionIsBoldCENTER));
  577. NpoiHelper.mergeHorizontal(table, 0, 1, 3);
  578. table.getRow(0).getCell(1).setParagraph(NpoiHelper.setCellText(table, res.getXmmc(), null));
  579. //Table 表格第二行
  580. table.getRow(1).getCell(0).setColor("DBE5F1");
  581. table.getRow(1).getCell(0).setParagraph(NpoiHelper.setCellText(table, "建设单位", optionIsBoldCENTER));
  582. NpoiHelper.mergeHorizontal(table, 1, 1, 3);
  583. table.getRow(1).getCell(1).setParagraph(NpoiHelper.setCellText(table, res.getJsdw(), null));
  584. //Table 表格第三行
  585. String ydmj = res.getYdmjbegin() + " - " + res.getYdmjend();
  586. if (StringUtils.isEmpty(res.getYdmjbegin()) || Double.parseDouble(res.getYdmjbegin()) == 0)
  587. ydmj = "≤" + res.getYdmjend();
  588. else if (StringUtils.isEmpty(res.getYdmjend()) || Double.parseDouble(res.getYdmjend()) == 0)
  589. ydmj = "≥" + res.getYdmjbegin();
  590. table.getRow(2).getCell(0).setColor("DBE5F1");
  591. table.getRow(2).getCell(0).setParagraph(NpoiHelper.setCellText(table, "用地面积", optionIsBoldCENTER));
  592. table.getRow(2).getCell(1).setParagraph(NpoiHelper.setCellText(table, ydmj + "平方米", null));
  593. table.getRow(2).getCell(2).setColor("DBE5F1");
  594. table.getRow(2).getCell(2).setParagraph(NpoiHelper.setCellText(table, "用地性质", optionIsBoldCENTER));
  595. table.getRow(2).getCell(3).setParagraph(NpoiHelper.setCellText(table, res.getYdmc(), null));
  596. //Table 表格第四行
  597. table.getRow(3).getCell(1).setColor("DBE5F1");
  598. table.getRow(3).getCell(2).setColor("DBE5F1");
  599. table.getRow(3).getCell(3).setColor("DBE5F1");
  600. table.getRow(3).getCell(0).setParagraph(NpoiHelper.setCellText(table, "选址因子", optionIsBoldCENTER));
  601. table.getRow(3).getCell(1).setParagraph(NpoiHelper.setCellText(table, "因子名称", optionIsBoldCENTER));
  602. table.getRow(3).getCell(2).setParagraph(NpoiHelper.setCellText(table, "影响条件", optionIsBoldCENTER));
  603. table.getRow(3).getCell(3).setParagraph(NpoiHelper.setCellText(table, "约束范围值(M)", optionIsBoldCENTER));
  604. //查询选址因子,并且转换为List<Map> 因子条件(包含:C、不包含:N、分析:A)
  605. QueryWrapper<FzxzXzyzDTO> wrapper = new QueryWrapper<FzxzXzyzDTO>();
  606. wrapper.eq("rwbsm", res.getBsm());
  607. List<FzxzXzyzDTO> fzxzXzyzDTOList = fzxzXzyzMapper.selectList(wrapper);
  608. for (int i = 0; i < fzxzXzyzDTOList.size(); i++) {
  609. FzxzXzyzDTO fzxzXzyzDTO = fzxzXzyzDTOList.get(i);
  610. String yztj = "";
  611. if (fzxzXzyzDTO.getYztj() != null) {
  612. if ("C".equals(fzxzXzyzDTO.getYztj().toUpperCase())) {
  613. yztj = "包含";
  614. }
  615. if ("N".equals(fzxzXzyzDTO.getYztj().toUpperCase())) {
  616. yztj = "不包含";
  617. }
  618. if ("A".equals(fzxzXzyzDTO.getYztj().toUpperCase()) || "F".equals(fzxzXzyzDTO.getYztj().toUpperCase())) {
  619. yztj = "分析";
  620. }
  621. }
  622. String yxz = "";
  623. if (StringUtils.isNotEmpty(fzxzXzyzDTO.getYxz())) {
  624. yxz = fzxzXzyzDTO.getYxz();
  625. }
  626. XWPFTableRow mr = table.createRow();
  627. mr.getCell(1).setParagraph(NpoiHelper.setCellText(table, fzxzXzyzDTO.getYxyzmc(), optionCENTER));
  628. mr.getCell(2).setParagraph(NpoiHelper.setCellText(table, yztj, optionCENTER));
  629. mr.getCell(3).setParagraph(NpoiHelper.setCellText(table, yxz, optionCENTER));
  630. }
  631. NpoiHelper.mergeCellsVertically(table, 0, 3, 3 + fzxzXzyzDTOList.size());
  632. table.getRow(3).getCell(0).setColor("DBE5F1");
  633. //选址分析
  634. NpoiHelper.catalog(document, "二、选址分析", pos++);
  635. XWPFTable tableFx = document.createTable(1, 4);
  636. tableFx.setWidth(8500);//总宽度
  637. for (int k = 0; k < 4; k++) {
  638. XWPFTableCell cell = tableFx.getRow(0).getCell(k);
  639. CTTcPr ctTcPr = cell.getCTTc().isSetTcPr() ? cell.getCTTc().getTcPr() : cell.getCTTc().addNewTcPr();
  640. CTTblWidth ctTblWidth = ctTcPr.addNewTcW();
  641. ctTblWidth.setW(BigInteger.valueOf(2125));
  642. ctTblWidth.setType(STTblWidth.DXA);
  643. }
  644. //查询结果GIS FzxzJgGisDTO
  645. QueryWrapper<FzxzJgGisDTO> giswrapper = new QueryWrapper<FzxzJgGisDTO>();
  646. giswrapper.eq("rwbsm", res.getBsm());
  647. //筛选方案,根据传参或者预选方案
  648. // giswrapper.ne("yxfa", "0");
  649. if (xzbsmList != null) {
  650. giswrapper.in("bsm", xzbsmList);
  651. }
  652. List<FzxzJgGisDTO> fzxzJgGisDTOList = fzxzJgGISMapper.selectList(giswrapper);
  653. int index = 1;
  654. NpoiHelper.Option optionIsBold = new NpoiHelper.Option();
  655. optionIsBold.setBold(true);
  656. int rowIndex = 0;
  657. for (int i = 0; i < fzxzJgGisDTOList.size(); i++) {
  658. FzxzJgGisDTO item = fzxzJgGisDTOList.get(i);
  659. XWPFTableRow mr = tableFx.createRow();
  660. rowIndex++;
  661. mr.getCell(0).setColor("DBE5F1");
  662. NpoiHelper.mergeHorizontal(tableFx, rowIndex, 0, 1);
  663. mr.getCell(0).setParagraph(NpoiHelper.setCellText(tableFx, "方案" + NumberUtil.int2chineseNum(index), optionIsBold));
  664. //tableFx 表格第二行
  665. mr = tableFx.createRow();
  666. rowIndex++;
  667. mr.getCell(0).setParagraph(NpoiHelper.setCellText(tableFx, "地类编码", optionIsBoldCENTER));
  668. mr.getCell(1).setParagraph(NpoiHelper.setCellText(tableFx, item.getDlbm(), null));
  669. mr.getCell(2).setParagraph(NpoiHelper.setCellText(tableFx, "地类名称", optionIsBoldCENTER));
  670. mr.getCell(3).setParagraph(NpoiHelper.setCellText(tableFx, item.getDlmc(), null));
  671. //tableFx 表格第三行
  672. mr = tableFx.createRow();
  673. rowIndex++;
  674. mr.getCell(0).setParagraph(NpoiHelper.setCellText(tableFx, "地块面积", optionIsBoldCENTER));
  675. mr.getCell(1).setParagraph(NpoiHelper.setCellText(tableFx, Math.round(item.getTbmj()) + "平方米", null));
  676. mr.getCell(2).setParagraph(NpoiHelper.setCellText(tableFx, "土地位置", optionIsBoldCENTER));
  677. mr.getCell(3).setParagraph(NpoiHelper.setCellText(tableFx, item.getXzqmc(), null));
  678. //tableFx 表格第四行
  679. mr = tableFx.createRow();
  680. rowIndex++;
  681. mr.getCell(0).setParagraph(NpoiHelper.setCellText(tableFx, "压占分析", optionIsBoldCENTER));
  682. NpoiHelper.mergeHorizontal(tableFx, rowIndex, 1, 3);
  683. XWPFParagraph para6 = mr.getCell(1).addParagraph();
  684. para6.setAlignment(ParagraphAlignment.LEFT);
  685. //查询选址因子 因子条件(包含:C、不包含:N、分析:A)
  686. QueryWrapper<FzxzJgyzDTO> fzxzJgyzDTOQueryWrapper = new QueryWrapper<FzxzJgyzDTO>();
  687. fzxzJgyzDTOQueryWrapper.eq("jbbsm", item.getBsm());
  688. fzxzJgyzDTOQueryWrapper.eq("yztj", "F");
  689. List<FzxzJgyzDTO> fzxzJgyzDTOList = fzxzJgyzMapper.selectList(fzxzJgyzDTOQueryWrapper);
  690. int jgyzid = 1;
  691. for (int j = 0; j < fzxzJgyzDTOList.size(); j++) {
  692. FzxzJgyzDTO jgyz = fzxzJgyzDTOList.get(j);
  693. XWPFRun run = para6.createRun();
  694. String jg = jgyz.getFxjg();
  695. Double jgDouble = 0.0;
  696. try {
  697. jgDouble = Double.parseDouble(jg);
  698. run.setText(jgyzid + "、压占" + jgyz.getYxyzmc() + "面积" + jg + "平方米");
  699. } catch (Exception e) {
  700. run.setText(jgyzid + "、" + jg);
  701. }
  702. if (jgyzid < fzxzJgyzDTOList.size())
  703. run.addBreak(BreakType.TEXT_WRAPPING);
  704. jgyzid++;
  705. }
  706. index++;
  707. }
  708. tableFx.removeRow(0);
  709. File fileDoc = new File(reportPath);
  710. if (fileDoc.exists()) {
  711. FileUtils.forceDelete(fileDoc);
  712. }
  713. //word文件输出流
  714. outputStream = new FileOutputStream(reportPath);
  715. document.write(outputStream);
  716. } catch (
  717. Exception e) {
  718. e.printStackTrace();
  719. System.out.println(e.toString());
  720. //插入任务日志
  721. FxrwrzDTO errorDto = new FxrwrzDTO();
  722. errorDto.setRwbsm(res.getBsm());
  723. errorDto.setRwlx("辅助选址");
  724. errorDto.setRzlr("生成选址简报错误:" + e.getMessage());
  725. errorDto.setRzsj(new Date());
  726. errorDto.setRzlx("error");
  727. fxrwrzMapper.insert(errorDto);
  728. } finally {
  729. if (document != null) {
  730. try {
  731. document.close();
  732. } catch (Exception ex) {
  733. System.out.println(ex.toString());
  734. }
  735. }
  736. if (outputStream != null) {
  737. try {
  738. outputStream.close();
  739. } catch (IOException e) {
  740. e.printStackTrace();
  741. }
  742. }
  743. }
  744. //转PDF
  745. String outfilepath = reportPath.replace(".docx", ".pdf");
  746. NpoiHelper.doc2pdf(reportPath, outfilepath);
  747. System.out.println(outfilepath);
  748. }
  749. /**
  750. * zip打包
  751. */
  752. private void createZipReport(FzxzEntityDTO res, String reportZip, String reportPath, String simplePath) {
  753. File f = new File(fzxzShp);
  754. System.out.println("选址分析报告打包zip:" + reportPath);
  755. System.out.println("选址分析报告打包shp:" + fzxzShp);
  756. if (StringUtils.isNotEmpty(fzxzShp)) {
  757. String filePath = fzxzShp.substring(0, fzxzShp.lastIndexOf("."));
  758. String docReportPath = reportPath.substring(0, reportPath.lastIndexOf("."));
  759. String docSimplePath = reportPath.substring(0, reportPath.lastIndexOf("."));
  760. // 生成的压缩文件
  761. ZipFile zipFile = null;
  762. try {
  763. zipFile = new ZipFile(reportZip);
  764. ZipParameters parameters = new ZipParameters();
  765. // 压缩方式
  766. parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
  767. // 压缩级别
  768. parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
  769. zipFile.addFile(new File(simplePath), parameters);
  770. zipFile.addFile(new File(reportPath), parameters);
  771. zipFile.addFile(new File(filePath + ".shp"), parameters);
  772. zipFile.addFile(new File(filePath + ".shx"), parameters);
  773. zipFile.addFile(new File(filePath + ".dbf"), parameters);
  774. zipFile.addFile(new File(filePath + ".cpg"), parameters);
  775. zipFile.addFile(new File(filePath + ".prj"), parameters);
  776. } catch (ZipException e) {
  777. System.out.println(e.toString());
  778. //插入任务日志
  779. FxrwrzDTO errorDto = new FxrwrzDTO();
  780. errorDto.setRwbsm(res.getBsm());
  781. errorDto.setRwlx("辅助选址");
  782. errorDto.setRzlr("生成选址报告zip打包错误:" + e.getMessage());
  783. errorDto.setRzsj(new Date());
  784. errorDto.setRzlx("error");
  785. fxrwrzMapper.insert(errorDto);
  786. }
  787. }
  788. }
  789. /**
  790. * 导出报告图片
  791. *
  792. * @return
  793. */
  794. private List<String> ReportImg(FzxzEntityDTO res, List<String> xzbsmList) {
  795. //查询分析结果图斑信息
  796. QueryWrapper<FzxzJgGisDTO> giswrapper = new QueryWrapper<FzxzJgGisDTO>();
  797. giswrapper.eq("rwbsm", res.getBsm());
  798. //筛选方案,根据传参或者预选方案
  799. // giswrapper.ne("yxfa", "0");
  800. if (xzbsmList != null) {
  801. giswrapper.in("bsm", xzbsmList);
  802. }
  803. List<FzxzJgGisDTO> fzxzJgGisDTOList = fzxzJgGISMapper.selectList(giswrapper);
  804. //SDE转SHP
  805. String inShp = fzxzShp;
  806. List objidlist = new ArrayList<>();
  807. for (FzxzJgGisDTO cur : fzxzJgGisDTOList) {
  808. objidlist.add(cur.getObjectid());
  809. }
  810. String objids = Joiner.on(",").join(objidlist);
  811. sde2Shp("KJGH.T_FZSS_FZXZ_JG_GIS", inShp, "\"OBJECTID\" IN (" + objids + ")");
  812. //执行python
  813. Map<String, String> params = new HashMap<>();
  814. List<String> result = PythonExecute.RunFzxzMap(mapFunctionId, params);
  815. return result;
  816. }
  817. /**
  818. * SDE转SHP
  819. *
  820. * @param table SDE表
  821. * @param shpfile shp文件
  822. * @param where 追加字段
  823. * @return
  824. */
  825. public static String sde2Shp(String table, String shpfile, String where) {
  826. if (StringUtils.isNotEmpty(where)) {
  827. Map<String, String> params = new HashMap<>();
  828. params.put("table", table);
  829. params.put("shpfile", shpfile);
  830. params.put("where", where);
  831. return PythonExecute.RunGisHelper("sde2shp", params);
  832. } else {
  833. Map<String, String> params = new HashMap<>();
  834. params.put("table", table);
  835. params.put("shpfile", shpfile);
  836. return PythonExecute.RunGisHelper("sde2shp", params);
  837. }
  838. }
  839. }