Commit 52c86cd290d22bea92dcddf9ea6569b5c6c9d056

Authored by YunaiV
1 parent d0d1d6d2

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

@@ -16,12 +16,12 @@ @@ -16,12 +16,12 @@
16 <module>yudao-module-system</module> 16 <module>yudao-module-system</module>
17 <module>yudao-module-infra</module> 17 <module>yudao-module-infra</module>
18 <!-- <module>yudao-module-member</module>--> 18 <!-- <module>yudao-module-member</module>-->
19 -<!-- <module>yudao-module-bpm</module>--> 19 + <module>yudao-module-bpm</module>
20 <!-- <module>yudao-module-report</module>--> 20 <!-- <module>yudao-module-report</module>-->
21 <!-- <module>yudao-module-mp</module>--> 21 <!-- <module>yudao-module-mp</module>-->
22 <!-- <module>yudao-module-pay</module>--> 22 <!-- <module>yudao-module-pay</module>-->
23 <!-- <module>yudao-module-mall</module>--> 23 <!-- <module>yudao-module-mall</module>-->
24 -<!-- <module>yudao-module-crm</module>--> 24 + <module>yudao-module-crm</module>
25 <!-- <module>yudao-module-erp</module>--> 25 <!-- <module>yudao-module-erp</module>-->
26 </modules> 26 </modules>
27 27
yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/statistics/CrmStatisticsPerformanceServiceImpl.java
@@ -2,7 +2,7 @@ package cn.iocoder.yudao.module.crm.service.statistics; @@ -2,7 +2,7 @@ package cn.iocoder.yudao.module.crm.service.statistics;
2 2
3 import cn.hutool.core.collection.CollUtil; 3 import cn.hutool.core.collection.CollUtil;
4 import cn.hutool.core.collection.ListUtil; 4 import cn.hutool.core.collection.ListUtil;
5 -import cn.hutool.core.date.LocalDateTimeUtil; 5 +import cn.hutool.core.date.DateUtil;
6 import cn.hutool.core.util.ObjUtil; 6 import cn.hutool.core.util.ObjUtil;
7 import cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.performance.CrmStatisticsPerformanceReqVO; 7 import cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.performance.CrmStatisticsPerformanceReqVO;
8 import cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.performance.CrmStatisticsPerformanceRespVO; 8 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; @@ -11,16 +11,19 @@ import cn.iocoder.yudao.module.system.api.dept.DeptApi;
11 import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO; 11 import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
12 import cn.iocoder.yudao.module.system.api.user.AdminUserApi; 12 import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
13 import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO; 13 import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
  14 +import jakarta.annotation.Resource;
14 import org.springframework.stereotype.Service; 15 import org.springframework.stereotype.Service;
15 import org.springframework.validation.annotation.Validated; 16 import org.springframework.validation.annotation.Validated;
16 17
17 -import jakarta.annotation.Resource;  
18 -  
19 import java.math.BigDecimal; 18 import java.math.BigDecimal;
20 -import java.util.*; 19 +import java.util.ArrayList;
  20 +import java.util.Collections;
  21 +import java.util.List;
  22 +import java.util.Map;
21 import java.util.function.Function; 23 import java.util.function.Function;
22 24
23 import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; 25 import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
  26 +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
