|
@@ -3,8 +3,9 @@ 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.customer.CrmStatisticsCustomerCountVO;
|
|
|
-import cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.customer.CrmStatisticsCustomerReqVO;
|
|
|
+import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
|
|
|
+import cn.iocoder.yudao.framework.common.util.number.NumberUtils;
|
|
|
+import cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.customer.*;
|
|
|
import cn.iocoder.yudao.module.crm.dal.mysql.statistics.CrmStatisticsCustomerMapper;
|
|
|
import cn.iocoder.yudao.module.crm.enums.common.CrmBizTypeEnum;
|
|
|
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
|
|
@@ -17,17 +18,19 @@ import jakarta.annotation.Resource;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
|
|
|
+import java.math.BigDecimal;
|
|
|
import java.time.LocalDateTime;
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.Collections;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
-import java.util.function.Function;
|
|
|
+import java.util.stream.Stream;
|
|
|
|
|
|
-import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
|
|
|
-import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
|
|
|
+import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.*;
|
|
|
+import static cn.iocoder.yudao.module.crm.enums.DictTypeConstants.*;
|
|
|
|
|
|
/**
|
|
|
- * CRM 数据统计 员工客户分析 Service 实现类
|
|
|
+ * CRM 客户分析 Service 实现类
|
|
|
*
|
|
|
* @author dhb52
|
|
|
*/
|
|
@@ -35,6 +38,13 @@ import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.
|
|
|
@Validated
|
|
|
public class CrmStatisticsCustomerServiceImpl implements CrmStatisticsCustomerService {
|
|
|
|
|
|
+ private static final String SQL_DATE_FORMAT_BY_MONTH = "%Y%m";
|
|
|
+ private static final String SQL_DATE_FORMAT_BY_DAY = "%Y%m%d";
|
|
|
+
|
|
|
+ private static final String TIME_FORMAT_BY_MONTH = "yyyyMM";
|
|
|
+ private static final String TIME_FORMAT_BY_DAY = "yyyyMMdd";
|
|
|
+
|
|
|
+
|
|
|
@Resource
|
|
|
private CrmStatisticsCustomerMapper customerMapper;
|
|
|
|
|
@@ -45,130 +55,354 @@ public class CrmStatisticsCustomerServiceImpl implements CrmStatisticsCustomerSe
|
|
|
@Resource
|
|
|
private DictDataApi dictDataApi;
|
|
|
|
|
|
+
|
|
|
@Override
|
|
|
- public List<CrmStatisticsCustomerCountVO> getTotalCustomerCount(CrmStatisticsCustomerReqVO reqVO) {
|
|
|
- return getStat(reqVO, customerMapper::selectCustomerCountGroupbyDate);
|
|
|
+ public List<CrmStatisticsCustomerSummaryByDateRespVO> getCustomerSummaryByDate(CrmStatisticsCustomerReqVO reqVO) {
|
|
|
+ // 1. 获得用户编号数组
|
|
|
+ final List<Long> userIds = getUserIds(reqVO);
|
|
|
+ if (CollUtil.isEmpty(userIds)) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ reqVO.setUserIds(userIds);
|
|
|
+
|
|
|
+ // 2. 获取分项统计数据
|
|
|
+ reqVO.setSqlDateFormat(getSqlDateFormat(reqVO.getTimes()[0], reqVO.getTimes()[1]));
|
|
|
+ final List<CrmStatisticsCustomerSummaryByDateRespVO> customerCreateCount = customerMapper.selectCustomerCreateCountGroupbyDate(reqVO);
|
|
|
+ final List<CrmStatisticsCustomerSummaryByDateRespVO> customerDealCount = customerMapper.selectCustomerDealCountGroupbyDate(reqVO);
|
|
|
+
|
|
|
+ // 3. 获取时间序列
|
|
|
+ final List<String> times = generateTimeSeries(reqVO.getTimes()[0], reqVO.getTimes()[1]);
|
|
|
+
|
|
|
+ // 4. 合并统计数据
|
|
|
+ List<CrmStatisticsCustomerSummaryByDateRespVO> respVoList = new ArrayList<>(times.size());
|
|
|
+ final Map<String, Integer> customerCreateCountMap = convertMap(customerCreateCount,
|
|
|
+ CrmStatisticsCustomerSummaryByDateRespVO::getTime,
|
|
|
+ CrmStatisticsCustomerSummaryByDateRespVO::getCustomerCreateCount);
|
|
|
+ final Map<String, Integer> customerDealCountMap = convertMap(customerDealCount,
|
|
|
+ CrmStatisticsCustomerSummaryByDateRespVO::getTime,
|
|
|
+ CrmStatisticsCustomerSummaryByDateRespVO::getCustomerDealCount);
|
|
|
+ times.forEach(time -> respVoList.add(
|
|
|
+ new CrmStatisticsCustomerSummaryByDateRespVO().setTime(time)
|
|
|
+ .setCustomerCreateCount(customerCreateCountMap.getOrDefault(time, 0))
|
|
|
+ .setCustomerDealCount(customerDealCountMap.getOrDefault(time, 0))
|
|
|
+ ));
|
|
|
+
|
|
|
+ return respVoList;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public List<CrmStatisticsCustomerCountVO> getDealTotalCustomerCount(CrmStatisticsCustomerReqVO reqVO) {
|
|
|
- return getStat(reqVO, customerMapper::selectDealCustomerCountGroupbyDate);
|
|
|
+ public List<CrmStatisticsCustomerSummaryByUserRespVO> getCustomerSummaryByUser(CrmStatisticsCustomerReqVO reqVO) {
|
|
|
+ // 1. 获得用户编号数组
|
|
|
+ final List<Long> userIds = getUserIds(reqVO);
|
|
|
+ if (CollUtil.isEmpty(userIds)) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ reqVO.setUserIds(userIds);
|
|
|
+
|
|
|
+ // 2. 获取分项统计数据
|
|
|
+ final List<CrmStatisticsCustomerSummaryByUserRespVO> customerCreateCount = customerMapper.selectCustomerCreateCountGroupbyUser(reqVO);
|
|
|
+ final List<CrmStatisticsCustomerSummaryByUserRespVO> customerDealCount = customerMapper.selectCustomerDealCountGroupbyUser(reqVO);
|
|
|
+ final List<CrmStatisticsCustomerSummaryByUserRespVO> contractPrice = customerMapper.selectContractPriceGroupbyUser(reqVO);
|
|
|
+ final List<CrmStatisticsCustomerSummaryByUserRespVO> receivablePrice = customerMapper.selectReceivablePriceGroupbyUser(reqVO);
|
|
|
+
|
|
|
+ // 3. 合并统计数据
|
|
|
+ final Map<Long, Integer> customerCreateCountMap = convertMap(customerCreateCount,
|
|
|
+ CrmStatisticsCustomerSummaryByUserRespVO::getOwnerUserId,
|
|
|
+ CrmStatisticsCustomerSummaryByUserRespVO::getCustomerCreateCount);
|
|
|
+ final Map<Long, Integer> customerDealCountMap = convertMap(customerDealCount,
|
|
|
+ CrmStatisticsCustomerSummaryByUserRespVO::getOwnerUserId,
|
|
|
+ CrmStatisticsCustomerSummaryByUserRespVO::getCustomerDealCount);
|
|
|
+ final Map<Long, BigDecimal> contractPriceMap = convertMap(contractPrice,
|
|
|
+ CrmStatisticsCustomerSummaryByUserRespVO::getOwnerUserId,
|
|
|
+ CrmStatisticsCustomerSummaryByUserRespVO::getContractPrice);
|
|
|
+ final Map<Long, BigDecimal> receivablePriceMap = convertMap(receivablePrice,
|
|
|
+ CrmStatisticsCustomerSummaryByUserRespVO::getOwnerUserId,
|
|
|
+ CrmStatisticsCustomerSummaryByUserRespVO::getReceivablePrice);
|
|
|
+ List<CrmStatisticsCustomerSummaryByUserRespVO> respVoList = new ArrayList<>(userIds.size());
|
|
|
+ userIds.forEach(userId -> {
|
|
|
+ final CrmStatisticsCustomerSummaryByUserRespVO vo = new CrmStatisticsCustomerSummaryByUserRespVO();
|
|
|
+ vo.setOwnerUserId(userId);
|
|
|
+ vo.setCustomerCreateCount(customerCreateCountMap.getOrDefault(userId, 0))
|
|
|
+ .setCustomerDealCount(customerDealCountMap.getOrDefault(userId, 0))
|
|
|
+ .setContractPrice(contractPriceMap.getOrDefault(userId, BigDecimal.ZERO))
|
|
|
+ .setReceivablePrice(receivablePriceMap.getOrDefault(userId, BigDecimal.ZERO));
|
|
|
+ respVoList.add(vo);
|
|
|
+ });
|
|
|
+
|
|
|
+ // 4. 拼接用户信息
|
|
|
+ appendUserInfo(respVoList);
|
|
|
+
|
|
|
+ return respVoList;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public List<CrmStatisticsCustomerCountVO> getRecordCount(CrmStatisticsCustomerReqVO reqVO) {
|
|
|
+ public List<CrmStatisticsFollowupSummaryByDateRespVO> getFollowupSummaryByDate(CrmStatisticsCustomerReqVO reqVO) {
|
|
|
+ // 1. 获得用户编号数组
|
|
|
+ final List<Long> userIds = getUserIds(reqVO);
|
|
|
+ if (CollUtil.isEmpty(userIds)) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ reqVO.setUserIds(userIds);
|
|
|
+
|
|
|
+ // 2. 获取分项统计数据
|
|
|
+ reqVO.setSqlDateFormat(getSqlDateFormat(reqVO.getTimes()[0], reqVO.getTimes()[1]));
|
|
|
reqVO.setBizType(CrmBizTypeEnum.CRM_CUSTOMER.getType());
|
|
|
- return getStat(reqVO, customerMapper::selectRecordCountGroupbyDate);
|
|
|
+ final List<CrmStatisticsFollowupSummaryByDateRespVO> followupRecordCount = customerMapper.selectFollowupRecordCountGroupbyDate(reqVO);
|
|
|
+ final List<CrmStatisticsFollowupSummaryByDateRespVO> followupCustomerCount = customerMapper.selectFollowupCustomerCountGroupbyDate(reqVO);
|
|
|
+
|
|
|
+ // 3. 获取时间序列
|
|
|
+ final List<String> times = generateTimeSeries(reqVO.getTimes()[0], reqVO.getTimes()[1]);
|
|
|
+
|
|
|
+ // 4. 合并统计数据
|
|
|
+ List<CrmStatisticsFollowupSummaryByDateRespVO> respVoList = new ArrayList<>(times.size());
|
|
|
+ final Map<String, Integer> followupRecordCountMap = convertMap(followupRecordCount,
|
|
|
+ CrmStatisticsFollowupSummaryByDateRespVO::getTime,
|
|
|
+ CrmStatisticsFollowupSummaryByDateRespVO::getFollowupRecordCount);
|
|
|
+ final Map<String, Integer> followupCustomerCountMap = convertMap(followupCustomerCount,
|
|
|
+ CrmStatisticsFollowupSummaryByDateRespVO::getTime,
|
|
|
+ CrmStatisticsFollowupSummaryByDateRespVO::getFollowupCustomerCount);
|
|
|
+ times.forEach(time -> respVoList.add(
|
|
|
+ new CrmStatisticsFollowupSummaryByDateRespVO().setTime(time)
|
|
|
+ .setFollowupRecordCount(followupRecordCountMap.getOrDefault(time, 0))
|
|
|
+ .setFollowupCustomerCount(followupCustomerCountMap.getOrDefault(time, 0))
|
|
|
+ ));
|
|
|
+
|
|
|
+ return respVoList;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public List<CrmStatisticsCustomerCountVO> getDistinctRecordCount(CrmStatisticsCustomerReqVO reqVO) {
|
|
|
+ public List<CrmStatisticsFollowupSummaryByUserRespVO> getFollowupSummaryByUser(CrmStatisticsCustomerReqVO reqVO) {
|
|
|
+ // 1. 获得用户编号数组
|
|
|
+ final List<Long> userIds = getUserIds(reqVO);
|
|
|
+ if (CollUtil.isEmpty(userIds)) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ reqVO.setUserIds(userIds);
|
|
|
+
|
|
|
+ // 2. 获取分项统计数据
|
|
|
reqVO.setBizType(CrmBizTypeEnum.CRM_CUSTOMER.getType());
|
|
|
- return getStat(reqVO, customerMapper::selectDistinctRecordCountGroupbyDate);
|
|
|
+ final List<CrmStatisticsFollowupSummaryByUserRespVO> followupRecordCount = customerMapper.selectFollowupRecordCountGroupbyUser(reqVO);
|
|
|
+ final List<CrmStatisticsFollowupSummaryByUserRespVO> followupCustomerCount = customerMapper.selectFollowupCustomerCountGroupbyUser(reqVO);
|
|
|
+
|
|
|
+ // 3. 合并统计数据
|
|
|
+ final Map<Long, Integer> followupRecordCountMap = convertMap(followupRecordCount,
|
|
|
+ CrmStatisticsFollowupSummaryByUserRespVO::getOwnerUserId,
|
|
|
+ CrmStatisticsFollowupSummaryByUserRespVO::getFollowupRecordCount);
|
|
|
+ final Map<Long, Integer> followupCustomerCountMap = convertMap(followupCustomerCount,
|
|
|
+ CrmStatisticsFollowupSummaryByUserRespVO::getOwnerUserId,
|
|
|
+ CrmStatisticsFollowupSummaryByUserRespVO::getFollowupCustomerCount);
|
|
|
+ List<CrmStatisticsFollowupSummaryByUserRespVO> respVoList = new ArrayList<>(userIds.size());
|
|
|
+ userIds.forEach(userId -> {
|
|
|
+ final CrmStatisticsFollowupSummaryByUserRespVO vo = new CrmStatisticsFollowupSummaryByUserRespVO()
|
|
|
+ .setFollowupRecordCount(followupRecordCountMap.getOrDefault(userId, 0))
|
|
|
+ .setFollowupCustomerCount(followupCustomerCountMap.getOrDefault(userId, 0));
|
|
|
+ vo.setOwnerUserId(userId);
|
|
|
+ respVoList.add(vo);
|
|
|
+ });
|
|
|
+
|
|
|
+ // 4. 拼接用户信息
|
|
|
+ appendUserInfo(respVoList);
|
|
|
+
|
|
|
+ return respVoList;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public List<CrmStatisticsCustomerCountVO> getRecordTypeCount(CrmStatisticsCustomerReqVO reqVO) {
|
|
|
- // 1. 获得用户编号数组: 如果用户编号为空, 则获得部门下的用户编号数组
|
|
|
- if (ObjUtil.isNotNull(reqVO.getUserId())) {
|
|
|
- reqVO.setUserIds(List.of(reqVO.getUserId()));
|
|
|
- } else {
|
|
|
- reqVO.setUserIds(getUserIds(reqVO.getDeptId()));
|
|
|
- }
|
|
|
- if (CollUtil.isEmpty(reqVO.getUserIds())) {
|
|
|
+ public List<CrmStatisticsFollowupSummaryByTypeRespVO> getFollowupSummaryByType(CrmStatisticsCustomerReqVO reqVO) {
|
|
|
+ // 1. 获得用户编号数组
|
|
|
+ final List<Long> userIds = getUserIds(reqVO);
|
|
|
+ if (CollUtil.isEmpty(userIds)) {
|
|
|
return Collections.emptyList();
|
|
|
}
|
|
|
+ reqVO.setUserIds(userIds);
|
|
|
|
|
|
// 2. 获得排行数据
|
|
|
reqVO.setBizType(CrmBizTypeEnum.CRM_CUSTOMER.getType());
|
|
|
- List<CrmStatisticsCustomerCountVO> stats = customerMapper.selectRecordCountGroupbyType(reqVO);
|
|
|
+ List<CrmStatisticsFollowupSummaryByTypeRespVO> respVoList = customerMapper.selectFollowupRecordCountGroupbyType(reqVO);
|
|
|
|
|
|
// 3. 获取字典数据
|
|
|
- List<DictDataRespDTO> followUpTypes = dictDataApi.getDictDataList("crm_follow_up_type");
|
|
|
- final Map<String, String> followUpTypeMap = convertMap(followUpTypes, DictDataRespDTO::getValue, DictDataRespDTO::getLabel);
|
|
|
- stats.forEach(stat -> {
|
|
|
- stat.setCategory(followUpTypeMap.get(stat.getCategory()));
|
|
|
+ List<DictDataRespDTO> followUpTypes = dictDataApi.getDictDataList(CRM_FOLLOW_UP_TYPE);
|
|
|
+ final Map<String, String> followUpTypeMap = convertMap(followUpTypes,
|
|
|
+ DictDataRespDTO::getValue, DictDataRespDTO::getLabel);
|
|
|
+ respVoList.forEach(vo -> {
|
|
|
+ vo.setFollowupType(followUpTypeMap.get(vo.getFollowupType()));
|
|
|
+ });
|
|
|
+
|
|
|
+ return respVoList;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<CrmStatisticsCustomerContractSummaryRespVO> getContractSummary(CrmStatisticsCustomerReqVO reqVO) {
|
|
|
+ // 1. 获得用户编号数组
|
|
|
+ final List<Long> userIds = getUserIds(reqVO);
|
|
|
+ if (CollUtil.isEmpty(userIds)) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ reqVO.setUserIds(userIds);
|
|
|
+
|
|
|
+ // 2. 获取统计数据
|
|
|
+ List<CrmStatisticsCustomerContractSummaryRespVO> respVoList = customerMapper.selectContractSummary(reqVO);
|
|
|
+
|
|
|
+ // 3. 设置 创建人、负责人、行业、来源
|
|
|
+ // 获取客户所属行业
|
|
|
+ Map<String, String> industryMap = convertMap(dictDataApi.getDictDataList(CRM_CUSTOMER_INDUSTRY),
|
|
|
+ DictDataRespDTO::getValue, DictDataRespDTO::getLabel);
|
|
|
+ // 获取客户来源
|
|
|
+ Map<String, String> sourceMap = convertMap(dictDataApi.getDictDataList(CRM_CUSTOMER_SOURCE),
|
|
|
+ DictDataRespDTO::getValue, DictDataRespDTO::getLabel);
|
|
|
+ // 获取创建人、负责人列表
|
|
|
+ Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(convertSetByFlatMap(respVoList,
|
|
|
+ vo -> Stream.of(NumberUtils.parseLong(vo.getCreatorUserId()), vo.getOwnerUserId())));
|
|
|
+
|
|
|
+ respVoList.forEach(vo -> {
|
|
|
+ MapUtils.findAndThen(industryMap, vo.getIndustryId(), vo::setIndustryName);
|
|
|
+ MapUtils.findAndThen(sourceMap, vo.getSource(), vo::setSourceName);
|
|
|
+ MapUtils.findAndThen(userMap, NumberUtils.parseLong(vo.getCreatorUserId()),
|
|
|
+ user -> vo.setCreatorUserName(user.getNickname()));
|
|
|
+ MapUtils.findAndThen(userMap, vo.getOwnerUserId(), user -> vo.setOwnerUserName(user.getNickname()));
|
|
|
});
|
|
|
|
|
|
- return stats;
|
|
|
+ return respVoList;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<CrmStatisticsCustomerDealCycleByDateRespVO> getCustomerDealCycleByDate(CrmStatisticsCustomerReqVO reqVO) {
|
|
|
+ // 1. 获得用户编号数组
|
|
|
+ final List<Long> userIds = getUserIds(reqVO);
|
|
|
+ if (CollUtil.isEmpty(userIds)) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ reqVO.setUserIds(userIds);
|
|
|
+
|
|
|
+ // 2. 获取分项统计数据
|
|
|
+ reqVO.setSqlDateFormat(getSqlDateFormat(reqVO.getTimes()[0], reqVO.getTimes()[1]));
|
|
|
+ reqVO.setBizType(CrmBizTypeEnum.CRM_CUSTOMER.getType());
|
|
|
+ final List<CrmStatisticsCustomerDealCycleByDateRespVO> customerDealCycle = customerMapper.selectCustomerDealCycleGroupbyDate(reqVO);
|
|
|
+
|
|
|
+ // 3. 获取时间序列
|
|
|
+ final List<String> times = generateTimeSeries(reqVO.getTimes()[0], reqVO.getTimes()[1]);
|
|
|
+
|
|
|
+ // 4. 合并统计数据
|
|
|
+ List<CrmStatisticsCustomerDealCycleByDateRespVO> respVoList = new ArrayList<>(times.size());
|
|
|
+ final Map<String, Double> customerDealCycleMap = convertMap(customerDealCycle,
|
|
|
+ CrmStatisticsCustomerDealCycleByDateRespVO::getTime,
|
|
|
+ CrmStatisticsCustomerDealCycleByDateRespVO::getCustomerDealCycle);
|
|
|
+ times.forEach(time -> respVoList.add(
|
|
|
+ new CrmStatisticsCustomerDealCycleByDateRespVO().setTime(time)
|
|
|
+ .setCustomerDealCycle(customerDealCycleMap.getOrDefault(time, 0D))
|
|
|
+ ));
|
|
|
+
|
|
|
+ return respVoList;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public List<CrmStatisticsCustomerCountVO> getCustomerCycle(CrmStatisticsCustomerReqVO reqVO) {
|
|
|
- return getStat(reqVO, customerMapper::selectCustomerCycleGroupbyDate);
|
|
|
+ public List<CrmStatisticsCustomerDealCycleByUserRespVO> getCustomerDealCycleByUser(CrmStatisticsCustomerReqVO reqVO) {
|
|
|
+ // 1. 获得用户编号数组
|
|
|
+ final List<Long> userIds = getUserIds(reqVO);
|
|
|
+ if (CollUtil.isEmpty(userIds)) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ reqVO.setUserIds(userIds);
|
|
|
+
|
|
|
+ // 2. 获取分项统计数据
|
|
|
+ reqVO.setBizType(CrmBizTypeEnum.CRM_CUSTOMER.getType());
|
|
|
+ final List<CrmStatisticsCustomerDealCycleByUserRespVO> customerDealCycle = customerMapper.selectCustomerDealCycleGroupbyUser(reqVO);
|
|
|
+ final List<CrmStatisticsCustomerSummaryByUserRespVO> customerDealCount = customerMapper.selectCustomerDealCountGroupbyUser(reqVO);
|
|
|
+
|
|
|
+ // 3. 合并统计数据
|
|
|
+ final Map<Long, Double> customerDealCycleMap = convertMap(customerDealCycle,
|
|
|
+ CrmStatisticsCustomerDealCycleByUserRespVO::getOwnerUserId,
|
|
|
+ CrmStatisticsCustomerDealCycleByUserRespVO::getCustomerDealCycle);
|
|
|
+ final Map<Long, Integer> customerDealCountMap = convertMap(customerDealCount,
|
|
|
+ CrmStatisticsCustomerSummaryByUserRespVO::getOwnerUserId,
|
|
|
+ CrmStatisticsCustomerSummaryByUserRespVO::getCustomerDealCount);
|
|
|
+ List<CrmStatisticsCustomerDealCycleByUserRespVO> respVoList = new ArrayList<>(userIds.size());
|
|
|
+ userIds.forEach(userId -> {
|
|
|
+ final CrmStatisticsCustomerDealCycleByUserRespVO vo = new CrmStatisticsCustomerDealCycleByUserRespVO()
|
|
|
+ .setCustomerDealCycle(customerDealCycleMap.getOrDefault(userId, 0.0))
|
|
|
+ .setCustomerDealCount(customerDealCountMap.getOrDefault(userId, 0));
|
|
|
+ vo.setOwnerUserId(userId);
|
|
|
+ respVoList.add(vo);
|
|
|
+ });
|
|
|
+
|
|
|
+ // 4. 拼接用户信息
|
|
|
+ appendUserInfo(respVoList);
|
|
|
+
|
|
|
+ return respVoList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 拼接用户信息(昵称)
|
|
|
+ *
|
|
|
+ * @param respVoList 统计数据
|
|
|
+ */
|
|
|
+ private <T extends CrmStatisticsCustomerByUserBaseRespVO> void appendUserInfo(List<T> respVoList) {
|
|
|
+ Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(convertSet(respVoList,
|
|
|
+ CrmStatisticsCustomerByUserBaseRespVO::getOwnerUserId));
|
|
|
+ respVoList.forEach(vo -> MapUtils.findAndThen(userMap,
|
|
|
+ vo.getOwnerUserId(), user -> vo.setOwnerUserName(user.getNickname())));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 获得统计数据
|
|
|
+ * 获取用户编号数组。如果用户编号为空, 则获得部门下的用户编号数组,包括子部门的所有用户编号
|
|
|
*
|
|
|
- * @param reqVO 参数
|
|
|
- * @param statFunction 统计方法
|
|
|
- * @return 统计数据
|
|
|
+ * @param reqVO 请求参数
|
|
|
+ * @return 用户编号数组
|
|
|
*/
|
|
|
- private List<CrmStatisticsCustomerCountVO> getStat(CrmStatisticsCustomerReqVO reqVO, Function<CrmStatisticsCustomerReqVO, List<CrmStatisticsCustomerCountVO>> statFunction) {
|
|
|
- // 1. 获得用户编号数组: 如果用户编号为空, 则获得部门下的用户编号数组
|
|
|
+ private List<Long> getUserIds(CrmStatisticsCustomerReqVO reqVO) {
|
|
|
if (ObjUtil.isNotNull(reqVO.getUserId())) {
|
|
|
- reqVO.setUserIds(List.of(reqVO.getUserId()));
|
|
|
+ return List.of(reqVO.getUserId());
|
|
|
} else {
|
|
|
- reqVO.setUserIds(getUserIds(reqVO.getDeptId()));
|
|
|
- }
|
|
|
- if (CollUtil.isEmpty(reqVO.getUserIds())) {
|
|
|
- return Collections.emptyList();
|
|
|
+ // 1. 获得部门列表
|
|
|
+ final Long deptId = reqVO.getDeptId();
|
|
|
+ List<Long> deptIds = convertList(deptApi.getChildDeptList(deptId), DeptRespDTO::getId);
|
|
|
+ deptIds.add(deptId);
|
|
|
+ // 2. 获得用户编号
|
|
|
+ return convertList(adminUserApi.getUserListByDeptIds(deptIds), AdminUserRespDTO::getId);
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- // 2. 生成日期格式
|
|
|
- LocalDateTime startTime = reqVO.getTimes()[0];
|
|
|
- final LocalDateTime endTime = reqVO.getTimes()[1];
|
|
|
- final long days = LocalDateTimeUtil.between(startTime, endTime).toDays();
|
|
|
- boolean byMonth = days > 31;
|
|
|
- if (byMonth) {
|
|
|
- // 按月
|
|
|
- reqVO.setSqlDateFormat("%Y%m");
|
|
|
- } else {
|
|
|
- // 按日
|
|
|
- reqVO.setSqlDateFormat("%Y%m%d");
|
|
|
- }
|
|
|
|
|
|
- // 3. 获得排行数据
|
|
|
- List<CrmStatisticsCustomerCountVO> stats = statFunction.apply(reqVO);
|
|
|
+ /**
|
|
|
+ * 判断是否按照 月粒度 统计
|
|
|
+ *
|
|
|
+ * @param startTime 开始时间
|
|
|
+ * @param endTime 结束时间
|
|
|
+ * @return 是, 按月粒度, 否则按天粒度统计。
|
|
|
+ */
|
|
|
+ private boolean queryByMonth(LocalDateTime startTime, LocalDateTime endTime) {
|
|
|
+ return LocalDateTimeUtil.between(startTime, endTime).toDays() > 31;
|
|
|
+ }
|
|
|
|
|
|
- // 4. 生成时间序列
|
|
|
- List<CrmStatisticsCustomerCountVO> result = CollUtil.newArrayList();
|
|
|
+ /**
|
|
|
+ * 生成时间序列
|
|
|
+ *
|
|
|
+ * @param startTime 开始时间
|
|
|
+ * @param endTime 结束时间
|
|
|
+ * @return 时间序列
|
|
|
+ */
|
|
|
+ private List<String> generateTimeSeries(LocalDateTime startTime, LocalDateTime endTime) {
|
|
|
+ boolean byMonth = queryByMonth(startTime, endTime);
|
|
|
+ List<String> times = CollUtil.newArrayList();
|
|
|
while (!startTime.isAfter(endTime)) {
|
|
|
- final String category = LocalDateTimeUtil.format(startTime, byMonth ? "yyyyMM" : "yyyyMMdd");
|
|
|
- result.add(new CrmStatisticsCustomerCountVO().setCategory(category));
|
|
|
+ times.add(LocalDateTimeUtil.format(startTime, byMonth ? TIME_FORMAT_BY_MONTH : TIME_FORMAT_BY_DAY));
|
|
|
if (byMonth)
|
|
|
startTime = startTime.plusMonths(1);
|
|
|
else
|
|
|
startTime = startTime.plusDays(1);
|
|
|
}
|
|
|
|
|
|
- // 5. 使用时间序列填充结果
|
|
|
- final Map<String, CrmStatisticsCustomerCountVO> statMap = convertMap(stats,
|
|
|
- CrmStatisticsCustomerCountVO::getCategory,
|
|
|
- Function.identity());
|
|
|
- result.forEach(r -> {
|
|
|
- if (statMap.containsKey(r.getCategory())) {
|
|
|
- r.setCount(statMap.get(r.getCategory()).getCount())
|
|
|
- .setCycle(statMap.get(r.getCategory()).getCycle());
|
|
|
- }
|
|
|
- });
|
|
|
-
|
|
|
- return result;
|
|
|
+ return times;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
- * 获得部门下的用户编号数组,包括子部门的
|
|
|
+ * 获取 SQL 查询 GROUP BY 的时间格式
|
|
|
*
|
|
|
- * @param deptId 部门编号
|
|
|
- * @return 用户编号数组
|
|
|
+ * @param startTime 开始时间
|
|
|
+ * @param endTime 结束时间
|
|
|
+ * @return SQL 查询 GROUP BY 的时间格式
|
|
|
*/
|
|
|
- public List<Long> getUserIds(Long deptId) {
|
|
|
- // 1. 获得部门列表
|
|
|
- List<Long> deptIds = convertList(deptApi.getChildDeptList(deptId), DeptRespDTO::getId);
|
|
|
- deptIds.add(deptId);
|
|
|
- // 2. 获得用户编号
|
|
|
- return convertList(adminUserApi.getUserListByDeptIds(deptIds), AdminUserRespDTO::getId);
|
|
|
+ private String getSqlDateFormat(LocalDateTime startTime, LocalDateTime endTime) {
|
|
|
+ return queryByMonth(startTime, endTime) ? SQL_DATE_FORMAT_BY_MONTH : SQL_DATE_FORMAT_BY_DAY;
|
|
|
}
|
|
|
+
|
|
|
}
|