Commit 99ab07de69258ee04dee4e84a888f56151d83476
1 parent
3afd2cba
完成todo xml部分
Showing
2 changed files
with
50 additions
and
86 deletions
yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/statistics/CrmStatisticsPerformanceServiceImpl.java
| @@ -16,10 +16,12 @@ import org.springframework.validation.annotation.Validated; | @@ -16,10 +16,12 @@ import org.springframework.validation.annotation.Validated; | ||
| 16 | import jakarta.annotation.Resource; | 16 | import jakarta.annotation.Resource; |
| 17 | 17 | ||
| 18 | import java.math.BigDecimal; | 18 | import java.math.BigDecimal; |
| 19 | +import java.time.LocalDateTime; | ||
| 19 | import java.util.*; | 20 | import java.util.*; |
| 20 | import java.util.function.Function; | 21 | import java.util.function.Function; |
| 21 | 22 | ||
| 22 | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; | 23 | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; |
| 24 | +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap; | ||
| 23 | 25 | ||
| 24 | /** | 26 | /** |
| 25 | * CRM 员工业绩分析 Service 实现类 | 27 | * CRM 员工业绩分析 Service 实现类 |
| @@ -55,85 +57,47 @@ public class CrmStatisticsPerformanceServiceImpl implements CrmStatisticsPerform | @@ -55,85 +57,47 @@ public class CrmStatisticsPerformanceServiceImpl implements CrmStatisticsPerform | ||
| 55 | } | 57 | } |
| 56 | 58 | ||
| 57 | /** | 59 | /** |
| 58 | - * 获得员工业绩数据,并通过如下方法拿到对应的lastYearCount,lastMonthCount | ||
| 59 | - * 比如说,构造2024 年的CrmStatisticsPerformanceRespVO,获得 2023-01 到 2024-12的月统计数据即可 | ||
| 60 | - * 可以数据 group by 年-月,2023-01 到 2024-12的,然后聚合出 CrmStatisticsPerformanceRespVO | 60 | + * 获得员工业绩数据 |
| 61 | + * | ||
| 62 | + * 1. 获得今年 + 去年的数据 | ||
| 63 | + * 2. 遍历今年的月份,逐个拼接去年的月份数据 | ||
| 64 | + * | ||
| 61 | * @param performanceReqVO 参数 | 65 | * @param performanceReqVO 参数 |
| 62 | * @param performanceFunction 员工业绩统计方法 | 66 | * @param performanceFunction 员工业绩统计方法 |
| 63 | * @return 员工业绩数据 | 67 | * @return 员工业绩数据 |
| 64 | */ | 68 | */ |
| 65 | private List<CrmStatisticsPerformanceRespVO> getPerformance(CrmStatisticsPerformanceReqVO performanceReqVO, | 69 | private List<CrmStatisticsPerformanceRespVO> getPerformance(CrmStatisticsPerformanceReqVO performanceReqVO, |
| 66 | - Function<CrmStatisticsPerformanceReqVO, List<CrmStatisticsPerformanceRespVO>> performanceFunction) { | 70 | + Function<CrmStatisticsPerformanceReqVO, List<CrmStatisticsPerformanceRespVO>> performanceFunction) { |
| 67 | 71 | ||
| 68 | // 1. 获得用户编号数组 | 72 | // 1. 获得用户编号数组 |
| 69 | - final List<Long> userIds = getUserIds(performanceReqVO); | 73 | + List<Long> userIds = getUserIds(performanceReqVO); |
| 70 | if (CollUtil.isEmpty(userIds)) { | 74 | if (CollUtil.isEmpty(userIds)) { |
| 71 | return Collections.emptyList(); | 75 | return Collections.emptyList(); |
| 72 | } | 76 | } |
| 73 | performanceReqVO.setUserIds(userIds); | 77 | performanceReqVO.setUserIds(userIds); |
| 74 | 78 | ||
| 75 | // 2. 获得业绩数据 | 79 | // 2. 获得业绩数据 |
| 80 | + int year = Integer.parseInt(LocalDateTimeUtil.format(performanceReqVO.getTimes()[0],"yyyy"));//获取查询的年份 | ||
| 81 | + LocalDateTime[] timesRange = performanceReqVO.getTimes();//以时间段形式去数据库查询,时间段为所查询年份和前一年,两年时间的数据,便于计算同比数据 | ||
| 82 | + timesRange[0] = performanceReqVO.getTimes()[0].minusYears(1);//查询的起始时间往前推一年 | ||
| 83 | + timesRange[1] = performanceReqVO.getTimes()[1];//查询的结束时间 | ||
| 84 | + performanceReqVO.setTimes(timesRange); | ||
| 76 | List<CrmStatisticsPerformanceRespVO> performanceList = performanceFunction.apply(performanceReqVO); | 85 | List<CrmStatisticsPerformanceRespVO> performanceList = performanceFunction.apply(performanceReqVO); |
| 86 | + Map<String, BigDecimal> performanceMap = convertMap(performanceList, CrmStatisticsPerformanceRespVO::getTime, | ||
| 87 | + CrmStatisticsPerformanceRespVO::getCurrentMonthCount); | ||
| 77 | 88 | ||
| 78 | - // 获取查询的年份 | ||
| 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数据 | ||
| 82 | - | 89 | + // 3. 组装数据返回 |
| 90 | + List<CrmStatisticsPerformanceRespVO> result = new ArrayList<>(); | ||
| 83 | for (int month = 1; month <= 12; month++) { | 91 | 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); | ||
| 87 | - | ||
| 88 | - //根据数据库的月销售数据查询结果,构造查询前一年的map数据 | ||
| 89 | - String lastYearKey = String.format("%d%02d", Integer.parseInt(currentYear)-1, month); | ||
| 90 | - buildYearMapData(performanceList, lastYearMap, lastYearKey); | ||
| 91 | - } | ||
| 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(); | ||
| 101 | - } | ||
| 102 | - | ||
| 103 | - currentYearMap.get(key).setLastYearCount(lastYearCount); | ||
| 104 | - currentYearMap.get(key).setLastMonthCount(lastMonthCount); | ||
| 105 | - | ||
| 106 | - respVOList.add(currentYearMap.get(key)); | ||
| 107 | - } | ||
| 108 | - | ||
| 109 | - return respVOList; | ||
| 110 | - } | ||
| 111 | - | ||
| 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); | 92 | + String currentMonth = String.format("%d%02d", year, month); |
| 93 | + String lastMonth = month == 1 ? String.format("%d%02d", year - 1, 12) : String.format("%d%02d", year, month - 1); | ||
| 94 | + String lastYear = String.format("%d%02d", year - 1, month); | ||
| 95 | + result.add(new CrmStatisticsPerformanceRespVO().setTime(currentMonth) | ||
| 96 | + .setCurrentMonthCount(performanceMap.getOrDefault(currentMonth, BigDecimal.ZERO)) | ||
| 97 | + .setLastMonthCount(performanceMap.getOrDefault(lastMonth, BigDecimal.ZERO)) | ||
| 98 | + .setLastYearCount(performanceMap.getOrDefault(lastYear, BigDecimal.ZERO))); | ||
| 136 | } | 99 | } |
| 100 | + return result; | ||
| 137 | } | 101 | } |
| 138 | 102 | ||
| 139 | /** | 103 | /** |
yudao-module-crm/yudao-module-crm-biz/src/main/resources/mapper/statistics/CrmStatisticsPerformanceMapper.xml
| @@ -5,51 +5,51 @@ | @@ -5,51 +5,51 @@ | ||
| 5 | <select id="selectContractCountPerformance" | 5 | <select id="selectContractCountPerformance" |
| 6 | resultType="cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.performance.CrmStatisticsPerformanceRespVO"> | 6 | resultType="cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.performance.CrmStatisticsPerformanceRespVO"> |
| 7 | SELECT | 7 | SELECT |
| 8 | - DATE_FORMAT(order_date, '%Y%m') AS time, | ||
| 9 | - COUNT(1) AS currentMonthCount | 8 | + DATE_FORMAT(order_date, '%Y%m') AS time, |
| 9 | + COUNT(1) AS currentMonthCount | ||
| 10 | FROM crm_contract | 10 | FROM crm_contract |
| 11 | WHERE deleted = 0 | 11 | WHERE deleted = 0 |
| 12 | - AND audit_status = 20 | 12 | + AND audit_status = ${@cn.iocoder.yudao.module.crm.enums.common.CrmAuditStatusEnum@APPROVE.status} |
| 13 | AND owner_user_id in | 13 | AND owner_user_id in |
| 14 | - <foreach collection="userIds" item="userId" open="(" close=")" separator=","> | ||
| 15 | - #{userId} | ||
| 16 | - </foreach> | ||
| 17 | - AND (DATE_FORMAT(order_date, '%Y') = DATE_FORMAT(#{times[0],javaType=java.time.LocalDateTime},'%Y') | ||
| 18 | - or DATE_FORMAT(order_date, '%Y') = DATE_FORMAT(#{times[0],javaType=java.time.LocalDateTime},'%Y')-1) | 14 | + <foreach collection="userIds" item="userId" open="(" close=")" separator=","> |
| 15 | + #{userId} | ||
| 16 | + </foreach> | ||
| 17 | + AND order_date between #{times[0],javaType=java.time.LocalDateTime} and | ||
| 18 | + #{times[1],javaType=java.time.LocalDateTime} | ||
| 19 | GROUP BY time | 19 | GROUP BY time |
| 20 | </select> | 20 | </select> |
| 21 | 21 | ||
| 22 | <select id="selectContractPricePerformance" | 22 | <select id="selectContractPricePerformance" |
| 23 | resultType="cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.performance.CrmStatisticsPerformanceRespVO"> | 23 | resultType="cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.performance.CrmStatisticsPerformanceRespVO"> |
| 24 | SELECT | 24 | SELECT |
| 25 | - DATE_FORMAT(order_date, '%Y%m') AS time, | ||
| 26 | - IFNULL(SUM(total_price), 0) AS currentMonthCount | 25 | + DATE_FORMAT(order_date, '%Y%m') AS time, |
| 26 | + IFNULL(SUM(total_price), 0) AS currentMonthCount | ||
| 27 | FROM crm_contract | 27 | FROM crm_contract |
| 28 | WHERE deleted = 0 | 28 | WHERE deleted = 0 |
| 29 | - AND audit_status = 20 | 29 | + AND audit_status = ${@cn.iocoder.yudao.module.crm.enums.common.CrmAuditStatusEnum@APPROVE.status} |
| 30 | AND owner_user_id in | 30 | AND owner_user_id in |
| 31 | - <foreach collection="userIds" item="userId" open="(" close=")" separator=","> | ||
| 32 | - #{userId} | ||
| 33 | - </foreach> | ||
| 34 | - AND (DATE_FORMAT(order_date, '%Y') = DATE_FORMAT(#{times[0],javaType=java.time.LocalDateTime},'%Y') | ||
| 35 | - or DATE_FORMAT(order_date, '%Y') = DATE_FORMAT(#{times[0],javaType=java.time.LocalDateTime},'%Y')-1) | 31 | + <foreach collection="userIds" item="userId" open="(" close=")" separator=","> |
| 32 | + #{userId} | ||
| 33 | + </foreach> | ||
| 34 | + AND order_date between #{times[0],javaType=java.time.LocalDateTime} and | ||
| 35 | + #{times[1],javaType=java.time.LocalDateTime} | ||
| 36 | GROUP BY time | 36 | GROUP BY time |
| 37 | </select> | 37 | </select> |
| 38 | 38 | ||
| 39 | <select id="selectReceivablePricePerformance" | 39 | <select id="selectReceivablePricePerformance" |
| 40 | resultType="cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.performance.CrmStatisticsPerformanceRespVO"> | 40 | resultType="cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.performance.CrmStatisticsPerformanceRespVO"> |
| 41 | SELECT | 41 | SELECT |
| 42 | - DATE_FORMAT(return_time, '%Y%m') AS time, | ||
| 43 | - IFNULL(SUM(price), 0) AS currentMonthCount | 42 | + DATE_FORMAT(return_time, '%Y%m') AS time, |
| 43 | + IFNULL(SUM(price), 0) AS currentMonthCount | ||
| 44 | FROM crm_receivable | 44 | FROM crm_receivable |
| 45 | WHERE deleted = 0 | 45 | WHERE deleted = 0 |
| 46 | - AND audit_status = 20 | 46 | + AND audit_status = ${@cn.iocoder.yudao.module.crm.enums.common.CrmAuditStatusEnum@APPROVE.status} |
| 47 | AND owner_user_id in | 47 | AND owner_user_id in |
| 48 | - <foreach collection="userIds" item="userId" open="(" close=")" separator=","> | ||
| 49 | - #{userId} | ||
| 50 | - </foreach> | ||
| 51 | - AND (DATE_FORMAT(return_time, '%Y') = DATE_FORMAT(#{times[0],javaType=java.time.LocalDateTime},'%Y') | ||
| 52 | - or DATE_FORMAT(return_time, '%Y') = DATE_FORMAT(#{times[0],javaType=java.time.LocalDateTime},'%Y')-1) | 48 | + <foreach collection="userIds" item="userId" open="(" close=")" separator=","> |
| 49 | + #{userId} | ||
| 50 | + </foreach> | ||
| 51 | + AND return_time between #{times[0],javaType=java.time.LocalDateTime} and | ||
| 52 | + #{times[1],javaType=java.time.LocalDateTime} | ||
| 53 | GROUP BY time | 53 | GROUP BY time |
| 54 | </select> | 54 | </select> |
| 55 | 55 |