ReportExportService.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. package cn.cslg.pas.service;
  2. import cn.cslg.pas.common.dto.PatentColumnDTO;
  3. import cn.cslg.pas.common.dto.PatentDTO;
  4. import cn.cslg.pas.common.dto.es.EsCustomFieldValueDTO;
  5. import cn.cslg.pas.common.model.SystemMO;
  6. import cn.cslg.pas.common.model.cronModel.PersonnelVO;
  7. import cn.cslg.pas.common.model.request.StringRequest;
  8. import cn.cslg.pas.common.utils.*;
  9. import cn.cslg.pas.common.vo.*;
  10. import cn.cslg.pas.domain.ReportDocument;
  11. import cn.cslg.pas.domain.business.*;
  12. import cn.cslg.pas.domain.es.Text;
  13. import cn.cslg.pas.exception.XiaoShiException;
  14. import cn.cslg.pas.mapper.AvoidDesignMapper;
  15. import cn.cslg.pas.service.business.*;
  16. import cn.cslg.pas.service.business.es.*;
  17. import cn.cslg.pas.service.common.FileManagerService;
  18. import cn.hutool.core.util.IdUtil;
  19. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  20. import com.deepoove.poi.XWPFTemplate;
  21. import com.deepoove.poi.config.Configure;
  22. import com.deepoove.poi.data.PictureRenderData;
  23. import com.deepoove.poi.data.PictureType;
  24. import com.deepoove.poi.data.Pictures;
  25. import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
  26. import lombok.extern.slf4j.Slf4j;
  27. import org.apache.commons.lang3.StringUtils;
  28. import org.ddr.poi.html.HtmlRenderPolicy;
  29. import org.springframework.beans.factory.annotation.Autowired;
  30. import org.springframework.stereotype.Service;
  31. import java.io.File;
  32. import java.io.FileInputStream;
  33. import java.io.IOException;
  34. import java.util.*;
  35. @Slf4j
  36. @Service
  37. public class ReportExportService {
  38. @Autowired
  39. private ReportTempleService templeService;
  40. @Autowired
  41. private FileUtils fileUtils;
  42. @Autowired
  43. private ReportProjectService reportProjectService;
  44. @Autowired
  45. private FileManagerService fileManagerService;
  46. @Autowired
  47. private EsPatentService esPatentService;
  48. @Autowired
  49. private AvoidDesignMapper avoidDesignMapper;
  50. @Autowired
  51. private TemplateConfigService templateConfigService;
  52. @Autowired
  53. private CustomFieldService customFieldService;
  54. @Autowired
  55. private EsService esService;
  56. @Autowired
  57. private FTOCompareRecordService ftoCompareRecordService;
  58. @Autowired
  59. private EsQuotePatentService esQuotePatentService;
  60. @Autowired
  61. private EsFamilyService esFamilyService;
  62. @Autowired
  63. private LoginUtils loginUtils;
  64. @Autowired
  65. private CacheUtils cacheUtils;
  66. @Autowired
  67. private EsLegalEventService esLegalEventService;
  68. /**
  69. * @param projectId
  70. * @param templeId
  71. * @return
  72. * @throws IOException
  73. * @function: 导出报告
  74. */
  75. public String exportReport(Integer projectId, Integer templeId) throws IOException {
  76. //根据模板ID获得模板
  77. ReportTemple reportTemplate = templeService.getById(templeId);
  78. //获得模板路径
  79. String templateFilePath = fileUtils.getPath(reportTemplate.getTemplatePath());
  80. //读取模板后保存生成word的地址
  81. String fileName = IdUtil.simpleUUID() + ".docx";
  82. String directoryName = fileUtils.createDirectory();
  83. String outPath = fileUtils.getSavePath(directoryName) + fileName;
  84. //根据ProjectId 获得报告
  85. LambdaQueryWrapper<ReportProject> queryWrapper = new LambdaQueryWrapper<>();
  86. queryWrapper.eq(ReportProject::getProjectId, projectId);
  87. ReportProject report = reportProjectService.getOne(queryWrapper, false);
  88. if (report == null) {
  89. throw new XiaoShiException("报告不存在");
  90. }
  91. XWPFTemplate template = null;
  92. if (report.getReportType() == 0 || report.getReportType() == 2) {
  93. // template = this.getstabilityTemplate(report, filePath);
  94. } else if (report.getReportType() == 3) {
  95. template = this.FTOTemplate(projectId, templateFilePath, templeId);
  96. } else if (report.getReportType() == 4) {
  97. template = this.Torttemplate(projectId, templateFilePath);
  98. } else if (report.getReportType() == 5) {
  99. template = this.avoidDesignTemplate(projectId, templateFilePath);
  100. } else if (report.getReportType() == 1) {
  101. // template = this.getThirdTemplate(report, filePath);
  102. }
  103. // 读取模板、数据并渲染
  104. // 文件是否已存在,则删除
  105. File file = new File(outPath);
  106. // 生成word保存在指定目录
  107. template.writeToFile(outPath);
  108. template.close();
  109. //导出成功后,导出报告记录入库
  110. List<String> ids = fileManagerService.uploadFileGetGuid2(Arrays.asList(file));
  111. if (ids == null || ids.size() == 0) {
  112. throw new XiaoShiException("保存记录失败");
  113. }
  114. // PersonnelVO personnelVO = cacheUtils.getLoginUser(loginUtils.getId());
  115. PersonnelVO personnelVO = new PersonnelVO();
  116. personnelVO.setId("1");
  117. ReportDocument reportDocument = new ReportDocument();
  118. reportDocument.setProjectId(projectId);
  119. reportDocument.setFileName(fileName);
  120. reportDocument.setFileGuid(ids.get(0));
  121. reportDocument.setCreateId(personnelVO.getId());
  122. reportDocument.setTemplateId(reportTemplate.getId());
  123. reportDocument.setTemplateName(reportTemplate.getTemplateName());
  124. reportDocument.insert();
  125. if (file.exists()) {
  126. file.delete();
  127. }
  128. return ids.get(0);
  129. }
  130. private XWPFTemplate Torttemplate(Integer reportId, String filePath) throws IOException {
  131. //根据报告Id查询标的专利号
  132. LambdaQueryWrapper<ReportProject> projectLambdaQueryWrapper =new LambdaQueryWrapper<>();
  133. projectLambdaQueryWrapper.eq(ReportProject::getProjectId,reportId);
  134. ReportProject report = reportProjectService.getOne(projectLambdaQueryWrapper,false);
  135. String patentNo = report.getSignPatentNo();
  136. // PatentVO patentVO = new PatentVO();
  137. // patentVO.setPatentNos(Arrays.asList(patentNo));
  138. // //根据专利号查询专利详细信息
  139. // String resBody = outInterfaceService.getPatentDTOListForRMS(patentVO);
  140. // JSONObject jsonObject = JSONObject.parseObject(resBody);
  141. // List<JSONObject> patentDTOS = JSON.parseArray(jsonObject.get("data").toString(), JSONObject.class);
  142. // //装载单个专利的信息
  143. Map<String, Object> patentMap = new HashMap<>();
  144. // JSONObject patent = patentDTOS.get(0);
  145. // List<JSONObject> applicantJSONs = JSON.parseArray(patent.get("applicant").toString(), JSONObject.class);
  146. // StringBuilder applicants = new StringBuilder();
  147. // StringBuilder rightPerson = new StringBuilder();
  148. // applicantJSONs.forEach(tem -> {
  149. // if (Integer.parseInt(tem.get("dataType").toString()) == 1) {
  150. // applicants.append(tem.get("name") + "\r");
  151. // } else {
  152. // rightPerson.append(tem.get("name") + "\r");
  153. // }
  154. // });
  155. // //装载同族信息
  156. // JSONObject familys = JSONObject.parseObject(patent.get("family").toString());
  157. // List<String> patSnaps = JSON.parseArray(familys.get("patSnap").toString(), String.class);
  158. // List<String> simples = JSON.parseArray(familys.get("simple").toString(), String.class);
  159. // List<String> inpadocs = JSON.parseArray(familys.get("inpadoc").toString(), String.class);
  160. // patSnaps.addAll(simples);
  161. // patSnaps.addAll(inpadocs);
  162. // String familyStr = StringUtils.join(patSnaps, ",");
  163. // //装载法律状态
  164. // StringBuilder affair = new StringBuilder();
  165. // List<JSONObject> affaires = JSON.parseArray(patent.get("affair").toString(), JSONObject.class);
  166. // affaires.forEach(item -> {
  167. // affair.append(item.get("status") + "\r");
  168. // }
  169. // );
  170. // //同族专利
  171. // patentMap.put("simpleFamilys", familyStr);
  172. // //引用专利
  173. // patentMap.put("quotePatents", patent.get("quote"));
  174. // //申请日
  175. // patentMap.put("applicationDate", patent.get("applicationDate"));
  176. // //公开日
  177. // patentMap.put("publicDate", patent.get("publicDate"));
  178. // //专利号
  179. // patentMap.put("publicNo", patent.get("publicNo"));
  180. // patentMap.put("firstPublicDate", patent.get("firstPublicDate"));
  181. // patentMap.put("applicant", applicants);
  182. // patentMap.put("rightPerson", rightPerson);
  183. // patentMap.put("affair", affair);
  184. // patentMap.put("name", patent.get("name"));
  185. //图示
  186. patentMap.put("abstractPath", "");
  187. //获得专利对比记录的信息
  188. // Map<String, Object> temMap = compareMessageService.queryforTemplate(patent.get("patentNo").toString(), reportId);
  189. // List<CompareMessageVO> compareMessageVOS = (List<CompareMessageVO>) temMap.get("compareMessageVOs");
  190. //装载对比记录的信息
  191. // patentMap.put("cM", compareMessageVOS);
  192. // patentMap.put("rightNum", temMap.get("right"));
  193. // patentMap.put("mainRightNum", temMap.get("mainRight"));
  194. Map<String, Object> map = new HashMap<>();
  195. String date = DateUtils.formatDate(new Date(), DateUtils.YYYY_MM_DD);
  196. String[] ds = date.split("-");
  197. map.put("sys", new SystemMO(ds[0], ds[1], ds[2], "", "reportName"));
  198. map.put("patentMap", patentMap);
  199. map.put("", patentNo);
  200. // 为表格的显示绑定行循环
  201. LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
  202. HtmlRenderPolicy htmlRenderPolicy = new HtmlRenderPolicy();
  203. // 将bz设置为行循环绑定的数据源的key,即key是bz的value会在模板中的{{bz}}处进行解析
  204. Configure configure = Configure.builder().bind("cM", policy).bind("targetDescription", htmlRenderPolicy).build();
  205. XWPFTemplate template = XWPFTemplate.compile(filePath, configure).render(map);
  206. return template;
  207. }
  208. private XWPFTemplate avoidDesignTemplate(Integer projectId,String path) throws IOException {
  209. String filePath = fileUtils.getPath(path);
  210. log.info("开始处理导出规避设计报告,参数为:{}, {}", projectId, filePath);
  211. LambdaQueryWrapper<ReportProject> queryWrapper = new LambdaQueryWrapper<>();
  212. queryWrapper.eq(ReportProject::getProjectId, projectId);
  213. ReportProject report = reportProjectService.getOne(queryWrapper, false);
  214. String signPatentNo = report.getSignPatentNo();
  215. //装载标的专利信息
  216. ;
  217. //装载年月日
  218. String date = DateUtils.formatDate(new Date(), DateUtils.YYYY_MM_DD);
  219. String[] dates = date.split("-");
  220. SystemMO systemMO = new SystemMO()
  221. .setYear(dates[0])
  222. .setMonth(dates[1])
  223. .setDay(dates[2]);
  224. //
  225. //装载特征、解释、回避设计方向、回避设计总体方向
  226. List<AvoidFeaturesVO> features = avoidDesignMapper.selectAvoidDesign(projectId);
  227. // String wholeDirection = avoidDesignWholeDirectionMapper.selectWholeDirectionByReportId(reportId);
  228. // map.put("wholeDirection", wholeDirection);
  229. //
  230. //绑定政策(绑定集合和元素循环遍历)
  231. LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
  232. //把指定元素内容识别传输成html格式
  233. HtmlRenderPolicy htmlRenderPolicy = new HtmlRenderPolicy();
  234. Configure configure = Configure.builder()
  235. .bind("signPatent.rights", policy)
  236. .bind("features", policy)
  237. .bind("explainText", htmlRenderPolicy)
  238. .bind("littleDirection", htmlRenderPolicy)
  239. .bind("wholeDirection", htmlRenderPolicy)
  240. .build();
  241. XWPFTemplate template = XWPFTemplate.compile(filePath, configure).render(new HashMap<>());
  242. return template;
  243. }
  244. private PictureRenderData guidToStream(String guid) {
  245. PictureRenderData pictureRenderData = null;
  246. byte[] bytes = null;
  247. if (bytes != null) {
  248. try {
  249. bytes = fileManagerService.downloadSystemFileFromFMS(guid);
  250. } catch (Exception e) {
  251. }
  252. }
  253. FileInputStream fileInputStream = FileUtils.byteToFile(bytes);
  254. pictureRenderData = Pictures.ofStream(fileInputStream, PictureType.PNG)
  255. .create();
  256. return pictureRenderData;
  257. }
  258. private List<Map<String, Object>> getClaimMaps(List<Text> claims, String patentNo) {
  259. List<Map<String, Object>> rights = new ArrayList<>();
  260. if (claims == null || claims.size() == 0) {
  261. return rights;
  262. }
  263. Text claim = claims.stream().filter(item -> item.getIfOrigin().equals(true)).findFirst().orElse(null);
  264. if (claim == null) {
  265. return rights;
  266. }
  267. PatentRightParams params = new PatentRightParams();
  268. params.setContent(claim.getTextContent());
  269. params.setPatentNo(patentNo);
  270. List<RePatentClaim> patentRights = PatentRightUtils.formatPatentRight(params);
  271. //给权要进行格式更改
  272. patentRights.forEach(item -> {
  273. Map<String, Object> temMap = new HashMap<>();
  274. temMap.put("rightName", "权" + (item.getSort() + 1));
  275. temMap.put("content", item.getContent());
  276. temMap.put("rightNameLong", "权利要求" + (item.getSort() + 1));
  277. temMap.put("parentSort", item.getParentSort());
  278. temMap.put("sort", item.getSort());
  279. rights.add(temMap);
  280. });
  281. return rights;
  282. }
  283. /**
  284. * @param projectId
  285. * @param filePath
  286. * @return
  287. * @description 装载FTO模板
  288. */
  289. private XWPFTemplate FTOTemplate(Integer projectId, String filePath, Integer templateId) throws IOException {
  290. //获得模板的配置信息
  291. LambdaQueryWrapper<TemplateConfig> templateConfigWrapper = new LambdaQueryWrapper<TemplateConfig>();
  292. templateConfigWrapper.eq(TemplateConfig::getTemplateId, templateId);
  293. List<TemplateConfig> templateConfigs = templateConfigService.list(templateConfigWrapper);
  294. List<Map<String, Object>> classifyMaps = new ArrayList<>();
  295. for (TemplateConfig templateConfig : templateConfigs) {
  296. Map<String, Object> classifyMap = new HashMap<>();
  297. String value = templateConfig.getValueName();
  298. //根据value 查询专利
  299. List<EsCustomFieldValueDTO> esCustomFieldDTOS = customFieldService.getCustomFieldByValueName(value, projectId);
  300. if (esCustomFieldDTOS.size() != 0) {
  301. classifyMap.put("classify", templateConfig.getMapName());
  302. StringRequest stringRequest = new StringRequest();
  303. stringRequest.setCustomFields(esCustomFieldDTOS);
  304. stringRequest.setProjectId(projectId);
  305. List<PatentColumnDTO> patentColumnDTOS = new ArrayList<>();
  306. try {
  307. PatentDTO patentDTO = esService.esSearch(stringRequest);
  308. patentColumnDTOS = patentDTO.getPatents();
  309. } catch (Exception e) {
  310. return null;
  311. }
  312. //存放公告专利
  313. List<Map<String, Object>> announcePatents = new ArrayList<>();
  314. //存放公开专利
  315. List<Map<String, Object>> publicPatents = new ArrayList<>();
  316. //存放失效专利信息
  317. List<Map<String, Object>> lapsePatents = new ArrayList<>();
  318. for (PatentColumnDTO patent : patentColumnDTOS) {
  319. String applicants = "";
  320. StringBuilder rightPerson = new StringBuilder();
  321. if (patent.getApplicant() != null && patent.getApplicant().size() != 0) {
  322. applicants = StringUtils.join(patent.getApplicant(), "\r");
  323. }
  324. List<String> affairs = esLegalEventService.getStrLegalEvent(patent.getAppNo());
  325. String affairStrs = "";
  326. if (affairs != null && affairs.size() != 0) {
  327. affairStrs = StringUtils.join(affairs, "\r");
  328. }
  329. //装载单个专利的信息
  330. Map<String, Object> patentMap = new HashMap<>();
  331. //同族专利
  332. try {
  333. //装载同族信息
  334. List<Map<String, Object>> familyMaps = esFamilyService.getPatentFamilyByNos(Arrays.asList(patent.getPatentNo()), "inpadoc");
  335. if (familyMaps != null && familyMaps.size() > 0) {
  336. Map<String, Object> familyMap = familyMaps.get(0);
  337. if (familyMap.get("nos") != null) {
  338. List<String> nos = (List<String>) familyMap.get("nos");
  339. if (nos != null && nos.size() > 0) {
  340. String nosStr = StringUtils.join(nos, "\r");
  341. patentMap.put("simpleFamilys", nosStr);
  342. }
  343. }
  344. }
  345. } catch (Exception e) {
  346. }
  347. //引用专利
  348. try {
  349. Map<String, Object> map = esQuotePatentService.getEsQutePatentByNos(patent.getPatentNo());
  350. if (map.get("nos") != null) {
  351. List<String> nos = (List<String>) map.get("nos");
  352. if (nos != null && nos.size() > 0) {
  353. String nosStr = StringUtils.join(nos, "\r");
  354. patentMap.put("quotePatents", nosStr);
  355. }
  356. }
  357. } catch (Exception e) {
  358. }
  359. //申请日
  360. patentMap.put("applicationDate", patent.getAppDate());
  361. //公开日
  362. patentMap.put("publicDate", patent.getPublicDate());
  363. //专利号
  364. patentMap.put("publicNo", patent.getPatentNo());
  365. patentMap.put("firstPublicDate", patent.getPublicDate());
  366. patentMap.put("applicant", applicants);
  367. patentMap.put("rightPerson", rightPerson);
  368. patentMap.put("affair", affairStrs);
  369. //优先权日
  370. if (patent.getPriorities() != null && patent.getPriorities().size() != 0) {
  371. patentMap.put("priorityDate", patent.getPriorities().get(0).getPriorityDate());
  372. }
  373. //图示
  374. String guid = FormatUtil.getPictureFormat(patent.getAppNo());
  375. PictureRenderData pictureData = this.guidToStream(guid);
  376. if (pictureData != null) {
  377. patentMap.put("abstractPath", pictureData);
  378. }
  379. //获得专利对比记录的信息
  380. Map<String, Object> temMap = ftoCompareRecordService.LoadCompareMessageForExport(projectId, patent.getPatentNo());
  381. List<CompareMessageVO> compareMessageVOS = (List<CompareMessageVO>) temMap.get("compareMessageVOs");
  382. //装载对比记录的信息
  383. patentMap.put("cM", compareMessageVOS);
  384. patentMap.put("rightNum", temMap.get("right"));
  385. patentMap.put("mainRightNum", temMap.get("mainRight"));
  386. Integer status = Integer.parseInt(patent.getSimpleStatus());
  387. if (status == 1 || status == 5 || status == 6) {
  388. announcePatents.add(patentMap);
  389. } else if (status == 2) {
  390. lapsePatents.add(patentMap);
  391. } else if (status == 3) {
  392. publicPatents.add(patentMap);
  393. }
  394. classifyMap.put("publicPatents", publicPatents);
  395. classifyMap.put("lapsePatents", lapsePatents);
  396. classifyMap.put("announcePatents", announcePatents);
  397. }
  398. classifyMaps.add(classifyMap);
  399. }
  400. }
  401. Map<String, Object> map = new HashMap<>();
  402. String date = DateUtils.formatDate(new Date(), DateUtils.YYYY_MM_DD);
  403. String[] ds = date.split("-");
  404. map.put("sys", new SystemMO(ds[0], ds[1], ds[2], "", "reportName"));
  405. map.put("classifys", classifyMaps);
  406. // 为表格的显示绑定行循环
  407. LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
  408. HtmlRenderPolicy htmlRenderPolicy = new HtmlRenderPolicy();
  409. // 将bz设置为行循环绑定的数据源的key,即key是bz的value会在模板中的{{bz}}处进行解析
  410. Configure configure = Configure.builder().bind("cM", policy).bind("targetDescription", htmlRenderPolicy).build();
  411. XWPFTemplate template = XWPFTemplate.compile(filePath, configure).render(map);
  412. return template;
  413. }
  414. }