commit
af610c621f
@ -1,6 +1,7 @@
|
||||
package cn.iocoder.yudao.module.crm.service.statistics;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.date.LocalDateTimeUtil;
|
||||
import cn.hutool.core.util.ObjUtil;
|
||||
import cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.performance.CrmStatisticsPerformanceReqVO;
|
||||
import cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.performance.CrmStatisticsPerformanceRespVO;
|
||||
@ -13,8 +14,9 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
|
||||
@ -59,24 +61,87 @@ public class CrmStatisticsPerformanceServiceImpl implements CrmStatisticsPerform
|
||||
* 获得员工业绩数据
|
||||
*
|
||||
* @param performanceReqVO 参数
|
||||
* @param performanceFunction 排行榜方法
|
||||
* @return 排行版数据
|
||||
* @param performanceFunction 员工业绩统计方法
|
||||
* @return 员工业绩数据
|
||||
*/
|
||||
private List<CrmStatisticsPerformanceRespVO> getPerformance(CrmStatisticsPerformanceReqVO performanceReqVO, Function<CrmStatisticsPerformanceReqVO,
|
||||
List<CrmStatisticsPerformanceRespVO>> performanceFunction) {
|
||||
|
||||
List<CrmStatisticsPerformanceRespVO> performanceRespVOList;
|
||||
|
||||
// 1. 获得用户编号数组
|
||||
final List<Long> userIds = getUserIds(performanceReqVO);
|
||||
if (CollUtil.isEmpty(userIds)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
performanceReqVO.setUserIds(userIds);
|
||||
// 2. 获得排行数据
|
||||
// 2. 获得业绩数据
|
||||
List<CrmStatisticsPerformanceRespVO> performance = performanceFunction.apply(performanceReqVO);
|
||||
if (CollUtil.isEmpty(performance)) {
|
||||
return Collections.emptyList();
|
||||
|
||||
// 获取查询的年份
|
||||
String currentYear = LocalDateTimeUtil.format(performanceReqVO.getTimes()[0],"yyyy");
|
||||
|
||||
// 构造查询当年和前一年,每年12个月的年月组合
|
||||
List<String> 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));
|
||||
}
|
||||
}
|
||||
return performance;
|
||||
|
||||
List<CrmStatisticsPerformanceRespVO> computedList = new ArrayList<>();
|
||||
List<CrmStatisticsPerformanceRespVO> 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 (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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -5,27 +5,8 @@
|
||||
<select id="selectContractCountPerformance"
|
||||
resultType="cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.performance.CrmStatisticsPerformanceRespVO">
|
||||
SELECT
|
||||
t.time as time,
|
||||
COALESCE(t.currentMonthCount,0) as currentMonthCount,
|
||||
COALESCE(y.lastMonthCount,0) as lastMonthCount,
|
||||
COALESCE(z.lastYearCount,0) as lastYearCount
|
||||
FROM
|
||||
(SELECT
|
||||
COUNT(1) AS currentMonthCount,
|
||||
DATE_FORMAT(order_date, '%Y-%m') AS time
|
||||
FROM crm_contract
|
||||
WHERE deleted = 0
|
||||
AND audit_status = 20
|
||||
AND owner_user_id in
|
||||
<foreach collection="userIds" item="userId" open="(" close=")" separator=",">
|
||||
#{userId}
|
||||
</foreach>
|
||||
AND DATE_FORMAT(order_date, '%Y') = DATE_FORMAT(#{times[0],javaType=java.time.LocalDateTime},'%Y')
|
||||
GROUP BY time)t
|
||||
LEFT JOIN
|
||||
(SELECT
|
||||
COUNT(1) AS lastMonthCount,
|
||||
DATE_FORMAT(DATE_ADD(order_date,INTERVAL 1 MONTH), '%Y-%m') AS time
|
||||
DATE_FORMAT(order_date, '%Y%m') AS time,
|
||||
COUNT(1) AS currentMonthCount
|
||||
FROM crm_contract
|
||||
WHERE deleted = 0
|
||||
AND audit_status = 20
|
||||
@ -35,45 +16,14 @@
|
||||
</foreach>
|
||||
AND (DATE_FORMAT(order_date, '%Y') = DATE_FORMAT(#{times[0],javaType=java.time.LocalDateTime},'%Y')
|
||||
or DATE_FORMAT(order_date, '%Y') = DATE_FORMAT(#{times[0],javaType=java.time.LocalDateTime},'%Y')-1)
|
||||
GROUP BY time)y ON t.time = y.time
|
||||
LEFT JOIN
|
||||
(SELECT
|
||||
COUNT(1) AS lastYearCount,
|
||||
DATE_FORMAT(DATE_ADD(order_date,INTERVAL 1 YEAR), '%Y-%m') AS time
|
||||
FROM crm_contract
|
||||
WHERE deleted = 0
|
||||
AND audit_status = 20
|
||||
AND owner_user_id in
|
||||
<foreach collection="userIds" item="userId" open="(" close=")" separator=",">
|
||||
#{userId}
|
||||
</foreach>
|
||||
AND DATE_FORMAT(order_date, '%Y') = DATE_FORMAT(#{times[0],javaType=java.time.LocalDateTime},'%Y')-1
|
||||
GROUP BY time)z ON t.time = z.time
|
||||
GROUP BY time
|
||||
</select>
|
||||
|
||||
<select id="selectContractPricePerformance"
|
||||
resultType="cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.performance.CrmStatisticsPerformanceRespVO">
|
||||
SELECT
|
||||
t.time as time,
|
||||
COALESCE(t.currentMonthCount,0) as currentMonthCount,
|
||||
COALESCE(y.lastMonthCount,0) as lastMonthCount,
|
||||
COALESCE(z.lastYearCount,0) as lastYearCount
|
||||
FROM
|
||||
(SELECT
|
||||
IFNULL(SUM(total_price), 0) AS currentMonthCount,
|
||||
DATE_FORMAT(order_date, '%Y-%m') AS time
|
||||
FROM crm_contract
|
||||
WHERE deleted = 0
|
||||
AND audit_status = 20
|
||||
AND owner_user_id in
|
||||
<foreach collection="userIds" item="userId" open="(" close=")" separator=",">
|
||||
#{userId}
|
||||
</foreach>
|
||||
AND DATE_FORMAT(order_date, '%Y') = DATE_FORMAT(#{times[0],javaType=java.time.LocalDateTime},'%Y')
|
||||
GROUP BY time)t
|
||||
LEFT JOIN
|
||||
(SELECT
|
||||
IFNULL(SUM(total_price), 0) AS lastMonthCount,
|
||||
DATE_FORMAT(DATE_ADD(order_date,INTERVAL 1 MONTH), '%Y-%m') AS time
|
||||
DATE_FORMAT(order_date, '%Y%m') AS time,
|
||||
IFNULL(SUM(total_price), 0) AS currentMonthCount
|
||||
FROM crm_contract
|
||||
WHERE deleted = 0
|
||||
AND audit_status = 20
|
||||
@ -83,46 +33,14 @@
|
||||
</foreach>
|
||||
AND (DATE_FORMAT(order_date, '%Y') = DATE_FORMAT(#{times[0],javaType=java.time.LocalDateTime},'%Y')
|
||||
or DATE_FORMAT(order_date, '%Y') = DATE_FORMAT(#{times[0],javaType=java.time.LocalDateTime},'%Y')-1)
|
||||
GROUP BY time)y ON t.time = y.time
|
||||
LEFT JOIN
|
||||
(SELECT
|
||||
IFNULL(SUM(total_price), 0) AS lastYearCount,
|
||||
DATE_FORMAT(DATE_ADD(order_date,INTERVAL 1 YEAR), '%Y-%m') AS time
|
||||
FROM crm_contract
|
||||
WHERE deleted = 0
|
||||
AND audit_status = 20
|
||||
AND owner_user_id in
|
||||
<foreach collection="userIds" item="userId" open="(" close=")" separator=",">
|
||||
#{userId}
|
||||
</foreach>
|
||||
AND DATE_FORMAT(order_date, '%Y') = DATE_FORMAT(#{times[0],javaType=java.time.LocalDateTime},'%Y')-1
|
||||
GROUP BY time)z ON t.time = z.time
|
||||
GROUP BY time
|
||||
</select>
|
||||
|
||||
<select id="selectReceivablePricePerformance"
|
||||
resultType="cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.performance.CrmStatisticsPerformanceRespVO">
|
||||
SELECT
|
||||
t.time as time,
|
||||
COALESCE(t.currentMonthCount,0) as currentMonthCount,
|
||||
COALESCE(y.lastMonthCount,0) as lastMonthCount,
|
||||
COALESCE(z.lastYearCount,0) as lastYearCount
|
||||
FROM
|
||||
(SELECT
|
||||
IFNULL(SUM(price), 0) AS currentMonthCount,
|
||||
DATE_FORMAT(return_time, '%Y-%m') AS time
|
||||
FROM crm_receivable
|
||||
WHERE deleted = 0
|
||||
AND audit_status = 20
|
||||
AND owner_user_id in
|
||||
<foreach collection="userIds" item="userId" open="(" close=")" separator=",">
|
||||
#{userId}
|
||||
</foreach>
|
||||
AND DATE_FORMAT(return_time, '%Y') = DATE_FORMAT(#{times[0],javaType=java.time.LocalDateTime},'%Y')
|
||||
GROUP BY time)t
|
||||
LEFT JOIN
|
||||
(SELECT
|
||||
IFNULL(SUM(price), 0) AS lastMonthCount,
|
||||
DATE_FORMAT(DATE_ADD(return_time,INTERVAL 1 MONTH), '%Y-%m') AS time
|
||||
DATE_FORMAT(return_time, '%Y%m') AS time,
|
||||
IFNULL(SUM(price), 0) AS currentMonthCount
|
||||
FROM crm_receivable
|
||||
WHERE deleted = 0
|
||||
AND audit_status = 20
|
||||
@ -132,21 +50,7 @@
|
||||
</foreach>
|
||||
AND (DATE_FORMAT(return_time, '%Y') = DATE_FORMAT(#{times[0],javaType=java.time.LocalDateTime},'%Y')
|
||||
or DATE_FORMAT(return_time, '%Y') = DATE_FORMAT(#{times[0],javaType=java.time.LocalDateTime},'%Y')-1)
|
||||
GROUP BY time)y ON t.time = y.time
|
||||
LEFT JOIN
|
||||
(SELECT
|
||||
IFNULL(SUM(price), 0) AS lastYearCount,
|
||||
DATE_FORMAT(DATE_ADD(return_time,INTERVAL 1 YEAR), '%Y-%m') AS time
|
||||
FROM crm_receivable
|
||||
WHERE deleted = 0
|
||||
AND audit_status = 20
|
||||
AND owner_user_id in
|
||||
<foreach collection="userIds" item="userId" open="(" close=")" separator=",">
|
||||
#{userId}
|
||||
</foreach>
|
||||
AND DATE_FORMAT(return_time, '%Y') = DATE_FORMAT(#{times[0],javaType=java.time.LocalDateTime},'%Y')-1
|
||||
GROUP BY time)z ON t.time = z.time
|
||||
GROUP BY time
|
||||
</select>
|
||||
|
||||
|
||||
</mapper>
|
||||
|
Loading…
Reference in New Issue
Block a user