Commit 3afd2cba0cd5e46e04a0dc9f20449f1adbaa83e0
1 parent
9e6474a1
完成todo部分,优化环比同比数据的计算。
Showing
1 changed file
with
58 additions
and
63 deletions
yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/statistics/CrmStatisticsPerformanceServiceImpl.java
| @@ -55,16 +55,15 @@ public class CrmStatisticsPerformanceServiceImpl implements CrmStatisticsPerform | @@ -55,16 +55,15 @@ public class CrmStatisticsPerformanceServiceImpl implements CrmStatisticsPerform | ||
| 55 | } | 55 | } |
| 56 | 56 | ||
| 57 | /** | 57 | /** |
| 58 | - * 获得员工业绩数据 | ||
| 59 | - * | 58 | + * 获得员工业绩数据,并通过如下方法拿到对应的lastYearCount,lastMonthCount |
| 59 | + * 比如说,构造2024 年的CrmStatisticsPerformanceRespVO,获得 2023-01 到 2024-12的月统计数据即可 | ||
| 60 | + * 可以数据 group by 年-月,2023-01 到 2024-12的,然后聚合出 CrmStatisticsPerformanceRespVO | ||
| 60 | * @param performanceReqVO 参数 | 61 | * @param performanceReqVO 参数 |
| 61 | * @param performanceFunction 员工业绩统计方法 | 62 | * @param performanceFunction 员工业绩统计方法 |
| 62 | * @return 员工业绩数据 | 63 | * @return 员工业绩数据 |
| 63 | */ | 64 | */ |
| 64 | - private List<CrmStatisticsPerformanceRespVO> getPerformance(CrmStatisticsPerformanceReqVO performanceReqVO, Function<CrmStatisticsPerformanceReqVO, | ||
| 65 | - List<CrmStatisticsPerformanceRespVO>> performanceFunction) { | ||
| 66 | - | ||
| 67 | - List<CrmStatisticsPerformanceRespVO> performanceRespVOList; | 65 | + private List<CrmStatisticsPerformanceRespVO> getPerformance(CrmStatisticsPerformanceReqVO performanceReqVO, |
| 66 | + Function<CrmStatisticsPerformanceReqVO, List<CrmStatisticsPerformanceRespVO>> performanceFunction) { | ||
| 68 | 67 | ||
| 69 | // 1. 获得用户编号数组 | 68 | // 1. 获得用户编号数组 |
| 70 | final List<Long> userIds = getUserIds(performanceReqVO); | 69 | final List<Long> userIds = getUserIds(performanceReqVO); |
| @@ -72,76 +71,72 @@ public class CrmStatisticsPerformanceServiceImpl implements CrmStatisticsPerform | @@ -72,76 +71,72 @@ public class CrmStatisticsPerformanceServiceImpl implements CrmStatisticsPerform | ||
| 72 | return Collections.emptyList(); | 71 | return Collections.emptyList(); |
| 73 | } | 72 | } |
| 74 | performanceReqVO.setUserIds(userIds); | 73 | performanceReqVO.setUserIds(userIds); |
| 74 | + | ||
| 75 | // 2. 获得业绩数据 | 75 | // 2. 获得业绩数据 |
| 76 | - List<CrmStatisticsPerformanceRespVO> performance = performanceFunction.apply(performanceReqVO); | 76 | + List<CrmStatisticsPerformanceRespVO> performanceList = performanceFunction.apply(performanceReqVO); |
| 77 | 77 | ||
| 78 | // 获取查询的年份 | 78 | // 获取查询的年份 |
| 79 | String currentYear = LocalDateTimeUtil.format(performanceReqVO.getTimes()[0],"yyyy"); | 79 | String currentYear = LocalDateTimeUtil.format(performanceReqVO.getTimes()[0],"yyyy"); |
| 80 | + Map<Integer, CrmStatisticsPerformanceRespVO> currentYearMap = new TreeMap<>();//查询当年的map数据 | ||
| 81 | + Map<Integer, CrmStatisticsPerformanceRespVO> lastYearMap = new TreeMap<>();//前一年的map数据 | ||
| 80 | 82 | ||
| 81 | - // 构造查询当年和前一年,每年12个月的年月组合 | ||
| 82 | - List<String> allMonths = new ArrayList<>(); | ||
| 83 | - for (int year = Integer.parseInt(currentYear)-1; year <= Integer.parseInt(currentYear); year++) { | ||
| 84 | - for (int month = 1; month <= 12; month++) { | ||
| 85 | - allMonths.add(String.format("%d%02d", year, month)); | ||
| 86 | - } | ||
| 87 | - } | 83 | + for (int month = 1; month <= 12; month++) { |
| 84 | + //根据数据库的月销售数据查询结果,构造查询当年的map数据 | ||
| 85 | + String currentYearKey = String.format("%d%02d", Integer.parseInt(currentYear), month); | ||
| 86 | + buildYearMapData(performanceList, currentYearMap, currentYearKey); | ||
| 88 | 87 | ||
| 89 | - List<CrmStatisticsPerformanceRespVO> computedList = new ArrayList<>(); | ||
| 90 | - List<CrmStatisticsPerformanceRespVO> respVOList = new ArrayList<>(); | ||
| 91 | - | ||
| 92 | - // 生成computedList基础数据 | ||
| 93 | - // 构造完整的2*12个月的数据,如果某月数据缺失,需要补上0,一年12个月不能有缺失 | ||
| 94 | - for (String month : allMonths) { | ||
| 95 | - CrmStatisticsPerformanceRespVO foundData = performance.stream() | ||
| 96 | - .filter(data -> data.getTime().equals(month)) | ||
| 97 | - .findFirst() | ||
| 98 | - .orElse(null); | ||
| 99 | - | ||
| 100 | - if (foundData != null) { | ||
| 101 | - computedList.add(foundData); | ||
| 102 | - } else { | ||
| 103 | - CrmStatisticsPerformanceRespVO missingData = new CrmStatisticsPerformanceRespVO(); | ||
| 104 | - missingData.setTime(month); | ||
| 105 | - missingData.setCurrentMonthCount(BigDecimal.ZERO); | ||
| 106 | - missingData.setLastMonthCount(BigDecimal.ZERO); | ||
| 107 | - missingData.setLastYearCount(BigDecimal.ZERO); | ||
| 108 | - computedList.add(missingData); | ||
| 109 | - } | 88 | + //根据数据库的月销售数据查询结果,构造查询前一年的map数据 |
| 89 | + String lastYearKey = String.format("%d%02d", Integer.parseInt(currentYear)-1, month); | ||
| 90 | + buildYearMapData(performanceList, lastYearMap, lastYearKey); | ||
| 110 | } | 91 | } |
| 111 | - //根据查询年份和前一年的数据,计算查询年份的同比环比数据 | ||
| 112 | - for (CrmStatisticsPerformanceRespVO currentData : computedList) { | ||
| 113 | - String currentMonth = currentData.getTime(); | ||
| 114 | - | ||
| 115 | - // 根据当年和前一年的月销售数据,计算currentYear的完整数据 | ||
| 116 | - if (currentMonth.startsWith(currentYear)) { | ||
| 117 | - // 计算 LastMonthCount | ||
| 118 | - int currentIndex = computedList.indexOf(currentData); | ||
| 119 | - if (currentIndex > 0) { | ||
| 120 | - CrmStatisticsPerformanceRespVO lastMonthData = computedList.get(currentIndex - 1); | ||
| 121 | - currentData.setLastMonthCount(lastMonthData.getCurrentMonthCount()); | ||
| 122 | - } else { | ||
| 123 | - currentData.setLastMonthCount(BigDecimal.ZERO); // 第一个月的 LastMonthCount 设为0 | ||
| 124 | - } | ||
| 125 | - | ||
| 126 | - // 计算 LastYearCount | ||
| 127 | - String lastYearMonth = String.valueOf(Integer.parseInt(currentMonth) - 100); | ||
| 128 | - CrmStatisticsPerformanceRespVO lastYearData = computedList.stream() | ||
| 129 | - .filter(data -> data.getTime().equals(lastYearMonth)) | ||
| 130 | - .findFirst() | ||
| 131 | - .orElse(null); | ||
| 132 | - | ||
| 133 | - if (lastYearData != null) { | ||
| 134 | - currentData.setLastYearCount(lastYearData.getCurrentMonthCount()); | ||
| 135 | - } else { | ||
| 136 | - currentData.setLastYearCount(BigDecimal.ZERO); // 如果去年同月数据不存在,设为0 | ||
| 137 | - } | ||
| 138 | - respVOList.add(currentData);//给前端只需要返回查询当年的数据,不需要前一年数据 | 92 | + //根据构造好的map数据,计算查询当年的环比和同比数据,并构造好返回的respVOList |
| 93 | + List<CrmStatisticsPerformanceRespVO> respVOList = new ArrayList<>(); | ||
| 94 | + for (int key : currentYearMap.keySet()) { | ||
| 95 | + BigDecimal lastYearCount = lastYearMap.get(key-100).getCurrentMonthCount(); | ||
| 96 | + BigDecimal lastMonthCount; | ||
| 97 | + if (key % 100 > 1) {//2-12月份的前一个月数据 | ||
| 98 | + lastMonthCount = currentYearMap.get(key-1).getCurrentMonthCount(); | ||
| 99 | + } else {//1月份的前一个月数据 | ||
| 100 | + lastMonthCount = lastYearMap.get(key-89).getCurrentMonthCount(); | ||
| 139 | } | 101 | } |
| 102 | + | ||
| 103 | + currentYearMap.get(key).setLastYearCount(lastYearCount); | ||
| 104 | + currentYearMap.get(key).setLastMonthCount(lastMonthCount); | ||
| 105 | + | ||
| 106 | + respVOList.add(currentYearMap.get(key)); | ||
| 140 | } | 107 | } |
| 108 | + | ||
| 141 | return respVOList; | 109 | return respVOList; |
| 142 | } | 110 | } |
| 143 | 111 | ||
| 144 | /** | 112 | /** |
| 113 | + * 根据mapKey,添加当年和前一年的月销售记录到对应的map结构中 | ||
| 114 | + * @param performanceList 数据库中查询到的月销售记录 | ||
| 115 | + * @param YearDataMap 将查询到的月销售记录put到对应的map中,如果月销售记录为null,置为0 | ||
| 116 | + * @param mapKey 对应的mapKey | ||
| 117 | + */ | ||
| 118 | + private void buildYearMapData(List<CrmStatisticsPerformanceRespVO> performanceList, | ||
| 119 | + Map<Integer, CrmStatisticsPerformanceRespVO> YearDataMap, | ||
| 120 | + String mapKey) | ||
| 121 | + { | ||
| 122 | + CrmStatisticsPerformanceRespVO currentYearData = performanceList.stream() | ||
| 123 | + .filter(data -> data.getTime().equals(mapKey)) | ||
| 124 | + .findFirst() | ||
| 125 | + .orElse(null); | ||
| 126 | + | ||
| 127 | + if(currentYearData != null) { | ||
| 128 | + YearDataMap.put(Integer.parseInt(mapKey), currentYearData); | ||
| 129 | + } else { | ||
| 130 | + CrmStatisticsPerformanceRespVO defaultVO = new CrmStatisticsPerformanceRespVO(); | ||
| 131 | + defaultVO.setTime(mapKey); | ||
| 132 | + defaultVO.setCurrentMonthCount(BigDecimal.ZERO); | ||
| 133 | + defaultVO.setLastMonthCount(BigDecimal.ZERO); | ||
| 134 | + defaultVO.setLastYearCount(BigDecimal.ZERO); | ||
| 135 | + YearDataMap.put(Integer.parseInt(mapKey), defaultVO); | ||
| 136 | + } | ||
| 137 | + } | ||
| 138 | + | ||
| 139 | + /** | ||
| 145 | * 获取用户编号数组。如果用户编号为空, 则获得部门下的用户编号数组,包括子部门的所有用户编号 | 140 | * 获取用户编号数组。如果用户编号为空, 则获得部门下的用户编号数组,包括子部门的所有用户编号 |
| 146 | * | 141 | * |
| 147 | * @param reqVO 请求参数 | 142 | * @param reqVO 请求参数 |