ReportService.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. package com.cslg.ppa.service;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  4. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  5. import com.cslg.ppa.common.core.auth.Response;
  6. import com.cslg.ppa.common.exception.ExceptionEnum;
  7. import com.cslg.ppa.common.exception.XiaoShiException;
  8. import com.cslg.ppa.common.utils.DateUtil;
  9. import com.cslg.ppa.common.utils.FileUtils;
  10. import com.cslg.ppa.dto.*;
  11. import com.cslg.ppa.entity.*;
  12. import com.cslg.ppa.entity.commom.Records;
  13. import com.cslg.ppa.mapper.ArticleInfoMapper;
  14. import com.cslg.ppa.mapper.AssoReportArticleMapper;
  15. import com.cslg.ppa.mapper.ReportMapper;
  16. import com.cslg.ppa.service.commom.FileManagerService;
  17. import com.cslg.ppa.vo.*;
  18. import com.deepoove.poi.XWPFTemplate;
  19. import com.deepoove.poi.config.Configure;
  20. import com.deepoove.poi.config.ConfigureBuilder;
  21. import com.deepoove.poi.plugin.toc.TOCRenderPolicy;
  22. import lombok.RequiredArgsConstructor;
  23. import lombok.extern.slf4j.Slf4j;
  24. import org.apache.commons.lang3.StringUtils;
  25. import org.apache.poi.xwpf.usermodel.*;
  26. import org.apache.xmlbeans.XmlCursor;
  27. import org.ddr.poi.html.HtmlRenderPolicy;
  28. import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
  29. import org.springframework.beans.factory.annotation.Autowired;
  30. import org.springframework.context.annotation.Lazy;
  31. import org.springframework.scheduling.annotation.Scheduled;
  32. import org.springframework.stereotype.Service;
  33. import org.springframework.transaction.annotation.Propagation;
  34. import org.springframework.transaction.annotation.Transactional;
  35. import org.springframework.util.CollectionUtils;
  36. import java.io.File;
  37. import java.io.FileInputStream;
  38. import java.io.FileOutputStream;
  39. import java.io.IOException;
  40. import java.util.*;
  41. import java.util.stream.Collectors;
  42. @Slf4j
  43. @Service
  44. @RequiredArgsConstructor(onConstructor_ = {@Lazy})
  45. public class ReportService extends ServiceImpl<ReportMapper, Report> {
  46. @Autowired
  47. private ReportTempleService reportTempleService;
  48. @Autowired
  49. private FileUtils fileUtils;
  50. @Autowired
  51. private FileManagerService fileManagerService;
  52. @Autowired
  53. private ReportMapper reportMapper;
  54. @Autowired
  55. private AssoReportArticleMapper assoReportArticleMapper;
  56. @Autowired
  57. private ArticleInfoMapper articleInfoMapper;
  58. private static final List<String> SPECIAL_KEYWORDS = Arrays.asList("活动", "研讨", "座谈会", "举办", "举行", "培训",
  59. "课堂", "论证会", "讨论", "调研", "会议", "召开","招聘","主题","讲座","大会","专利代理师","博览会","资助","参评","年会","参观","学习");
  60. public String selectReportList(SelectReportListDTO vo) {
  61. LambdaQueryWrapper<Report> wrapper = new LambdaQueryWrapper<>();
  62. if (StringUtils.isNotEmpty(vo.getReportName())) {
  63. wrapper.eq(Report::getReportName, vo.getReportName());
  64. }
  65. wrapper.orderByDesc(Report::getCreateTime);
  66. Page<Report> page = new Page<>(vo.getPageNum(), vo.getPageSize());
  67. Page<Report> reportPage = reportMapper.selectPage(page, wrapper);
  68. List<Report> reports = reportPage.getRecords();
  69. List<SelectReportListVO> reportList = new ArrayList<>();
  70. if (!CollectionUtils.isEmpty(reports)) {
  71. for (Report report : reports) {
  72. SelectReportListVO reportVO = new SelectReportListVO();
  73. reportVO.setReportId(report.getId());
  74. reportVO.setReportName(report.getReportName());
  75. reportVO.setApprove(report.getApprove());
  76. reportVO.setCreator(report.getCreator());
  77. reportVO.setApproveStatus(report.getApproveStatus());
  78. reportVO.setCreateTime(report.getCreateTime());
  79. reportVO.setApproveTime(report.getApproveTime());
  80. reportList.add(reportVO);
  81. }
  82. }
  83. Records records = new Records();
  84. long total = reportPage.getTotal();
  85. records.setCurrent(vo.getPageNum());
  86. records.setTotal(total);
  87. records.setSize(vo.getPageSize());
  88. records.setData(reportList);
  89. return Response.success(records);
  90. }
  91. public String selectReportDetail(ReportIdDTO vo) {
  92. Report report = reportMapper.selectById(vo.getReportId());
  93. return Response.success(report);
  94. }
  95. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
  96. public Integer addReport(ReportDTO vo) {
  97. String reportName = vo.getReportName();
  98. if (StringUtils.isEmpty(reportName)) {
  99. throw new XiaoShiException(ExceptionEnum.BUSINESS_ERROR, "报告名称不可为空");
  100. }
  101. //todo 创建者字段后续再加
  102. Report report = new Report();
  103. report.setReportName(vo.getReportName());
  104. report.insert();
  105. List<Integer> articleIds = vo.getArticleIds();
  106. if (!CollectionUtils.isEmpty(articleIds)) {
  107. for (Integer articleId : articleIds) {
  108. Long count = assoReportArticleMapper.selectCount(new LambdaQueryWrapper<AssoReportArticle>()
  109. .eq(AssoReportArticle::getReportId,report.getId())
  110. .eq(AssoReportArticle::getArticleId, articleId)
  111. .eq(AssoReportArticle::getIfUse, 1));
  112. if (count > 1) {
  113. continue;
  114. }
  115. AssoReportArticle assoReportArticle = new AssoReportArticle();
  116. assoReportArticle.setReportId(report.getId());
  117. assoReportArticle.setArticleId(articleId);
  118. assoReportArticle.insert();
  119. }
  120. }
  121. return report.getId();
  122. }
  123. public Integer editReport(ReportDTO vo) {
  124. Report report = reportMapper.selectById(vo.getReportId());
  125. report.setReportName(vo.getReportName());
  126. report.updateById();
  127. return report.getId();
  128. }
  129. public void deleteReport(ReportIdDTO vo) {
  130. List<Integer> reportIds = vo.getReportIds();
  131. if (!CollectionUtils.isEmpty(reportIds)) {
  132. for (Integer reportId : reportIds) {
  133. //删除关联
  134. List<AssoReportArticle> assoReportArticles = assoReportArticleMapper.selectList(new LambdaQueryWrapper<AssoReportArticle>()
  135. .eq(AssoReportArticle::getReportId, reportId));
  136. if (!CollectionUtils.isEmpty(assoReportArticles)) {
  137. List<Integer> collect = assoReportArticles.stream().map(BaseEntity::getId).collect(Collectors.toList());
  138. assoReportArticleMapper.deleteBatchIds(collect);
  139. }
  140. //删除报告
  141. reportMapper.deleteById(reportId);
  142. }
  143. }
  144. }
  145. public List<SelectAssoReportArticleVO> selectAssoReportArticleList(SelectAssoReportArticleDTO vo) {
  146. Integer reportId = vo.getReportId();
  147. List<SelectAssoReportArticleVO> articleVOS = reportMapper.selectAssoReportArticleList(reportId);
  148. if (CollectionUtils.isEmpty(articleVOS)) {
  149. articleVOS = new ArrayList<>();
  150. }
  151. return articleVOS;
  152. }
  153. public Integer addArticleToReport(ReportDTO vo) {
  154. //todo 创建者字段后续再加
  155. Integer reportId = vo.getReportId();
  156. List<Integer> articleIds = vo.getArticleIds();
  157. if (!CollectionUtils.isEmpty(articleIds)) {
  158. for (Integer articleId : articleIds) {
  159. Long count = assoReportArticleMapper.selectCount(new LambdaQueryWrapper<AssoReportArticle>()
  160. .eq(AssoReportArticle::getReportId,reportId)
  161. .eq(AssoReportArticle::getArticleId, articleId)
  162. .eq(AssoReportArticle::getIfUse, 1));
  163. if (count > 1) {
  164. continue;
  165. }
  166. AssoReportArticle assoReportArticle = new AssoReportArticle();
  167. assoReportArticle.setReportId(reportId);
  168. assoReportArticle.setArticleId(articleId);
  169. assoReportArticle.insert();
  170. }
  171. }
  172. return reportId;
  173. }
  174. public Integer removeAssoArticleReport(AssoReportArticleIdDTO vo) {
  175. Integer assoId = vo.getAssoId();
  176. AssoReportArticle assoReportArticle = assoReportArticleMapper.selectById(assoId);
  177. assoReportArticle.setIfUse(0);
  178. assoReportArticle.updateById();
  179. return assoId;
  180. }
  181. public String exportReport(Integer reportId) {
  182. List<SelectAssoReportArticleVO> articleVOS = reportMapper.selectAssoReportArticleList(reportId);
  183. List<ExportReportVO> reportVOS = new ArrayList<>();
  184. if (!CollectionUtils.isEmpty(articleVOS)) {
  185. Map<String, List<SelectAssoReportArticleVO>> map = articleVOS.stream().collect(Collectors.groupingBy(SelectAssoReportArticleVO::getCategoryName));
  186. if (!CollectionUtils.isEmpty(map)) {
  187. for (String key : map.keySet()) {
  188. ExportReportVO reportVO = new ExportReportVO();
  189. reportVO.setCategoryName(key);
  190. List<SelectAssoReportArticleVO> reportArticleVOS = map.get(key);
  191. List<ExportReportDetailVO> detailVOS = new ArrayList<>();
  192. if (!CollectionUtils.isEmpty(reportArticleVOS)) {
  193. int i = 1;
  194. for (SelectAssoReportArticleVO reportArticleVO : reportArticleVOS) {
  195. ExportReportDetailVO detailVO = new ExportReportDetailVO();
  196. detailVO.setTitle(i + "、" + reportArticleVO.getTitle());
  197. detailVO.setDigest(reportArticleVO.getDigest());
  198. String htmlLink = "<a href='" + reportArticleVO.getArticleUrl() + "'>" + reportArticleVO.getArticleUrl() + "</a>";
  199. detailVO.setArticle_url(htmlLink);
  200. detailVOS.add(detailVO);
  201. i++;
  202. }
  203. }
  204. reportVO.setDetailVOS(detailVOS);
  205. reportVOS.add(reportVO);
  206. }
  207. }
  208. }
  209. List<ExportReportDetailVO> list = new ArrayList<>();
  210. List<ExportReportDetailVO> list1 = new ArrayList<>();
  211. List<ExportReportDetailVO> list2 = new ArrayList<>();
  212. List<ExportReportDetailVO> list3 = new ArrayList<>();
  213. List<ExportReportDetailVO> list4 = new ArrayList<>();
  214. if (!CollectionUtils.isEmpty(reportVOS)) {
  215. for (ExportReportVO reportVO : reportVOS) {
  216. String categoryName = reportVO.getCategoryName();
  217. if (StringUtils.equals(categoryName, "国家知识产权局")) {
  218. list.addAll(reportVO.getDetailVOS());
  219. } else if (StringUtils.equals(categoryName, "地方相关法律法规")) {
  220. list1.addAll(reportVO.getDetailVOS());
  221. } else if (StringUtils.equals(categoryName, "判例")) {
  222. list2.addAll(reportVO.getDetailVOS());
  223. } else if (StringUtils.equals(categoryName, "国外相关资讯")) {
  224. list3.addAll(reportVO.getDetailVOS());
  225. } else if (StringUtils.equals(categoryName, "行业资讯")) {
  226. list4.addAll(reportVO.getDetailVOS());
  227. }
  228. }
  229. }
  230. Map<String, Object> map = new HashMap<>();
  231. map.put("country_list", list);
  232. map.put("local_list", list1);
  233. map.put("case_list", list2);
  234. map.put("foreign_list", list3);
  235. map.put("business_list", list4);
  236. ReportTemple reportTemplate = reportTempleService.getById(1);
  237. String templateFilePath = fileUtils.getPath(reportTemplate.getTemplatePath());
  238. Report report = this.getById(reportId);
  239. String reportName = report.getReportName();
  240. //生成文档
  241. String fileGuid = null;
  242. try {
  243. fileGuid = this.generateReportFile(map, templateFilePath, reportName);
  244. //todo 后续添加导出记录
  245. } catch (Exception e) {
  246. throw new XiaoShiException(ExceptionEnum.BUSINESS_ERROR, "导出资讯报告失败");
  247. }
  248. return fileGuid;
  249. }
  250. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
  251. public String oneClickExport() {
  252. OneClickExportDTO vo = new OneClickExportDTO();
  253. vo.setBeginTime(DateUtil.getFirstDayOfMonthStr());
  254. vo.setEndTime(DateUtil.getLastDayOfMonthStr());
  255. List<OneClickExportVO> articleVOS = articleInfoMapper.selectArticleList(vo);
  256. List<ExportReportVO> reportVOS = new ArrayList<>();
  257. List<Integer> articleIds = new ArrayList<>();
  258. if (!CollectionUtils.isEmpty(articleVOS)) {
  259. Map<String, List<OneClickExportVO>> map = articleVOS.stream().collect(Collectors.groupingBy(OneClickExportVO::getCategoryName));
  260. if (!CollectionUtils.isEmpty(map)) {
  261. for (String key : map.keySet()) {
  262. ExportReportVO reportVO = new ExportReportVO();
  263. reportVO.setCategoryName(key);
  264. List<OneClickExportVO> reportArticleVOS = map.get(key);
  265. List<ExportReportDetailVO> detailVOS = new ArrayList<>();
  266. if (!CollectionUtils.isEmpty(reportArticleVOS)) {
  267. int i = 1;
  268. for (OneClickExportVO reportArticleVO : reportArticleVOS) {
  269. Integer categoryId = reportArticleVO.getCategoryId();
  270. String title = reportArticleVO.getTitle();
  271. if (categoryId == 2 && SPECIAL_KEYWORDS.stream().anyMatch(title::contains)) {
  272. continue;
  273. }
  274. ExportReportDetailVO detailVO = new ExportReportDetailVO();
  275. detailVO.setTitle(i + "、" + title);
  276. detailVO.setDigest(reportArticleVO.getDigest());
  277. String htmlLink = "<a href='" + reportArticleVO.getArticleUrl() + "'>" + reportArticleVO.getArticleUrl() + "</a>";
  278. detailVO.setArticle_url(htmlLink);
  279. detailVOS.add(detailVO);
  280. articleIds.add(reportArticleVO.getArticleId());
  281. i++;
  282. }
  283. }
  284. reportVO.setDetailVOS(detailVOS);
  285. reportVOS.add(reportVO);
  286. }
  287. }
  288. }
  289. String nowDateStr = DateUtil.getNowDateStr();
  290. String year = nowDateStr.substring(0, 4);
  291. String month = nowDateStr.substring(4, 6);
  292. String reportName = nowDateStr + "-知识产权相关法律法规资讯整理(威世博" + year + "年第" + month + "月)-v1r00";
  293. //添加报告
  294. ReportDTO reportDTO = new ReportDTO();
  295. reportDTO.setReportName(reportName);
  296. reportDTO.setArticleIds(articleIds);
  297. System.out.println(articleIds.size());
  298. this.addReport(reportDTO);
  299. List<ExportReportDetailVO> list = new ArrayList<>();
  300. List<ExportReportDetailVO> list1 = new ArrayList<>();
  301. List<ExportReportDetailVO> list2 = new ArrayList<>();
  302. List<ExportReportDetailVO> list3 = new ArrayList<>();
  303. List<ExportReportDetailVO> list4 = new ArrayList<>();
  304. if (!CollectionUtils.isEmpty(reportVOS)) {
  305. for (ExportReportVO reportVO : reportVOS) {
  306. String categoryName = reportVO.getCategoryName();
  307. if (StringUtils.equals(categoryName, "国家知识产权局")) {
  308. list.addAll(reportVO.getDetailVOS());
  309. } else if (StringUtils.equals(categoryName, "地方相关法律法规")) {
  310. list1.addAll(reportVO.getDetailVOS());
  311. } else if (StringUtils.equals(categoryName, "判例")) {
  312. list2.addAll(reportVO.getDetailVOS());
  313. } else if (StringUtils.equals(categoryName, "国外相关资讯")) {
  314. list3.addAll(reportVO.getDetailVOS());
  315. } else if (StringUtils.equals(categoryName, "行业资讯")) {
  316. list4.addAll(reportVO.getDetailVOS());
  317. }
  318. }
  319. }
  320. Map<String, Object> map = new HashMap<>();
  321. map.put("country_list", list);
  322. map.put("local_list", list1);
  323. map.put("case_list", list2);
  324. map.put("foreign_list", list3);
  325. map.put("business_list", list4);
  326. ReportTemple reportTemplate = reportTempleService.getById(1);
  327. String templateFilePath = fileUtils.getPath(reportTemplate.getTemplatePath());
  328. //生成文档
  329. String fileGuid = null;
  330. try {
  331. fileGuid = this.generateReportFile(map, templateFilePath, reportName);
  332. //todo 后续添加导出记录
  333. } catch (Exception e) {
  334. throw new XiaoShiException(ExceptionEnum.BUSINESS_ERROR, "导出资讯报告失败");
  335. }
  336. return fileGuid;
  337. }
  338. public String generateReportFile(Map<String, Object> map, String templateFilePath, String name) throws Exception {
  339. XWPFTemplate xwpfTemplate = this.getHtmlTemplate(map, templateFilePath);
  340. String fileName = name + ".docx";
  341. String directoryName = fileUtils.createDirectory();
  342. String outPath = fileUtils.getSavePath(directoryName) + fileName;
  343. File file = new File(outPath);
  344. // 生成word保存在指定目录
  345. xwpfTemplate.writeToFile(outPath);
  346. xwpfTemplate.close();
  347. List<String> ids = fileManagerService.uploadFileGetGuid2(Collections.singletonList(file));
  348. if (CollectionUtils.isEmpty(ids)) {
  349. throw new XiaoShiException("保存记录失败");
  350. }
  351. return ids.get(0);
  352. }
  353. private XWPFTemplate getHtmlTemplate(Map<String, Object> map, String filePath) {
  354. XWPFTemplate template = null;
  355. try {
  356. HtmlRenderPolicy htmlRenderPolicy = new HtmlRenderPolicy();
  357. ConfigureBuilder configureBuilder = Configure.builder().useSpringEL(false);
  358. configureBuilder.bind("article_url", htmlRenderPolicy);
  359. Configure configure = configureBuilder.build();
  360. template = XWPFTemplate.compile(filePath, configure).render(map);
  361. } catch (Exception e) {
  362. e.printStackTrace();
  363. throw new XiaoShiException(ExceptionEnum.BUSINESS_ERROR, "未匹配到模版文件");
  364. }
  365. return template;
  366. }
  367. }