ReportExportService.java 21 KB

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