24 27
25 /** 28 /**
26 * CRM 员工业绩分析 Service 实现类 29 * CRM 员工业绩分析 Service 实现类
@@ -56,9 +59,11 @@ public class CrmStatisticsPerformanceServiceImpl implements CrmStatisticsPerform @@ -56,9 +59,11 @@ public class CrmStatisticsPerformanceServiceImpl implements CrmStatisticsPerform
56 } 59 }
57 60
58 /** 61 /**
59 - * 获得员工业绩数据,并通过如下方法拿到对应的lastYearCount,lastMonthCount  
60 - * 比如说,构造2024 年的CrmStatisticsPerformanceRespVO,获得 2023-01 到 2024-12的月统计数据即可  
61 - * 可以数据 group by 年-月,2023-01 到 2024-12的,然后聚合出 CrmStatisticsPerformanceRespVO 62 + * 获得员工业绩数据
  63 + *
  64 + * 1. 获得今年 + 去年的数据
  65 + * 2. 遍历今年的月份,逐个拼接去年的月份数据
  66 + *
62 * @param performanceReqVO 参数 67 * @param performanceReqVO 参数
63 * @param performanceFunction 员工业绩统计方法 68 * @param performanceFunction 员工业绩统计方法
64 * @return 员工业绩数据 69 * @return 员工业绩数据
@@ -67,7 +72,7 @@ public class CrmStatisticsPerformanceServiceImpl implements CrmStatisticsPerform @@ -67,7 +72,7 @@ public class CrmStatisticsPerformanceServiceImpl implements CrmStatisticsPerform
67 Function<CrmStatisticsPerformanceReqVO, List<CrmStatisticsPerformanceRespVO>> performanceFunction) { 72 Function<CrmStatisticsPerformanceReqVO, List<CrmStatisticsPerformanceRespVO>> performanceFunction) {
68 73
69 // 1. 获得用户编号数组 74 // 1. 获得用户编号数组
70 - final List<Long> userIds = getUserIds(performanceReqVO); 75 + List<Long> userIds = getUserIds(performanceReqVO);
71 if (CollUtil.isEmpty(userIds)) { 76 if (CollUtil.isEmpty(userIds)) {
72 return Collections.emptyList(); 77 return Collections.emptyList();
73 } 78 }
@@ -75,66 +80,22 @@ public class CrmStatisticsPerformanceServiceImpl implements CrmStatisticsPerform @@ -75,66 +80,22 @@ public class CrmStatisticsPerformanceServiceImpl implements CrmStatisticsPerform
75 80
76 // 2. 获得业绩数据 81 // 2. 获得业绩数据
77 List<CrmStatisticsPerformanceRespVO> performanceList = performanceFunction.apply(performanceReqVO); 82 List<CrmStatisticsPerformanceRespVO> performanceList = performanceFunction.apply(performanceReqVO);
  83 + Map<String, BigDecimal> performanceMap = convertMap(performanceList, CrmStatisticsPerformanceRespVO::getTime,
  84 + CrmStatisticsPerformanceRespVO::getCurrentMonthCount);
78 85
79 - // 获取查询的年份  
80 - String currentYear = LocalDateTimeUtil.format(performanceReqVO.getTimes()[0],"yyyy");  
81 - Map<Integer, CrmStatisticsPerformanceRespVO> currentYearMap = new TreeMap<>();//查询当年的map数据  
82 - Map<Integer, CrmStatisticsPerformanceRespVO> lastYearMap = new TreeMap<>();//前一年的map数据  
83 - 86 + // 3. 组装数据返回
  87 + List<CrmStatisticsPerformanceRespVO> result = new ArrayList<>();
  88 + int year = DateUtil.thisYear();
84 for (int month = 1; month <= 12; month++) { 89 for (int month = 1; month <= 12; month++) {
85 - //根据数据库的月销售数据查询结果,构造查询当年的map数据  
86 - String currentYearKey = String.format("%d%02d", Integer.parseInt(currentYear), month);  
87 - buildYearMapData(performanceList, currentYearMap, currentYearKey);  
88 -  
89 - //根据数据库的月销售数据查询结果,构造查询前一年的map数据  
90 - String lastYearKey = String.format("%d%02d", Integer.parseInt(currentYear)-1, month);  
91 - buildYearMapData(performanceList, lastYearMap, lastYearKey);  
92 - }  
93 - //根据构造好的map数据,计算查询当年的环比和同比数据,并构造好返回的respVOList  
94 - List<CrmStatisticsPerformanceRespVO> respVOList = new ArrayList<>();  
95 - for (int key : currentYearMap.keySet()) {  
96 - BigDecimal lastYearCount = lastYearMap.get(key-100).getCurrentMonthCount();  
97 - BigDecimal lastMonthCount;  
98 - if (key % 100 > 1) {//2-12月份的前一个月数据  
99 - lastMonthCount = currentYearMap.get(key-1).getCurrentMonthCount();  
100 - } else {//1月份的前一个月数据  
101 - lastMonthCount = lastYearMap.get(key-89).getCurrentMonthCount();  
102 - }  
103 -  
104 - currentYearMap.get(key).setLastYearCount(lastYearCount);  
105 - currentYearMap.get(key).setLastMonthCount(lastMonthCount);  
106 -  
107 - respVOList.add(currentYearMap.get(key));  
108 - }  
109 -  
110 - return respVOList;  
111 - }  
112 -  
113 - /**  
114 - * 根据mapKey,添加当年和前一年的月销售记录到对应的map结构中  
115 - * @param performanceList 数据库中查询到的月销售记录  
116 - * @param YearDataMap 将查询到的月销售记录put到对应的map中,如果月销售记录为null,置为0  
117 - * @param mapKey 对应的mapKey  
118 - */  
119 - private void buildYearMapData(List<CrmStatisticsPerformanceRespVO> performanceList,  
120 - Map<Integer, CrmStatisticsPerformanceRespVO> YearDataMap,  
121 - String mapKey)  
122 - {  
123 - CrmStatisticsPerformanceRespVO currentYearData = performanceList.stream()  
124 - .filter(data -> data.getTime().equals(mapKey))  
125 - .findFirst()  
126 - .orElse(null);  
127 -  
128 - if(currentYearData != null) {  
129 - YearDataMap.put(Integer.parseInt(mapKey), currentYearData);  
130 - } else {  
131 - CrmStatisticsPerformanceRespVO defaultVO = new CrmStatisticsPerformanceRespVO();  
132 - defaultVO.setTime(mapKey);  
133 - defaultVO.setCurrentMonthCount(BigDecimal.ZERO);  
134 - defaultVO.setLastMonthCount(BigDecimal.ZERO);  
135 - defaultVO.setLastYearCount(BigDecimal.ZERO);  
136 - YearDataMap.put(Integer.parseInt(mapKey), defaultVO); 90 + String currentMonth = String.format("%d%02d", year, month);
  91 + String lastMonth = month == 1 ? String.format("%d%02d", year - 1, 12) : String.format("%d%02d", year, month - 1);
  92 + String lastYear = String.format("%d%02d", year - 1, month);
  93 + result.add(new CrmStatisticsPerformanceRespVO().setTime(currentMonth)
  94 + .setCurrentMonthCount(performanceMap.getOrDefault(currentMonth, BigDecimal.ZERO))
  95 + .setLastMonthCount(performanceMap.getOrDefault(lastMonth, BigDecimal.ZERO))
  96 + .setLastYearCount(performanceMap.getOrDefault(lastYear, BigDecimal.ZERO)));
137 } 97 }
  98 + return result;
