Commit 7d120a2f3699d402c6f1d1424c57bee8fb27a78d
1 parent
b285548b
feat: CRM/数据统计/客户总量分析
Showing
8 changed files
with
360 additions
and
0 deletions
yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/statistics/CrmStatisticsCustomerController.http
0 → 100644
| 1 | +### 新建客户总量分析(按日) | ||
| 2 | +GET {{baseUrl}}/crm/statistics-customer/get-total-customer-count?deptId=100×[0]=2024-12-01 00:00:00×[1]=2024-12-12 23:59:59 | ||
| 3 | +Authorization: Bearer {{token}} | ||
| 4 | +tenant-id: {{adminTenentId}} | ||
| 5 | + | ||
| 6 | +### 新建客户总量分析(按月) | ||
| 7 | +GET {{baseUrl}}/crm/statistics-customer/get-total-customer-count?deptId=100×[0]=2023-01-01 00:00:00×[1]=2024-12-12 23:59:59 | ||
| 8 | +Authorization: Bearer {{token}} | ||
| 9 | +tenant-id: {{adminTenentId}} | ||
| 10 | + | ||
| 11 | +### 成交客户总量分析(按日) | ||
| 12 | +GET {{baseUrl}}/crm/statistics-customer/get-deal-total-customer-count?deptId=100×[0]=2024-12-01 00:00:00×[1]=2024-12-12 23:59:59 | ||
| 13 | +Authorization: Bearer {{token}} | ||
| 14 | +tenant-id: {{adminTenentId}} | ||
| 15 | + | ||
| 16 | +### 成交客户总量分析(按月) | ||
| 17 | +GET {{baseUrl}}/crm/statistics-customer/get-deal-total-customer-count?deptId=100×[0]=2023-01-01 00:00:00×[1]=2024-12-12 23:59:59 | ||
| 18 | +Authorization: Bearer {{token}} | ||
| 19 | +tenant-id: {{adminTenentId}} | ||
| 0 | \ No newline at end of file | 20 | \ No newline at end of file |
yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/statistics/CrmStatisticsCustomerController.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.crm.controller.admin.statistics; | ||
| 2 | + | ||
| 3 | +import cn.iocoder.yudao.framework.common.pojo.CommonResult; | ||
| 4 | +import cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.customer.CrmStatisticsCustomerCountVO; | ||
| 5 | +import cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.customer.CrmStatisticsCustomerReqVO; | ||
| 6 | +import cn.iocoder.yudao.module.crm.service.statistics.CrmStatisticsCustomerService; | ||
| 7 | +import io.swagger.v3.oas.annotations.Operation; | ||
| 8 | +import io.swagger.v3.oas.annotations.tags.Tag; | ||
| 9 | +import jakarta.annotation.Resource; | ||
| 10 | +import jakarta.validation.Valid; | ||
| 11 | +import org.springframework.security.access.prepost.PreAuthorize; | ||
| 12 | +import org.springframework.validation.annotation.Validated; | ||
| 13 | +import org.springframework.web.bind.annotation.GetMapping; | ||
| 14 | +import org.springframework.web.bind.annotation.RequestMapping; | ||
| 15 | +import org.springframework.web.bind.annotation.RestController; | ||
| 16 | + | ||
| 17 | +import java.util.List; | ||
| 18 | + | ||
| 19 | +import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; | ||
| 20 | + | ||
| 21 | +@Tag(name = "管理后台 - CRM 数据统计 员工客户分析") | ||
| 22 | +@RestController | ||
| 23 | +@RequestMapping("/crm/statistics-customer") | ||
| 24 | +@Validated | ||
| 25 | +public class CrmStatisticsCustomerController { | ||
| 26 | + | ||
| 27 | + @Resource | ||
| 28 | + private CrmStatisticsCustomerService customerService; | ||
| 29 | + | ||
| 30 | + @GetMapping("/get-total-customer-count") | ||
| 31 | + @Operation(summary = "获得新建客户数量") | ||
| 32 | + @PreAuthorize("@ss.hasPermission('crm:statistics-customer:query')") | ||
| 33 | + public CommonResult<List<CrmStatisticsCustomerCountVO>> getTotalCustomerCount(@Valid CrmStatisticsCustomerReqVO reqVO) { | ||
| 34 | + return success(customerService.getTotalCustomerCount(reqVO)); | ||
| 35 | + } | ||
| 36 | + | ||
| 37 | + @GetMapping("/get-deal-total-customer-count") | ||
| 38 | + @Operation(summary = "获得成交客户数量") | ||
| 39 | + @PreAuthorize("@ss.hasPermission('crm:statistics-customer:query')") | ||
| 40 | + public CommonResult<List<CrmStatisticsCustomerCountVO>> getDealTotalCustomerCount(@Valid CrmStatisticsCustomerReqVO reqVO) { | ||
| 41 | + return success(customerService.getDealTotalCustomerCount(reqVO)); | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | +} |
yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/statistics/vo/customer/CrmStatisticsCustomerCountVO.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.customer; | ||
| 2 | + | ||
| 3 | +import io.swagger.v3.oas.annotations.media.Schema; | ||
| 4 | +import lombok.Data; | ||
| 5 | + | ||
| 6 | + | ||
| 7 | +@Schema(description = "管理后台 - CRM 数据统计 员工客户分析 VO") | ||
| 8 | +@Data | ||
| 9 | +public class CrmStatisticsCustomerCountVO { | ||
| 10 | + | ||
| 11 | + /** | ||
| 12 | + * 时间轴 | ||
| 13 | + * <p> | ||
| 14 | + * group by DATE_FORMAT(create_date, '%Y%m') | ||
| 15 | + */ | ||
| 16 | + @Schema(description = "时间轴", requiredMode = Schema.RequiredMode.REQUIRED, example = "202401") | ||
| 17 | + private String category; | ||
| 18 | + | ||
| 19 | + /** | ||
| 20 | + * 数量是个特别“抽象”的概念,在不同排行下,代表不同含义 | ||
| 21 | + * <p> | ||
| 22 | + * 1. 金额:合同金额排行、回款金额排行 | ||
| 23 | + * 2. 个数:签约合同排行、产品销量排行、产品销量排行、新增客户数排行、新增联系人排行、跟进次数排行、跟进客户数排行 | ||
| 24 | + */ | ||
| 25 | + @Schema(description = "数量", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") | ||
| 26 | + private Integer count; | ||
| 27 | + | ||
| 28 | +} |
yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/statistics/vo/customer/CrmStatisticsCustomerReqVO.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.customer; | ||
| 2 | + | ||
| 3 | +import io.swagger.v3.oas.annotations.media.Schema; | ||
| 4 | +import jakarta.validation.constraints.NotEmpty; | ||
| 5 | +import jakarta.validation.constraints.NotNull; | ||
| 6 | +import lombok.Data; | ||
| 7 | +import org.springframework.format.annotation.DateTimeFormat; | ||
| 8 | + | ||
| 9 | +import java.time.LocalDateTime; | ||
| 10 | +import java.util.List; | ||
| 11 | + | ||
| 12 | +import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND; | ||
| 13 | + | ||
| 14 | +@Schema(description = "管理后台 - CRM 数据统计 员工客户分析 Request VO") | ||
| 15 | +@Data | ||
| 16 | +public class CrmStatisticsCustomerReqVO { | ||
| 17 | + | ||
| 18 | + @Schema(description = "部门 id", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") | ||
| 19 | + @NotNull(message = "部门 id 不能为空") | ||
| 20 | + private Long deptId; | ||
| 21 | + | ||
| 22 | + /** | ||
| 23 | + * 负责人用户 id, 当用户为空, 则计算部门下用户 | ||
| 24 | + */ | ||
| 25 | + @Schema(description = "负责人用户 id", requiredMode = Schema.RequiredMode.NOT_REQUIRED, example = "1") | ||
| 26 | + private Long userId; | ||
| 27 | + | ||
| 28 | + /** | ||
| 29 | + * userIds 目前不用前端传递,目前是方便后端通过 deptId 读取编号后,设置回来 | ||
| 30 | + * <p> | ||
| 31 | + * 后续,可能会支持选择部分用户进行查询 | ||
| 32 | + */ | ||
| 33 | + @Schema(description = "负责人用户 id 集合", requiredMode = Schema.RequiredMode.NOT_REQUIRED, example = "2") | ||
| 34 | + private List<Long> userIds; | ||
| 35 | + | ||
| 36 | + @Schema(description = "时间范围", requiredMode = Schema.RequiredMode.REQUIRED) | ||
| 37 | + @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) | ||
| 38 | + @NotEmpty(message = "时间范围不能为空") | ||
| 39 | + private LocalDateTime[] times; | ||
| 40 | + | ||
| 41 | + /** | ||
| 42 | + * group by DATE_FORMAT(field, #{dateFormat}) | ||
| 43 | + */ | ||
| 44 | + @Schema(description = "Group By 日期格式", requiredMode = Schema.RequiredMode.NOT_REQUIRED, example = "%Y%m") | ||
| 45 | + private String sqlDateFormat; | ||
| 46 | + | ||
| 47 | +} |
yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/dal/mysql/statistics/CrmStatisticsCustomerMapper.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.crm.dal.mysql.statistics; | ||
| 2 | + | ||
| 3 | +import cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.customer.CrmStatisticsCustomerCountVO; | ||
| 4 | +import cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.customer.CrmStatisticsCustomerReqVO; | ||
| 5 | +import org.apache.ibatis.annotations.Mapper; | ||
| 6 | + | ||
| 7 | +import java.util.List; | ||
| 8 | + | ||
| 9 | +/** | ||
| 10 | + * CRM 数据统计 员工客户分析 Mapper | ||
| 11 | + * | ||
| 12 | + * @author dhb52 | ||
| 13 | + */ | ||
| 14 | +@Mapper | ||
| 15 | +public interface CrmStatisticsCustomerMapper { | ||
| 16 | + | ||
| 17 | + List<CrmStatisticsCustomerCountVO> selectCustomerCountGroupbyDate(CrmStatisticsCustomerReqVO reqVO); | ||
| 18 | + | ||
| 19 | + List<CrmStatisticsCustomerCountVO> selectDealCustomerCountGroupbyDate(CrmStatisticsCustomerReqVO reqVO); | ||
| 20 | + | ||
| 21 | +} |
yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/statistics/CrmStatisticsCustomerService.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.crm.service.statistics; | ||
| 2 | + | ||
| 3 | +import cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.customer.CrmStatisticsCustomerReqVO; | ||
| 4 | +import cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.customer.CrmStatisticsCustomerCountVO; | ||
| 5 | + | ||
| 6 | +import java.util.List; | ||
| 7 | + | ||
| 8 | +/** | ||
| 9 | + * CRM 数据统计 员工客户分析 Service 接口 | ||
| 10 | + * | ||
| 11 | + * @author dhb52 | ||
| 12 | + */ | ||
| 13 | +public interface CrmStatisticsCustomerService { | ||
| 14 | + | ||
| 15 | + /** | ||
| 16 | + * 获取新建客户数量 | ||
| 17 | + * | ||
| 18 | + * @param reqVO 请求参数 | ||
| 19 | + * @return 新建客户数量统计 | ||
| 20 | + */ | ||
| 21 | + List<CrmStatisticsCustomerCountVO> getTotalCustomerCount(CrmStatisticsCustomerReqVO reqVO); | ||
| 22 | + | ||
| 23 | + /** | ||
| 24 | + * 获取成交客户数量 | ||
| 25 | + * | ||
| 26 | + * @param reqVO 请求参数 | ||
| 27 | + * @return 成交客户数量统计 | ||
| 28 | + */ | ||
| 29 | + List<CrmStatisticsCustomerCountVO> getDealTotalCustomerCount(CrmStatisticsCustomerReqVO reqVO); | ||
| 30 | + | ||
| 31 | +} |
yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/statistics/CrmStatisticsCustomerServiceImpl.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.crm.service.statistics; | ||
| 2 | + | ||
| 3 | +import cn.hutool.core.collection.CollUtil; | ||
| 4 | +import cn.hutool.core.date.LocalDateTimeUtil; | ||
| 5 | +import cn.hutool.core.util.ObjUtil; | ||
| 6 | +import cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.customer.CrmStatisticsCustomerReqVO; | ||
| 7 | +import cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.customer.CrmStatisticsCustomerCountVO; | ||
| 8 | +import cn.iocoder.yudao.module.crm.dal.mysql.statistics.CrmStatisticsCustomerMapper; | ||
| 9 | +import cn.iocoder.yudao.module.system.api.dept.DeptApi; | ||
| 10 | +import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO; | ||
| 11 | +import cn.iocoder.yudao.module.system.api.user.AdminUserApi; | ||
| 12 | +import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO; | ||
| 13 | +import jakarta.annotation.Resource; | ||
| 14 | +import org.springframework.stereotype.Service; | ||
| 15 | +import org.springframework.validation.annotation.Validated; | ||
| 16 | + | ||
| 17 | +import java.time.LocalDateTime; | ||
| 18 | +import java.util.Collections; | ||
| 19 | +import java.util.List; | ||
| 20 | +import java.util.Map; | ||
| 21 | +import java.util.function.Function; | ||
| 22 | + | ||
| 23 | +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.*; | ||
| 24 | + | ||
| 25 | +/** | ||
| 26 | + * CRM 数据统计 员工客户分析 Service 实现类 | ||
| 27 | + * | ||
| 28 | + * @author dhb52 | ||
| 29 | + */ | ||
| 30 | +@Service | ||
| 31 | +@Validated | ||
| 32 | +public class CrmStatisticsCustomerServiceImpl implements CrmStatisticsCustomerService { | ||
| 33 | + | ||
| 34 | + @Resource | ||
| 35 | + private CrmStatisticsCustomerMapper customerMapper; | ||
| 36 | + | ||
| 37 | + @Resource | ||
| 38 | + private AdminUserApi adminUserApi; | ||
| 39 | + @Resource | ||
| 40 | + private DeptApi deptApi; | ||
| 41 | + | ||
| 42 | + @Override | ||
| 43 | + public List<CrmStatisticsCustomerCountVO> getTotalCustomerCount(CrmStatisticsCustomerReqVO reqVO) { | ||
| 44 | + return getStat(reqVO, customerMapper::selectCustomerCountGroupbyDate); | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | + @Override | ||
| 48 | + public List<CrmStatisticsCustomerCountVO> getDealTotalCustomerCount(CrmStatisticsCustomerReqVO reqVO) { | ||
| 49 | + return getStat(reqVO, customerMapper::selectDealCustomerCountGroupbyDate); | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + /** | ||
| 53 | + * 获得统计数据 | ||
| 54 | + * | ||
| 55 | + * @param reqVO 参数 | ||
| 56 | + * @param statFunction 统计方法 | ||
| 57 | + * @return 统计数据 | ||
| 58 | + */ | ||
| 59 | + private List<CrmStatisticsCustomerCountVO> getStat(CrmStatisticsCustomerReqVO reqVO, Function<CrmStatisticsCustomerReqVO, List<CrmStatisticsCustomerCountVO>> statFunction) { | ||
| 60 | + // 1. 获得用户编号数组: 如果用户编号为空, 则获得部门下的用户编号数组 | ||
| 61 | + if (ObjUtil.isNotNull(reqVO.getUserId())) { | ||
| 62 | + reqVO.setUserIds(List.of(reqVO.getUserId())); | ||
| 63 | + } else { | ||
| 64 | + reqVO.setUserIds(getUserIds(reqVO.getDeptId())); | ||
| 65 | + } | ||
| 66 | + if (CollUtil.isEmpty(reqVO.getUserIds())) { | ||
| 67 | + return Collections.emptyList(); | ||
| 68 | + } | ||
| 69 | + | ||
| 70 | + // 2. 生成日期格式 | ||
| 71 | + LocalDateTime startTime = reqVO.getTimes()[0]; | ||
| 72 | + final LocalDateTime endTime = reqVO.getTimes()[1]; | ||
| 73 | + final long days = LocalDateTimeUtil.between(startTime, endTime).toDays(); | ||
| 74 | + boolean byMonth = days > 31; | ||
| 75 | + if (byMonth) { | ||
| 76 | + // 按月 | ||
| 77 | + reqVO.setSqlDateFormat("%Y%m"); | ||
| 78 | + } else { | ||
| 79 | + // 按日 | ||
| 80 | + reqVO.setSqlDateFormat("%Y%m%d"); | ||
| 81 | + } | ||
| 82 | + | ||
| 83 | + // 3. 获得排行数据 | ||
| 84 | + List<CrmStatisticsCustomerCountVO> stats = statFunction.apply(reqVO); | ||
| 85 | + | ||
| 86 | + // 4. 生成时间序列 | ||
| 87 | + List<CrmStatisticsCustomerCountVO> result = CollUtil.newArrayList(); | ||
| 88 | + while (startTime.compareTo(endTime) <= 0) { | ||
| 89 | + final String category = LocalDateTimeUtil.format(startTime, byMonth ? "yyyyMM" : "yyyyMMdd"); | ||
| 90 | + result.add(new CrmStatisticsCustomerCountVO().setCategory(category).setCount(0)); | ||
| 91 | + if (byMonth) | ||
| 92 | + startTime = startTime.plusMonths(1); | ||
| 93 | + else | ||
| 94 | + startTime = startTime.plusDays(1); | ||
| 95 | + } | ||
| 96 | + | ||
| 97 | + // 5. 使用时间序列填充结果 | ||
| 98 | + final Map<String, Integer> statMap = convertMap(stats, | ||
| 99 | + CrmStatisticsCustomerCountVO::getCategory, | ||
| 100 | + CrmStatisticsCustomerCountVO::getCount); | ||
| 101 | + result.forEach(r -> { | ||
| 102 | + if (statMap.containsKey(r.getCategory())) { | ||
| 103 | + r.setCount(statMap.get(r.getCategory())); | ||
| 104 | + } | ||
| 105 | + }); | ||
| 106 | + | ||
| 107 | + return result; | ||
| 108 | + } | ||
| 109 | + | ||
| 110 | + | ||
| 111 | + /** | ||
| 112 | + * 获得部门下的用户编号数组,包括子部门的 | ||
| 113 | + * | ||
| 114 | + * @param deptId 部门编号 | ||
| 115 | + * @return 用户编号数组 | ||
| 116 | + */ | ||
| 117 | + public List<Long> getUserIds(Long deptId) { | ||
| 118 | + // 1. 获得部门列表 | ||
| 119 | + List<Long> deptIds = convertList(deptApi.getChildDeptList(deptId), DeptRespDTO::getId); | ||
| 120 | + deptIds.add(deptId); | ||
| 121 | + // 2. 获得用户编号 | ||
| 122 | + return convertList(adminUserApi.getUserListByDeptIds(deptIds), AdminUserRespDTO::getId); | ||
| 123 | + } | ||
| 124 | +} |
yudao-module-crm/yudao-module-crm-biz/src/main/resources/mapper/statistics/CrmStatisticsCustomerMapper.xml
0 → 100644
| 1 | +<?xml version="1.0" encoding="UTF-8"?> | ||
| 2 | +<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> | ||
| 3 | +<mapper namespace="cn.iocoder.yudao.module.crm.dal.mysql.statistics.CrmStatisticsCustomerMapper"> | ||
| 4 | + | ||
| 5 | + | ||
| 6 | + <select id="selectCustomerCountGroupbyDate" | ||
| 7 | + resultType="cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.customer.CrmStatisticsCustomerCountVO"> | ||
| 8 | + SELECT | ||
| 9 | + count(*) AS count, | ||
| 10 | + DATE_FORMAT( create_time, #{sqlDateFormat,javaType=java.lang.String} ) AS category | ||
| 11 | + FROM | ||
| 12 | + crm_customer | ||
| 13 | + WHERE | ||
| 14 | + deleted = 0 | ||
| 15 | + AND owner_user_id IN | ||
| 16 | + <foreach collection="userIds" item="userId" open="(" close=")" separator=","> | ||
| 17 | + #{userId} | ||
| 18 | + </foreach> | ||
| 19 | + AND create_time BETWEEN #{times[0],javaType=java.time.LocalDateTime} AND | ||
| 20 | + #{times[1],javaType=java.time.LocalDateTime} | ||
| 21 | + GROUP BY | ||
| 22 | + DATE_FORMAT( create_time, #{sqlDateFormat,javaType=java.lang.String} ) | ||
| 23 | + </select> | ||
| 24 | + | ||
| 25 | + <select id="selectDealCustomerCountGroupbyDate" | ||
| 26 | + resultType="cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.customer.CrmStatisticsCustomerCountVO"> | ||
| 27 | + SELECT | ||
| 28 | + count( DISTINCT a.id ) AS count, | ||
| 29 | + DATE_FORMAT( b.order_date, #{sqlDateFormat,javaType=java.lang.String} ) AS category | ||
| 30 | + FROM | ||
| 31 | + crm_customer AS a | ||
| 32 | + LEFT JOIN crm_contract AS b ON b.customer_id = a.id | ||
| 33 | + WHERE | ||
| 34 | + a.deleted = 0 AND b.deleted = 0 | ||
| 35 | + AND b.audit_status = 20 | ||
| 36 | + AND a.owner_user_id IN | ||
| 37 | + <foreach collection="userIds" item="userId" open="(" close=")" separator=","> | ||
| 38 | + #{userId} | ||
| 39 | + </foreach> | ||
| 40 | + AND b.create_time BETWEEN #{times[0],javaType=java.time.LocalDateTime} AND | ||
| 41 | + #{times[1],javaType=java.time.LocalDateTime} | ||
| 42 | + GROUP BY | ||
| 43 | + DATE_FORMAT( b.order_date, #{sqlDateFormat,javaType=java.lang.String} ) | ||
| 44 | + </select> | ||
| 45 | + | ||
| 46 | +</mapper> |