AnalysisCommonServiceImpl.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. package com.redxun.knowledge.analysis.service;
  2. import com.alibaba.fastjson.JSON;
  3. import com.baomidou.mybatisplus.core.metadata.IPage;
  4. import com.redxun.common.base.entity.QueryData;
  5. import com.redxun.common.tool.StringUtils;
  6. import com.redxun.knowledge.analysis.entity.consts.PlatformConst;
  7. import com.redxun.knowledge.analysis.entity.dao.KnowledgeDao;
  8. import com.redxun.knowledge.analysis.entity.vo.*;
  9. import com.redxun.knowledge.analysis.mapper.AnalysisCommonMapper;
  10. import com.redxun.knowledge.analysis.mapper.PvLogMapper;
  11. import com.redxun.knowledge.utils.DateUtils;
  12. import com.redxun.knowledge.utils.PageListUtils;
  13. import lombok.extern.slf4j.Slf4j;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.stereotype.Service;
  16. import java.math.BigDecimal;
  17. import java.util.*;
  18. import java.util.stream.Collectors;
  19. /**
  20. * 文件名: CommonServiceImpl
  21. * 作者: zizg
  22. * 时间: 2023/3/17
  23. * 描述:
  24. * 修改人:
  25. * 修改时间:
  26. * 修改内容:
  27. */
  28. @Service
  29. @Slf4j
  30. public class AnalysisCommonServiceImpl {
  31. @Autowired
  32. private AnalysisCommonMapper analysisCommonMapper;
  33. @Autowired
  34. private PvLogMapper pvLogMapper;
  35. /**
  36. * 各模块整体数量统计:知识仓库(知识总数量);知识地图(地图总数量);知识专辑(专辑总数量)
  37. *
  38. * @return
  39. */
  40. public Map<String, Object> totalAmount() {
  41. Map<String, Object> result = new HashMap<>();
  42. Integer knowledgeTotal = analysisCommonMapper.knowledgeTotalAmount();
  43. Integer mapTotal = analysisCommonMapper.mapTotalAmount();
  44. Integer albumTotal = analysisCommonMapper.albumTotalAmount();
  45. result.put("knowledge", knowledgeTotal);
  46. result.put("map", mapTotal);
  47. result.put("album", albumTotal);
  48. return result;
  49. }
  50. /**
  51. * 查询各类型(维基、文档)知识数量
  52. *
  53. * @return
  54. */
  55. public Map<String, Object> knowledgeTypeAmount() {
  56. Map<String, Object> result = new HashMap<>();
  57. //类型为1(文档知识)
  58. Integer archiveTotal = analysisCommonMapper.knowledgeTotalByType(1);
  59. //类型为2(维基知识)
  60. Integer wikiTotal = analysisCommonMapper.knowledgeTotalByType(2);
  61. result.put("archive", archiveTotal);
  62. result.put("wiki", wikiTotal);
  63. result.put("total", archiveTotal + wikiTotal);
  64. return result;
  65. }
  66. /**
  67. * 获取搜索服务分词的词云数据
  68. *
  69. * @param tops
  70. * @return
  71. */
  72. public List<SearchParticipleWordCloudVo> searchParticipleWordCloud(Integer tops) {
  73. List<SearchParticipleWordCloudVo> searchParticipleWordCloudVos = analysisCommonMapper.searchParticipleWordCloud(tops);
  74. List<SearchParticipleWordCloudVo> result = searchParticipleWordCloudVos.stream().limit(tops).collect(Collectors.toList());
  75. return result;
  76. }
  77. /**
  78. * 获取搜索服务访问量分布柱状图数据,查看一次详情算一次PV
  79. *
  80. * @return
  81. */
  82. public SearchVisitHistogramVo searchVisitHistogram() {
  83. SearchVisitHistogramVo searchVisitHistogramVo = new SearchVisitHistogramVo();
  84. Calendar calendar = Calendar.getInstance();
  85. int year = calendar.get(Calendar.YEAR);
  86. //统计当前年的搜索访问总量
  87. int yearTotal = pvLogMapper.selectYearTotal(DateUtils.format(DateUtils.getFirstOfYear(year)),
  88. DateUtils.format(DateUtils.getLastOfYear(year)));
  89. //统计每月各个系统的搜索访问量
  90. List<LablesVo> lablesVoList = new ArrayList<>();
  91. for (int i = 1; i <= 12; i++) {
  92. LablesVo lablesVo = new LablesVo();
  93. List<Map<String, Object>> values = new ArrayList<>();
  94. lablesVo.setName(i + "月");
  95. //查询出全部的业务系统名称
  96. List<String> platformList = PlatformConst.platformList();
  97. //获取每月的第一天和最后一天
  98. String firstOfMonth = DateUtils.getFirstOfMonth(year, i, 15);
  99. String lastOfMonth = DateUtils.getLastOfMonth(year, i, 15);
  100. Integer sumByMonth = pvLogMapper.selectSearchVisitHistogramByMonth(firstOfMonth, lastOfMonth, null);
  101. //根据月份查询各个业务系统访问量
  102. platformList.forEach(e -> {
  103. Map<String, Object> map = new HashMap<>();
  104. Integer count = pvLogMapper.selectSearchVisitHistogramByMonth(firstOfMonth, lastOfMonth, e);
  105. map.put("legend", e);
  106. map.put("value", count);
  107. if (sumByMonth == 0) {
  108. map.put("percentage", 0.0000);
  109. } else {
  110. double sum = count / (sumByMonth.doubleValue());
  111. BigDecimal bd = new BigDecimal(sum);
  112. double f1 = bd.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
  113. map.put("percentage", f1);
  114. }
  115. values.add(map);
  116. });
  117. lablesVo.setValues(values);
  118. lablesVoList.add(lablesVo);
  119. }
  120. //返回值
  121. searchVisitHistogramVo.setTotal(yearTotal);
  122. searchVisitHistogramVo.setLables(lablesVoList);
  123. return searchVisitHistogramVo;
  124. }
  125. /**
  126. * 知识类型访问量分布
  127. *
  128. * @return
  129. */
  130. public Map<String, Object> knowledgeTypeVisitProportion(String type) {
  131. Map<String, Object> map = new HashMap<>();
  132. //获取当前年
  133. Calendar calendar = Calendar.getInstance();
  134. int year = calendar.get(Calendar.YEAR);
  135. //查询出结果
  136. List<String> collection = new ArrayList<>();
  137. if (type.equals("total")) {
  138. collection = analysisCommonMapper.knowledgeTypeVisitProportion(null, null);
  139. } else if (type.equals("year")) {
  140. collection = analysisCommonMapper.knowledgeTypeVisitProportion(DateUtils.format(DateUtils.getFirstOfYear(year)),
  141. DateUtils.format(DateUtils.getLastOfYear(year)));
  142. } else if (type.equals("month")) {
  143. int month = calendar.get(Calendar.MONTH);
  144. String firstOfMonth = DateUtils.getFirstOfMonth(year, month + 1, 15);
  145. String lastOfMonth = DateUtils.getLastOfMonth(year, month + 1, 15);
  146. collection = analysisCommonMapper.knowledgeTypeVisitProportion(firstOfMonth, lastOfMonth);
  147. } else {
  148. return null;
  149. }
  150. //json解析
  151. List<Integer> count = collection.parallelStream().map(e -> {
  152. KnowledgeTypeParse knowledgeTypeParse = JSON.parseObject(e, KnowledgeTypeParse.class);
  153. return knowledgeTypeParse.getType();
  154. }).collect(Collectors.toList());
  155. //文档知识和维基知识查看数量
  156. int archive = (int) count.stream().filter(e -> e == 1).count();
  157. int wiki = (int) count.stream().filter(e -> e == 2).count();
  158. map.put("archive", archive);
  159. map.put("wiki", wiki);
  160. //计算百分比
  161. if (count.size() == 0) {
  162. map.put("archivePercentage", 0.00);
  163. map.put("wikiPercentage", 0.00);
  164. } else {
  165. BigDecimal archivebg = new BigDecimal(archive / (double) count.size());
  166. double archivedouble = archivebg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
  167. map.put("archivePercentage", archivedouble);
  168. BigDecimal wikibg = new BigDecimal(wiki / (double) count.size());
  169. double wikidouble = wikibg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
  170. map.put("wikiPercentage", wikidouble);
  171. }
  172. return map;
  173. }
  174. /**
  175. * 各一级分类知识创建数量(包含审核中和驳回的数据)
  176. *
  177. * @param type
  178. * @param sort
  179. * @return
  180. */
  181. public List<KnowledgeCategoryVo> level1KnowledgeHistogram(String type, String sort) {
  182. //查询全部一级分类
  183. List<String> level1KnowledgeIdList = analysisCommonMapper.level1Knowledge();
  184. List<KnowledgeCategoryVo> result = level1KnowledgeIdList.parallelStream().map(e -> {
  185. //一级分类下知识数量(包含审核中的、驳回的)
  186. List<Integer> count = new ArrayList<>();
  187. //获取当前年
  188. Calendar calendar = Calendar.getInstance();
  189. int year = calendar.get(Calendar.YEAR);
  190. KnowledgeCategoryVo knowledgeCategoryVo = new KnowledgeCategoryVo();
  191. knowledgeCategoryVo.setLabel(analysisCommonMapper.selectCategoryName(e));
  192. //查询知识分类下创建的知识数量(纬度:年 最近1个月 全部)
  193. if (type.equals("total")) {
  194. count = analysisCommonMapper.level1KnowledgeHistogram(e, null, null);
  195. } else if (type.equals("year")) {
  196. count = analysisCommonMapper.level1KnowledgeHistogram(e, DateUtils.format(DateUtils.getFirstOfYear(year)),
  197. DateUtils.format(DateUtils.getLastOfYear(year)));
  198. } else if (type.equals("month")) {
  199. int month = calendar.get(Calendar.MONTH);
  200. String firstOfMonth = DateUtils.getFirstOfMonth(year, month + 1, 15);
  201. String lastOfMonth = DateUtils.getLastOfMonth(year, month + 1, 15);
  202. count = analysisCommonMapper.level1KnowledgeHistogram(e, firstOfMonth, lastOfMonth);
  203. }
  204. long archive = count.stream().filter(typeKnowledge -> typeKnowledge == 1).count();
  205. long wiki = count.stream().filter(typeKnowledge -> typeKnowledge == 2).count();
  206. knowledgeCategoryVo.setArchive((int) archive);
  207. knowledgeCategoryVo.setWiki((int) wiki);
  208. return knowledgeCategoryVo;
  209. }).collect(Collectors.toList());
  210. //排序
  211. if (StringUtils.isEmpty(sort)) {
  212. result = result.stream().sorted((t1, t2) -> t2.getWiki().compareTo(t1.getWiki())).collect(Collectors.toList());
  213. } else {
  214. if ("wikiDesc".equals(sort)) {
  215. result = result.stream().sorted((t1, t2) -> t2.getWiki().compareTo(t1.getWiki())).collect(Collectors.toList());
  216. } else {
  217. result = result.stream().sorted((t1, t2) -> t2.getArchive().compareTo(t1.getArchive())).collect(Collectors.toList());
  218. }
  219. }
  220. return result;
  221. }
  222. /**
  223. * 组织创建知识数量
  224. * @param queryData
  225. * @return
  226. */
  227. public IPage organizationKnowledges(QueryData queryData) {
  228. //获取当前年
  229. Calendar calendar = Calendar.getInstance();
  230. int year = calendar.get(Calendar.YEAR);
  231. List<KnowledgeDao> count = new ArrayList<>();
  232. if (("total").equals(queryData.getParams().get("type"))) {
  233. count = analysisCommonMapper.organizationKnowledges(null, null);
  234. } else if ("year".equals(queryData.getParams().get("type"))) {
  235. count = analysisCommonMapper.organizationKnowledges(DateUtils.format(DateUtils.getFirstOfYear(year)),
  236. DateUtils.format(DateUtils.getLastOfYear(year)));
  237. } else if ("month".equals(queryData.getParams().get("type"))) {
  238. int month = calendar.get(Calendar.MONTH);
  239. String firstOfMonth = DateUtils.getFirstOfMonth(year, month + 1, 15);
  240. String lastOfMonth = DateUtils.getLastOfMonth(year, month + 1, 15);
  241. count = analysisCommonMapper.organizationKnowledges(firstOfMonth, lastOfMonth);
  242. }
  243. List<KnowledgeCompanyVo> result = count.parallelStream().map(e -> {
  244. KnowledgeCompanyVo knowledgeCompanyVo = new KnowledgeCompanyVo();
  245. knowledgeCompanyVo.setCompany(e.getCompanyId());
  246. e.getCounts().forEach(c -> {
  247. if (c.containsKey("type") && c.get("type").intValue() == 1) {
  248. knowledgeCompanyVo.setArchive(c.get("count").intValue());
  249. }
  250. if (c.containsKey("type") && c.get("type").intValue() == 2) {
  251. knowledgeCompanyVo.setWiki(c.get("count").intValue());
  252. }
  253. });
  254. if (knowledgeCompanyVo.getArchive() == null){
  255. knowledgeCompanyVo.setArchive(0);
  256. }
  257. if (knowledgeCompanyVo.getWiki() == null){
  258. knowledgeCompanyVo.setWiki(0);
  259. }
  260. knowledgeCompanyVo.setTotal(knowledgeCompanyVo.getArchive() + knowledgeCompanyVo.getWiki());
  261. return knowledgeCompanyVo;
  262. }).collect(Collectors.toList());
  263. //排序
  264. String sort = queryData.getParams().get("sort");
  265. if (sort != null) {
  266. switch (sort) {
  267. case "totalDesc":
  268. result = result.stream().sorted((t1, t2) -> t2.getTotal().compareTo(t1.getTotal())).collect(Collectors.toList());
  269. break;
  270. case "totalAsc":
  271. result = result.stream().sorted(Comparator.comparing(KnowledgeCompanyVo::getTotal)).collect(Collectors.toList());
  272. break;
  273. case "archiveDesc":
  274. result = result.stream().sorted((t1, t2) -> t2.getArchive().compareTo(t1.getArchive())).collect(Collectors.toList());
  275. break;
  276. case "archiveAsc":
  277. result = result.stream().sorted(Comparator.comparing(KnowledgeCompanyVo::getArchive)).collect(Collectors.toList());
  278. break;
  279. case "wikiDesc":
  280. result = result.stream().sorted((t1, t2) -> t2.getWiki().compareTo(t1.getWiki())).collect(Collectors.toList());
  281. break;
  282. case "wikiAsc":
  283. result = result.stream().sorted(Comparator.comparing(KnowledgeCompanyVo::getWiki)).collect(Collectors.toList());
  284. break;
  285. }
  286. } else {
  287. result = result.stream().sorted((t1, t2) -> t2.getTotal().compareTo(t1.getTotal())).collect(Collectors.toList());
  288. }
  289. //获取top10公司数据
  290. String tops = queryData.getParams().get("tops");
  291. List<KnowledgeCompanyVo> resultTop = new ArrayList<>();
  292. if (StringUtils.isNotEmpty(tops)){
  293. resultTop = result.stream().limit(Long.parseLong(tops)).collect(Collectors.toList());
  294. } else {
  295. resultTop = result.stream().limit(10).collect(Collectors.toList());
  296. }
  297. return PageListUtils.getPages(queryData.getPageNo(),queryData.getPageSize(),resultTop);
  298. }
  299. }