Commit 408378f5ab10859247e0e82e37b8cf223194b67d
Merge branch 'develop' of https://gitee.com/scholarli/ruoyi-vue-pro_1 into develop
# Conflicts: # yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/statistics/CrmStatisticsPerformanceServiceImpl.java
Showing
1 changed file
with
59 additions
and
75 deletions
yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/statistics/CrmStatisticsPerformanceServiceImpl.java
| ... | ... | @@ -38,12 +38,9 @@ public class CrmStatisticsPerformanceServiceImpl implements CrmStatisticsPerform |
| 38 | 38 | @Resource |
| 39 | 39 | private DeptApi deptApi; |
| 40 | 40 | |
| 41 | + | |
| 41 | 42 | @Override |
| 42 | 43 | public List<CrmStatisticsPerformanceRespVO> getContractCountPerformance(CrmStatisticsPerformanceReqVO performanceReqVO) { |
| 43 | - // TODO @scholar:可以把下面这个注释,你理解后,重新整理下,写到 getPerformance 里; | |
| 44 | - // 比如说,2024 年的合同数据,是不是 2022-12 到 2024-12-31,每个月的统计呢? | |
| 45 | - // 理解之后,我们可以数据 group by 年-月,20222-12 到 2024-12-31 的,然后内存在聚合出 CrmStatisticsPerformanceRespVO 这样 | |
| 46 | - // 这样,我们就可以减少数据库的计算量,提升性能;同时 SQL 也会很简单,开发者理解起来也简单哈; | |
| 47 | 44 | return getPerformance(performanceReqVO, performanceMapper::selectContractCountPerformance); |
| 48 | 45 | } |
| 49 | 46 | |
| ... | ... | @@ -57,20 +54,16 @@ public class CrmStatisticsPerformanceServiceImpl implements CrmStatisticsPerform |
| 57 | 54 | return getPerformance(performanceReqVO, performanceMapper::selectReceivablePricePerformance); |
| 58 | 55 | } |
| 59 | 56 | |
| 60 | - // TODO @scholar:代码注释,应该有 3 个变量哈; | |
| 61 | 57 | /** |
| 62 | - * 获得员工业绩数据 | |
| 63 | - * | |
| 58 | + * 获得员工业绩数据,并通过如下方法拿到对应的lastYearCount,lastMonthCount | |
| 59 | + * 比如说,构造2024 年的CrmStatisticsPerformanceRespVO,获得 2023-01 到 2024-12的月统计数据即可 | |
| 60 | + * 可以数据 group by 年-月,2023-01 到 2024-12的,然后聚合出 CrmStatisticsPerformanceRespVO | |
| 64 | 61 | * @param performanceReqVO 参数 |
| 65 | 62 | * @param performanceFunction 员工业绩统计方法 |
| 66 | 63 | * @return 员工业绩数据 |
| 67 | 64 | */ |
| 68 | - // TODO @scholar:下面一行的变量,超过一行了,阅读不美观;可以考虑每一行一个变量; | |
| 69 | - private List<CrmStatisticsPerformanceRespVO> getPerformance(CrmStatisticsPerformanceReqVO performanceReqVO, Function<CrmStatisticsPerformanceReqVO, | |
| 70 | - List<CrmStatisticsPerformanceRespVO>> performanceFunction) { | |
| 71 | - | |
| 72 | - // TODO @scholar:没使用到的变量,建议删除; | |
| 73 | - List<CrmStatisticsPerformanceRespVO> performanceRespVOList; | |
| 65 | + private List<CrmStatisticsPerformanceRespVO> getPerformance(CrmStatisticsPerformanceReqVO performanceReqVO, | |
| 66 | + Function<CrmStatisticsPerformanceReqVO, List<CrmStatisticsPerformanceRespVO>> performanceFunction) { | |
| 74 | 67 | |
| 75 | 68 | // 1. 获得用户编号数组 |
| 76 | 69 | final List<Long> userIds = getUserIds(performanceReqVO); |
| ... | ... | @@ -78,81 +71,72 @@ public class CrmStatisticsPerformanceServiceImpl implements CrmStatisticsPerform |
| 78 | 71 | return Collections.emptyList(); |
| 79 | 72 | } |
| 80 | 73 | performanceReqVO.setUserIds(userIds); |
| 81 | - // TODO @scholar:1. 和 2. 之间,可以考虑换一行;保证每一块逻辑的间隔; | |
| 74 | + | |
| 82 | 75 | // 2. 获得业绩数据 |
| 83 | - // TODO @scholar:复数变量,建议使用 s 或者 list 结果;这里用 performanceList 好列; | |
| 84 | - List<CrmStatisticsPerformanceRespVO> performance = performanceFunction.apply(performanceReqVO); | |
| 76 | + List<CrmStatisticsPerformanceRespVO> performanceList = performanceFunction.apply(performanceReqVO); | |
| 85 | 77 | |
| 86 | 78 | // 获取查询的年份 |
| 87 | - // TODO @scholar:逻辑可以简化一下; | |
| 88 | - // TODO 1)把 performance 转换成 map;key 是 time,value 是 count | |
| 89 | - // TODO 2)当前年,遍历 1-12 月份,去 map 拿到 count;接着月份 -1,去 map 拿 count;再年份 -1,拿 count | |
| 90 | 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数据 | |
| 91 | 82 | |
| 92 | - // 构造查询当年和前一年,每年12个月的年月组合 | |
| 93 | - List<String> allMonths = new ArrayList<>(); | |
| 94 | - for (int year = Integer.parseInt(currentYear)-1; year <= Integer.parseInt(currentYear); year++) { | |
| 95 | - for (int month = 1; month <= 12; month++) { | |
| 96 | - allMonths.add(String.format("%d%02d", year, month)); | |
| 97 | - } | |
| 98 | - } | |
| 99 | - | |
| 100 | - List<CrmStatisticsPerformanceRespVO> computedList = new ArrayList<>(); | |
| 101 | - List<CrmStatisticsPerformanceRespVO> respVOList = new ArrayList<>(); | |
| 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); | |
| 102 | 87 | |
| 103 | - // 生成computedList基础数据 | |
| 104 | - // 构造完整的2*12个月的数据,如果某月数据缺失,需要补上0,一年12个月不能有缺失 | |
| 105 | - for (String month : allMonths) { | |
| 106 | - CrmStatisticsPerformanceRespVO foundData = performance.stream() | |
| 107 | - .filter(data -> data.getTime().equals(month)) | |
| 108 | - .findFirst() | |
| 109 | - .orElse(null); | |
| 110 | - | |
| 111 | - if (foundData != null) { | |
| 112 | - computedList.add(foundData); | |
| 113 | - } else { | |
| 114 | - CrmStatisticsPerformanceRespVO missingData = new CrmStatisticsPerformanceRespVO(); | |
| 115 | - missingData.setTime(month); | |
| 116 | - missingData.setCurrentMonthCount(BigDecimal.ZERO); | |
| 117 | - missingData.setLastMonthCount(BigDecimal.ZERO); | |
| 118 | - missingData.setLastYearCount(BigDecimal.ZERO); | |
| 119 | - computedList.add(missingData); | |
| 120 | - } | |
| 88 | + //根据数据库的月销售数据查询结果,构造查询前一年的map数据 | |
| 89 | + String lastYearKey = String.format("%d%02d", Integer.parseInt(currentYear)-1, month); | |
| 90 | + buildYearMapData(performanceList, lastYearMap, lastYearKey); | |
| 121 | 91 | } |
| 122 | - //根据查询年份和前一年的数据,计算查询年份的同比环比数据 | |
| 123 | - for (CrmStatisticsPerformanceRespVO currentData : computedList) { | |
| 124 | - String currentMonth = currentData.getTime(); | |
| 125 | - | |
| 126 | - // 根据当年和前一年的月销售数据,计算currentYear的完整数据 | |
| 127 | - if (currentMonth.startsWith(currentYear)) { | |
| 128 | - // 计算 LastMonthCount | |
| 129 | - int currentIndex = computedList.indexOf(currentData); | |
| 130 | - if (currentIndex > 0) { | |
| 131 | - CrmStatisticsPerformanceRespVO lastMonthData = computedList.get(currentIndex - 1); | |
| 132 | - currentData.setLastMonthCount(lastMonthData.getCurrentMonthCount()); | |
| 133 | - } else { | |
| 134 | - currentData.setLastMonthCount(BigDecimal.ZERO); // 第一个月的 LastMonthCount 设为0 | |
| 135 | - } | |
| 136 | - | |
| 137 | - // 计算 LastYearCount | |
| 138 | - String lastYearMonth = String.valueOf(Integer.parseInt(currentMonth) - 100); | |
| 139 | - CrmStatisticsPerformanceRespVO lastYearData = computedList.stream() | |
| 140 | - .filter(data -> data.getTime().equals(lastYearMonth)) | |
| 141 | - .findFirst() | |
| 142 | - .orElse(null); | |
| 143 | - | |
| 144 | - if (lastYearData != null) { | |
| 145 | - currentData.setLastYearCount(lastYearData.getCurrentMonthCount()); | |
| 146 | - } else { | |
| 147 | - currentData.setLastYearCount(BigDecimal.ZERO); // 如果去年同月数据不存在,设为0 | |
| 148 | - } | |
| 149 | - 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(); | |
| 150 | 101 | } |
| 102 | + | |
| 103 | + currentYearMap.get(key).setLastYearCount(lastYearCount); | |
| 104 | + currentYearMap.get(key).setLastMonthCount(lastMonthCount); | |
| 105 | + | |
| 106 | + respVOList.add(currentYearMap.get(key)); | |
| 151 | 107 | } |
| 108 | + | |
| 152 | 109 | return respVOList; |
| 153 | 110 | } |
| 154 | 111 | |
| 155 | 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 | + /** | |
| 156 | 140 | * 获取用户编号数组。如果用户编号为空, 则获得部门下的用户编号数组,包括子部门的所有用户编号 |
| 157 | 141 | * |
| 158 | 142 | * @param reqVO 请求参数 | ... | ... |