【优化】CRM:员工业绩统计的实现

This commit is contained in:
YunaiV 2024-05-07 23:08:38 +08:00
parent d0d1d6d208
commit 52c86cd290
3 changed files with 38 additions and 77 deletions

View File

@ -16,12 +16,12 @@
<module>yudao-module-system</module> <module>yudao-module-system</module>
<module>yudao-module-infra</module> <module>yudao-module-infra</module>
<!-- <module>yudao-module-member</module>--> <!-- <module>yudao-module-member</module>-->
<!-- <module>yudao-module-bpm</module>--> <module>yudao-module-bpm</module>
<!-- <module>yudao-module-report</module>--> <!-- <module>yudao-module-report</module>-->
<!-- <module>yudao-module-mp</module>--> <!-- <module>yudao-module-mp</module>-->
<!-- <module>yudao-module-pay</module>--> <!-- <module>yudao-module-pay</module>-->
<!-- <module>yudao-module-mall</module>--> <!-- <module>yudao-module-mall</module>-->
<!-- <module>yudao-module-crm</module>--> <module>yudao-module-crm</module>
<!-- <module>yudao-module-erp</module>--> <!-- <module>yudao-module-erp</module>-->
</modules> </modules>

View File

@ -2,7 +2,7 @@ package cn.iocoder.yudao.module.crm.service.statistics;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil; import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.LocalDateTimeUtil; import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjUtil; 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.CrmStatisticsPerformanceReqVO;
import cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.performance.CrmStatisticsPerformanceRespVO; import cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.performance.CrmStatisticsPerformanceRespVO;
@ -11,16 +11,19 @@ import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO; import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi; import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO; import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import jakarta.annotation.Resource;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.*; import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function; import java.util.function.Function;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
/** /**
* CRM 员工业绩分析 Service 实现类 * CRM 员工业绩分析 Service 实现类
@ -56,9 +59,11 @@ public class CrmStatisticsPerformanceServiceImpl implements CrmStatisticsPerform
} }
/** /**
* 获得员工业绩数据并通过如下方法拿到对应的lastYearCountlastMonthCount * 获得员工业绩数据
* 比如说构造2024 年的CrmStatisticsPerformanceRespVO获得 2023-01 2024-12的月统计数据即可 *
* 可以数据 group by -2023-01 2024-12的然后聚合出 CrmStatisticsPerformanceRespVO * 1. 获得今年 + 去年的数据
* 2. 遍历今年的月份逐个拼接去年的月份数据
*
* @param performanceReqVO 参数 * @param performanceReqVO 参数
* @param performanceFunction 员工业绩统计方法 * @param performanceFunction 员工业绩统计方法
* @return 员工业绩数据 * @return 员工业绩数据
@ -67,7 +72,7 @@ public class CrmStatisticsPerformanceServiceImpl implements CrmStatisticsPerform
Function<CrmStatisticsPerformanceReqVO, List<CrmStatisticsPerformanceRespVO>> performanceFunction) { Function<CrmStatisticsPerformanceReqVO, List<CrmStatisticsPerformanceRespVO>> performanceFunction) {
// 1. 获得用户编号数组 // 1. 获得用户编号数组
final List<Long> userIds = getUserIds(performanceReqVO); List<Long> userIds = getUserIds(performanceReqVO);
if (CollUtil.isEmpty(userIds)) { if (CollUtil.isEmpty(userIds)) {
return Collections.emptyList(); return Collections.emptyList();
} }
@ -75,66 +80,22 @@ public class CrmStatisticsPerformanceServiceImpl implements CrmStatisticsPerform
// 2. 获得业绩数据 // 2. 获得业绩数据
List<CrmStatisticsPerformanceRespVO> performanceList = performanceFunction.apply(performanceReqVO); List<CrmStatisticsPerformanceRespVO> performanceList = performanceFunction.apply(performanceReqVO);
Map<String, BigDecimal> performanceMap = convertMap(performanceList, CrmStatisticsPerformanceRespVO::getTime,
CrmStatisticsPerformanceRespVO::getCurrentMonthCount);
// 获取查询的年份 // 3. 组装数据返回
String currentYear = LocalDateTimeUtil.format(performanceReqVO.getTimes()[0],"yyyy"); List<CrmStatisticsPerformanceRespVO> result = new ArrayList<>();
Map<Integer, CrmStatisticsPerformanceRespVO> currentYearMap = new TreeMap<>();//查询当年的map数据 int year = DateUtil.thisYear();
Map<Integer, CrmStatisticsPerformanceRespVO> lastYearMap = new TreeMap<>();//前一年的map数据
for (int month = 1; month <= 12; month++) { for (int month = 1; month <= 12; month++) {
//根据数据库的月销售数据查询结果构造查询当年的map数据 String currentMonth = String.format("%d%02d", year, month);
String currentYearKey = String.format("%d%02d", Integer.parseInt(currentYear), month); String lastMonth = month == 1 ? String.format("%d%02d", year - 1, 12) : String.format("%d%02d", year, month - 1);
buildYearMapData(performanceList, currentYearMap, currentYearKey); String lastYear = String.format("%d%02d", year - 1, month);
result.add(new CrmStatisticsPerformanceRespVO().setTime(currentMonth)
//根据数据库的月销售数据查询结果构造查询前一年的map数据 .setCurrentMonthCount(performanceMap.getOrDefault(currentMonth, BigDecimal.ZERO))
String lastYearKey = String.format("%d%02d", Integer.parseInt(currentYear)-1, month); .setLastMonthCount(performanceMap.getOrDefault(lastMonth, BigDecimal.ZERO))
buildYearMapData(performanceList, lastYearMap, lastYearKey); .setLastYearCount(performanceMap.getOrDefault(lastYear, BigDecimal.ZERO)));
}
//根据构造好的map数据计算查询当年的环比和同比数据并构造好返回的respVOList
List<CrmStatisticsPerformanceRespVO> respVOList = new ArrayList<>();
for (int key : currentYearMap.keySet()) {
BigDecimal lastYearCount = lastYearMap.get(key-100).getCurrentMonthCount();
BigDecimal lastMonthCount;
if (key % 100 > 1) {//2-12月份的前一个月数据
lastMonthCount = currentYearMap.get(key-1).getCurrentMonthCount();
} else {//1月份的前一个月数据
lastMonthCount = lastYearMap.get(key-89).getCurrentMonthCount();
}
currentYearMap.get(key).setLastYearCount(lastYearCount);
currentYearMap.get(key).setLastMonthCount(lastMonthCount);
respVOList.add(currentYearMap.get(key));
}
return respVOList;
}
/**
* 根据mapKey添加当年和前一年的月销售记录到对应的map结构中
* @param performanceList 数据库中查询到的月销售记录
* @param YearDataMap 将查询到的月销售记录put到对应的map中如果月销售记录为null置为0
* @param mapKey 对应的mapKey
*/
private void buildYearMapData(List<CrmStatisticsPerformanceRespVO> performanceList,
Map<Integer, CrmStatisticsPerformanceRespVO> YearDataMap,
String mapKey)
{
CrmStatisticsPerformanceRespVO currentYearData = performanceList.stream()
.filter(data -> data.getTime().equals(mapKey))
.findFirst()
.orElse(null);
if(currentYearData != null) {
YearDataMap.put(Integer.parseInt(mapKey), currentYearData);
} else {
CrmStatisticsPerformanceRespVO defaultVO = new CrmStatisticsPerformanceRespVO();
defaultVO.setTime(mapKey);
defaultVO.setCurrentMonthCount(BigDecimal.ZERO);
defaultVO.setLastMonthCount(BigDecimal.ZERO);
defaultVO.setLastYearCount(BigDecimal.ZERO);
YearDataMap.put(Integer.parseInt(mapKey), defaultVO);
} }
return result;
} }
/** /**

View File

@ -46,11 +46,11 @@
<!-- <version>${revision}</version>--> <!-- <version>${revision}</version>-->
<!-- </dependency>--> <!-- </dependency>-->
<!-- 工作流。默认注释,保证编译速度 --> <!-- 工作流。默认注释,保证编译速度 -->
<!-- <dependency>--> <dependency>
<!-- <groupId>cn.iocoder.boot</groupId>--> <groupId>cn.iocoder.boot</groupId>
<!-- <artifactId>yudao-module-bpm-biz</artifactId>--> <artifactId>yudao-module-bpm-biz</artifactId>
<!-- <version>${revision}</version>--> <version>${revision}</version>
<!-- </dependency>--> </dependency>
<!-- 支付服务。默认注释,保证编译速度 --> <!-- 支付服务。默认注释,保证编译速度 -->
<!-- <dependency>--> <!-- <dependency>-->
<!-- <groupId>cn.iocoder.boot</groupId>--> <!-- <groupId>cn.iocoder.boot</groupId>-->
@ -88,11 +88,11 @@
<!-- </dependency>--> <!-- </dependency>-->
<!-- CRM 相关模块。默认注释,保证编译速度 --> <!-- CRM 相关模块。默认注释,保证编译速度 -->
<!-- <dependency>--> <dependency>
<!-- <groupId>cn.iocoder.boot</groupId>--> <groupId>cn.iocoder.boot</groupId>
<!-- <artifactId>yudao-module-crm-biz</artifactId>--> <artifactId>yudao-module-crm-biz</artifactId>
<!-- <version>${revision}</version>--> <version>${revision}</version>
<!-- </dependency>--> </dependency>
<!-- ERP 相关模块。默认注释,保证编译速度 --> <!-- ERP 相关模块。默认注释,保证编译速度 -->
<!-- <dependency>--> <!-- <dependency>-->