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 fcd7267a8..b8fa94c7a 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 @@ -38,12 +38,9 @@ public class CrmStatisticsPerformanceServiceImpl implements CrmStatisticsPerform @Resource private DeptApi deptApi; + @Override public List getContractCountPerformance(CrmStatisticsPerformanceReqVO performanceReqVO) { - // TODO @scholar:可以把下面这个注释,你理解后,重新整理下,写到 getPerformance 里; - // 比如说,2024 年的合同数据,是不是 2022-12 到 2024-12-31,每个月的统计呢? - // 理解之后,我们可以数据 group by 年-月,20222-12 到 2024-12-31 的,然后内存在聚合出 CrmStatisticsPerformanceRespVO 这样 - // 这样,我们就可以减少数据库的计算量,提升性能;同时 SQL 也会很简单,开发者理解起来也简单哈; return getPerformance(performanceReqVO, performanceMapper::selectContractCountPerformance); } @@ -57,20 +54,16 @@ public class CrmStatisticsPerformanceServiceImpl implements CrmStatisticsPerform return getPerformance(performanceReqVO, performanceMapper::selectReceivablePricePerformance); } - // TODO @scholar:代码注释,应该有 3 个变量哈; /** - * 获得员工业绩数据 - * + * 获得员工业绩数据,并通过如下方法拿到对应的lastYearCount,lastMonthCount + * 比如说,构造2024 年的CrmStatisticsPerformanceRespVO,获得 2023-01 到 2024-12的月统计数据即可 + * 可以数据 group by 年-月,2023-01 到 2024-12的,然后聚合出 CrmStatisticsPerformanceRespVO * @param performanceReqVO 参数 * @param performanceFunction 员工业绩统计方法 * @return 员工业绩数据 */ - // TODO @scholar:下面一行的变量,超过一行了,阅读不美观;可以考虑每一行一个变量; - private List getPerformance(CrmStatisticsPerformanceReqVO performanceReqVO, Function> performanceFunction) { - - // TODO @scholar:没使用到的变量,建议删除; - List performanceRespVOList; + private List getPerformance(CrmStatisticsPerformanceReqVO performanceReqVO, + Function> performanceFunction) { // 1. 获得用户编号数组 final List userIds = getUserIds(performanceReqVO); @@ -78,80 +71,71 @@ public class CrmStatisticsPerformanceServiceImpl implements CrmStatisticsPerform return Collections.emptyList(); } performanceReqVO.setUserIds(userIds); - // TODO @scholar:1. 和 2. 之间,可以考虑换一行;保证每一块逻辑的间隔; + // 2. 获得业绩数据 - // TODO @scholar:复数变量,建议使用 s 或者 list 结果;这里用 performanceList 好列; - List performance = performanceFunction.apply(performanceReqVO); + List performanceList = performanceFunction.apply(performanceReqVO); // 获取查询的年份 - // TODO @scholar:逻辑可以简化一下; - // TODO 1)把 performance 转换成 map;key 是 time,value 是 count - // TODO 2)当前年,遍历 1-12 月份,去 map 拿到 count;接着月份 -1,去 map 拿 count;再年份 -1,拿 count String currentYear = LocalDateTimeUtil.format(performanceReqVO.getTimes()[0],"yyyy"); + Map currentYearMap = new TreeMap<>();//查询当年的map数据 + Map lastYearMap = new TreeMap<>();//前一年的map数据 - // 构造查询当年和前一年,每年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)); - } + for (int month = 1; month <= 12; month++) { + //根据数据库的月销售数据查询结果,构造查询当年的map数据 + String currentYearKey = String.format("%d%02d", Integer.parseInt(currentYear), month); + buildYearMapData(performanceList, currentYearMap, currentYearKey); + + //根据数据库的月销售数据查询结果,构造查询前一年的map数据 + String lastYearKey = String.format("%d%02d", Integer.parseInt(currentYear)-1, month); + buildYearMapData(performanceList, lastYearMap, lastYearKey); } - - List computedList = new ArrayList<>(); + //根据构造好的map数据,计算查询当年的环比和同比数据,并构造好返回的respVOList 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 (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)); } - //根据查询年份和前一年的数据,计算查询年份的同比环比数据 - 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; } + /** + * 根据mapKey,添加当年和前一年的月销售记录到对应的map结构中 + * @param performanceList 数据库中查询到的月销售记录 + * @param YearDataMap 将查询到的月销售记录put到对应的map中,如果月销售记录为null,置为0 + * @param mapKey 对应的mapKey + */ + private void buildYearMapData(List performanceList, + Map 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); + } + } + /** * 获取用户编号数组。如果用户编号为空, 则获得部门下的用户编号数组,包括子部门的所有用户编号 *