AnalysisAlbumServiceImpl.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. package com.redxun.knowledge.analysis.service;
  2. import com.baomidou.mybatisplus.core.metadata.IPage;
  3. import com.redxun.common.base.entity.QueryData;
  4. import com.redxun.common.tool.StringUtils;
  5. import com.redxun.knowledge.album.mapper.AlbumCategoryMapper;
  6. import com.redxun.knowledge.analysis.entity.vo.*;
  7. import com.redxun.knowledge.analysis.mapper.AnalysisAlbumMapper;
  8. import com.redxun.knowledge.analysis.mapper.PvLogMapper;
  9. import com.redxun.knowledge.common.UserService;
  10. import com.redxun.knowledge.utils.DateUtils;
  11. import com.redxun.knowledge.utils.PageListUtils;
  12. import lombok.extern.slf4j.Slf4j;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.stereotype.Service;
  15. import java.math.BigDecimal;
  16. import java.util.*;
  17. import java.util.stream.Collectors;
  18. /**
  19. * 文件名: AnalysisAlbumServiceImpl
  20. * 作者: zizg
  21. * 时间: 2023/3/21
  22. * 描述:
  23. * 修改人:
  24. * 修改时间:
  25. * 修改内容:
  26. */
  27. @Service
  28. @Slf4j
  29. public class AnalysisAlbumServiceImpl {
  30. @Autowired
  31. private PvLogMapper pvLogMapper;
  32. @Autowired
  33. private AnalysisAlbumMapper analysisAlbumMapper;
  34. @Autowired
  35. private UserService userService;
  36. /**
  37. * 专辑创建总量
  38. *
  39. * @param type
  40. * @return
  41. */
  42. public CreateCountTotal albumAmount(String type) {
  43. CreateCountTotal createCountTotal = new CreateCountTotal();
  44. List<CreateCountLabel> createCountLabel = new ArrayList<>();
  45. Calendar calendar = Calendar.getInstance();
  46. int year = calendar.get(Calendar.YEAR);
  47. if (("total").equals(type)) {
  48. createCountLabel = analysisAlbumMapper.albumAmount(null, null,"total");
  49. } else if ("year".equals(type)) {
  50. createCountLabel = analysisAlbumMapper.albumAmount(DateUtils.format(DateUtils.getFirstOfYear(year)),
  51. DateUtils.format(DateUtils.getLastOfYear(year)),"year");
  52. } else if ("month".equals(type)) {
  53. int month = calendar.get(Calendar.MONTH);
  54. String firstOfMonth = DateUtils.getFirstOfMonth(year, month + 1, 15);
  55. String lastOfMonth = DateUtils.getLastOfMonth(year, month + 1, 15);
  56. createCountLabel = analysisAlbumMapper.albumAmount(firstOfMonth, lastOfMonth,"month");
  57. }
  58. int sum = createCountLabel.stream().mapToInt(CreateCountLabel::getValue).sum();
  59. createCountTotal.setTotal(sum);
  60. createCountTotal.setLables(createCountLabel);
  61. return createCountTotal;
  62. }
  63. /**
  64. * 专辑详情Top访问量(pv)统计
  65. *
  66. * @param type
  67. * @return
  68. */
  69. public List<AlbumDetailVisitsVo> albumDetailVisits(String type, Integer tops) {
  70. List<AlbumDetailVisitsVo> result = new ArrayList<>();
  71. Calendar calendar = Calendar.getInstance();
  72. int year = calendar.get(Calendar.YEAR);
  73. if (("total").equals(type)) {
  74. result = pvLogMapper.albumDetailVisits(null, null);
  75. } else if ("year".equals(type)) {
  76. result = pvLogMapper.albumDetailVisits(DateUtils.format(DateUtils.getFirstOfYear(year)),
  77. DateUtils.format(DateUtils.getLastOfYear(year)));
  78. } else if ("month".equals(type)) {
  79. int month = calendar.get(Calendar.MONTH);
  80. String firstOfMonth = DateUtils.getFirstOfMonth(year, month + 1, 15);
  81. String lastOfMonth = DateUtils.getLastOfMonth(year, month + 1, 15);
  82. result = pvLogMapper.albumDetailVisits(firstOfMonth, lastOfMonth);
  83. }
  84. //取tops10
  85. result = result.stream().limit(tops).collect(Collectors.toList());
  86. int countSum = result.stream().mapToInt(AlbumDetailVisitsVo::getValue).sum();
  87. //计算百分比
  88. if (countSum != 0) {
  89. result.forEach(e -> {
  90. BigDecimal countbg = BigDecimal.valueOf(e.getValue() / (double) countSum);
  91. e.setPercentage(countbg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
  92. });
  93. }
  94. return result;
  95. }
  96. /**
  97. * 各一级分类专辑创建数量
  98. *
  99. * @param type
  100. * @param sort
  101. * @return
  102. */
  103. public List<AlbumCategoryVo> level1AlbumHistogram(String type, String sort) {
  104. Calendar calendar = Calendar.getInstance();
  105. int year = calendar.get(Calendar.YEAR);
  106. //获取全部一级分类id
  107. List<String> albumCategoryIdList = analysisAlbumMapper.selectCategory();
  108. List<AlbumCategoryVo> result = albumCategoryIdList.stream().map(categoryId -> {
  109. Integer albumIdList = 0;
  110. Integer knowledgeList = 0;
  111. AlbumCategoryVo albumCategoryVo = new AlbumCategoryVo();
  112. if (("total").equals(type)) {
  113. //根据分类Id查询其下所有专辑Id数量
  114. albumIdList = analysisAlbumMapper.selectAlbumId(categoryId, null, null);
  115. knowledgeList = analysisAlbumMapper.selectKnowledge(categoryId, null, null);
  116. } else if (("year").equals(type)) {
  117. //根据分类Id查询其下所有专辑Id(按年统计)
  118. albumIdList = analysisAlbumMapper.selectAlbumId(categoryId, DateUtils.format(DateUtils.getFirstOfYear(year)),
  119. DateUtils.format(DateUtils.getLastOfYear(year)));
  120. knowledgeList = analysisAlbumMapper.selectKnowledge(categoryId, DateUtils.format(DateUtils.getFirstOfYear(year)),
  121. DateUtils.format(DateUtils.getLastOfYear(year)));
  122. } else if (("month").equals(type)) {
  123. //根据分类Id查询其下所有专辑Id(按月统计)
  124. int month = calendar.get(Calendar.MONTH);
  125. String firstOfMonth = DateUtils.getFirstOfMonth(year, month + 1, 15);
  126. String lastOfMonth = DateUtils.getLastOfMonth(year, month + 1, 15);
  127. albumIdList = analysisAlbumMapper.selectAlbumId(categoryId, firstOfMonth, lastOfMonth);
  128. knowledgeList = analysisAlbumMapper.selectKnowledge(categoryId, firstOfMonth, lastOfMonth);
  129. }
  130. albumCategoryVo.setLabel(analysisAlbumMapper.selectCategoryName(categoryId));
  131. albumCategoryVo.setAlbum(albumIdList);
  132. albumCategoryVo.setKnowledge(knowledgeList);
  133. return albumCategoryVo;
  134. }).collect(Collectors.toList());
  135. //排序
  136. if (StringUtils.isEmpty(sort)) {
  137. result = result.stream().
  138. sorted(Comparator.comparingInt(AlbumCategoryVo::getKnowledge).thenComparingInt(AlbumCategoryVo::getAlbum).reversed()).
  139. collect(Collectors.toList());
  140. } else {
  141. if ("albumDesc".equals(sort)) {
  142. result = result.stream().sorted((t1, t2) -> t2.getAlbum().compareTo(t1.getAlbum())).collect(Collectors.toList());
  143. } else {
  144. result = result.stream().sorted((t1, t2) -> t2.getKnowledge().compareTo(t1.getKnowledge())).collect(Collectors.toList());
  145. }
  146. }
  147. return result;
  148. }
  149. /**
  150. * 组织创建和访问专辑数量
  151. * @param queryData
  152. * @return
  153. */
  154. public IPage organizationAlbums(QueryData queryData) {
  155. //创建过专辑的公司Id集合
  156. Calendar calendar = Calendar.getInstance();
  157. int year = calendar.get(Calendar.YEAR);
  158. List<AlbumCompanyVo> result = new ArrayList<>();
  159. if ("total".equals(queryData.getParams().get("type"))) {
  160. result = analysisAlbumMapper.organizationAlbums(null, null);
  161. } else if ("year".equals(queryData.getParams().get("type"))) {
  162. result = analysisAlbumMapper.organizationAlbums(DateUtils.format(DateUtils.getFirstOfYear(year)),
  163. DateUtils.format(DateUtils.getLastOfYear(year)));
  164. } else if ("month".equals(queryData.getParams().get("type"))) {
  165. int month = calendar.get(Calendar.MONTH);
  166. String firstOfMonth = DateUtils.getFirstOfMonth(year, month + 1, 15);
  167. String lastOfMonth = DateUtils.getLastOfMonth(year, month + 1, 15);
  168. result = analysisAlbumMapper.organizationAlbums(firstOfMonth, lastOfMonth);
  169. }
  170. //排序
  171. String sort = queryData.getSortField();
  172. if (sort != null) {
  173. sort = sort + queryData.getSortOrder();
  174. switch (sort) {
  175. case "albumdesc":
  176. result = result.stream().sorted((t1, t2) -> t2.getAlbum().compareTo(t1.getAlbum())).collect(Collectors.toList());
  177. break;
  178. case "albumasc":
  179. result = result.stream().sorted(Comparator.comparing(AlbumCompanyVo::getAlbum)).collect(Collectors.toList());
  180. break;
  181. case "pvdesc":
  182. result = result.stream().sorted((t1, t2) -> t2.getPv().compareTo(t1.getPv())).collect(Collectors.toList());
  183. break;
  184. case "pvasc":
  185. result = result.stream().sorted(Comparator.comparing(AlbumCompanyVo::getPv)).collect(Collectors.toList());
  186. break;
  187. }
  188. }
  189. //取tops10
  190. String tops = queryData.getParams().get("tops");
  191. if(StringUtils.isNotEmpty(tops)){
  192. result = result.stream().limit(Long.parseLong(tops)).collect(Collectors.toList());
  193. } else {
  194. result = result.stream().limit(10).collect(Collectors.toList());
  195. }
  196. result.forEach(e -> e.setOrganization(userService.findDeptByDeptId(e.getOrganization()).getName()));
  197. return PageListUtils.getPages(queryData.getPageNo(),queryData.getPageSize(),result);
  198. }
  199. /**
  200. * 组织创建和访问专辑数量(新)
  201. * @param type
  202. * @param organizationId
  203. * @return
  204. */
  205. public AlbumCompanyVo organizationAlbum(String type, String organizationId) {
  206. Calendar calendar = Calendar.getInstance();
  207. int year = calendar.get(Calendar.YEAR);
  208. AlbumCompanyVo result = new AlbumCompanyVo();
  209. if ("total".equals(type)) {
  210. result = analysisAlbumMapper.organizationAlbum(null, null,organizationId);
  211. } else if ("year".equals(type)) {
  212. result = analysisAlbumMapper.organizationAlbum(DateUtils.format(DateUtils.getFirstOfYear(year)),
  213. DateUtils.format(DateUtils.getLastOfYear(year)),organizationId);
  214. } else if ("month".equals(type)) {
  215. int month = calendar.get(Calendar.MONTH);
  216. String firstOfMonth = DateUtils.getFirstOfMonth(year, month + 1, 15);
  217. String lastOfMonth = DateUtils.getLastOfMonth(year, month + 1, 15);
  218. result = analysisAlbumMapper.organizationAlbum(firstOfMonth, lastOfMonth,organizationId);
  219. }
  220. String deptPath = pvLogMapper.findAllDeptIdByDeptId(organizationId);
  221. deptPath = deptPath.substring(deptPath.indexOf(".") + 1);
  222. String[] split = deptPath.split("\\.");
  223. if (split.length > 6){
  224. String first = Arrays.stream(split).limit(3).map(e -> userService.findDeptByDeptId(e).getName()).collect(Collectors.joining(">"));
  225. String end = Arrays.stream(split).skip(split.length - 3).map(e -> userService.findDeptByDeptId(e).getName()).collect(Collectors.joining(">"));
  226. result.setOrganization(first + ">...>" + end);
  227. } else {
  228. String organization = Arrays.stream(split).map(e -> userService.findDeptByDeptId(e).getName()).collect(Collectors.joining(">"));
  229. result.setOrganization(organization);
  230. }
  231. return result;
  232. }
  233. }