package cn.cslg.pas.service; import cn.cslg.pas.common.dto.PatentColumnDTO; import cn.cslg.pas.common.dto.PatentDTO; import cn.cslg.pas.common.dto.es.EsCustomFieldValueDTO; import cn.cslg.pas.common.model.SystemMO; import cn.cslg.pas.common.model.cronModel.PersonnelVO; import cn.cslg.pas.common.model.request.StringRequest; import cn.cslg.pas.common.utils.*; import cn.cslg.pas.common.vo.*; import cn.cslg.pas.domain.ReportDocument; import cn.cslg.pas.domain.business.*; import cn.cslg.pas.domain.es.Text; import cn.cslg.pas.exception.XiaoShiException; import cn.cslg.pas.mapper.AvoidDesignMapper; import cn.cslg.pas.service.business.*; import cn.cslg.pas.service.business.es.*; import cn.cslg.pas.service.common.FileManagerService; import cn.hutool.core.util.IdUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.deepoove.poi.XWPFTemplate; import com.deepoove.poi.config.Configure; import com.deepoove.poi.data.PictureRenderData; import com.deepoove.poi.data.PictureType; import com.deepoove.poi.data.Pictures; import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.ddr.poi.html.HtmlRenderPolicy; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.*; @Slf4j @Service public class ReportExportService { @Autowired private ReportTempleService templeService; @Autowired private FileUtils fileUtils; @Autowired private ReportProjectService reportProjectService; @Autowired private FileManagerService fileManagerService; @Autowired private EsPatentService esPatentService; @Autowired private AvoidDesignMapper avoidDesignMapper; @Autowired private TemplateConfigService templateConfigService; @Autowired private CustomFieldService customFieldService; @Autowired private EsService esService; @Autowired private FTOCompareRecordService ftoCompareRecordService; @Autowired private EsQuotePatentService esQuotePatentService; @Autowired private EsFamilyService esFamilyService; @Autowired private LoginUtils loginUtils; @Autowired private CacheUtils cacheUtils; @Autowired private EsLegalEventService esLegalEventService; /** * @param projectId * @param templeId * @return * @throws IOException * @function: 导出报告 */ public String exportReport(Integer projectId, Integer templeId) throws IOException { //根据模板ID获得模板 ReportTemple reportTemplate = templeService.getById(templeId); //获得模板路径 String templateFilePath = fileUtils.getPath(reportTemplate.getTemplatePath()); //读取模板后保存生成word的地址 String fileName = IdUtil.simpleUUID() + ".docx"; String directoryName = fileUtils.createDirectory(); String outPath = fileUtils.getSavePath(directoryName) + fileName; //根据ProjectId 获得报告 LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(ReportProject::getProjectId, projectId); ReportProject report = reportProjectService.getOne(queryWrapper, false); if (report == null) { throw new XiaoShiException("报告不存在"); } XWPFTemplate template = null; if (report.getReportType() == 0 || report.getReportType() == 2) { // template = this.getstabilityTemplate(report, filePath); } else if (report.getReportType() == 3) { template = this.FTOTemplate(projectId, templateFilePath, templeId); } else if (report.getReportType() == 4) { template = this.Torttemplate(projectId, templateFilePath); } else if (report.getReportType() == 5) { template = this.avoidDesignTemplate(projectId, templateFilePath); } else if (report.getReportType() == 1) { // template = this.getThirdTemplate(report, filePath); } // 读取模板、数据并渲染 // 文件是否已存在,则删除 File file = new File(outPath); // 生成word保存在指定目录 template.writeToFile(outPath); template.close(); //导出成功后,导出报告记录入库 List ids = fileManagerService.uploadFileGetGuid2(Arrays.asList(file)); if (ids == null || ids.size() == 0) { throw new XiaoShiException("保存记录失败"); } // PersonnelVO personnelVO = cacheUtils.getLoginUser(loginUtils.getId()); PersonnelVO personnelVO = new PersonnelVO(); personnelVO.setId("1"); ReportDocument reportDocument = new ReportDocument(); reportDocument.setProjectId(projectId); reportDocument.setFileName(fileName); reportDocument.setFileGuid(ids.get(0)); reportDocument.setCreateId(personnelVO.getId()); reportDocument.setTemplateId(reportTemplate.getId()); reportDocument.setTemplateName(reportTemplate.getTemplateName()); reportDocument.insert(); if (file.exists()) { file.delete(); } return ids.get(0); } private XWPFTemplate Torttemplate(Integer reportId, String filePath) throws IOException { //根据报告Id查询标的专利号 LambdaQueryWrapper projectLambdaQueryWrapper =new LambdaQueryWrapper<>(); projectLambdaQueryWrapper.eq(ReportProject::getProjectId,reportId); ReportProject report = reportProjectService.getOne(projectLambdaQueryWrapper,false); String patentNo = report.getSignPatentNo(); // PatentVO patentVO = new PatentVO(); // patentVO.setPatentNos(Arrays.asList(patentNo)); // //根据专利号查询专利详细信息 // String resBody = outInterfaceService.getPatentDTOListForRMS(patentVO); // JSONObject jsonObject = JSONObject.parseObject(resBody); // List patentDTOS = JSON.parseArray(jsonObject.get("data").toString(), JSONObject.class); // //装载单个专利的信息 Map patentMap = new HashMap<>(); // JSONObject patent = patentDTOS.get(0); // List applicantJSONs = JSON.parseArray(patent.get("applicant").toString(), JSONObject.class); // StringBuilder applicants = new StringBuilder(); // StringBuilder rightPerson = new StringBuilder(); // applicantJSONs.forEach(tem -> { // if (Integer.parseInt(tem.get("dataType").toString()) == 1) { // applicants.append(tem.get("name") + "\r"); // } else { // rightPerson.append(tem.get("name") + "\r"); // } // }); // //装载同族信息 // JSONObject familys = JSONObject.parseObject(patent.get("family").toString()); // List patSnaps = JSON.parseArray(familys.get("patSnap").toString(), String.class); // List simples = JSON.parseArray(familys.get("simple").toString(), String.class); // List inpadocs = JSON.parseArray(familys.get("inpadoc").toString(), String.class); // patSnaps.addAll(simples); // patSnaps.addAll(inpadocs); // String familyStr = StringUtils.join(patSnaps, ","); // //装载法律状态 // StringBuilder affair = new StringBuilder(); // List affaires = JSON.parseArray(patent.get("affair").toString(), JSONObject.class); // affaires.forEach(item -> { // affair.append(item.get("status") + "\r"); // } // ); // //同族专利 // patentMap.put("simpleFamilys", familyStr); // //引用专利 // patentMap.put("quotePatents", patent.get("quote")); // //申请日 // patentMap.put("applicationDate", patent.get("applicationDate")); // //公开日 // patentMap.put("publicDate", patent.get("publicDate")); // //专利号 // patentMap.put("publicNo", patent.get("publicNo")); // patentMap.put("firstPublicDate", patent.get("firstPublicDate")); // patentMap.put("applicant", applicants); // patentMap.put("rightPerson", rightPerson); // patentMap.put("affair", affair); // patentMap.put("name", patent.get("name")); //图示 patentMap.put("abstractPath", ""); //获得专利对比记录的信息 // Map temMap = compareMessageService.queryforTemplate(patent.get("patentNo").toString(), reportId); // List compareMessageVOS = (List) temMap.get("compareMessageVOs"); //装载对比记录的信息 // patentMap.put("cM", compareMessageVOS); // patentMap.put("rightNum", temMap.get("right")); // patentMap.put("mainRightNum", temMap.get("mainRight")); Map map = new HashMap<>(); String date = DateUtils.formatDate(new Date(), DateUtils.YYYY_MM_DD); String[] ds = date.split("-"); map.put("sys", new SystemMO(ds[0], ds[1], ds[2], "", "reportName")); map.put("patentMap", patentMap); map.put("", patentNo); // 为表格的显示绑定行循环 LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy(); HtmlRenderPolicy htmlRenderPolicy = new HtmlRenderPolicy(); // 将bz设置为行循环绑定的数据源的key,即key是bz的value会在模板中的{{bz}}处进行解析 Configure configure = Configure.builder().bind("cM", policy).bind("targetDescription", htmlRenderPolicy).build(); XWPFTemplate template = XWPFTemplate.compile(filePath, configure).render(map); return template; } private XWPFTemplate avoidDesignTemplate(Integer projectId,String path) throws IOException { String filePath = fileUtils.getPath(path); log.info("开始处理导出规避设计报告,参数为:{}, {}", projectId, filePath); LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(ReportProject::getProjectId, projectId); ReportProject report = reportProjectService.getOne(queryWrapper, false); String signPatentNo = report.getSignPatentNo(); //装载标的专利信息 ; //装载年月日 String date = DateUtils.formatDate(new Date(), DateUtils.YYYY_MM_DD); String[] dates = date.split("-"); SystemMO systemMO = new SystemMO() .setYear(dates[0]) .setMonth(dates[1]) .setDay(dates[2]); // //装载特征、解释、回避设计方向、回避设计总体方向 List features = avoidDesignMapper.selectAvoidDesign(projectId); // String wholeDirection = avoidDesignWholeDirectionMapper.selectWholeDirectionByReportId(reportId); // map.put("wholeDirection", wholeDirection); // //绑定政策(绑定集合和元素循环遍历) LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy(); //把指定元素内容识别传输成html格式 HtmlRenderPolicy htmlRenderPolicy = new HtmlRenderPolicy(); Configure configure = Configure.builder() .bind("signPatent.rights", policy) .bind("features", policy) .bind("explainText", htmlRenderPolicy) .bind("littleDirection", htmlRenderPolicy) .bind("wholeDirection", htmlRenderPolicy) .build(); XWPFTemplate template = XWPFTemplate.compile(filePath, configure).render(new HashMap<>()); return template; } private PictureRenderData guidToStream(String guid) { PictureRenderData pictureRenderData = null; byte[] bytes = null; if (bytes != null) { try { bytes = fileManagerService.downloadSystemFileFromFMS(guid); } catch (Exception e) { } } FileInputStream fileInputStream = FileUtils.byteToFile(bytes); pictureRenderData = Pictures.ofStream(fileInputStream, PictureType.PNG) .create(); return pictureRenderData; } private List> getClaimMaps(List claims, String patentNo) { List> rights = new ArrayList<>(); if (claims == null || claims.size() == 0) { return rights; } Text claim = claims.stream().filter(item -> item.getIfOrigin().equals(true)).findFirst().orElse(null); if (claim == null) { return rights; } PatentRightParams params = new PatentRightParams(); params.setContent(claim.getTextContent()); params.setPatentNo(patentNo); List patentRights = PatentRightUtils.formatPatentRight(params); //给权要进行格式更改 patentRights.forEach(item -> { Map temMap = new HashMap<>(); temMap.put("rightName", "权" + (item.getSort() + 1)); temMap.put("content", item.getContent()); temMap.put("rightNameLong", "权利要求" + (item.getSort() + 1)); temMap.put("parentSort", item.getParentSort()); temMap.put("sort", item.getSort()); rights.add(temMap); }); return rights; } /** * @param projectId * @param filePath * @return * @description 装载FTO模板 */ private XWPFTemplate FTOTemplate(Integer projectId, String filePath, Integer templateId) throws IOException { //获得模板的配置信息 LambdaQueryWrapper templateConfigWrapper = new LambdaQueryWrapper(); templateConfigWrapper.eq(TemplateConfig::getTemplateId, templateId); List templateConfigs = templateConfigService.list(templateConfigWrapper); List> classifyMaps = new ArrayList<>(); for (TemplateConfig templateConfig : templateConfigs) { Map classifyMap = new HashMap<>(); String value = templateConfig.getValueName(); //根据value 查询专利 List esCustomFieldDTOS = customFieldService.getCustomFieldByValueName(value, projectId); if (esCustomFieldDTOS.size() != 0) { classifyMap.put("classify", templateConfig.getMapName()); StringRequest stringRequest = new StringRequest(); stringRequest.setCustomFields(esCustomFieldDTOS); stringRequest.setProjectId(projectId); List patentColumnDTOS = new ArrayList<>(); try { PatentDTO patentDTO = esService.esSearch(stringRequest); patentColumnDTOS = patentDTO.getPatents(); } catch (Exception e) { return null; } //存放公告专利 List> announcePatents = new ArrayList<>(); //存放公开专利 List> publicPatents = new ArrayList<>(); //存放失效专利信息 List> lapsePatents = new ArrayList<>(); for (PatentColumnDTO patent : patentColumnDTOS) { String applicants = ""; StringBuilder rightPerson = new StringBuilder(); if (patent.getApplicant() != null && patent.getApplicant().size() != 0) { applicants = StringUtils.join(patent.getApplicant(), "\r"); } List affairs = esLegalEventService.getStrLegalEvent(patent.getAppNo()); String affairStrs = ""; if (affairs != null && affairs.size() != 0) { affairStrs = StringUtils.join(affairs, "\r"); } //装载单个专利的信息 Map patentMap = new HashMap<>(); //同族专利 try { //装载同族信息 List> familyMaps = esFamilyService.getPatentFamilyByNos(Arrays.asList(patent.getPatentNo()), "inpadoc"); if (familyMaps != null && familyMaps.size() > 0) { Map familyMap = familyMaps.get(0); if (familyMap.get("nos") != null) { List nos = (List) familyMap.get("nos"); if (nos != null && nos.size() > 0) { String nosStr = StringUtils.join(nos, "\r"); patentMap.put("simpleFamilys", nosStr); } } } } catch (Exception e) { } //引用专利 try { Map map = esQuotePatentService.getEsQutePatentByNos(patent.getPatentNo()); if (map.get("nos") != null) { List nos = (List) map.get("nos"); if (nos != null && nos.size() > 0) { String nosStr = StringUtils.join(nos, "\r"); patentMap.put("quotePatents", nosStr); } } } catch (Exception e) { } //申请日 patentMap.put("applicationDate", patent.getAppDate()); //公开日 patentMap.put("publicDate", patent.getPublicDate()); //专利号 patentMap.put("publicNo", patent.getPatentNo()); patentMap.put("firstPublicDate", patent.getPublicDate()); patentMap.put("applicant", applicants); patentMap.put("rightPerson", rightPerson); patentMap.put("affair", affairStrs); //优先权日 if (patent.getPriorities() != null && patent.getPriorities().size() != 0) { patentMap.put("priorityDate", patent.getPriorities().get(0).getPriorityDate()); } //图示 String guid = FormatUtil.getPictureFormat(patent.getAppNo()); PictureRenderData pictureData = this.guidToStream(guid); if (pictureData != null) { patentMap.put("abstractPath", pictureData); } //获得专利对比记录的信息 Map temMap = ftoCompareRecordService.LoadCompareMessageForExport(projectId, patent.getPatentNo()); List compareMessageVOS = (List) temMap.get("compareMessageVOs"); //装载对比记录的信息 patentMap.put("cM", compareMessageVOS); patentMap.put("rightNum", temMap.get("right")); patentMap.put("mainRightNum", temMap.get("mainRight")); Integer status = Integer.parseInt(patent.getSimpleStatus()); if (status == 1 || status == 5 || status == 6) { announcePatents.add(patentMap); } else if (status == 2) { lapsePatents.add(patentMap); } else if (status == 3) { publicPatents.add(patentMap); } classifyMap.put("publicPatents", publicPatents); classifyMap.put("lapsePatents", lapsePatents); classifyMap.put("announcePatents", announcePatents); } classifyMaps.add(classifyMap); } } Map map = new HashMap<>(); String date = DateUtils.formatDate(new Date(), DateUtils.YYYY_MM_DD); String[] ds = date.split("-"); map.put("sys", new SystemMO(ds[0], ds[1], ds[2], "", "reportName")); map.put("classifys", classifyMaps); // 为表格的显示绑定行循环 LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy(); HtmlRenderPolicy htmlRenderPolicy = new HtmlRenderPolicy(); // 将bz设置为行循环绑定的数据源的key,即key是bz的value会在模板中的{{bz}}处进行解析 Configure configure = Configure.builder().bind("cM", policy).bind("targetDescription", htmlRenderPolicy).build(); XWPFTemplate template = XWPFTemplate.compile(filePath, configure).render(map); return template; } }