123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- package com.redxun.knowledge.analysis.service;
- import com.alibaba.fastjson.JSON;
- import com.baomidou.mybatisplus.core.metadata.IPage;
- import com.redxun.common.base.entity.QueryData;
- import com.redxun.common.tool.StringUtils;
- import com.redxun.knowledge.analysis.entity.consts.PlatformConst;
- import com.redxun.knowledge.analysis.entity.dao.KnowledgeDao;
- import com.redxun.knowledge.analysis.entity.vo.*;
- import com.redxun.knowledge.analysis.mapper.AnalysisCommonMapper;
- import com.redxun.knowledge.analysis.mapper.PvLogMapper;
- import com.redxun.knowledge.utils.DateUtils;
- import com.redxun.knowledge.utils.PageListUtils;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import java.math.BigDecimal;
- import java.util.*;
- import java.util.stream.Collectors;
- /**
- * 文件名: CommonServiceImpl
- * 作者: zizg
- * 时间: 2023/3/17
- * 描述:
- * 修改人:
- * 修改时间:
- * 修改内容:
- */
- @Service
- @Slf4j
- public class AnalysisCommonServiceImpl {
- @Autowired
- private AnalysisCommonMapper analysisCommonMapper;
- @Autowired
- private PvLogMapper pvLogMapper;
- /**
- * 各模块整体数量统计:知识仓库(知识总数量);知识地图(地图总数量);知识专辑(专辑总数量)
- *
- * @return
- */
- public Map<String, Object> totalAmount() {
- Map<String, Object> result = new HashMap<>();
- Integer knowledgeTotal = analysisCommonMapper.knowledgeTotalAmount();
- Integer mapTotal = analysisCommonMapper.mapTotalAmount();
- Integer albumTotal = analysisCommonMapper.albumTotalAmount();
- result.put("knowledge", knowledgeTotal);
- result.put("map", mapTotal);
- result.put("album", albumTotal);
- return result;
- }
- /**
- * 查询各类型(维基、文档)知识数量
- *
- * @return
- */
- public Map<String, Object> knowledgeTypeAmount() {
- Map<String, Object> result = new HashMap<>();
- //类型为1(文档知识)
- Integer archiveTotal = analysisCommonMapper.knowledgeTotalByType(1);
- //类型为2(维基知识)
- Integer wikiTotal = analysisCommonMapper.knowledgeTotalByType(2);
- result.put("archive", archiveTotal);
- result.put("wiki", wikiTotal);
- result.put("total", archiveTotal + wikiTotal);
- return result;
- }
- /**
- * 获取搜索服务分词的词云数据
- *
- * @param tops
- * @return
- */
- public List<SearchParticipleWordCloudVo> searchParticipleWordCloud(Integer tops) {
- List<SearchParticipleWordCloudVo> searchParticipleWordCloudVos = analysisCommonMapper.searchParticipleWordCloud(tops);
- List<SearchParticipleWordCloudVo> result = searchParticipleWordCloudVos.stream().limit(tops).collect(Collectors.toList());
- return result;
- }
- /**
- * 获取搜索服务访问量分布柱状图数据,查看一次详情算一次PV
- *
- * @return
- */
- public SearchVisitHistogramVo searchVisitHistogram() {
- SearchVisitHistogramVo searchVisitHistogramVo = new SearchVisitHistogramVo();
- Calendar calendar = Calendar.getInstance();
- int year = calendar.get(Calendar.YEAR);
- //统计当前年的搜索访问总量
- int yearTotal = pvLogMapper.selectYearTotal(DateUtils.format(DateUtils.getFirstOfYear(year)),
- DateUtils.format(DateUtils.getLastOfYear(year)));
- //统计每月各个系统的搜索访问量
- List<LablesVo> lablesVoList = new ArrayList<>();
- for (int i = 1; i <= 12; i++) {
- LablesVo lablesVo = new LablesVo();
- List<Map<String, Object>> values = new ArrayList<>();
- lablesVo.setName(i + "月");
- //查询出全部的业务系统名称
- List<String> platformList = PlatformConst.platformList();
- //获取每月的第一天和最后一天
- String firstOfMonth = DateUtils.getFirstOfMonth(year, i, 15);
- String lastOfMonth = DateUtils.getLastOfMonth(year, i, 15);
- Integer sumByMonth = pvLogMapper.selectSearchVisitHistogramByMonth(firstOfMonth, lastOfMonth, null);
- //根据月份查询各个业务系统访问量
- platformList.forEach(e -> {
- Map<String, Object> map = new HashMap<>();
- Integer count = pvLogMapper.selectSearchVisitHistogramByMonth(firstOfMonth, lastOfMonth, e);
- map.put("legend", e);
- map.put("value", count);
- if (sumByMonth == 0) {
- map.put("percentage", 0.0000);
- } else {
- double sum = count / (sumByMonth.doubleValue());
- BigDecimal bd = new BigDecimal(sum);
- double f1 = bd.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
- map.put("percentage", f1);
- }
- values.add(map);
- });
- lablesVo.setValues(values);
- lablesVoList.add(lablesVo);
- }
- //返回值
- searchVisitHistogramVo.setTotal(yearTotal);
- searchVisitHistogramVo.setLables(lablesVoList);
- return searchVisitHistogramVo;
- }
- /**
- * 知识类型访问量分布
- *
- * @return
- */
- public Map<String, Object> knowledgeTypeVisitProportion(String type) {
- Map<String, Object> map = new HashMap<>();
- //获取当前年
- Calendar calendar = Calendar.getInstance();
- int year = calendar.get(Calendar.YEAR);
- //查询出结果
- List<String> collection = new ArrayList<>();
- if (type.equals("total")) {
- collection = analysisCommonMapper.knowledgeTypeVisitProportion(null, null);
- } else if (type.equals("year")) {
- collection = analysisCommonMapper.knowledgeTypeVisitProportion(DateUtils.format(DateUtils.getFirstOfYear(year)),
- DateUtils.format(DateUtils.getLastOfYear(year)));
- } else if (type.equals("month")) {
- int month = calendar.get(Calendar.MONTH);
- String firstOfMonth = DateUtils.getFirstOfMonth(year, month + 1, 15);
- String lastOfMonth = DateUtils.getLastOfMonth(year, month + 1, 15);
- collection = analysisCommonMapper.knowledgeTypeVisitProportion(firstOfMonth, lastOfMonth);
- } else {
- return null;
- }
- //json解析
- List<Integer> count = collection.parallelStream().map(e -> {
- KnowledgeTypeParse knowledgeTypeParse = JSON.parseObject(e, KnowledgeTypeParse.class);
- return knowledgeTypeParse.getType();
- }).collect(Collectors.toList());
- //文档知识和维基知识查看数量
- int archive = (int) count.stream().filter(e -> e == 1).count();
- int wiki = (int) count.stream().filter(e -> e == 2).count();
- map.put("archive", archive);
- map.put("wiki", wiki);
- //计算百分比
- if (count.size() == 0) {
- map.put("archivePercentage", 0.00);
- map.put("wikiPercentage", 0.00);
- } else {
- BigDecimal archivebg = new BigDecimal(archive / (double) count.size());
- double archivedouble = archivebg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
- map.put("archivePercentage", archivedouble);
- BigDecimal wikibg = new BigDecimal(wiki / (double) count.size());
- double wikidouble = wikibg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
- map.put("wikiPercentage", wikidouble);
- }
- return map;
- }
- /**
- * 各一级分类知识创建数量(包含审核中和驳回的数据)
- *
- * @param type
- * @param sort
- * @return
- */
- public List<KnowledgeCategoryVo> level1KnowledgeHistogram(String type, String sort) {
- //查询全部一级分类
- List<String> level1KnowledgeIdList = analysisCommonMapper.level1Knowledge();
- List<KnowledgeCategoryVo> result = level1KnowledgeIdList.parallelStream().map(e -> {
- //一级分类下知识数量(包含审核中的、驳回的)
- List<Integer> count = new ArrayList<>();
- //获取当前年
- Calendar calendar = Calendar.getInstance();
- int year = calendar.get(Calendar.YEAR);
- KnowledgeCategoryVo knowledgeCategoryVo = new KnowledgeCategoryVo();
- knowledgeCategoryVo.setLabel(analysisCommonMapper.selectCategoryName(e));
- //查询知识分类下创建的知识数量(纬度:年 最近1个月 全部)
- if (type.equals("total")) {
- count = analysisCommonMapper.level1KnowledgeHistogram(e, null, null);
- } else if (type.equals("year")) {
- count = analysisCommonMapper.level1KnowledgeHistogram(e, DateUtils.format(DateUtils.getFirstOfYear(year)),
- DateUtils.format(DateUtils.getLastOfYear(year)));
- } else if (type.equals("month")) {
- int month = calendar.get(Calendar.MONTH);
- String firstOfMonth = DateUtils.getFirstOfMonth(year, month + 1, 15);
- String lastOfMonth = DateUtils.getLastOfMonth(year, month + 1, 15);
- count = analysisCommonMapper.level1KnowledgeHistogram(e, firstOfMonth, lastOfMonth);
- }
- long archive = count.stream().filter(typeKnowledge -> typeKnowledge == 1).count();
- long wiki = count.stream().filter(typeKnowledge -> typeKnowledge == 2).count();
- knowledgeCategoryVo.setArchive((int) archive);
- knowledgeCategoryVo.setWiki((int) wiki);
- return knowledgeCategoryVo;
- }).collect(Collectors.toList());
- //排序
- if (StringUtils.isEmpty(sort)) {
- result = result.stream().sorted((t1, t2) -> t2.getWiki().compareTo(t1.getWiki())).collect(Collectors.toList());
- } else {
- if ("wikiDesc".equals(sort)) {
- result = result.stream().sorted((t1, t2) -> t2.getWiki().compareTo(t1.getWiki())).collect(Collectors.toList());
- } else {
- result = result.stream().sorted((t1, t2) -> t2.getArchive().compareTo(t1.getArchive())).collect(Collectors.toList());
- }
- }
- return result;
- }
- /**
- * 组织创建知识数量
- * @param queryData
- * @return
- */
- public IPage organizationKnowledges(QueryData queryData) {
- //获取当前年
- Calendar calendar = Calendar.getInstance();
- int year = calendar.get(Calendar.YEAR);
- List<KnowledgeDao> count = new ArrayList<>();
- if (("total").equals(queryData.getParams().get("type"))) {
- count = analysisCommonMapper.organizationKnowledges(null, null);
- } else if ("year".equals(queryData.getParams().get("type"))) {
- count = analysisCommonMapper.organizationKnowledges(DateUtils.format(DateUtils.getFirstOfYear(year)),
- DateUtils.format(DateUtils.getLastOfYear(year)));
- } else if ("month".equals(queryData.getParams().get("type"))) {
- int month = calendar.get(Calendar.MONTH);
- String firstOfMonth = DateUtils.getFirstOfMonth(year, month + 1, 15);
- String lastOfMonth = DateUtils.getLastOfMonth(year, month + 1, 15);
- count = analysisCommonMapper.organizationKnowledges(firstOfMonth, lastOfMonth);
- }
- List<KnowledgeCompanyVo> result = count.parallelStream().map(e -> {
- KnowledgeCompanyVo knowledgeCompanyVo = new KnowledgeCompanyVo();
- knowledgeCompanyVo.setCompany(e.getCompanyId());
- e.getCounts().forEach(c -> {
- if (c.containsKey("type") && c.get("type").intValue() == 1) {
- knowledgeCompanyVo.setArchive(c.get("count").intValue());
- }
- if (c.containsKey("type") && c.get("type").intValue() == 2) {
- knowledgeCompanyVo.setWiki(c.get("count").intValue());
- }
- });
- if (knowledgeCompanyVo.getArchive() == null){
- knowledgeCompanyVo.setArchive(0);
- }
- if (knowledgeCompanyVo.getWiki() == null){
- knowledgeCompanyVo.setWiki(0);
- }
- knowledgeCompanyVo.setTotal(knowledgeCompanyVo.getArchive() + knowledgeCompanyVo.getWiki());
- return knowledgeCompanyVo;
- }).collect(Collectors.toList());
- //排序
- String sort = queryData.getParams().get("sort");
- if (sort != null) {
- switch (sort) {
- case "totalDesc":
- result = result.stream().sorted((t1, t2) -> t2.getTotal().compareTo(t1.getTotal())).collect(Collectors.toList());
- break;
- case "totalAsc":
- result = result.stream().sorted(Comparator.comparing(KnowledgeCompanyVo::getTotal)).collect(Collectors.toList());
- break;
- case "archiveDesc":
- result = result.stream().sorted((t1, t2) -> t2.getArchive().compareTo(t1.getArchive())).collect(Collectors.toList());
- break;
- case "archiveAsc":
- result = result.stream().sorted(Comparator.comparing(KnowledgeCompanyVo::getArchive)).collect(Collectors.toList());
- break;
- case "wikiDesc":
- result = result.stream().sorted((t1, t2) -> t2.getWiki().compareTo(t1.getWiki())).collect(Collectors.toList());
- break;
- case "wikiAsc":
- result = result.stream().sorted(Comparator.comparing(KnowledgeCompanyVo::getWiki)).collect(Collectors.toList());
- break;
- }
- } else {
- result = result.stream().sorted((t1, t2) -> t2.getTotal().compareTo(t1.getTotal())).collect(Collectors.toList());
- }
- //获取top10公司数据
- String tops = queryData.getParams().get("tops");
- List<KnowledgeCompanyVo> resultTop = new ArrayList<>();
- if (StringUtils.isNotEmpty(tops)){
- resultTop = result.stream().limit(Long.parseLong(tops)).collect(Collectors.toList());
- } else {
- resultTop = result.stream().limit(10).collect(Collectors.toList());
- }
- return PageListUtils.getPages(queryData.getPageNo(),queryData.getPageSize(),resultTop);
- }
- }
|