4
0

AnalysisCommonServiceImpl.java 13 KB

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