123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- package com.redxun.knowledge.analysis.service;
- import com.baomidou.mybatisplus.core.metadata.IPage;
- import com.redxun.common.base.entity.QueryData;
- import com.redxun.common.tool.StringUtils;
- import com.redxun.knowledge.album.mapper.AlbumCategoryMapper;
- import com.redxun.knowledge.analysis.entity.vo.*;
- import com.redxun.knowledge.analysis.mapper.AnalysisAlbumMapper;
- import com.redxun.knowledge.analysis.mapper.PvLogMapper;
- import com.redxun.knowledge.common.UserService;
- 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;
- /**
- * 文件名: AnalysisAlbumServiceImpl
- * 作者: zizg
- * 时间: 2023/3/21
- * 描述:
- * 修改人:
- * 修改时间:
- * 修改内容:
- */
- @Service
- @Slf4j
- public class AnalysisAlbumServiceImpl {
- @Autowired
- private PvLogMapper pvLogMapper;
- @Autowired
- private AnalysisAlbumMapper analysisAlbumMapper;
- @Autowired
- private UserService userService;
- /**
- * 专辑创建总量
- *
- * @param type
- * @return
- */
- public CreateCountTotal albumAmount(String type) {
- CreateCountTotal createCountTotal = new CreateCountTotal();
- List<CreateCountLabel> createCountLabel = new ArrayList<>();
- Calendar calendar = Calendar.getInstance();
- int year = calendar.get(Calendar.YEAR);
- if (("total").equals(type)) {
- createCountLabel = analysisAlbumMapper.albumAmount(null, null,"total");
- } else if ("year".equals(type)) {
- createCountLabel = analysisAlbumMapper.albumAmount(DateUtils.format(DateUtils.getFirstOfYear(year)),
- DateUtils.format(DateUtils.getLastOfYear(year)),"year");
- } else if ("month".equals(type)) {
- int month = calendar.get(Calendar.MONTH);
- String firstOfMonth = DateUtils.getFirstOfMonth(year, month + 1, 15);
- String lastOfMonth = DateUtils.getLastOfMonth(year, month + 1, 15);
- createCountLabel = analysisAlbumMapper.albumAmount(firstOfMonth, lastOfMonth,"month");
- }
- int sum = createCountLabel.stream().mapToInt(CreateCountLabel::getValue).sum();
- createCountTotal.setTotal(sum);
- createCountTotal.setLables(createCountLabel);
- return createCountTotal;
- }
- /**
- * 专辑详情Top访问量(pv)统计
- *
- * @param type
- * @return
- */
- public List<AlbumDetailVisitsVo> albumDetailVisits(String type, Integer tops) {
- List<AlbumDetailVisitsVo> result = new ArrayList<>();
- Calendar calendar = Calendar.getInstance();
- int year = calendar.get(Calendar.YEAR);
- if (("total").equals(type)) {
- result = pvLogMapper.albumDetailVisits(null, null);
- } else if ("year".equals(type)) {
- result = pvLogMapper.albumDetailVisits(DateUtils.format(DateUtils.getFirstOfYear(year)),
- DateUtils.format(DateUtils.getLastOfYear(year)));
- } else if ("month".equals(type)) {
- int month = calendar.get(Calendar.MONTH);
- String firstOfMonth = DateUtils.getFirstOfMonth(year, month + 1, 15);
- String lastOfMonth = DateUtils.getLastOfMonth(year, month + 1, 15);
- result = pvLogMapper.albumDetailVisits(firstOfMonth, lastOfMonth);
- }
- //取tops10
- result = result.stream().limit(tops).collect(Collectors.toList());
- int countSum = result.stream().mapToInt(AlbumDetailVisitsVo::getValue).sum();
- //计算百分比
- if (countSum != 0) {
- result.forEach(e -> {
- BigDecimal countbg = BigDecimal.valueOf(e.getValue() / (double) countSum);
- e.setPercentage(countbg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
- });
- }
- return result;
- }
- /**
- * 各一级分类专辑创建数量
- *
- * @param type
- * @param sort
- * @return
- */
- public List<AlbumCategoryVo> level1AlbumHistogram(String type, String sort) {
- Calendar calendar = Calendar.getInstance();
- int year = calendar.get(Calendar.YEAR);
- //获取全部一级分类id
- List<String> albumCategoryIdList = analysisAlbumMapper.selectCategory();
- List<AlbumCategoryVo> result = albumCategoryIdList.stream().map(categoryId -> {
- Integer albumIdList = 0;
- Integer knowledgeList = 0;
- AlbumCategoryVo albumCategoryVo = new AlbumCategoryVo();
- if (("total").equals(type)) {
- //根据分类Id查询其下所有专辑Id数量
- albumIdList = analysisAlbumMapper.selectAlbumId(categoryId, null, null);
- knowledgeList = analysisAlbumMapper.selectKnowledge(categoryId, null, null);
- } else if (("year").equals(type)) {
- //根据分类Id查询其下所有专辑Id(按年统计)
- albumIdList = analysisAlbumMapper.selectAlbumId(categoryId, DateUtils.format(DateUtils.getFirstOfYear(year)),
- DateUtils.format(DateUtils.getLastOfYear(year)));
- knowledgeList = analysisAlbumMapper.selectKnowledge(categoryId, DateUtils.format(DateUtils.getFirstOfYear(year)),
- DateUtils.format(DateUtils.getLastOfYear(year)));
- } else if (("month").equals(type)) {
- //根据分类Id查询其下所有专辑Id(按月统计)
- int month = calendar.get(Calendar.MONTH);
- String firstOfMonth = DateUtils.getFirstOfMonth(year, month + 1, 15);
- String lastOfMonth = DateUtils.getLastOfMonth(year, month + 1, 15);
- albumIdList = analysisAlbumMapper.selectAlbumId(categoryId, firstOfMonth, lastOfMonth);
- knowledgeList = analysisAlbumMapper.selectKnowledge(categoryId, firstOfMonth, lastOfMonth);
- }
- albumCategoryVo.setLabel(analysisAlbumMapper.selectCategoryName(categoryId));
- albumCategoryVo.setAlbum(albumIdList);
- albumCategoryVo.setKnowledge(knowledgeList);
- return albumCategoryVo;
- }).collect(Collectors.toList());
- //排序
- if (StringUtils.isEmpty(sort)) {
- result = result.stream().
- sorted(Comparator.comparingInt(AlbumCategoryVo::getKnowledge).thenComparingInt(AlbumCategoryVo::getAlbum).reversed()).
- collect(Collectors.toList());
- } else {
- if ("albumDesc".equals(sort)) {
- result = result.stream().sorted((t1, t2) -> t2.getAlbum().compareTo(t1.getAlbum())).collect(Collectors.toList());
- } else {
- result = result.stream().sorted((t1, t2) -> t2.getKnowledge().compareTo(t1.getKnowledge())).collect(Collectors.toList());
- }
- }
- return result;
- }
- /**
- * 组织创建和访问专辑数量
- * @param queryData
- * @return
- */
- public IPage organizationAlbums(QueryData queryData) {
- //创建过专辑的公司Id集合
- Calendar calendar = Calendar.getInstance();
- int year = calendar.get(Calendar.YEAR);
- List<AlbumCompanyVo> result = new ArrayList<>();
- if ("total".equals(queryData.getParams().get("type"))) {
- result = analysisAlbumMapper.organizationAlbums(null, null);
- } else if ("year".equals(queryData.getParams().get("type"))) {
- result = analysisAlbumMapper.organizationAlbums(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);
- result = analysisAlbumMapper.organizationAlbums(firstOfMonth, lastOfMonth);
- }
- //排序
- String sort = queryData.getSortField();
- if (sort != null) {
- sort = sort + queryData.getSortOrder();
- switch (sort) {
- case "albumdesc":
- result = result.stream().sorted((t1, t2) -> t2.getAlbum().compareTo(t1.getAlbum())).collect(Collectors.toList());
- break;
- case "albumasc":
- result = result.stream().sorted(Comparator.comparing(AlbumCompanyVo::getAlbum)).collect(Collectors.toList());
- break;
- case "pvdesc":
- result = result.stream().sorted((t1, t2) -> t2.getPv().compareTo(t1.getPv())).collect(Collectors.toList());
- break;
- case "pvasc":
- result = result.stream().sorted(Comparator.comparing(AlbumCompanyVo::getPv)).collect(Collectors.toList());
- break;
- }
- }
- //取tops10
- String tops = queryData.getParams().get("tops");
- if(StringUtils.isNotEmpty(tops)){
- result = result.stream().limit(Long.parseLong(tops)).collect(Collectors.toList());
- } else {
- result = result.stream().limit(10).collect(Collectors.toList());
- }
- result.forEach(e -> e.setOrganization(userService.findDeptByDeptId(e.getOrganization()).getName()));
- return PageListUtils.getPages(queryData.getPageNo(),queryData.getPageSize(),result);
- }
- /**
- * 组织创建和访问专辑数量(新)
- * @param type
- * @param organizationId
- * @return
- */
- public AlbumCompanyVo organizationAlbum(String type, String organizationId) {
- Calendar calendar = Calendar.getInstance();
- int year = calendar.get(Calendar.YEAR);
- AlbumCompanyVo result = new AlbumCompanyVo();
- if ("total".equals(type)) {
- result = analysisAlbumMapper.organizationAlbum(null, null,organizationId);
- } else if ("year".equals(type)) {
- result = analysisAlbumMapper.organizationAlbum(DateUtils.format(DateUtils.getFirstOfYear(year)),
- DateUtils.format(DateUtils.getLastOfYear(year)),organizationId);
- } else if ("month".equals(type)) {
- int month = calendar.get(Calendar.MONTH);
- String firstOfMonth = DateUtils.getFirstOfMonth(year, month + 1, 15);
- String lastOfMonth = DateUtils.getLastOfMonth(year, month + 1, 15);
- result = analysisAlbumMapper.organizationAlbum(firstOfMonth, lastOfMonth,organizationId);
- }
- String deptPath = pvLogMapper.findAllDeptIdByDeptId(organizationId);
- deptPath = deptPath.substring(deptPath.indexOf(".") + 1);
- String[] split = deptPath.split("\\.");
- if (split.length > 6){
- String first = Arrays.stream(split).limit(3).map(e -> userService.findDeptByDeptId(e).getName()).collect(Collectors.joining(">"));
- String end = Arrays.stream(split).skip(split.length - 3).map(e -> userService.findDeptByDeptId(e).getName()).collect(Collectors.joining(">"));
- result.setOrganization(first + ">...>" + end);
- } else {
- String organization = Arrays.stream(split).map(e -> userService.findDeptByDeptId(e).getName()).collect(Collectors.joining(">"));
- result.setOrganization(organization);
- }
- return result;
- }
- }
|