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.analysis.entity.consts.ComanyColumnsConst; import com.redxun.knowledge.analysis.entity.dto.PersonVisitDto; import com.redxun.knowledge.analysis.entity.enums.SystemDetailVisitsEnum; import com.redxun.knowledge.analysis.entity.vo.MapCompanyVo; import com.redxun.knowledge.analysis.entity.vo.OrganizationSearchVo; import com.redxun.knowledge.analysis.entity.vo.SystemCompanyVo; import com.redxun.knowledge.analysis.entity.vo.SystemDetailVisitsVo; import com.redxun.knowledge.analysis.mapper.AnalysisSearchMapper; 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 org.springframework.util.CollectionUtils; import java.math.BigDecimal; import java.util.*; import java.util.stream.Collectors; /** * 文件名: AnalysisSearchServiceImpl * 作者: zizg * 时间: 2023/3/23 * 描述: * 修改人: * 修改时间: * 修改内容: */ @Service @Slf4j public class AnalysisSearchServiceImpl { @Autowired private AnalysisSearchMapper analysisSearchMapper; @Autowired private PvLogMapper pvLogMapper; @Autowired private UserService userService; /** * 各业务系统分布 * * @param type * @return */ public List searchSystemDetailVisits(String type) { Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); List result = new ArrayList<>(); if (("total").equals(type)) { result = analysisSearchMapper.searchSystemDetailVisits(null, null); } else if (("year").equals(type)) { result = analysisSearchMapper.searchSystemDetailVisits(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 = analysisSearchMapper.searchSystemDetailVisits(firstOfMonth, lastOfMonth); } return result; } /** * 组织访问量 * * @param queryData * @return */ public Map organizationSearchs(QueryData queryData) { //获取排序规则 String sort = queryData.getSortField(); if (sort != null) { sort = sort + queryData.getSortOrder(); } Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); List result = new ArrayList<>(); if (("total").equals(queryData.getParams().get("type"))) { result = analysisSearchMapper.organizationSearchs(null, null, sort); } else if (("year").equals(queryData.getParams().get("type"))) { result = analysisSearchMapper.organizationSearchs(DateUtils.format(DateUtils.getFirstOfYear(year)), DateUtils.format(DateUtils.getLastOfYear(year)), sort); } 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 = analysisSearchMapper.organizationSearchs(firstOfMonth, lastOfMonth, sort); } 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.setCompany(userService.findDeptByDeptId(e.getCompany()).getName())); Map mapList = new LinkedHashMap<>(); mapList.put("columns", ComanyColumnsConst.companyColumnsList()); mapList.put("rows", result); return mapList; } /** * 组织访问量(新) * * @param type * @param organizationId * @return */ public OrganizationSearchVo organizationSearch(String type, String organizationId,String createTime,String endTime,List personId) { //存在人员,统计人员 if (!CollectionUtils.isEmpty(personId)){ organizationId = null; } //开始时间和结束时间都不为空,按照选中时间过滤,忽略type if (!StringUtils.isEmpty(createTime) && !StringUtils.isEmpty(endTime)){ type = null; } Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); OrganizationSearchVo organizationSearchVo = new OrganizationSearchVo(); List> item = new ArrayList<>(); if (("total").equals(type)) { item = analysisSearchMapper.organizationSearch(null, null, organizationId,personId); } else if (("year").equals(type)) { item = analysisSearchMapper.organizationSearch(DateUtils.format(DateUtils.getFirstOfYear(year)), DateUtils.format(DateUtils.getLastOfYear(year)), organizationId,personId); } 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); item = analysisSearchMapper.organizationSearch(firstOfMonth, lastOfMonth, organizationId,personId); } else { item = analysisSearchMapper.organizationSearch(createTime,endTime, organizationId,personId); } String deptPath = pvLogMapper.findAllDeptIdByDeptId(organizationId); if (CollectionUtils.isEmpty(personId)) { if (StringUtils.isNotEmpty(deptPath)) { 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(">")); organizationSearchVo.setOrganization(first + ">...>" + end); } else { String organization = Arrays.stream(split).map(e -> userService.findDeptByDeptId(e).getName()).collect(Collectors.joining(">")); organizationSearchVo.setOrganization(organization); } } } else { String fullName = personId.parallelStream().map(e -> userService.queryOsUserDto(e).getFullName()).collect(Collectors.joining(",")); organizationSearchVo.setOrganization(fullName); } Optional total = item.stream().map(e -> (BigDecimal) e.get("value")).reduce(BigDecimal::add); Map map = new HashMap<>(); map.put("name", "访问总量"); map.put("value", total.get()); item.add(0,map); organizationSearchVo.setItem(item); return organizationSearchVo; } /** * 人员访问量 * @param personVisitDto * @return */ public OrganizationSearchVo personSearch(PersonVisitDto personVisitDto) { //如果组织id不为空,人员id为空情况下,显示组织访问量 OrganizationSearchVo organizationSearchVo = this.organizationSearch(personVisitDto.getType(), personVisitDto.getOrganizationId(), personVisitDto.getCreateTime(), personVisitDto.getEndTime(), personVisitDto.getPersonId()); return organizationSearchVo; } }