138 } 99 }
139 100
140 /** 101 /**
yudao-server/pom.xml
@@ -46,11 +46,11 @@ @@ -46,11 +46,11 @@
46 <!-- <version>${revision}</version>--> 46 <!-- <version>${revision}</version>-->
47 <!-- </dependency>--> 47 <!-- </dependency>-->
48 <!-- 工作流。默认注释,保证编译速度 --> 48 <!-- 工作流。默认注释,保证编译速度 -->
49 -<!-- <dependency>-->  
50 -<!-- <groupId>cn.iocoder.boot</groupId>-->  
51 -<!-- <artifactId>yudao-module-bpm-biz</artifactId>-->  
52 -<!-- <version>${revision}</version>-->  
53 -<!-- </dependency>--> 49 + <dependency>
  50 + <groupId>cn.iocoder.boot</groupId>
  51 + <artifactId>yudao-module-bpm-biz</artifactId>
  52 + <version>${revision}</version>
  53 + </dependency>
54 <!-- 支付服务。默认注释,保证编译速度 --> 54 <!-- 支付服务。默认注释,保证编译速度 -->
55 <!-- <dependency>--> 55 <!-- <dependency>-->
56 <!-- <groupId>cn.iocoder.boot</groupId>--> 56 <!-- <groupId>cn.iocoder.boot</groupId>-->
@@ -88,11 +88,11 @@ @@ -88,11 +88,11 @@
88 <!-- </dependency>--> 88 <!-- </dependency>-->
89 89
90 <!-- CRM 相关模块。默认注释,保证编译速度 --> 90 <!-- CRM 相关模块。默认注释,保证编译速度 -->
91 -<!-- <dependency>-->  
92 -<!-- <groupId>cn.iocoder.boot</groupId>-->  
93 -<!-- <artifactId>yudao-module-crm-biz</artifactId>-->  
94 -<!-- <version>${revision}</version>-->  
95 -<!-- </dependency>--> 91 + <dependency>
  92 + <groupId>cn.iocoder.boot</groupId>
  93 + <artifactId>yudao-module-crm-biz</artifactId>
  94 + <version>${revision}</version>
  95 + </dependency>
96 96
97 <!-- ERP 相关模块。默认注释,保证编译速度 --> 97 <!-- ERP 相关模块。默认注释,保证编译速度 -->
98 <!-- <dependency>--> 98 <!-- <dependency>-->