From 9e6474a123fd6fec6be055002ca309abe2a012f2 Mon Sep 17 00:00:00 2001 From: scholar <1145227973@qq.com> Date: Fri, 19 Apr 2024 17:09:12 +0800 Subject: [PATCH] =?UTF-8?q?1=EF=BC=8C=E4=BC=98=E5=8C=96mapper.xml=E7=9A=84?= =?UTF-8?q?sql=E8=AF=AD=E5=8F=A5=EF=BC=9B=202=EF=BC=8C=E5=AF=B9=E4=BA=8E?= =?UTF-8?q?=E6=97=A0=E9=94=80=E5=94=AE=E6=95=B0=E6=8D=AE=E7=9A=84=E6=9C=88?= =?UTF-8?q?=E4=BB=BD=EF=BC=8C=E6=8F=92=E5=85=A5=E9=94=80=E5=94=AE=E7=BB=93?= =?UTF-8?q?=E6=9E=9C=E4=B8=BA0=E7=9A=84=E6=95=B0=E6=8D=AE=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CrmStatisticsPerformanceServiceImpl.java | 81 ++++++++++-- .../CrmStatisticsPerformanceMapper.xml | 116 ++---------------- 2 files changed, 83 insertions(+), 114 deletions(-) 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 067be4f43..c93951f81 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; @@ -56,24 +58,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 @@ + -