package com.onemap.analyse.task; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.onemap.analyse.domain.*; import com.onemap.analyse.mapper.*; import com.onemap.analyse.utils.NpoiHelper; import com.onemap.common.core.utils.StringUtils; import org.apache.commons.io.FileUtils; import org.apache.poi.xwpf.usermodel.*; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; import java.math.BigInteger; import java.text.SimpleDateFormat; import java.util.*; /** * 合规性分析模块定时刷新任务 */ @Component public class HgxfxSchedule { //间隔时间 单位ms public static final long TIME_INTERVAL = 1 * 15000; //任务状态标识码 private RwztDTO Rwzt = new RwztDTO(); @Resource private HgxfxMapper hgxfxMapper; @Resource HgxfxHgscxMapper hgxfxHgscxMapper; @Resource HgxfxScxMapper hgxfxScxMapper; @Resource private FxrwrzMapper fxrwrzMapper; @Resource private FzssChscMapper fzssChscMapper; //合规性分析python方法名标识 @Value("${Hgxfx.functionId}") private String functionId; @Value("${Hgxfx.temp}") private String temp; @Value("${Hgxfx.ftpOut}") private String ftpOut; private static final Logger log = LoggerFactory.getLogger(HgxfxSchedule.class); /** * Scheduled 定时器参数 * cron表达式:指定任务在特定时间执行 * fixedDelay:表示上一次任务执行完成后多久再执行,参数类型long,单位:ms * fixedDelayString:与fixedDelay一样,只是参数类型是String * fixedRate:表示按一定的频率执行任务,参数类型long,单位:ms 如: fixedRate(5000),表示这个定时器任务每5秒执行一次 * fixedRateString:与fixedRate一样,只是参数类型变为String * initialDelay:表示延迟多久再第一次执行任务,参数类型为long ,单位:ms * initialDelayString:与initialDelay一样,只是参数类型String */ /** * @Async("name") 开启多线程 */ /** * 合规性分析执行入口 */ @Async("taskExecutor") @Scheduled(fixedDelay = TIME_INTERVAL) public void HgxfxStatus() { try { QueryWrapper wrapper = new QueryWrapper(); wrapper.eq("rwzt", Rwzt.getCreate()); //以创建时间正序排列 wrapper.orderByAsc("cjsj"); //每次只操作一条记录 List ress = hgxfxMapper.selectList(wrapper); if (ress.size() > 0) { HgxfxDTO res = ress.get(0); //插入执行状态 res.setRwzt(Rwzt.getRun()); res.setRwkssj(new Date()); QueryWrapper query = new QueryWrapper(); query.eq("bsm", res.getBsm()); hgxfxMapper.update(res, query); //插入任务日志 FxrwrzDTO rzDto = new FxrwrzDTO(); rzDto.setRwbsm(res.getBsm()); rzDto.setRwlx("合规性检查"); rzDto.setRzlr("开始分析"); rzDto.setRzlx("info"); rzDto.setRzsj(new Date()); fxrwrzMapper.insert(rzDto); //执行python Map params = new HashMap<>(); params.put("bsm", res.getBsm()); String result = PythonExecute.Run(functionId, params); if (!StringUtils.isEmpty(result) && result.contains("OK")) {//成功 res.setRwjssj(new Date()); res.setRwzt(Rwzt.getComplete()); //捕获报告生成异常 try { //TODO 生成合规性分析结果报告 返回 FxReport实体 FxReport report = createReport(res.getBsm()); res.setFxbg(report.getReportfile()); hgxfxMapper.update(res, query); //TODO 策划生成 项目 if ("OpenAPI".equals(res.getRwly())) { QueryWrapper chscwrapper = new QueryWrapper(); chscwrapper.eq("bsm", res.getBsm()); FzxzChscDTO chscDto = fzssChscMapper.selectOne(chscwrapper); chscDto.setRwzt(Rwzt.getComplete()); chscDto.setRwjg("分析完成"); chscDto.setRwjd(100); fzssChscMapper.update(chscDto, chscwrapper); //拷贝合规性分析报告 com.onemap.common.core.utils.file.FileUtils.copyNewFileAndRename(ftpOut, report.getPdffile(), chscDto.getXmmc() + ".pdf"); } //插入任务日志 FxrwrzDTO rzDto2 = new FxrwrzDTO(); rzDto2.setRwbsm(res.getBsm()); rzDto2.setRwlx("合规性检查"); rzDto2.setRzlr("生成分析报告完成"); rzDto2.setRzlx("info"); rzDto2.setRzsj(new Date()); fxrwrzMapper.insert(rzDto2); //休眠1S,避免日志插入时间相同 Thread.sleep(1000 * 1); //插入成功日志 FxrwrzDTO successDto = new FxrwrzDTO(); successDto.setRwbsm(res.getBsm()); successDto.setRwlx("合规性检查"); successDto.setRzlr("分析完成"); successDto.setRzlx("info"); successDto.setRzsj(new Date()); fxrwrzMapper.insert(successDto); } catch (Exception e) { e.printStackTrace(); res.setRwzt(Rwzt.getError()); hgxfxMapper.update(res, query); if ("OpenAPI".equals(res.getRwly())) { QueryWrapper chscwrapper = new QueryWrapper(); chscwrapper.eq("bsm", res.getBsm()); FzxzChscDTO chscDto = fzssChscMapper.selectOne(chscwrapper); chscDto.setRwzt(Rwzt.getError()); chscDto.setRwjg("报告生成异常"); fzssChscMapper.update(chscDto, chscwrapper); } //插入任务日志 FxrwrzDTO errorDto = new FxrwrzDTO(); errorDto.setRwbsm(res.getBsm()); errorDto.setRwlx("合规性检查"); errorDto.setRzlr("报告生成异常"); errorDto.setRzlx("error"); errorDto.setRzsj(new Date()); fxrwrzMapper.insert(errorDto); } } else {//失败 res.setRwjssj(new Date()); res.setRwzt(Rwzt.getError()); hgxfxMapper.update(res, query); if ("OpenAPI".equals(res.getRwly())) { QueryWrapper chscwrapper = new QueryWrapper(); chscwrapper.eq("bsm", res.getBsm()); FzxzChscDTO chscDto = fzssChscMapper.selectOne(chscwrapper); chscDto.setRwzt(Rwzt.getError()); chscDto.setRwjg("分析错误"); fzssChscMapper.update(chscDto, chscwrapper); } //插入任务日志 FxrwrzDTO errorDto = new FxrwrzDTO(); errorDto.setRwbsm(res.getBsm()); errorDto.setRwlx("合规性检查"); errorDto.setRzlr("分析范围内无基础数据"); errorDto.setRzlx("error"); errorDto.setFxjg(result); errorDto.setRzsj(new Date()); fxrwrzMapper.insert(errorDto); } System.out.println("合规性分析模块:" + res.getBsm() + "完成!!!!!!!! " + new Date().toString()); } } catch (Exception e) { e.printStackTrace(); } } /** * 根据任务标识码生成报告 * * @return */ public FxReport createReport(String bsm) { //插入任务日志 // FxrwrzDTO rzDto = new FxrwrzDTO(); // rzDto.setRwbsm(bsm); // rzDto.setRwlx("合规性检查"); // rzDto.setRzlr("开始生成分析报告"); // rzDto.setRzlx("info"); // rzDto.setRzsj(new Date()); // fxrwrzMapper.insert(rzDto); FxReport fxReport = new FxReport(); QueryWrapper wrapper = new QueryWrapper(); wrapper.eq("bsm", bsm); HgxfxDTO res = hgxfxMapper.selectOne(wrapper); //文件硬盘真实路径 String timeStamp = new Date().getTime() + ""; String rootPath = temp + File.separator + "合规性检查报告" + File.separator + res.getBsm(); File rootFile = new File(rootPath); if (!rootFile.exists()) { rootFile.mkdirs(); } String reportPath = rootPath + File.separator + res.getXmmc() + "(合规性检查报告)_" + timeStamp + ".docx"; //解析文件路径为web路径 String webFilePath = StringUtils.getFileStaticPath(reportPath, temp); double mj = 0; if (res != null) { XWPFDocument document = null; OutputStream outputStream = null;//把doc输出到输出流 try { //前端映射路径 System.out.println(reportPath); //创建poi XWPFDocument doc文件 document = new XWPFDocument(); int pos = 0; //文本标题 String title = res.getXmmc() + "合规性检查报告"; NpoiHelper.title(document, title, pos++); NpoiHelper.Option option = new NpoiHelper.Option(); option.setFontSize(19); option.setAlign(ParagraphAlignment.CENTER); option.setBold(true); List list = new ArrayList<>(); list.add(""); document.setParagraph(NpoiHelper.newParagraph(document, list, option), pos++); SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日"); option = new NpoiHelper.Option(); option.setAlign(ParagraphAlignment.CENTER); document.setParagraph(NpoiHelper.newParagraph(document, sdf.format(new Date()) , option), pos++); NpoiHelper.catalog(document, "一、项目合规性审查", pos++); XWPFTable table = document.createTable(1, 4); int tableIndex = 0; table.setWidth(9000);//总宽度 table.setCellMargins(100, 0, 200, 0); for (int i = 0; i < 4; i++) { XWPFTableCell cell = table.getRow(0).getCell(i); CTTcPr ctTcPr = cell.getCTTc().isSetTcPr() ? cell.getCTTc().getTcPr() : cell.getCTTc().addNewTcPr(); CTTblWidth ctTblWidth = ctTcPr.addNewTcW(); ctTblWidth.setW(BigInteger.valueOf(2250)); ctTblWidth.setType(STTblWidth.DXA); } //Table 表格第1行 NpoiHelper.Option optionCenterAndBold = new NpoiHelper.Option(); optionCenterAndBold.setBold(true); optionCenterAndBold.setAlign(ParagraphAlignment.CENTER); NpoiHelper.Option optionCenter = new NpoiHelper.Option(); optionCenter.setAlign(ParagraphAlignment.CENTER); table.getRow(0).getCell(0).setParagraph(NpoiHelper.setCellText(table, "项目名称", optionCenterAndBold)); table.getRow(0).getCell(1).setParagraph(NpoiHelper.setCellText(table, res.getXmmc(), null)); table.getRow(0).getCell(2).setParagraph(NpoiHelper.setCellText(table, "项目类型", optionCenterAndBold)); table.getRow(0).getCell(3).setParagraph(NpoiHelper.setCellText(table, res.getXmlx(), null)); XWPFTableRow row = table.createRow(); tableIndex++; row.getCell(0).setParagraph(NpoiHelper.setCellText(table, "用地面积", optionCenterAndBold)); row.getCell(1).setParagraph(NpoiHelper.setCellText(table, res.getFxmj() + "㎡", null)); row.getCell(2).setParagraph(NpoiHelper.setCellText(table, "建设单位", optionCenterAndBold)); row.getCell(3).setParagraph(NpoiHelper.setCellText(table, res.getJsdw(), null)); row = table.createRow(); tableIndex++; row.getCell(0).setParagraph(NpoiHelper.setCellText(table, "管控要素分类", optionCenterAndBold)); row.getCell(1).setParagraph(NpoiHelper.setCellText(table, "检查要素", optionCenterAndBold)); row.getCell(2).setParagraph(NpoiHelper.setCellText(table, "涉及面积", optionCenterAndBold)); row.getCell(3).setParagraph(NpoiHelper.setCellText(table, "结论", optionCenterAndBold)); //先查询FzssHgxfxHgscx,再查询FzssHgxfxScx QueryWrapper wrapperHgxfxHgscxDTO = new QueryWrapper(); wrapperHgxfxHgscxDTO.eq("rwbsm", res.getBsm()); List hgxfxHgscxDTOList = hgxfxHgscxMapper.queryScxList(res.getBsm()); //一级控制线 Map[] mapArray = new Map[hgxfxHgscxDTOList.size()]; //审查项数组排序序号 int index = 0; for (int i = 0; i < hgxfxHgscxDTOList.size(); i++) { HgxfxHgscxExportDTO hgxfxHgscxDTO = hgxfxHgscxDTOList.get(i); if (hgxfxHgscxDTO.getKzxjb() == 1) { Map map = new HashMap<>(); map.put("scx", hgxfxHgscxDTO); map.put("hgscx", hgxfxHgscxDTO); mapArray[index] = map; index++; } } int startRowIndex = tableIndex; for (int i = 0; i < mapArray.length; i++) { if (mapArray[i] != null) { HgxfxHgscxExportDTO hgxfxHgscxDTO = (HgxfxHgscxExportDTO) mapArray[i].get("hgscx"); if (hgxfxHgscxDTO != null) { hgxfxHgscxDTO.setZdmj(hgxfxHgscxDTO.getZdmj() == null ? 0 : hgxfxHgscxDTO.getZdmj()); row = table.createRow(); tableIndex++; row.getCell(0).setParagraph(NpoiHelper.setCellText(table, "一级控制线", optionCenterAndBold)); row.getCell(1).setParagraph(NpoiHelper.setCellText(table, hgxfxHgscxDTO.getYsmc(), null)); row.getCell(2).setParagraph(NpoiHelper.setCellText(table, Math.round(hgxfxHgscxDTO.getZdmj()) + "m²", optionCenter)); row.getCell(3).setParagraph(NpoiHelper.setCellText(table, hgxfxHgscxDTO.getJcjg(), null)); mj = mj + hgxfxHgscxDTO.getZdmj(); } } } //合并列 NpoiHelper.mergeCellsVertically(table, 0, startRowIndex + 1, tableIndex); //二级控制线 mapArray = new Map[hgxfxHgscxDTOList.size()]; //审查项数组排序序号 index = 0; for (int i = 0; i < hgxfxHgscxDTOList.size(); i++) { //二级控制线 HgxfxHgscxExportDTO hgxfxHgscxDTO = hgxfxHgscxDTOList.get(i); if (hgxfxHgscxDTO != null) { if (hgxfxHgscxDTO.getKzxjb() == 2) { Map map = new HashMap<>(); map.put("scx", hgxfxHgscxDTO); map.put("hgscx", hgxfxHgscxDTO); mapArray[index] = map; index++; } } } startRowIndex = tableIndex; for (int i = 0; i < mapArray.length; i++) { if (mapArray[i] != null) { HgxfxHgscxExportDTO hgxfxHgscxDTO = (HgxfxHgscxExportDTO) mapArray[i].get("hgscx"); if (hgxfxHgscxDTO != null) { row = table.createRow(); tableIndex++; row.getCell(0).setParagraph(NpoiHelper.setCellText(table, "二级控制线", optionCenterAndBold)); row.getCell(1).setParagraph(NpoiHelper.setCellText(table, hgxfxHgscxDTO.getYsmc(), null)); row.getCell(2).setParagraph(NpoiHelper.setCellText(table, Math.round(hgxfxHgscxDTO.getZdmj()) + "m²", optionCenter)); row.getCell(3).setParagraph(NpoiHelper.setCellText(table, hgxfxHgscxDTO.getJcjg(), null)); mj = mj + hgxfxHgscxDTO.getZdmj(); } } } //设置用地面积 table.getRow(1).getCell(1).setParagraph(NpoiHelper.setCellText(table, res.getFxmj() + "㎡", null)); NpoiHelper.mergeCellsVertically(table, 0, startRowIndex + 1, tableIndex); //TODO wanger 将其他审查项插入到表 Map> scxMap = new HashMap<>(); //组织数据 for (int i = 0; i < hgxfxHgscxDTOList.size(); i++) { HgxfxHgscxExportDTO hgxfxHgscxDTO = hgxfxHgscxDTOList.get(i); if (hgxfxHgscxDTO.getKzxjb() > 2) { String classify = hgxfxHgscxDTO.getTjzd(); if (scxMap.get(classify) == null) { scxMap.put(classify, new ArrayList<>()); } scxMap.get(classify).add(hgxfxHgscxDTO); } } //循环添加 Iterator>> iterator = scxMap.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry> entry = iterator.next(); String key = entry.getKey(); List value = entry.getValue(); mapArray = new Map[value.size()]; //审查项数组排序序号 index = 0; for (int i = 0; i < value.size(); i++) { //二级控制线 HgxfxHgscxExportDTO hgxfxHgscxDTO = value.get(i); Map map = new HashMap<>(); map.put("scx", hgxfxHgscxDTO); map.put("hgscx", hgxfxHgscxDTO); mapArray[index] = map; index++; } startRowIndex = tableIndex; for (int i = 0; i < mapArray.length; i++) { if (mapArray[i] != null) { HgxfxHgscxExportDTO hgxfxHgscxDTO = (HgxfxHgscxExportDTO) mapArray[i].get("hgscx"); if (hgxfxHgscxDTO != null) { row = table.createRow(); tableIndex++; row.getCell(0).setParagraph(NpoiHelper.setCellText(table, key, optionCenterAndBold)); row.getCell(1).setParagraph(NpoiHelper.setCellText(table, hgxfxHgscxDTO.getYsmc(), null)); row.getCell(2).setParagraph(NpoiHelper.setCellText(table, Math.round(hgxfxHgscxDTO.getZdmj()) + "m²", optionCenter)); row.getCell(3).setParagraph(NpoiHelper.setCellText(table, hgxfxHgscxDTO.getJcjg(), null)); mj = mj + hgxfxHgscxDTO.getZdmj(); } } } //设置用地面积 table.getRow(1).getCell(1).setParagraph(NpoiHelper.setCellText(table, res.getFxmj() + "㎡", null)); NpoiHelper.mergeCellsVertically(table, 0, startRowIndex + 1, tableIndex); } //第二章节 NpoiHelper.catalog(document, "二、项目用地明细", pos++); XWPFTable tableSecond = document.createTable(1, 5); tableSecond.setCellMargins(100, 0, 200, 0); tableSecond.setWidth(9000);//总宽度 //Table 表格第0行 tableSecond.getRow(0).getCell(0).setParagraph(NpoiHelper.setCellText(tableSecond, "一级地类", optionCenterAndBold)); tableSecond.getRow(0).getCell(2).setParagraph(NpoiHelper.setCellText(tableSecond, "二级地类", optionCenterAndBold)); tableSecond.getRow(0).getCell(4).setParagraph(NpoiHelper.setCellText(tableSecond, "占地面积", optionCenterAndBold)); //合并列 NpoiHelper.mergeHorizontal(tableSecond, 0, 0, 1); NpoiHelper.mergeHorizontal(tableSecond, 0, 2, 3); //查询HgxfxFxtbDTO List hgxfxFxtbDTOList = hgxfxMapper.getFxtb(res.getBsm()); NpoiHelper.Option optionFontSizeAlign = new NpoiHelper.Option(); optionFontSizeAlign.setAlign(ParagraphAlignment.CENTER); optionFontSizeAlign.setFontSize(10); NpoiHelper.Option optionFontSize = new NpoiHelper.Option(); optionFontSize.setFontSize(10); for (int i = 0; i < hgxfxFxtbDTOList.size(); i++) { HgxfxFxtbDTO hgxfxFxtbDTO = hgxfxFxtbDTOList.get(i); XWPFTableRow row1 = tableSecond.createRow(); tableIndex++; row1.getCell(0).setParagraph(NpoiHelper.setCellText(tableSecond, hgxfxFxtbDTO.getDldm1(), optionFontSizeAlign)); row1.getCell(1).setParagraph(NpoiHelper.setCellText(tableSecond, hgxfxFxtbDTO.getDlmc1(), optionFontSize)); row1.getCell(2).setParagraph(NpoiHelper.setCellText(tableSecond, hgxfxFxtbDTO.getDldm2(), optionFontSizeAlign)); row1.getCell(3).setParagraph(NpoiHelper.setCellText(tableSecond, hgxfxFxtbDTO.getDlmc2(), optionFontSize)); if (hgxfxFxtbDTO.getMj() == null) { hgxfxFxtbDTO.setMj(0.0); } row1.getCell(4).setParagraph(NpoiHelper.setCellText(tableSecond, Math.round(hgxfxFxtbDTO.getMj()) + "㎡", optionFontSizeAlign)); } if (tableSecond.getRow(1) != null) { for (int i = 0; i < 5; i++) { XWPFTableCell cell = tableSecond.getRow(1).getCell(i); CTTcPr ctTcPr = cell.getCTTc().isSetTcPr() ? cell.getCTTc().getTcPr() : cell.getCTTc().addNewTcPr(); CTTblWidth ctTblWidth = ctTcPr.addNewTcW(); ctTblWidth.setW(BigInteger.valueOf(1800)); ctTblWidth.setType(STTblWidth.DXA); } } File fileDoc = new File(reportPath); if (fileDoc.exists()) { FileUtils.delete(fileDoc); } //word文件输出流 outputStream = new FileOutputStream(reportPath); document.write(outputStream); } catch (Exception e) { log.error(e.toString()); //插入任务日志 FxrwrzDTO errorDto = new FxrwrzDTO(); errorDto.setRwbsm(res.getBsm()); errorDto.setRwlx("合规性检查"); errorDto.setRzlr("报告生成异常:" + e.getMessage()); errorDto.setRzlx("error"); errorDto.setRzsj(new Date()); fxrwrzMapper.insert(errorDto); // e.printStackTrace(); } finally { if (outputStream != null) { try { outputStream.close(); } catch (Exception ex) { log.error(ex.toString()); } } if (document != null) { try { document.close(); } catch (Exception ex) { log.error(ex.toString()); } } } //转PDF String outfilepath = reportPath.replace(".docx", ".pdf"); if (new File(reportPath).exists()) { NpoiHelper.doc2pdf(reportPath, outfilepath); //更新报告对象 fxReport.setReportfile(webFilePath); fxReport.setMj(mj); fxReport.setPdffile(outfilepath); } } return fxReport; } }