mirror of
				https://gitee.com/hhyykk/ipms-sjy.git
				synced 2025-10-31 10:18:42 +08:00 
			
		
		
		
	feat: CRM/数据统计/客户总量分析
This commit is contained in:
		| @@ -0,0 +1,19 @@ | ||||
| ### 新建客户总量分析(按日) | ||||
| 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 | ||||
| Authorization: Bearer {{token}} | ||||
| tenant-id: {{adminTenentId}} | ||||
|  | ||||
| ### 新建客户总量分析(按月) | ||||
| 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 | ||||
| Authorization: Bearer {{token}} | ||||
| tenant-id: {{adminTenentId}} | ||||
|  | ||||
| ### 成交客户总量分析(按日) | ||||
| 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 | ||||
| Authorization: Bearer {{token}} | ||||
| tenant-id: {{adminTenentId}} | ||||
|  | ||||
| ### 成交客户总量分析(按月) | ||||
| 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 | ||||
| Authorization: Bearer {{token}} | ||||
| tenant-id: {{adminTenentId}} | ||||
| @@ -0,0 +1,44 @@ | ||||
| package cn.iocoder.yudao.module.crm.controller.admin.statistics; | ||||
|  | ||||
| import cn.iocoder.yudao.framework.common.pojo.CommonResult; | ||||
| 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.module.crm.service.statistics.CrmStatisticsCustomerService; | ||||
| import io.swagger.v3.oas.annotations.Operation; | ||||
| import io.swagger.v3.oas.annotations.tags.Tag; | ||||
| import jakarta.annotation.Resource; | ||||
| import jakarta.validation.Valid; | ||||
| import org.springframework.security.access.prepost.PreAuthorize; | ||||
| import org.springframework.validation.annotation.Validated; | ||||
| import org.springframework.web.bind.annotation.GetMapping; | ||||
| import org.springframework.web.bind.annotation.RequestMapping; | ||||
| import org.springframework.web.bind.annotation.RestController; | ||||
|  | ||||
| import java.util.List; | ||||
|  | ||||
| import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; | ||||
|  | ||||
| @Tag(name = "管理后台 - CRM 数据统计 员工客户分析") | ||||
| @RestController | ||||
| @RequestMapping("/crm/statistics-customer") | ||||
| @Validated | ||||
| public class CrmStatisticsCustomerController { | ||||
|  | ||||
|     @Resource | ||||
|     private CrmStatisticsCustomerService customerService; | ||||
|  | ||||
|     @GetMapping("/get-total-customer-count") | ||||
|     @Operation(summary = "获得新建客户数量") | ||||
|     @PreAuthorize("@ss.hasPermission('crm:statistics-customer:query')") | ||||
|     public CommonResult<List<CrmStatisticsCustomerCountVO>> getTotalCustomerCount(@Valid CrmStatisticsCustomerReqVO reqVO) { | ||||
|         return success(customerService.getTotalCustomerCount(reqVO)); | ||||
|     } | ||||
|  | ||||
|     @GetMapping("/get-deal-total-customer-count") | ||||
|     @Operation(summary = "获得成交客户数量") | ||||
|     @PreAuthorize("@ss.hasPermission('crm:statistics-customer:query')") | ||||
|     public CommonResult<List<CrmStatisticsCustomerCountVO>> getDealTotalCustomerCount(@Valid CrmStatisticsCustomerReqVO reqVO) { | ||||
|         return success(customerService.getDealTotalCustomerCount(reqVO)); | ||||
|     } | ||||
|  | ||||
| } | ||||
| @@ -0,0 +1,28 @@ | ||||
| package cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.customer; | ||||
|  | ||||
| import io.swagger.v3.oas.annotations.media.Schema; | ||||
| import lombok.Data; | ||||
|  | ||||
|  | ||||
| @Schema(description = "管理后台 - CRM 数据统计 员工客户分析 VO") | ||||
| @Data | ||||
| public class CrmStatisticsCustomerCountVO { | ||||
|  | ||||
|     /** | ||||
|      * 时间轴 | ||||
|      * <p> | ||||
|      * group by DATE_FORMAT(create_date, '%Y%m') | ||||
|      */ | ||||
|     @Schema(description = "时间轴", requiredMode = Schema.RequiredMode.REQUIRED, example = "202401") | ||||
|     private String category; | ||||
|  | ||||
|     /** | ||||
|      * 数量是个特别“抽象”的概念,在不同排行下,代表不同含义 | ||||
|      * <p> | ||||
|      * 1. 金额:合同金额排行、回款金额排行 | ||||
|      * 2. 个数:签约合同排行、产品销量排行、产品销量排行、新增客户数排行、新增联系人排行、跟进次数排行、跟进客户数排行 | ||||
|      */ | ||||
|     @Schema(description = "数量", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") | ||||
|     private Integer count; | ||||
|  | ||||
| } | ||||
| @@ -0,0 +1,47 @@ | ||||
| package cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.customer; | ||||
|  | ||||
| import io.swagger.v3.oas.annotations.media.Schema; | ||||
| import jakarta.validation.constraints.NotEmpty; | ||||
| import jakarta.validation.constraints.NotNull; | ||||
| import lombok.Data; | ||||
| import org.springframework.format.annotation.DateTimeFormat; | ||||
|  | ||||
| import java.time.LocalDateTime; | ||||
| import java.util.List; | ||||
|  | ||||
| import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND; | ||||
|  | ||||
| @Schema(description = "管理后台 - CRM 数据统计 员工客户分析 Request VO") | ||||
| @Data | ||||
| public class CrmStatisticsCustomerReqVO { | ||||
|  | ||||
|     @Schema(description = "部门 id", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") | ||||
|     @NotNull(message = "部门 id 不能为空") | ||||
|     private Long deptId; | ||||
|  | ||||
|     /** | ||||
|      * 负责人用户 id, 当用户为空, 则计算部门下用户 | ||||
|      */ | ||||
|     @Schema(description = "负责人用户 id", requiredMode = Schema.RequiredMode.NOT_REQUIRED, example = "1") | ||||
|     private Long userId; | ||||
|  | ||||
|     /** | ||||
|      * userIds 目前不用前端传递,目前是方便后端通过 deptId 读取编号后,设置回来 | ||||
|      * <p> | ||||
|      * 后续,可能会支持选择部分用户进行查询 | ||||
|      */ | ||||
|     @Schema(description = "负责人用户 id 集合", requiredMode = Schema.RequiredMode.NOT_REQUIRED, example = "2") | ||||
|     private List<Long> userIds; | ||||
|  | ||||
|     @Schema(description = "时间范围", requiredMode = Schema.RequiredMode.REQUIRED) | ||||
|     @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) | ||||
|     @NotEmpty(message = "时间范围不能为空") | ||||
|     private LocalDateTime[] times; | ||||
|  | ||||
|     /** | ||||
|      * group by DATE_FORMAT(field, #{dateFormat}) | ||||
|      */ | ||||
|     @Schema(description = "Group By 日期格式", requiredMode = Schema.RequiredMode.NOT_REQUIRED, example = "%Y%m") | ||||
|     private String sqlDateFormat; | ||||
|  | ||||
| } | ||||
| @@ -0,0 +1,21 @@ | ||||
| package cn.iocoder.yudao.module.crm.dal.mysql.statistics; | ||||
|  | ||||
| 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 org.apache.ibatis.annotations.Mapper; | ||||
|  | ||||
| import java.util.List; | ||||
|  | ||||
| /** | ||||
|  * CRM 数据统计 员工客户分析 Mapper | ||||
|  * | ||||
|  * @author dhb52 | ||||
|  */ | ||||
| @Mapper | ||||
| public interface CrmStatisticsCustomerMapper { | ||||
|  | ||||
|     List<CrmStatisticsCustomerCountVO> selectCustomerCountGroupbyDate(CrmStatisticsCustomerReqVO reqVO); | ||||
|  | ||||
|     List<CrmStatisticsCustomerCountVO> selectDealCustomerCountGroupbyDate(CrmStatisticsCustomerReqVO reqVO); | ||||
|  | ||||
| } | ||||
| @@ -0,0 +1,31 @@ | ||||
| package cn.iocoder.yudao.module.crm.service.statistics; | ||||
|  | ||||
| import cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.customer.CrmStatisticsCustomerReqVO; | ||||
| import cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.customer.CrmStatisticsCustomerCountVO; | ||||
|  | ||||
| import java.util.List; | ||||
|  | ||||
| /** | ||||
|  * CRM 数据统计 员工客户分析 Service 接口 | ||||
|  * | ||||
|  * @author dhb52 | ||||
|  */ | ||||
| public interface CrmStatisticsCustomerService { | ||||
|  | ||||
|     /** | ||||
|      * 获取新建客户数量 | ||||
|      * | ||||
|      * @param reqVO 请求参数 | ||||
|      * @return 新建客户数量统计 | ||||
|      */ | ||||
|     List<CrmStatisticsCustomerCountVO> getTotalCustomerCount(CrmStatisticsCustomerReqVO reqVO); | ||||
|  | ||||
|     /** | ||||
|      * 获取成交客户数量 | ||||
|      * | ||||
|      * @param reqVO 请求参数 | ||||
|      * @return 成交客户数量统计 | ||||
|      */ | ||||
|     List<CrmStatisticsCustomerCountVO> getDealTotalCustomerCount(CrmStatisticsCustomerReqVO reqVO); | ||||
|  | ||||
| } | ||||
| @@ -0,0 +1,124 @@ | ||||
| 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.CrmStatisticsCustomerReqVO; | ||||
| import cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.customer.CrmStatisticsCustomerCountVO; | ||||
| import cn.iocoder.yudao.module.crm.dal.mysql.statistics.CrmStatisticsCustomerMapper; | ||||
| import cn.iocoder.yudao.module.system.api.dept.DeptApi; | ||||
| import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO; | ||||
| import cn.iocoder.yudao.module.system.api.user.AdminUserApi; | ||||
| import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO; | ||||
| import jakarta.annotation.Resource; | ||||
| import org.springframework.stereotype.Service; | ||||
| import org.springframework.validation.annotation.Validated; | ||||
|  | ||||
| import java.time.LocalDateTime; | ||||
| import java.util.Collections; | ||||
| import java.util.List; | ||||
| import java.util.Map; | ||||
| import java.util.function.Function; | ||||
|  | ||||
| import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.*; | ||||
|  | ||||
| /** | ||||
|  * CRM 数据统计 员工客户分析 Service 实现类 | ||||
|  * | ||||
|  * @author dhb52 | ||||
|  */ | ||||
| @Service | ||||
| @Validated | ||||
| public class CrmStatisticsCustomerServiceImpl implements CrmStatisticsCustomerService { | ||||
|  | ||||
|     @Resource | ||||
|     private CrmStatisticsCustomerMapper customerMapper; | ||||
|  | ||||
|     @Resource | ||||
|     private AdminUserApi adminUserApi; | ||||
|     @Resource | ||||
|     private DeptApi deptApi; | ||||
|  | ||||
|     @Override | ||||
|     public List<CrmStatisticsCustomerCountVO> getTotalCustomerCount(CrmStatisticsCustomerReqVO reqVO) { | ||||
|         return getStat(reqVO, customerMapper::selectCustomerCountGroupbyDate); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public List<CrmStatisticsCustomerCountVO> getDealTotalCustomerCount(CrmStatisticsCustomerReqVO reqVO) { | ||||
|         return getStat(reqVO, customerMapper::selectDealCustomerCountGroupbyDate); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 获得统计数据 | ||||
|      * | ||||
|      * @param reqVO        参数 | ||||
|      * @param statFunction 统计方法 | ||||
|      * @return 统计数据 | ||||
|      */ | ||||
|     private List<CrmStatisticsCustomerCountVO> getStat(CrmStatisticsCustomerReqVO reqVO, Function<CrmStatisticsCustomerReqVO, List<CrmStatisticsCustomerCountVO>> statFunction) { | ||||
|         // 1. 获得用户编号数组: 如果用户编号为空, 则获得部门下的用户编号数组 | ||||
|         if (ObjUtil.isNotNull(reqVO.getUserId())) { | ||||
|             reqVO.setUserIds(List.of(reqVO.getUserId())); | ||||
|         } else { | ||||
|             reqVO.setUserIds(getUserIds(reqVO.getDeptId())); | ||||
|         } | ||||
|         if (CollUtil.isEmpty(reqVO.getUserIds())) { | ||||
|             return Collections.emptyList(); | ||||
|         } | ||||
|  | ||||
|         // 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); | ||||
|  | ||||
|         // 4. 生成时间序列 | ||||
|         List<CrmStatisticsCustomerCountVO> result = CollUtil.newArrayList(); | ||||
|         while (startTime.compareTo(endTime) <= 0) { | ||||
|             final String category = LocalDateTimeUtil.format(startTime, byMonth ? "yyyyMM" : "yyyyMMdd"); | ||||
|             result.add(new CrmStatisticsCustomerCountVO().setCategory(category).setCount(0)); | ||||
|             if (byMonth) | ||||
|                 startTime = startTime.plusMonths(1); | ||||
|             else | ||||
|                 startTime = startTime.plusDays(1); | ||||
|         } | ||||
|  | ||||
|         // 5. 使用时间序列填充结果 | ||||
|         final Map<String, Integer> statMap = convertMap(stats, | ||||
|                 CrmStatisticsCustomerCountVO::getCategory, | ||||
|                 CrmStatisticsCustomerCountVO::getCount); | ||||
|         result.forEach(r -> { | ||||
|             if (statMap.containsKey(r.getCategory())) { | ||||
|                 r.setCount(statMap.get(r.getCategory())); | ||||
|             } | ||||
|         }); | ||||
|  | ||||
|         return result; | ||||
|     } | ||||
|  | ||||
|  | ||||
|     /** | ||||
|      * 获得部门下的用户编号数组,包括子部门的 | ||||
|      * | ||||
|      * @param deptId 部门编号 | ||||
|      * @return 用户编号数组 | ||||
|      */ | ||||
|     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); | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,46 @@ | ||||
| <?xml version="1.0" encoding="UTF-8"?> | ||||
| <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> | ||||
| <mapper namespace="cn.iocoder.yudao.module.crm.dal.mysql.statistics.CrmStatisticsCustomerMapper"> | ||||
|  | ||||
|  | ||||
|     <select id="selectCustomerCountGroupbyDate" | ||||
|             resultType="cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.customer.CrmStatisticsCustomerCountVO"> | ||||
|         SELECT | ||||
|             count(*) AS count, | ||||
|             DATE_FORMAT( create_time, #{sqlDateFormat,javaType=java.lang.String} ) AS category | ||||
|         FROM | ||||
|             crm_customer | ||||
|         WHERE | ||||
|             deleted = 0 | ||||
|           AND owner_user_id  IN | ||||
|                 <foreach collection="userIds" item="userId" open="(" close=")" separator=","> | ||||
|                     #{userId} | ||||
|                 </foreach> | ||||
|           AND create_time BETWEEN #{times[0],javaType=java.time.LocalDateTime} AND | ||||
|                 #{times[1],javaType=java.time.LocalDateTime} | ||||
|         GROUP BY | ||||
|             DATE_FORMAT( create_time, #{sqlDateFormat,javaType=java.lang.String} ) | ||||
|     </select> | ||||
|  | ||||
|     <select id="selectDealCustomerCountGroupbyDate" | ||||
|             resultType="cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.customer.CrmStatisticsCustomerCountVO"> | ||||
|         SELECT | ||||
|             count( DISTINCT a.id ) AS count, | ||||
|             DATE_FORMAT( b.order_date, #{sqlDateFormat,javaType=java.lang.String} ) AS category | ||||
|         FROM | ||||
|             crm_customer AS a | ||||
|                 LEFT JOIN crm_contract AS b ON b.customer_id = a.id | ||||
|         WHERE | ||||
|             a.deleted = 0 AND b.deleted = 0 | ||||
|           AND b.audit_status = 20 | ||||
|           AND a.owner_user_id IN | ||||
|                 <foreach collection="userIds" item="userId" open="(" close=")" separator=","> | ||||
|                     #{userId} | ||||
|                 </foreach> | ||||
|           AND b.create_time BETWEEN #{times[0],javaType=java.time.LocalDateTime} AND | ||||
|                 #{times[1],javaType=java.time.LocalDateTime} | ||||
|         GROUP BY | ||||
|             DATE_FORMAT( b.order_date, #{sqlDateFormat,javaType=java.lang.String} ) | ||||
|     </select> | ||||
|  | ||||
| </mapper> | ||||
		Reference in New Issue
	
	Block a user
	 dhb52
					dhb52