diff --git a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/statistics/CrmStatisticsPerformanceServiceImpl.java b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/statistics/CrmStatisticsPerformanceServiceImpl.java index 450d691ff..4c4193f7b 100644 --- a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/statistics/CrmStatisticsPerformanceServiceImpl.java +++ b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/statistics/CrmStatisticsPerformanceServiceImpl.java @@ -1,6 +1,7 @@ package cn.iocoder.yudao.module.crm.service.statistics; import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.date.LocalDateTimeUtil; import cn.hutool.core.util.ObjUtil; import cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.performance.CrmStatisticsPerformanceReqVO; import cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.performance.CrmStatisticsPerformanceRespVO; @@ -13,8 +14,9 @@ import org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; import jakarta.annotation.Resource; -import java.util.Collections; -import java.util.List; + +import java.math.BigDecimal; +import java.util.*; import java.util.function.Function; import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; @@ -59,24 +61,87 @@ public class CrmStatisticsPerformanceServiceImpl implements CrmStatisticsPerform * 获得员工业绩数据 * * @param performanceReqVO 参数 - * @param performanceFunction 排行榜方法 - * @return 排行版数据 + * @param performanceFunction 员工业绩统计方法 + * @return 员工业绩数据 */ private List getPerformance(CrmStatisticsPerformanceReqVO performanceReqVO, Function> performanceFunction) { + List performanceRespVOList; + // 1. 获得用户编号数组 final List userIds = getUserIds(performanceReqVO); if (CollUtil.isEmpty(userIds)) { return Collections.emptyList(); } performanceReqVO.setUserIds(userIds); - // 2. 获得排行数据 + // 2. 获得业绩数据 List performance = performanceFunction.apply(performanceReqVO); - if (CollUtil.isEmpty(performance)) { - return Collections.emptyList(); + + // 获取查询的年份 + String currentYear = LocalDateTimeUtil.format(performanceReqVO.getTimes()[0],"yyyy"); + + // 构造查询当年和前一年,每年12个月的年月组合 + List allMonths = new ArrayList<>(); + for (int year = Integer.parseInt(currentYear)-1; year <= Integer.parseInt(currentYear); year++) { + for (int month = 1; month <= 12; month++) { + allMonths.add(String.format("%d%02d", year, month)); + } } - return performance; + + List computedList = new ArrayList<>(); + List respVOList = new ArrayList<>(); + + // 生成computedList基础数据 + // 构造完整的2*12个月的数据,如果某月数据缺失,需要补上0,一年12个月不能有缺失 + for (String month : allMonths) { + CrmStatisticsPerformanceRespVO foundData = performance.stream() + .filter(data -> data.getTime().equals(month)) + .findFirst() + .orElse(null); + + if (foundData != null) { + computedList.add(foundData); + } else { + CrmStatisticsPerformanceRespVO missingData = new CrmStatisticsPerformanceRespVO(); + missingData.setTime(month); + missingData.setCurrentMonthCount(BigDecimal.ZERO); + missingData.setLastMonthCount(BigDecimal.ZERO); + missingData.setLastYearCount(BigDecimal.ZERO); + computedList.add(missingData); + } + } + //根据查询年份和前一年的数据,计算查询年份的同比环比数据 + for (CrmStatisticsPerformanceRespVO currentData : computedList) { + String currentMonth = currentData.getTime(); + + // 根据当年和前一年的月销售数据,计算currentYear的完整数据 + if (currentMonth.startsWith(currentYear)) { + // 计算 LastMonthCount + int currentIndex = computedList.indexOf(currentData); + if (currentIndex > 0) { + CrmStatisticsPerformanceRespVO lastMonthData = computedList.get(currentIndex - 1); + currentData.setLastMonthCount(lastMonthData.getCurrentMonthCount()); + } else { + currentData.setLastMonthCount(BigDecimal.ZERO); // 第一个月的 LastMonthCount 设为0 + } + + // 计算 LastYearCount + String lastYearMonth = String.valueOf(Integer.parseInt(currentMonth) - 100); + CrmStatisticsPerformanceRespVO lastYearData = computedList.stream() + .filter(data -> data.getTime().equals(lastYearMonth)) + .findFirst() + .orElse(null); + + if (lastYearData != null) { + currentData.setLastYearCount(lastYearData.getCurrentMonthCount()); + } else { + currentData.setLastYearCount(BigDecimal.ZERO); // 如果去年同月数据不存在,设为0 + } + respVOList.add(currentData);//给前端只需要返回查询当年的数据,不需要前一年数据 + } + } + return respVOList; } /** diff --git a/yudao-module-crm/yudao-module-crm-biz/src/main/resources/mapper/statistics/CrmStatisticsPerformanceMapper.xml b/yudao-module-crm/yudao-module-crm-biz/src/main/resources/mapper/statistics/CrmStatisticsPerformanceMapper.xml index 10b952bac..ee9ec4e44 100644 --- a/yudao-module-crm/yudao-module-crm-biz/src/main/resources/mapper/statistics/CrmStatisticsPerformanceMapper.xml +++ b/yudao-module-crm/yudao-module-crm-biz/src/main/resources/mapper/statistics/CrmStatisticsPerformanceMapper.xml @@ -5,27 +5,8 @@ + -