package com.redxun.knowledge.analysis.service; import com.redxun.knowledge.analysis.entity.vo.*; import com.redxun.knowledge.analysis.mapper.AnalysisMapMapper; import com.redxun.knowledge.common.UserService; import com.redxun.knowledge.entity.vo.DicVo; import com.redxun.knowledge.mapper.CommonMapper; import com.redxun.knowledge.utils.DateUtils; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.Calendar; import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; /** * 文件名: AnalysisMapServiceImpl * 作者: zizg * 时间: 2023/3/22 * 描述: * 修改人: * 修改时间: * 修改内容: */ @Slf4j @Service public class AnalysisMapServiceImpl { @Autowired private AnalysisMapMapper analysisMapMapper; @Autowired private UserService userService; /** * 地图创建总量 * @param type * @return */ public CreateCountTotal mapAmount(String type) { CreateCountTotal createCountTotal = new CreateCountTotal(); List createCountLabel = new ArrayList<>(); Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); if (("total").equals(type)) { createCountLabel = analysisMapMapper.mapAmount(null, null); } else if ("year".equals(type)) { createCountLabel = analysisMapMapper.mapAmount(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); createCountLabel = analysisMapMapper.mapAmount(firstOfMonth, lastOfMonth); } int sum = createCountLabel.stream().mapToInt(e -> Integer.parseInt(e.getValue())).sum(); createCountTotal.setTotal(sum); createCountTotal.setLables(createCountLabel); return createCountTotal; } /** * 各类型地图创建数量 * @param type * @return */ public List typeMapPie(String type) { List result = new ArrayList<>(); Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); if (("total").equals(type)){ result = analysisMapMapper.typeMapPie(null,null); } else if (("year").equals(type)){ result = analysisMapMapper.typeMapPie(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 = analysisMapMapper.typeMapPie(firstOfMonth,lastOfMonth); } //类型名称赋值 result.forEach(e -> { String name = analysisMapMapper.selectTypeName(e.getType()); e.setType(name); }); return result; } /** * 地图页面访问量 * @param type * @return */ public List mapPageHistogram(String type) { List result = new ArrayList<>(); Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); if (("total").equals(type)){ result = analysisMapMapper.mapPageHistogram(null,null); } else if (("year").equals(type)){ result = analysisMapMapper.mapPageHistogram(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 = analysisMapMapper.mapPageHistogram(firstOfMonth,lastOfMonth); } return result; } /** * 组织创建和访问地图数量 * @param type * @param tops * @param sort * @return */ public List organizationMaps(String type, Integer tops, String sort) { //创建过专辑的公司Id集合 Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); List result = new ArrayList<>(); if ("total".equals(type)) { result = analysisMapMapper.organizationMaps(null, null); } else if ("year".equals(type)) { result = analysisMapMapper.organizationMaps(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 = analysisMapMapper.organizationMaps(firstOfMonth, lastOfMonth); } //排序 if (sort != null) { switch (sort) { case "mapDesc": result = result.stream().sorted((t1, t2) -> t2.getMap().compareTo(t1.getMap())).collect(Collectors.toList()); break; case "mapAsc": result = result.stream().sorted(Comparator.comparing(MapCompanyVo::getMap)).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(MapCompanyVo::getPv)).collect(Collectors.toList()); break; } } //取tops10 result = result.stream().limit(tops).collect(Collectors.toList()); result.forEach(e -> e.setCompany(userService.findDeptByDeptId(e.getCompany()).getName())); return result; } }