Commit 9e6474a123fd6fec6be055002ca309abe2a012f2
1 parent
705a3e53
1,优化mapper.xml的sql语句;
2,对于无销售数据的月份,插入销售结果为0的数据;
Showing
2 changed files
with
83 additions
and
114 deletions
yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/statistics/CrmStatisticsPerformanceServiceImpl.java
| 1 | package cn.iocoder.yudao.module.crm.service.statistics; | 1 | 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.date.LocalDateTimeUtil; | ||
| 4 | import cn.hutool.core.util.ObjUtil; | 5 | import cn.hutool.core.util.ObjUtil; |
| 5 | import cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.performance.CrmStatisticsPerformanceReqVO; | 6 | import cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.performance.CrmStatisticsPerformanceReqVO; |
| 6 | import cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.performance.CrmStatisticsPerformanceRespVO; | 7 | import cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.performance.CrmStatisticsPerformanceRespVO; |
| @@ -13,8 +14,9 @@ import org.springframework.stereotype.Service; | @@ -13,8 +14,9 @@ import org.springframework.stereotype.Service; | ||
| 13 | import org.springframework.validation.annotation.Validated; | 14 | import org.springframework.validation.annotation.Validated; |
| 14 | 15 | ||
| 15 | import jakarta.annotation.Resource; | 16 | import jakarta.annotation.Resource; |
| 16 | -import java.util.Collections; | ||
| 17 | -import java.util.List; | 17 | + |
| 18 | +import java.math.BigDecimal; | ||
| 19 | +import java.util.*; | ||
| 18 | import java.util.function.Function; | 20 | import java.util.function.Function; |
| 19 | 21 | ||
| 20 | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; | 22 | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; |
| @@ -56,24 +58,87 @@ public class CrmStatisticsPerformanceServiceImpl implements CrmStatisticsPerform | @@ -56,24 +58,87 @@ public class CrmStatisticsPerformanceServiceImpl implements CrmStatisticsPerform | ||
| 56 | * 获得员工业绩数据 | 58 | * 获得员工业绩数据 |
| 57 | * | 59 | * |
| 58 | * @param performanceReqVO 参数 | 60 | * @param performanceReqVO 参数 |
| 59 | - * @param performanceFunction 排行榜方法 | ||
| 60 | - * @return 排行版数据 | 61 | + * @param performanceFunction 员工业绩统计方法 |
| 62 | + * @return 员工业绩数据 | ||
| 61 | */ | 63 | */ |
| 62 | private List<CrmStatisticsPerformanceRespVO> getPerformance(CrmStatisticsPerformanceReqVO performanceReqVO, Function<CrmStatisticsPerformanceReqVO, | 64 | private List<CrmStatisticsPerformanceRespVO> getPerformance(CrmStatisticsPerformanceReqVO performanceReqVO, Function<CrmStatisticsPerformanceReqVO, |
| 63 | List<CrmStatisticsPerformanceRespVO>> performanceFunction) { | 65 | List<CrmStatisticsPerformanceRespVO>> performanceFunction) { |
| 64 | 66 | ||
| 67 | + List<CrmStatisticsPerformanceRespVO> performanceRespVOList; | ||
| 68 | + | ||
| 65 | // 1. 获得用户编号数组 | 69 | // 1. 获得用户编号数组 |
| 66 | final List<Long> userIds = getUserIds(performanceReqVO); | 70 | final List<Long> userIds = getUserIds(performanceReqVO); |
| 67 | if (CollUtil.isEmpty(userIds)) { | 71 | if (CollUtil.isEmpty(userIds)) { |
| 68 | return Collections.emptyList(); | 72 | return Collections.emptyList(); |
| 69 | } | 73 | } |
| 70 | performanceReqVO.setUserIds(userIds); | 74 | performanceReqVO.setUserIds(userIds); |
| 71 | - // 2. 获得排行数据 | 75 | + // 2. 获得业绩数据 |
| 72 | List<CrmStatisticsPerformanceRespVO> performance = performanceFunction.apply(performanceReqVO); | 76 | List<CrmStatisticsPerformanceRespVO> performance = performanceFunction.apply(performanceReqVO); |
| 73 | - if (CollUtil.isEmpty(performance)) { | ||
| 74 | - return Collections.emptyList(); | 77 | + |
| 78 | + // 获取查询的年份 | ||
| 79 | + String currentYear = LocalDateTimeUtil.format(performanceReqVO.getTimes()[0],"yyyy"); | ||
| 80 | + | ||
| 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 | + } | ||
| 88 | + | ||
| 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 | + } | ||
| 110 | + } | ||
| 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);//给前端只需要返回查询当年的数据,不需要前一年数据 | ||
| 139 | + } | ||
| 75 | } | 140 | } |
| 76 | - return performance; | 141 | + return respVOList; |
| 77 | } | 142 | } |
| 78 | 143 | ||
| 79 | /** | 144 | /** |
yudao-module-crm/yudao-module-crm-biz/src/main/resources/mapper/statistics/CrmStatisticsPerformanceMapper.xml
| @@ -5,27 +5,8 @@ | @@ -5,27 +5,8 @@ | ||
| 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 | - t.time as time, | ||
| 9 | - COALESCE(t.currentMonthCount,0) as currentMonthCount, | ||
| 10 | - COALESCE(y.lastMonthCount,0) as lastMonthCount, | ||
| 11 | - COALESCE(z.lastYearCount,0) as lastYearCount | ||
| 12 | - FROM | ||
| 13 | - (SELECT | ||
| 14 | - COUNT(1) AS currentMonthCount, | ||
| 15 | - DATE_FORMAT(order_date, '%Y-%m') AS time | ||
| 16 | - FROM crm_contract | ||
| 17 | - WHERE deleted = 0 | ||
| 18 | - AND audit_status = 20 | ||
| 19 | - AND owner_user_id in | ||
| 20 | - <foreach collection="userIds" item="userId" open="(" close=")" separator=","> | ||
| 21 | - #{userId} | ||
| 22 | - </foreach> | ||
| 23 | - AND DATE_FORMAT(order_date, '%Y') = DATE_FORMAT(#{times[0],javaType=java.time.LocalDateTime},'%Y') | ||
| 24 | - GROUP BY time)t | ||
| 25 | - LEFT JOIN | ||
| 26 | - (SELECT | ||
| 27 | - COUNT(1) AS lastMonthCount, | ||
| 28 | - DATE_FORMAT(DATE_ADD(order_date,INTERVAL 1 MONTH), '%Y-%m') AS time | 8 | + DATE_FORMAT(order_date, '%Y%m') AS time, |
| 9 | + COUNT(1) AS currentMonthCount | ||
| 29 | FROM crm_contract | 10 | FROM crm_contract |
| 30 | WHERE deleted = 0 | 11 | WHERE deleted = 0 |
| 31 | AND audit_status = 20 | 12 | AND audit_status = 20 |
| @@ -35,45 +16,14 @@ | @@ -35,45 +16,14 @@ | ||
| 35 | </foreach> | 16 | </foreach> |
| 36 | AND (DATE_FORMAT(order_date, '%Y') = DATE_FORMAT(#{times[0],javaType=java.time.LocalDateTime},'%Y') | 17 | AND (DATE_FORMAT(order_date, '%Y') = DATE_FORMAT(#{times[0],javaType=java.time.LocalDateTime},'%Y') |
| 37 | or DATE_FORMAT(order_date, '%Y') = DATE_FORMAT(#{times[0],javaType=java.time.LocalDateTime},'%Y')-1) | 18 | or DATE_FORMAT(order_date, '%Y') = DATE_FORMAT(#{times[0],javaType=java.time.LocalDateTime},'%Y')-1) |
| 38 | - GROUP BY time)y ON t.time = y.time | ||
| 39 | - LEFT JOIN | ||
| 40 | - (SELECT | ||
| 41 | - COUNT(1) AS lastYearCount, | ||
| 42 | - DATE_FORMAT(DATE_ADD(order_date,INTERVAL 1 YEAR), '%Y-%m') AS time | ||
| 43 | - FROM crm_contract | ||
| 44 | - WHERE deleted = 0 | ||
| 45 | - AND audit_status = 20 | ||
| 46 | - AND owner_user_id in | ||
| 47 | - <foreach collection="userIds" item="userId" open="(" close=")" separator=","> | ||
| 48 | - #{userId} | ||
| 49 | - </foreach> | ||
| 50 | - AND DATE_FORMAT(order_date, '%Y') = DATE_FORMAT(#{times[0],javaType=java.time.LocalDateTime},'%Y')-1 | ||
| 51 | - GROUP BY time)z ON t.time = z.time | 19 | + GROUP BY time |
| 52 | </select> | 20 | </select> |
| 21 | + | ||
| 53 | <select id="selectContractPricePerformance" | 22 | <select id="selectContractPricePerformance" |
| 54 | 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"> |
| 55 | SELECT | 24 | SELECT |
| 56 | - t.time as time, | ||
| 57 | - COALESCE(t.currentMonthCount,0) as currentMonthCount, | ||
| 58 | - COALESCE(y.lastMonthCount,0) as lastMonthCount, | ||
| 59 | - COALESCE(z.lastYearCount,0) as lastYearCount | ||
| 60 | - FROM | ||
| 61 | - (SELECT | ||
| 62 | - IFNULL(SUM(total_price), 0) AS currentMonthCount, | ||
| 63 | - DATE_FORMAT(order_date, '%Y-%m') AS time | ||
| 64 | - FROM crm_contract | ||
| 65 | - WHERE deleted = 0 | ||
| 66 | - AND audit_status = 20 | ||
| 67 | - AND owner_user_id in | ||
| 68 | - <foreach collection="userIds" item="userId" open="(" close=")" separator=","> | ||
| 69 | - #{userId} | ||
| 70 | - </foreach> | ||
| 71 | - AND DATE_FORMAT(order_date, '%Y') = DATE_FORMAT(#{times[0],javaType=java.time.LocalDateTime},'%Y') | ||
| 72 | - GROUP BY time)t | ||
| 73 | - LEFT JOIN | ||
| 74 | - (SELECT | ||
| 75 | - IFNULL(SUM(total_price), 0) AS lastMonthCount, | ||
| 76 | - DATE_FORMAT(DATE_ADD(order_date,INTERVAL 1 MONTH), '%Y-%m') AS time | 25 | + DATE_FORMAT(order_date, '%Y%m') AS time, |
| 26 | + IFNULL(SUM(total_price), 0) AS currentMonthCount | ||
| 77 | FROM crm_contract | 27 | FROM crm_contract |
| 78 | WHERE deleted = 0 | 28 | WHERE deleted = 0 |
| 79 | AND audit_status = 20 | 29 | AND audit_status = 20 |
| @@ -83,46 +33,14 @@ | @@ -83,46 +33,14 @@ | ||
| 83 | </foreach> | 33 | </foreach> |
| 84 | AND (DATE_FORMAT(order_date, '%Y') = DATE_FORMAT(#{times[0],javaType=java.time.LocalDateTime},'%Y') | 34 | AND (DATE_FORMAT(order_date, '%Y') = DATE_FORMAT(#{times[0],javaType=java.time.LocalDateTime},'%Y') |
| 85 | or DATE_FORMAT(order_date, '%Y') = DATE_FORMAT(#{times[0],javaType=java.time.LocalDateTime},'%Y')-1) | 35 | or DATE_FORMAT(order_date, '%Y') = DATE_FORMAT(#{times[0],javaType=java.time.LocalDateTime},'%Y')-1) |
| 86 | - GROUP BY time)y ON t.time = y.time | ||
| 87 | - LEFT JOIN | ||
| 88 | - (SELECT | ||
| 89 | - IFNULL(SUM(total_price), 0) AS lastYearCount, | ||
| 90 | - DATE_FORMAT(DATE_ADD(order_date,INTERVAL 1 YEAR), '%Y-%m') AS time | ||
| 91 | - FROM crm_contract | ||
| 92 | - WHERE deleted = 0 | ||
| 93 | - AND audit_status = 20 | ||
| 94 | - AND owner_user_id in | ||
| 95 | - <foreach collection="userIds" item="userId" open="(" close=")" separator=","> | ||
| 96 | - #{userId} | ||
| 97 | - </foreach> | ||
| 98 | - AND DATE_FORMAT(order_date, '%Y') = DATE_FORMAT(#{times[0],javaType=java.time.LocalDateTime},'%Y')-1 | ||
| 99 | - GROUP BY time)z ON t.time = z.time | 36 | + GROUP BY time |
| 100 | </select> | 37 | </select> |
| 101 | 38 | ||
| 102 | <select id="selectReceivablePricePerformance" | 39 | <select id="selectReceivablePricePerformance" |
| 103 | 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"> |
| 104 | SELECT | 41 | SELECT |
| 105 | - t.time as time, | ||
| 106 | - COALESCE(t.currentMonthCount,0) as currentMonthCount, | ||
| 107 | - COALESCE(y.lastMonthCount,0) as lastMonthCount, | ||
| 108 | - COALESCE(z.lastYearCount,0) as lastYearCount | ||
| 109 | - FROM | ||
| 110 | - (SELECT | ||
| 111 | - IFNULL(SUM(price), 0) AS currentMonthCount, | ||
| 112 | - DATE_FORMAT(return_time, '%Y-%m') AS time | ||
| 113 | - FROM crm_receivable | ||
| 114 | - WHERE deleted = 0 | ||
| 115 | - AND audit_status = 20 | ||
| 116 | - AND owner_user_id in | ||
| 117 | - <foreach collection="userIds" item="userId" open="(" close=")" separator=","> | ||
| 118 | - #{userId} | ||
| 119 | - </foreach> | ||
| 120 | - AND DATE_FORMAT(return_time, '%Y') = DATE_FORMAT(#{times[0],javaType=java.time.LocalDateTime},'%Y') | ||
| 121 | - GROUP BY time)t | ||
| 122 | - LEFT JOIN | ||
| 123 | - (SELECT | ||
| 124 | - IFNULL(SUM(price), 0) AS lastMonthCount, | ||
| 125 | - DATE_FORMAT(DATE_ADD(return_time,INTERVAL 1 MONTH), '%Y-%m') AS time | 42 | + DATE_FORMAT(return_time, '%Y%m') AS time, |
| 43 | + IFNULL(SUM(price), 0) AS currentMonthCount | ||
| 126 | FROM crm_receivable | 44 | FROM crm_receivable |
| 127 | WHERE deleted = 0 | 45 | WHERE deleted = 0 |
| 128 | AND audit_status = 20 | 46 | AND audit_status = 20 |
| @@ -132,21 +50,7 @@ | @@ -132,21 +50,7 @@ | ||
| 132 | </foreach> | 50 | </foreach> |
| 133 | AND (DATE_FORMAT(return_time, '%Y') = DATE_FORMAT(#{times[0],javaType=java.time.LocalDateTime},'%Y') | 51 | AND (DATE_FORMAT(return_time, '%Y') = DATE_FORMAT(#{times[0],javaType=java.time.LocalDateTime},'%Y') |
| 134 | or DATE_FORMAT(return_time, '%Y') = DATE_FORMAT(#{times[0],javaType=java.time.LocalDateTime},'%Y')-1) | 52 | or DATE_FORMAT(return_time, '%Y') = DATE_FORMAT(#{times[0],javaType=java.time.LocalDateTime},'%Y')-1) |
| 135 | - GROUP BY time)y ON t.time = y.time | ||
| 136 | - LEFT JOIN | ||
| 137 | - (SELECT | ||
| 138 | - IFNULL(SUM(price), 0) AS lastYearCount, | ||
| 139 | - DATE_FORMAT(DATE_ADD(return_time,INTERVAL 1 YEAR), '%Y-%m') AS time | ||
| 140 | - FROM crm_receivable | ||
| 141 | - WHERE deleted = 0 | ||
| 142 | - AND audit_status = 20 | ||
| 143 | - AND owner_user_id in | ||
| 144 | - <foreach collection="userIds" item="userId" open="(" close=")" separator=","> | ||
| 145 | - #{userId} | ||
| 146 | - </foreach> | ||
| 147 | - AND DATE_FORMAT(return_time, '%Y') = DATE_FORMAT(#{times[0],javaType=java.time.LocalDateTime},'%Y')-1 | ||
| 148 | - GROUP BY time)z ON t.time = z.time | 53 | + GROUP BY time |
| 149 | </select> | 54 | </select> |
| 150 | 55 | ||
| 151 | - | ||
| 152 | </mapper> | 56 | </mapper> |