feat(customer-lead): 实现完整客资管理系统及全民推荐功能

- 新增客资管理系统数据库变更脚本,扩展客资表及新增派单、推荐关系等多张表
- 实现客资派单、跟进、统计、导出等核心业务逻辑,支持多管理员配置
- 开发Java后端实体、参数、Mapper和服务,实现完整业务流程接口
- 提供客资管理相关REST API,涵盖分页查询、详情、状态更新、派单、跟进和统计等
- 新增全民推荐模块,支持匿名及注册用户报备推荐客户,并提供推荐记录管理
- 开发推荐人相关API接口,支持推荐码生成与查询,推荐确认及结算功能
- Vue后台新增客资管理页面,实现客资列表、派单、跟进、详情查看等功能
- 微信小程序端新增推荐客户页面,支持推荐记录展示和推荐状态跟踪
- 完善数据字典和部署说明,涵盖状态说明、来源类型和跟进方式
- 提出后续优化建议,包括权限细化、数据看板、消息通知以及推荐海报功能等
This commit is contained in:
2026-04-14 11:57:09 +08:00
parent ad23922a7c
commit db5ca691d7
22 changed files with 2400 additions and 0 deletions

View File

@@ -0,0 +1,132 @@
package com.gxwebsoft.cms.controller;
import com.gxwebsoft.cms.param.CustomerLeadParam;
import com.gxwebsoft.cms.param.LeadDispatchParam;
import com.gxwebsoft.cms.param.LeadFollowParam;
import com.gxwebsoft.cms.service.CustomerLeadService;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
/**
* 客资管理控制器
*
* @author 科技小王子
* @since 2026-04-14
*/
@Slf4j
@RestController
@RequestMapping("/customer/lead")
@Tag(name = "客资管理", description = "客资管理相关接口")
public class CustomerLeadController extends BaseController {
@Autowired
private CustomerLeadService customerLeadService;
@Operation(summary = "分页查询客资列表")
@GetMapping("/page")
@PreAuthorize("@pms.hasPermission('customer:lead:page') or @pms.hasPermission('customer:lead:view')")
public ApiResult<Map<String, Object>> getLeadPage(CustomerLeadParam param) {
return success(customerLeadService.getLeadPage(param));
}
@Operation(summary = "获取客资详情")
@GetMapping("/detail/{leadId}")
@PreAuthorize("@pms.hasPermission('customer:lead:detail') or @pms.hasPermission('customer:lead:view')")
public ApiResult<?> getLeadDetail(
@Parameter(description = "客资ID") @PathVariable Integer leadId) {
return success(customerLeadService.getLeadDetail(leadId));
}
@Operation(summary = "创建客资(管理员录入)")
@PostMapping("/create")
@PreAuthorize("@pms.hasPermission('customer:lead:create')")
public ApiResult<?> createLead(@RequestBody CustomerLeadParam param) {
return success(customerLeadService.createLead(param));
}
@Operation(summary = "更新客资信息")
@PutMapping("/update")
@PreAuthorize("@pms.hasPermission('customer:lead:update')")
public ApiResult<?> updateLead(@RequestBody CustomerLeadParam param) {
return success(customerLeadService.updateLead(param));
}
@Operation(summary = "更新客资状态")
@PutMapping("/status/{leadId}")
@PreAuthorize("@pms.hasPermission('customer:lead:status')")
public ApiResult<?> updateLeadStatus(
@Parameter(description = "客资ID") @PathVariable Integer leadId,
@Parameter(description = "状态") @RequestParam Integer status,
@Parameter(description = "备注") @RequestParam(required = false) String remarks) {
return success(customerLeadService.updateLeadStatus(leadId, status, remarks));
}
@Operation(summary = "派单给业务员")
@PostMapping("/dispatch")
@PreAuthorize("@pms.hasPermission('customer:lead:dispatch')")
public ApiResult<?> dispatchLead(@RequestBody LeadDispatchParam param) {
return success(customerLeadService.dispatchLead(param));
}
@Operation(summary = "批量派单")
@PostMapping("/dispatch/batch")
@PreAuthorize("@pms.hasPermission('customer:lead:dispatch')")
public ApiResult<?> batchDispatchLeads(@RequestBody LeadDispatchParam param) {
return success(customerLeadService.batchDispatchLeads(param));
}
@Operation(summary = "添加跟进记录")
@PostMapping("/follow")
@PreAuthorize("@pms.hasPermission('customer:lead:follow')")
public ApiResult<?> addFollowLog(@RequestBody LeadFollowParam param) {
return success(customerLeadService.addFollowLog(param));
}
@Operation(summary = "获取跟进历史")
@GetMapping("/follow/history/{leadId}")
@PreAuthorize("@pms.hasPermission('customer:lead:view')")
public ApiResult<?> getFollowHistory(
@Parameter(description = "客资ID") @PathVariable Integer leadId) {
return success(customerLeadService.getFollowHistory(leadId));
}
@Operation(summary = "获取统计数据")
@GetMapping("/statistics")
@PreAuthorize("@pms.hasPermission('customer:lead:statistics')")
public ApiResult<?> getStatistics(CustomerLeadParam param) {
return success(customerLeadService.getStatistics(param));
}
@Operation(summary = "导出客资数据")
@GetMapping("/export")
@PreAuthorize("@pms.hasPermission('customer:lead:export')")
public ApiResult<?> exportLeads(CustomerLeadParam param) {
List<Map<String, Object>> data = customerLeadService.exportLeads(param);
return success(data);
}
@Operation(summary = "获取未分配客资")
@GetMapping("/unassigned")
@PreAuthorize("@pms.hasPermission('customer:lead:view')")
public ApiResult<?> getUnassignedLeads() {
return success(customerLeadService.getUnassignedLeads());
}
@Operation(summary = "获取业务员客资统计")
@GetMapping("/salesman/stats/{salesmanId}")
@PreAuthorize("@pms.hasPermission('customer:lead:view')")
public ApiResult<?> getSalesmanLeadStats(
@Parameter(description = "业务员ID") @PathVariable Integer salesmanId) {
return success(customerLeadService.getSalesmanLeadStats(salesmanId));
}
}

View File

@@ -0,0 +1,106 @@
package com.gxwebsoft.cms.controller;
import com.gxwebsoft.cms.param.CustomerLeadParam;
import com.gxwebsoft.cms.param.LeadReferralParam;
import com.gxwebsoft.cms.service.LeadReferralService;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
/**
* 推荐人控制器(全民推荐)
*
* @author 科技小王子
* @since 2026-04-14
*/
@Slf4j
@RestController
@RequestMapping("/lead/referral")
@Tag(name = "全民推荐", description = "推荐人报备相关接口")
public class LeadReferralController extends BaseController {
@Autowired
private LeadReferralService leadReferralService;
@Operation(summary = "匿名用户报备客户")
@PostMapping("/anonymous")
public ApiResult<?> anonymousReferral(@RequestBody LeadReferralParam param) {
return success(leadReferralService.anonymousReferral(param));
}
@Operation(summary = "注册用户报备客户")
@PostMapping("/user")
@PreAuthorize("@pms.hasPermission('referral:user') or true")
public ApiResult<?> userReferral(@RequestBody LeadReferralParam param) {
return success(leadReferralService.userReferral(param));
}
@Operation(summary = "获取推荐人的推荐记录")
@GetMapping("/page")
@PreAuthorize("@pms.hasPermission('referral:page') or @pms.hasPermission('referral:view')")
public ApiResult<Map<String, Object>> getReferralPage(CustomerLeadParam param) {
return success(leadReferralService.getReferralPage(param));
}
@Operation(summary = "获取推荐人统计")
@GetMapping("/stats/{userId}")
@PreAuthorize("@pms.hasPermission('referral:view')")
public ApiResult<?> getReferralStats(
@Parameter(description = "用户ID") @PathVariable Integer userId) {
return success(leadReferralService.getReferralStats(userId));
}
@Operation(summary = "生成推荐码")
@GetMapping("/generateCode")
@PreAuthorize("@pms.hasPermission('referral:generate')")
public ApiResult<?> generateReferralCode() {
return success(leadReferralService.generateReferralCode());
}
@Operation(summary = "根据推荐码获取推荐人信息")
@GetMapping("/referrer/{code}")
public ApiResult<?> getReferrerByCode(
@Parameter(description = "推荐码") @PathVariable String code) {
return success(leadReferralService.getReferrerByCode(code));
}
@Operation(summary = "确认推荐有效(管理员)")
@PutMapping("/confirm/{referralId}")
@PreAuthorize("@pms.hasPermission('referral:confirm')")
public ApiResult<?> confirmReferral(
@Parameter(description = "推荐ID") @PathVariable Integer referralId) {
return success(leadReferralService.confirmReferral(referralId));
}
@Operation(summary = "作废推荐(管理员)")
@PutMapping("/invalidate/{referralId}")
@PreAuthorize("@pms.hasPermission('referral:invalidate')")
public ApiResult<?> invalidateReferral(
@Parameter(description = "推荐ID") @PathVariable Integer referralId,
@Parameter(description = "原因") @RequestParam(required = false) String reason) {
return success(leadReferralService.invalidateReferral(referralId, reason));
}
@Operation(summary = "结算推荐费(管理员)")
@PutMapping("/settle/{referralId}")
@PreAuthorize("@pms.hasPermission('referral:settle')")
public ApiResult<?> settleReferral(
@Parameter(description = "推荐ID") @PathVariable Integer referralId) {
return success(leadReferralService.settleReferral(referralId));
}
@Operation(summary = "批量结算推荐费")
@PutMapping("/settle/batch")
@PreAuthorize("@pms.hasPermission('referral:settle')")
public ApiResult<?> batchSettleReferrals(@RequestBody Integer[] referralIds) {
return success(leadReferralService.batchSettleReferrals(referralIds));
}
}

View File

@@ -0,0 +1,112 @@
package com.gxwebsoft.cms.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
/**
* 客资管理扩展实体
* 在CmsContactLead基础上扩展派单、推荐人等字段
*
* @author 科技小王子
* @since 2026-04-14
*/
@Data
@EqualsAndHashCode(callSuper = true)
@TableName("cms_contact_lead")
@Schema(name = "CustomerLeadEntity", description = "客资管理扩展实体")
public class CustomerLeadEntity extends CmsContactLead implements Serializable {
private static final long serialVersionUID = 1L;
@Schema(description = "被分配的业务员用户ID")
private Integer assignedUserId;
@Schema(description = "推荐人用户ID")
private Integer referrerUserId;
@Schema(description = "推荐费金额")
private BigDecimal referralFee;
@Schema(description = "推荐费是否已支付 0否 1是")
private Integer referralFeePaid;
@Schema(description = "推荐人分成比例%")
private BigDecimal referrerShare;
@Schema(description = "派单时间")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime dispatchTime;
@Schema(description = "派单管理员ID")
private Integer dispatchAdminId;
@Schema(description = "跟进次数")
private Integer followCount;
@Schema(description = "最后跟进时间")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime lastFollowTime;
@Schema(description = "预约时间")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime appointmentTime;
@Schema(description = "成交金额")
private BigDecimal dealAmount;
@Schema(description = "成交时间")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime dealTime;
@Schema(description = "来源类型: form表单 website网站 miniapp小程序 referral推荐人 admin录入")
private String sourceType;
// ========== 关联用户信息(非数据库字段)==========
@Schema(description = "被分配的业务员昵称")
private String assignedUserName;
@Schema(description = "被分配的业务员真实姓名")
private String assignedRealName;
@Schema(description = "被分配的业务员电话")
private String assignedUserPhone;
@Schema(description = "推荐人昵称")
private String referrerName;
@Schema(description = "推荐人电话")
private String referrerPhone;
@Schema(description = "派单管理员昵称")
private String dispatchAdminName;
@Schema(description = "业务员跟进次数统计")
private Integer userLeadCount;
@Schema(description = "业务员本月成交数")
private Integer userDealCount;
// ========== 来源类型常量 ==========
public static final String SOURCE_FORM = "form"; // 表单
public static final String SOURCE_WEBSITE = "website"; // 网站
public static final String SOURCE_MINIAPP = "miniapp"; // 小程序
public static final String SOURCE_REFERRAL = "referral"; // 推荐人
public static final String SOURCE_ADMIN = "admin"; // 管理员录入
// ========== 状态常量 ==========
public static final int STATUS_PENDING = 0; // 待跟进
public static final int STATUS_FOLLOWING = 1; // 跟进中
public static final int STATUS_DEALED = 2; // 已成交
public static final int STATUS_INVALID = 3; // 无效
}

View File

@@ -0,0 +1,69 @@
package com.gxwebsoft.cms.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* 客资派单记录实体
*
* @author 科技小王子
* @since 2026-04-14
*/
@Data
@TableName("lead_dispatch")
@Schema(name = "LeadDispatch", description = "客资派单记录")
public class LeadDispatch implements Serializable {
private static final long serialVersionUID = 1L;
@Schema(description = "派单ID")
@TableId(type = IdType.AUTO)
private Integer dispatchId;
@Schema(description = "客资ID")
private Integer leadId;
@Schema(description = "原分配用户ID")
private Integer fromUserId;
@Schema(description = "新分配用户ID(业务员)")
private Integer toUserId;
@Schema(description = "执行派单的管理员ID")
private Integer adminId;
@Schema(description = "派单备注")
private String dispatchRemarks;
@Schema(description = "派单类型: 1新分配 2重新分配 3抢单")
private Integer dispatchType;
@Schema(description = "派单时间")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime createTime;
// ========== 关联信息(非数据库字段)==========
@Schema(description = "业务员昵称")
private String toUserName;
@Schema(description = "管理员昵称")
private String adminName;
@Schema(description = "客资客户姓名")
private String leadCustomerName;
@Schema(description = "客资客户电话")
private String leadCustomerPhone;
// ========== 派单类型常量 ==========
public static final int TYPE_NEW = 1; // 新分配
public static final int TYPE_REDISPATCH = 2; // 重新分配
public static final int TYPE_GRAB = 3; // 抢单
}

View File

@@ -0,0 +1,84 @@
package com.gxwebsoft.cms.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* 客资跟进记录实体
*
* @author 科技小王子
* @since 2026-04-14
*/
@Data
@TableName("lead_follow_log")
@Schema(name = "LeadFollowLog", description = "客资跟进记录")
public class LeadFollowLog implements Serializable {
private static final long serialVersionUID = 1L;
@Schema(description = "跟进ID")
@TableId(type = IdType.AUTO)
private Integer followId;
@Schema(description = "客资ID")
private Integer leadId;
@Schema(description = "跟进人ID")
private Integer userId;
@Schema(description = "跟进方式: 1电话 2微信 3上门 4短信 5其他")
private Integer followType;
@Schema(description = "跟进内容")
private String followContent;
@Schema(description = "下次跟进时间")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime nextFollowTime;
@Schema(description = "下次跟进计划")
private String nextFollowPlan;
@Schema(description = "附件URLs(JSON数组)")
private String attachmentUrls;
@Schema(description = "创建时间")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime createTime;
// ========== 关联信息(非数据库字段)==========
@Schema(description = "跟进人昵称")
private String userName;
@Schema(description = "客资客户姓名")
private String customerName;
@Schema(description = "客资客户电话")
private String customerPhone;
// ========== 跟进方式常量 ==========
public static final int TYPE_PHONE = 1; // 电话
public static final int TYPE_WECHAT = 2; // 微信
public static final int TYPE_VISIT = 3; // 上门
public static final int TYPE_SMS = 4; // 短信
public static final int TYPE_OTHER = 5; // 其他
public String getFollowTypeText() {
if (this.followType == null) return "";
return switch (this.followType) {
case TYPE_PHONE -> "电话";
case TYPE_WECHAT -> "微信";
case TYPE_VISIT -> "上门";
case TYPE_SMS -> "短信";
case TYPE_OTHER -> "其他";
default -> "";
};
}
}

View File

@@ -0,0 +1,89 @@
package com.gxwebsoft.cms.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
/**
* 推荐人关系实体(全民推荐)
*
* @author 科技小王子
* @since 2026-04-14
*/
@Data
@TableName("lead_referral")
@Schema(name = "LeadReferral", description = "推荐人关系")
public class LeadReferral implements Serializable {
private static final long serialVersionUID = 1L;
@Schema(description = "推荐关系ID")
@TableId(type = IdType.AUTO)
private Integer referralId;
@Schema(description = "推荐人用户ID")
private Integer referrerUserId;
@Schema(description = "被推荐的客资ID")
private Integer referredLeadId;
@Schema(description = "推荐码")
private String referralCode;
@Schema(description = "推荐费")
private BigDecimal referralFee;
@Schema(description = "推荐状态: 0待确认 1有效 2无效 3已结算")
private Integer referralStatus;
@Schema(description = "客户姓名")
private String customerName;
@Schema(description = "客户电话")
private String customerPhone;
@Schema(description = "结算时间")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime settlementTime;
@Schema(description = "创建时间")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime createTime;
// ========== 关联信息(非数据库字段)==========
@Schema(description = "推荐人昵称")
private String referrerName;
@Schema(description = "推荐人电话")
private String referrerPhone;
@Schema(description = "客资状态")
private Integer leadStatus;
@Schema(description = "客资成交金额")
private BigDecimal dealAmount;
// ========== 状态常量 ==========
public static final int STATUS_PENDING = 0; // 待确认
public static final int STATUS_VALID = 1; // 有效
public static final int STATUS_INVALID = 2; // 无效
public static final int STATUS_SETTLED = 3; // 已结算
public String getStatusText() {
if (this.referralStatus == null) return "";
return switch (this.referralStatus) {
case STATUS_PENDING -> "待确认";
case STATUS_VALID -> "有效";
case STATUS_INVALID -> "无效";
case STATUS_SETTLED -> "已结算";
default -> "";
};
}
}

View File

@@ -0,0 +1,72 @@
package com.gxwebsoft.cms.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
/**
* 客资数据统计实体
*
* @author 科技小王子
* @since 2026-04-14
*/
@Data
@TableName("lead_statistics")
@Schema(name = "LeadStatistics", description = "客资数据统计")
public class LeadStatistics implements Serializable {
private static final long serialVersionUID = 1L;
@Schema(description = "统计ID")
@TableId(type = IdType.AUTO)
private Integer statId;
@Schema(description = "统计日期")
@JsonFormat(pattern = "yyyy-MM-dd")
private LocalDateTime statDate;
@Schema(description = "用户ID")
private Integer userId;
@Schema(description = "角色类型")
private String roleType;
@Schema(description = "总客资数")
private Integer totalLeads;
@Schema(description = "新增客资数")
private Integer newLeads;
@Schema(description = "已分配客资数")
private Integer assignedLeads;
@Schema(description = "已跟进客资数")
private Integer followedLeads;
@Schema(description = "已成交客资数")
private Integer dealedLeads;
@Schema(description = "成交总金额")
private BigDecimal dealAmount;
@Schema(description = "推荐成功数")
private Integer referralCount;
@Schema(description = "推荐费总额")
private BigDecimal referralFee;
@Schema(description = "创建时间")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime createTime;
// ========== 关联信息(非数据库字段)==========
@Schema(description = "用户昵称")
private String userName;
}

View File

@@ -0,0 +1,78 @@
package com.gxwebsoft.cms.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gxwebsoft.cms.entity.CustomerLeadEntity;
import com.gxwebsoft.cms.param.CustomerLeadParam;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
/**
* 客资管理Mapper
*
* @author 科技小王子
* @since 2026-04-14
*/
@Mapper
public interface CustomerLeadMapper extends BaseMapper<CustomerLeadEntity> {
/**
* 分页查询客资列表
*/
List<CustomerLeadEntity> selectLeadPage(CustomerLeadParam param);
/**
* 查询客资详情
*/
CustomerLeadEntity selectLeadDetail(@Param("leadId") Integer leadId);
/**
* 获取业务员待处理客资数量
*/
Integer countBySalesman(@Param("salesmanId") Integer salesmanId, @Param("status") Integer status);
/**
* 批量更新客资状态
*/
int batchUpdateStatus(@Param("leadIds") List<Integer> leadIds, @Param("status") Integer status);
/**
* 派单给业务员
*/
int dispatchToUser(@Param("leadId") Integer leadId, @Param("userId") Integer userId,
@Param("adminId") Integer adminId, @Param("remarks") String remarks);
/**
* 统计数据
*/
Map<String, Object> selectStatistics(@Param("startDate") String startDate, @Param("endDate") String endDate,
@Param("salesmanId") Integer salesmanId);
/**
* 按状态统计
*/
List<Map<String, Object>> selectStatusStatistics(@Param("startDate") String startDate, @Param("endDate") String endDate);
/**
* 按来源统计
*/
List<Map<String, Object>> selectSourceStatistics(@Param("startDate") String startDate, @Param("endDate") String endDate);
/**
* 按日统计趋势
*/
List<Map<String, Object>> selectDailyTrend(@Param("startDate") String startDate, @Param("endDate") String endDate);
/**
* 查询未分配客资
*/
List<CustomerLeadEntity> selectUnassignedLeads();
/**
* 批量派单
*/
int batchDispatch(@Param("leadIds") List<Integer> leadIds, @Param("userId") Integer userId,
@Param("adminId") Integer adminId, @Param("remarks") String remarks);
}

View File

@@ -0,0 +1,34 @@
package com.gxwebsoft.cms.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gxwebsoft.cms.entity.LeadDispatch;
import com.gxwebsoft.cms.param.CustomerLeadParam;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* 客资派单Mapper
*
* @author 科技小王子
* @since 2026-04-14
*/
@Mapper
public interface LeadDispatchMapper extends BaseMapper<LeadDispatch> {
/**
* 查询客资的派单历史
*/
List<LeadDispatch> selectDispatchHistory(@Param("leadId") Integer leadId);
/**
* 查询业务员的派单记录
*/
List<LeadDispatch> selectSalesmanDispatches(CustomerLeadParam param);
/**
* 查询今日派单数量
*/
Integer countTodayDispatches(@Param("adminId") Integer adminId);
}

View File

@@ -0,0 +1,35 @@
package com.gxwebsoft.cms.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gxwebsoft.cms.entity.LeadFollowLog;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* 客资跟进记录Mapper
*
* @author 科技小王子
* @since 2026-04-14
*/
@Mapper
public interface LeadFollowLogMapper extends BaseMapper<LeadFollowLog> {
/**
* 查询客资的跟进历史
*/
List<LeadFollowLog> selectFollowHistory(@Param("leadId") Integer leadId);
/**
* 查询用户的跟进记录
*/
List<LeadFollowLog> selectUserFollows(@Param("userId") Integer userId,
@Param("startDate") String startDate,
@Param("endDate") String endDate);
/**
* 查询需要跟进的客资(根据下次跟进时间)
*/
List<Integer> selectNeedFollowLeadIds(@Param("userId") Integer userId);
}

View File

@@ -0,0 +1,44 @@
package com.gxwebsoft.cms.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gxwebsoft.cms.entity.LeadReferral;
import com.gxwebsoft.cms.param.CustomerLeadParam;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* 推荐人关系Mapper
*
* @author 科技小王子
* @since 2026-04-14
*/
@Mapper
public interface LeadReferralMapper extends BaseMapper<LeadReferral> {
/**
* 分页查询推荐关系
*/
List<LeadReferral> selectReferralPage(CustomerLeadParam param);
/**
* 查询用户的推荐记录
*/
List<LeadReferral> selectUserReferrals(@Param("userId") Integer userId);
/**
* 根据推荐码查询
*/
LeadReferral selectByCode(@Param("referralCode") String referralCode);
/**
* 统计推荐人的推荐数量
*/
Integer countUserReferrals(@Param("userId") Integer userId, @Param("status") Integer status);
/**
* 统计推荐人待结算金额
*/
java.math.BigDecimal sumPendingFee(@Param("userId") Integer userId);
}

View File

@@ -0,0 +1,81 @@
package com.gxwebsoft.cms.param;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.io.Serializable;
import java.math.BigDecimal;
/**
* 客资管理请求参数
*
* @author 科技小王子
* @since 2026-04-14
*/
@Data
@Schema(name = "CustomerLeadParam", description = "客资管理请求参数")
public class CustomerLeadParam implements Serializable {
private static final long serialVersionUID = 1L;
@Schema(description = "客资ID")
private Integer leadId;
@Schema(description = "客户姓名")
private String name;
@Schema(description = "客户电话")
private String phone;
@Schema(description = "单位/公司名称")
private String company;
@Schema(description = "需求描述")
private String need;
@Schema(description = "分配给的用户ID")
private Integer assignedUserId;
@Schema(description = "派单备注")
private String dispatchRemarks;
@Schema(description = "跟进状态: 0待跟进 1跟进中 2已成交 3无效")
private Integer status;
@Schema(description = "销售备注")
private String remarks;
@Schema(description = "预约时间")
private String appointmentTime;
@Schema(description = "成交金额")
private BigDecimal dealAmount;
@Schema(description = "来源类型")
private String sourceType;
// ========== 查询条件 ==========
@Schema(description = "开始日期")
private String startDate;
@Schema(description = "结束日期")
private String endDate;
@Schema(description = "跟进状态(可多选,逗号分隔)")
private String statusList;
@Schema(description = "业务员ID")
private Integer salesmanId;
@Schema(description = "推荐人ID")
private Integer referrerId;
@Schema(description = "关键词(姓名/电话/公司)")
private String keyword;
@Schema(description = "当前页码")
private Integer pageNum = 1;
@Schema(description = "每页数量")
private Integer pageSize = 10;
}

View File

@@ -0,0 +1,38 @@
package com.gxwebsoft.cms.param;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.io.Serializable;
/**
* 客资派单请求参数
*
* @author 科技小王子
* @since 2026-04-14
*/
@Data
@Schema(name = "LeadDispatchParam", description = "客资派单请求参数")
public class LeadDispatchParam implements Serializable {
private static final long serialVersionUID = 1L;
@Schema(description = "客资ID")
private Integer leadId;
@Schema(description = "目标用户ID(业务员)")
private Integer toUserId;
@Schema(description = "派单备注")
private String remarks;
@Schema(description = "派单类型: 1新分配 2重新分配")
private Integer dispatchType = 1;
// ========== 批量派单 ==========
@Schema(description = "批量客资ID列表")
private Integer[] leadIds;
@Schema(description = "是否批量派单")
private Boolean batchMode = false;
}

View File

@@ -0,0 +1,43 @@
package com.gxwebsoft.cms.param;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.io.Serializable;
/**
* 客资跟进请求参数
*
* @author 科技小王子
* @since 2026-04-14
*/
@Data
@Schema(name = "LeadFollowParam", description = "客资跟进请求参数")
public class LeadFollowParam implements Serializable {
private static final long serialVersionUID = 1L;
@Schema(description = "客资ID")
private Integer leadId;
@Schema(description = "跟进方式: 1电话 2微信 3上门 4短信 5其他")
private Integer followType;
@Schema(description = "跟进内容")
private String followContent;
@Schema(description = "下次跟进时间(yyyy-MM-dd HH:mm:ss)")
private String nextFollowTime;
@Schema(description = "下次跟进计划")
private String nextFollowPlan;
@Schema(description = "附件URLs(JSON数组字符串)")
private String attachmentUrls;
@Schema(description = "是否同时更新状态")
private Boolean updateStatus = false;
@Schema(description = "新状态(当updateStatus=true时生效): 0待跟进 1跟进中 2已成交 3无效")
private Integer newStatus;
}

View File

@@ -0,0 +1,44 @@
package com.gxwebsoft.cms.param;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.io.Serializable;
import java.math.BigDecimal;
/**
* 推荐人报备请求参数(全民推荐)
*
* @author 科技小王子
* @since 2026-04-14
*/
@Data
@Schema(name = "LeadReferralParam", description = "推荐人报备请求参数")
public class LeadReferralParam implements Serializable {
private static final long serialVersionUID = 1L;
@Schema(description = "推荐码(匿名推荐时使用)")
private String referralCode;
@Schema(description = "客户姓名")
private String customerName;
@Schema(description = "客户电话")
private String customerPhone;
@Schema(description = "客户公司")
private String customerCompany;
@Schema(description = "需求描述")
private String requirement;
@Schema(description = "预约时间")
private String appointmentTime;
@Schema(description = "备注说明")
private String remarks;
@Schema(description = "推荐费(可选,管理员可设置默认值)")
private BigDecimal referralFee;
}

View File

@@ -0,0 +1,84 @@
package com.gxwebsoft.cms.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.gxwebsoft.cms.entity.CustomerLeadEntity;
import com.gxwebsoft.cms.param.CustomerLeadParam;
import com.gxwebsoft.cms.param.LeadDispatchParam;
import com.gxwebsoft.cms.param.LeadFollowParam;
import java.util.List;
import java.util.Map;
/**
* 客资管理服务接口
*
* @author 科技小王子
* @since 2026-04-14
*/
public interface CustomerLeadService extends IService<CustomerLeadEntity> {
/**
* 分页查询客资列表
*/
Map<String, Object> getLeadPage(CustomerLeadParam param);
/**
* 获取客资详情
*/
CustomerLeadEntity getLeadDetail(Integer leadId);
/**
* 创建客资(管理员录入)
*/
CustomerLeadEntity createLead(CustomerLeadParam param);
/**
* 更新客资信息
*/
boolean updateLead(CustomerLeadParam param);
/**
* 更新客资状态
*/
boolean updateLeadStatus(Integer leadId, Integer status, String remarks);
/**
* 派单给业务员
*/
boolean dispatchLead(LeadDispatchParam param);
/**
* 批量派单
*/
Map<String, Object> batchDispatchLeads(LeadDispatchParam param);
/**
* 添加跟进记录
*/
boolean addFollowLog(LeadFollowParam param);
/**
* 获取客资跟进历史
*/
List<Map<String, Object>> getFollowHistory(Integer leadId);
/**
* 获取统计数据
*/
Map<String, Object> getStatistics(CustomerLeadParam param);
/**
* 导出客资数据
*/
List<Map<String, Object>> exportLeads(CustomerLeadParam param);
/**
* 获取未分配客资列表
*/
List<CustomerLeadEntity> getUnassignedLeads();
/**
* 获取业务员的客资统计
*/
Map<String, Object> getSalesmanLeadStats(Integer salesmanId);
}

View File

@@ -0,0 +1,73 @@
package com.gxwebsoft.cms.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.gxwebsoft.cms.entity.LeadReferral;
import com.gxwebsoft.cms.param.CustomerLeadParam;
import com.gxwebsoft.cms.param.LeadReferralParam;
import java.math.BigDecimal;
import java.util.Map;
/**
* 推荐人服务接口(全民推荐)
*
* @author 科技小王子
* @since 2026-04-14
*/
public interface LeadReferralService extends IService<LeadReferral> {
/**
* 匿名用户通过推荐码报备客户
*/
LeadReferral anonymousReferral(LeadReferralParam param);
/**
* 注册用户报备客户
*/
LeadReferral userReferral(LeadReferralParam param);
/**
* 获取推荐人的推荐记录
*/
Map<String, Object> getReferralPage(CustomerLeadParam param);
/**
* 获取推荐人的推荐统计
*/
Map<String, Object> getReferralStats(Integer userId);
/**
* 生成推荐码
*/
String generateReferralCode();
/**
* 根据推荐码获取推荐人信息
*/
Map<String, Object> getReferrerByCode(String referralCode);
/**
* 确认推荐有效
*/
boolean confirmReferral(Integer referralId);
/**
* 作废推荐
*/
boolean invalidateReferral(Integer referralId, String reason);
/**
* 结算推荐费
*/
boolean settleReferral(Integer referralId);
/**
* 批量结算推荐费
*/
Map<String, Object> batchSettleReferrals(Integer[] referralIds);
/**
* 计算推荐费(根据成交金额和配置比例)
*/
BigDecimal calculateReferralFee(BigDecimal dealAmount);
}

View File

@@ -0,0 +1,382 @@
package com.gxwebsoft.cms.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxwebsoft.cms.entity.CustomerLeadEntity;
import com.gxwebsoft.cms.entity.LeadDispatch;
import com.gxwebsoft.cms.entity.LeadFollowLog;
import com.gxwebsoft.cms.mapper.CustomerLeadMapper;
import com.gxwebsoft.cms.mapper.LeadDispatchMapper;
import com.gxwebsoft.cms.mapper.LeadFollowLogMapper;
import com.gxwebsoft.cms.param.CustomerLeadParam;
import com.gxwebsoft.cms.param.LeadDispatchParam;
import com.gxwebsoft.cms.param.LeadFollowParam;
import com.gxwebsoft.cms.service.CustomerLeadService;
import com.gxwebsoft.common.system.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;
/**
* 客资管理服务实现
*
* @author 科技小王子
* @since 2026-04-14
*/
@Service
public class CustomerLeadServiceImpl extends ServiceImpl<CustomerLeadMapper, CustomerLeadEntity> implements CustomerLeadService {
@Autowired
private CustomerLeadMapper customerLeadMapper;
@Autowired
private LeadDispatchMapper leadDispatchMapper;
@Autowired
private LeadFollowLogMapper leadFollowLogMapper;
/**
* 获取当前登录用户(与 BaseController.getLoginUser() 逻辑一致)
*/
private User getLoginUser() {
try {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null) {
Object principal = authentication.getPrincipal();
if (principal instanceof User) {
return (User) principal;
}
}
} catch (Exception ignored) {
}
return null;
}
@Override
public Map<String, Object> getLeadPage(CustomerLeadParam param) {
// 处理分页参数
if (param.getPageNum() == null || param.getPageNum() < 1) {
param.setPageNum(1);
}
if (param.getPageSize() == null || param.getPageSize() < 1 || param.getPageSize() > 100) {
param.setPageSize(10);
}
Page<CustomerLeadEntity> page = new Page<>(param.getPageNum(), param.getPageSize());
List<CustomerLeadEntity> list = customerLeadMapper.selectLeadPage(param);
long total = page.getTotal();
Map<String, Object> result = new HashMap<>();
result.put("list", list);
result.put("total", total);
result.put("pageNum", param.getPageNum());
result.put("pageSize", param.getPageSize());
result.put("pages", (total + param.getPageSize() - 1) / param.getPageSize());
return result;
}
@Override
public CustomerLeadEntity getLeadDetail(Integer leadId) {
return customerLeadMapper.selectLeadDetail(leadId);
}
@Override
@Transactional(rollbackFor = Exception.class)
public CustomerLeadEntity createLead(CustomerLeadParam param) {
CustomerLeadEntity lead = new CustomerLeadEntity();
lead.setName(param.getName());
lead.setPhone(param.getPhone());
lead.setCompany(param.getCompany());
lead.setNeed(param.getNeed());
lead.setStatus(CustomerLeadEntity.STATUS_PENDING); // 默认待跟进
lead.setSourceType(CustomerLeadEntity.SOURCE_ADMIN); // 管理员录入
lead.setReferralFee(BigDecimal.ZERO);
lead.setReferralFeePaid(0);
lead.setFollowCount(0);
// 如果指定了业务员,直接派单
if (param.getAssignedUserId() != null) {
User currentUser = getLoginUser();
lead.setAssignedUserId(param.getAssignedUserId());
lead.setDispatchTime(LocalDateTime.now());
lead.setDispatchAdminId(currentUser != null ? currentUser.getUserId() : null);
lead.setStatus(CustomerLeadEntity.STATUS_FOLLOWING);
}
this.save(lead);
return lead;
}
@Override
@Transactional(rollbackFor = Exception.class)
public boolean updateLead(CustomerLeadParam param) {
CustomerLeadEntity lead = this.getById(param.getLeadId());
if (lead == null) {
throw new RuntimeException("客资不存在");
}
if (StringUtils.hasText(param.getName())) {
lead.setName(param.getName());
}
if (StringUtils.hasText(param.getPhone())) {
lead.setPhone(param.getPhone());
}
if (StringUtils.hasText(param.getCompany())) {
lead.setCompany(param.getCompany());
}
if (StringUtils.hasText(param.getNeed())) {
lead.setNeed(param.getNeed());
}
if (StringUtils.hasText(param.getRemarks())) {
lead.setRemarks(param.getRemarks());
}
if (param.getDealAmount() != null) {
lead.setDealAmount(param.getDealAmount());
lead.setDealTime(LocalDateTime.now());
}
return this.updateById(lead);
}
@Override
@Transactional(rollbackFor = Exception.class)
public boolean updateLeadStatus(Integer leadId, Integer status, String remarks) {
CustomerLeadEntity lead = this.getById(leadId);
if (lead == null) {
throw new RuntimeException("客资不存在");
}
lead.setStatus(status);
if (StringUtils.hasText(remarks)) {
lead.setRemarks(remarks);
}
// 成交时记录成交时间
if (status == CustomerLeadEntity.STATUS_DEALED) {
lead.setDealTime(LocalDateTime.now());
}
return this.updateById(lead);
}
@Override
@Transactional(rollbackFor = Exception.class)
public boolean dispatchLead(LeadDispatchParam param) {
CustomerLeadEntity lead = this.getById(param.getLeadId());
if (lead == null) {
throw new RuntimeException("客资不存在");
}
User currentUser = getLoginUser();
Integer adminId = currentUser != null ? currentUser.getUserId() : null;
// 更新客资分配信息
lead.setAssignedUserId(param.getToUserId());
lead.setDispatchTime(LocalDateTime.now());
lead.setDispatchAdminId(adminId);
lead.setStatus(CustomerLeadEntity.STATUS_FOLLOWING); // 派单后变为跟进中
// 如果有备注,更新备注
if (StringUtils.hasText(param.getRemarks())) {
String oldRemarks = lead.getRemarks() != null ? lead.getRemarks() + "\n" : "";
lead.setRemarks(oldRemarks + "【派单备注】" + param.getRemarks());
}
this.updateById(lead);
// 记录派单历史
LeadDispatch dispatch = new LeadDispatch();
dispatch.setLeadId(param.getLeadId());
dispatch.setFromUserId(lead.getAssignedUserId()); // 原分配用户
dispatch.setToUserId(param.getToUserId());
dispatch.setAdminId(adminId);
dispatch.setDispatchRemarks(param.getRemarks());
dispatch.setDispatchType(param.getDispatchType() != null ? param.getDispatchType() : LeadDispatch.TYPE_NEW);
dispatch.setCreateTime(LocalDateTime.now());
leadDispatchMapper.insert(dispatch);
return true;
}
@Override
@Transactional(rollbackFor = Exception.class)
public Map<String, Object> batchDispatchLeads(LeadDispatchParam param) {
Map<String, Object> result = new HashMap<>();
int successCount = 0;
int failCount = 0;
List<String> errors = new ArrayList<>();
if (param.getLeadIds() == null || param.getLeadIds().length == 0) {
throw new RuntimeException("请选择要派单的客资");
}
User currentUser = getLoginUser();
Integer adminId = currentUser != null ? currentUser.getUserId() : null;
for (Integer leadId : param.getLeadIds()) {
try {
LeadDispatchParam singleParam = new LeadDispatchParam();
singleParam.setLeadId(leadId);
singleParam.setToUserId(param.getToUserId());
singleParam.setRemarks(param.getRemarks());
singleParam.setDispatchType(LeadDispatch.TYPE_NEW);
this.dispatchLead(singleParam);
successCount++;
} catch (Exception e) {
failCount++;
errors.add("客资ID[" + leadId + "]: " + e.getMessage());
}
}
result.put("successCount", successCount);
result.put("failCount", failCount);
result.put("errors", errors);
return result;
}
@Override
@Transactional(rollbackFor = Exception.class)
public boolean addFollowLog(LeadFollowParam param) {
CustomerLeadEntity lead = this.getById(param.getLeadId());
if (lead == null) {
throw new RuntimeException("客资不存在");
}
User currentUser = getLoginUser();
Integer userId = currentUser != null ? currentUser.getUserId() : null;
// 添加跟进记录
LeadFollowLog followLog = new LeadFollowLog();
followLog.setLeadId(param.getLeadId());
followLog.setUserId(userId);
followLog.setFollowType(param.getFollowType());
followLog.setFollowContent(param.getFollowContent());
followLog.setNextFollowPlan(param.getNextFollowPlan());
followLog.setAttachmentUrls(param.getAttachmentUrls());
followLog.setCreateTime(LocalDateTime.now());
if (StringUtils.hasText(param.getNextFollowTime())) {
followLog.setNextFollowTime(LocalDateTime.parse(param.getNextFollowTime().replace(" ", "T")));
}
leadFollowLogMapper.insert(followLog);
// 更新客资跟进信息
lead.setFollowCount(lead.getFollowCount() != null ? lead.getFollowCount() + 1 : 1);
lead.setLastFollowTime(LocalDateTime.now());
// 如果需要更新状态
if (Boolean.TRUE.equals(param.getUpdateStatus()) && param.getNewStatus() != null) {
lead.setStatus(param.getNewStatus());
}
// 如果是成交
if (param.getNewStatus() != null && param.getNewStatus() == CustomerLeadEntity.STATUS_DEALED) {
lead.setDealTime(LocalDateTime.now());
}
return this.updateById(lead);
}
@Override
public List<Map<String, Object>> getFollowHistory(Integer leadId) {
List<LeadFollowLog> logs = leadFollowLogMapper.selectFollowHistory(leadId);
List<Map<String, Object>> result = new ArrayList<>();
for (LeadFollowLog log : logs) {
Map<String, Object> item = new HashMap<>();
item.put("followId", log.getFollowId());
item.put("followType", log.getFollowType());
item.put("followTypeText", log.getFollowTypeText());
item.put("followContent", log.getFollowContent());
item.put("nextFollowTime", log.getNextFollowTime());
item.put("nextFollowPlan", log.getNextFollowPlan());
item.put("userName", log.getUserName());
item.put("createTime", log.getCreateTime());
result.add(item);
}
return result;
}
@Override
public Map<String, Object> getStatistics(CustomerLeadParam param) {
Map<String, Object> stats = new HashMap<>();
// 基础统计
Map<String, Object> basicStats = customerLeadMapper.selectStatistics(
param.getStartDate(), param.getEndDate(), param.getSalesmanId());
stats.put("basic", basicStats);
// 按状态统计
stats.put("byStatus", customerLeadMapper.selectStatusStatistics(param.getStartDate(), param.getEndDate()));
// 按来源统计
stats.put("bySource", customerLeadMapper.selectSourceStatistics(param.getStartDate(), param.getEndDate()));
// 每日趋势
stats.put("dailyTrend", customerLeadMapper.selectDailyTrend(param.getStartDate(), param.getEndDate()));
return stats;
}
@Override
public List<Map<String, Object>> exportLeads(CustomerLeadParam param) {
// 设置不分页,导出全部
param.setPageNum(1);
param.setPageSize(10000);
List<CustomerLeadEntity> leads = customerLeadMapper.selectLeadPage(param);
List<Map<String, Object>> result = new ArrayList<>();
for (CustomerLeadEntity lead : leads) {
Map<String, Object> item = new HashMap<>();
item.put("客户姓名", lead.getName());
item.put("联系电话", lead.getPhone());
item.put("公司名称", lead.getCompany());
item.put("需求描述", lead.getNeed());
item.put("状态", lead.getStatusText());
item.put("来源", lead.getSourceType());
item.put("业务员", lead.getAssignedUserName());
item.put("推荐人", lead.getReferrerName());
item.put("跟进次数", lead.getFollowCount());
item.put("成交金额", lead.getDealAmount());
item.put("创建时间", lead.getCreateTime());
item.put("派单时间", lead.getDispatchTime());
item.put("成交时间", lead.getDealTime());
item.put("备注", lead.getRemarks());
result.add(item);
}
return result;
}
@Override
public List<CustomerLeadEntity> getUnassignedLeads() {
return customerLeadMapper.selectUnassignedLeads();
}
@Override
public Map<String, Object> getSalesmanLeadStats(Integer salesmanId) {
Map<String, Object> stats = new HashMap<>();
stats.put("total", customerLeadMapper.countBySalesman(salesmanId, null));
stats.put("pending", customerLeadMapper.countBySalesman(salesmanId, CustomerLeadEntity.STATUS_PENDING));
stats.put("following", customerLeadMapper.countBySalesman(salesmanId, CustomerLeadEntity.STATUS_FOLLOWING));
stats.put("dealed", customerLeadMapper.countBySalesman(salesmanId, CustomerLeadEntity.STATUS_DEALED));
stats.put("invalid", customerLeadMapper.countBySalesman(salesmanId, CustomerLeadEntity.STATUS_INVALID));
return stats;
}
}

View File

@@ -0,0 +1,305 @@
package com.gxwebsoft.cms.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxwebsoft.cms.entity.CustomerLeadEntity;
import com.gxwebsoft.cms.entity.LeadReferral;
import com.gxwebsoft.cms.mapper.CustomerLeadMapper;
import com.gxwebsoft.cms.mapper.LeadReferralMapper;
import com.gxwebsoft.cms.param.CustomerLeadParam;
import com.gxwebsoft.cms.param.LeadReferralParam;
import com.gxwebsoft.cms.service.CustomerLeadService;
import com.gxwebsoft.cms.service.LeadReferralService;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.common.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.*;
/**
* 推荐人服务实现(全民推荐)
*
* @author 科技小王子
* @since 2026-04-14
*/
@Service
public class LeadReferralServiceImpl extends ServiceImpl<LeadReferralMapper, LeadReferral> implements LeadReferralService {
@Autowired
private LeadReferralMapper leadReferralMapper;
@Autowired
private CustomerLeadMapper customerLeadMapper;
@Autowired
private CustomerLeadService customerLeadService;
@Autowired
private UserService userService;
// 默认推荐费比例 5%
private static final BigDecimal DEFAULT_REFERRAL_RATE = new BigDecimal("0.05");
/**
* 获取当前登录用户(与 BaseController.getLoginUser() 逻辑一致)
*/
private User getLoginUser() {
try {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null) {
Object principal = authentication.getPrincipal();
if (principal instanceof User) {
return (User) principal;
}
}
} catch (Exception ignored) {
}
return null;
}
@Override
@Transactional(rollbackFor = Exception.class)
public LeadReferral anonymousReferral(LeadReferralParam param) {
// 1. 如果有推荐码,先查询推荐人
Integer referrerUserId = null;
if (StringUtils.hasText(param.getReferralCode())) {
LeadReferral referral = leadReferralMapper.selectByCode(param.getReferralCode());
if (referral != null) {
referrerUserId = referral.getReferrerUserId();
}
}
// 2. 创建客资
CustomerLeadEntity lead = new CustomerLeadEntity();
lead.setName(param.getCustomerName());
lead.setPhone(param.getCustomerPhone());
lead.setCompany(param.getCustomerCompany());
lead.setNeed(param.getRequirement());
lead.setSourceType(CustomerLeadEntity.SOURCE_REFERRAL);
lead.setStatus(CustomerLeadEntity.STATUS_PENDING);
lead.setReferrerUserId(referrerUserId);
lead.setFollowCount(0);
lead.setReferralFee(BigDecimal.ZERO);
lead.setReferralFeePaid(0);
if (StringUtils.hasText(param.getAppointmentTime())) {
lead.setAppointmentTime(LocalDateTime.parse(param.getAppointmentTime().replace(" ", "T")));
}
customerLeadMapper.insert(lead);
// 3. 创建推荐关系
LeadReferral referral = new LeadReferral();
referral.setReferrerUserId(referrerUserId);
referral.setReferredLeadId(lead.getLeadId());
referral.setReferralCode(StringUtils.hasText(param.getReferralCode()) ? param.getReferralCode() : null);
referral.setReferralFee(param.getReferralFee() != null ? param.getReferralFee() : BigDecimal.ZERO);
referral.setReferralStatus(LeadReferral.STATUS_PENDING); // 待确认
referral.setCustomerName(param.getCustomerName());
referral.setCustomerPhone(param.getCustomerPhone());
referral.setCreateTime(LocalDateTime.now());
leadReferralMapper.insert(referral);
// 4. 更新客资的推荐费
lead.setReferralFee(referral.getReferralFee());
customerLeadMapper.updateById(lead);
return referral;
}
@Override
@Transactional(rollbackFor = Exception.class)
public LeadReferral userReferral(LeadReferralParam param) {
User currentUser = getLoginUser();
if (currentUser == null) {
throw new RuntimeException("请先登录");
}
// 1. 创建客资
CustomerLeadEntity lead = new CustomerLeadEntity();
lead.setName(param.getCustomerName());
lead.setPhone(param.getCustomerPhone());
lead.setCompany(param.getCustomerCompany());
lead.setNeed(param.getRequirement());
lead.setSourceType(CustomerLeadEntity.SOURCE_REFERRAL);
lead.setStatus(CustomerLeadEntity.STATUS_PENDING);
lead.setReferrerUserId(currentUser.getUserId());
lead.setFollowCount(0);
lead.setReferralFee(param.getReferralFee() != null ? param.getReferralFee() : BigDecimal.ZERO);
lead.setReferralFeePaid(0);
if (StringUtils.hasText(param.getAppointmentTime())) {
lead.setAppointmentTime(LocalDateTime.parse(param.getAppointmentTime().replace(" ", "T")));
}
customerLeadMapper.insert(lead);
// 2. 创建推荐关系
LeadReferral referral = new LeadReferral();
referral.setReferrerUserId(currentUser.getUserId());
referral.setReferredLeadId(lead.getLeadId());
referral.setReferralCode(this.generateReferralCode()); // 生成推荐码
referral.setReferralFee(param.getReferralFee() != null ? param.getReferralFee() : BigDecimal.ZERO);
referral.setReferralStatus(LeadReferral.STATUS_PENDING);
referral.setCustomerName(param.getCustomerName());
referral.setCustomerPhone(param.getCustomerPhone());
referral.setCreateTime(LocalDateTime.now());
leadReferralMapper.insert(referral);
return referral;
}
@Override
public Map<String, Object> getReferralPage(CustomerLeadParam param) {
Page<LeadReferral> page = new Page<>(param.getPageNum(), param.getPageSize());
List<LeadReferral> list = leadReferralMapper.selectReferralPage(param);
Map<String, Object> result = new HashMap<>();
result.put("list", list);
result.put("total", page.getTotal());
result.put("pageNum", param.getPageNum());
result.put("pageSize", param.getPageSize());
return result;
}
@Override
public Map<String, Object> getReferralStats(Integer userId) {
Map<String, Object> stats = new HashMap<>();
stats.put("totalCount", leadReferralMapper.countUserReferrals(userId, null));
stats.put("pendingCount", leadReferralMapper.countUserReferrals(userId, LeadReferral.STATUS_PENDING));
stats.put("validCount", leadReferralMapper.countUserReferrals(userId, LeadReferral.STATUS_VALID));
stats.put("settledCount", leadReferralMapper.countUserReferrals(userId, LeadReferral.STATUS_SETTLED));
stats.put("pendingAmount", leadReferralMapper.sumPendingFee(userId));
return stats;
}
@Override
public String generateReferralCode() {
return "RF" + System.currentTimeMillis() + (int)(Math.random() * 100);
}
@Override
public Map<String, Object> getReferrerByCode(String referralCode) {
LeadReferral referral = leadReferralMapper.selectByCode(referralCode);
if (referral == null || referral.getReferrerUserId() == null) {
return null;
}
User referrer = userService.getByIdRel(referral.getReferrerUserId());
if (referrer == null) {
return null;
}
Map<String, Object> result = new HashMap<>();
result.put("referrerId", referrer.getUserId());
result.put("referrerName", referrer.getNickname());
result.put("referrerPhone", referrer.getPhone());
result.put("referralCode", referralCode);
return result;
}
@Override
@Transactional(rollbackFor = Exception.class)
public boolean confirmReferral(Integer referralId) {
LeadReferral referral = this.getById(referralId);
if (referral == null) {
throw new RuntimeException("推荐记录不存在");
}
referral.setReferralStatus(LeadReferral.STATUS_VALID);
return this.updateById(referral);
}
@Override
@Transactional(rollbackFor = Exception.class)
public boolean invalidateReferral(Integer referralId, String reason) {
LeadReferral referral = this.getById(referralId);
if (referral == null) {
throw new RuntimeException("推荐记录不存在");
}
referral.setReferralStatus(LeadReferral.STATUS_INVALID);
// 同步更新客资推荐费状态
CustomerLeadEntity lead = customerLeadMapper.selectById(referral.getReferredLeadId());
if (lead != null) {
lead.setReferralFeePaid(-1); // 标记为已作废
customerLeadMapper.updateById(lead);
}
return this.updateById(referral);
}
@Override
@Transactional(rollbackFor = Exception.class)
public boolean settleReferral(Integer referralId) {
LeadReferral referral = this.getById(referralId);
if (referral == null) {
throw new RuntimeException("推荐记录不存在");
}
if (referral.getReferralStatus() != LeadReferral.STATUS_VALID) {
throw new RuntimeException("只有有效的推荐才能结算");
}
referral.setReferralStatus(LeadReferral.STATUS_SETTLED);
referral.setSettlementTime(LocalDateTime.now());
// 同步更新客资推荐费状态
CustomerLeadEntity lead = customerLeadMapper.selectById(referral.getReferredLeadId());
if (lead != null) {
lead.setReferralFeePaid(1); // 已支付
customerLeadMapper.updateById(lead);
}
return this.updateById(referral);
}
@Override
@Transactional(rollbackFor = Exception.class)
public Map<String, Object> batchSettleReferrals(Integer[] referralIds) {
Map<String, Object> result = new HashMap<>();
int successCount = 0;
int failCount = 0;
List<String> errors = new ArrayList<>();
for (Integer id : referralIds) {
try {
this.settleReferral(id);
successCount++;
} catch (Exception e) {
failCount++;
errors.add("ID[" + id + "]: " + e.getMessage());
}
}
result.put("successCount", successCount);
result.put("failCount", failCount);
result.put("errors", errors);
return result;
}
@Override
public BigDecimal calculateReferralFee(BigDecimal dealAmount) {
if (dealAmount == null || dealAmount.compareTo(BigDecimal.ZERO) <= 0) {
return BigDecimal.ZERO;
}
return dealAmount.multiply(DEFAULT_REFERRAL_RATE).setScale(2, RoundingMode.HALF_UP);
}
}

View File

@@ -0,0 +1,87 @@
package com.gxwebsoft.common.system.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
/**
* 用户角色扩展实体(支持多角色)
*
* @author 科技小王子
* @since 2026-04-14
*/
@Data
@TableName("sys_user_role_extend")
@Schema(name = "UserRoleExtend", description = "用户角色扩展")
public class UserRoleExtend implements Serializable {
private static final long serialVersionUID = 1L;
@Schema(description = "ID")
@TableId(type = IdType.AUTO)
private Integer id;
@Schema(description = "用户ID")
private Integer userId;
@Schema(description = "角色类型: admin管理员 salesman业务员 referrer推荐人")
private String roleType;
@Schema(description = "是否主角色 0否 1是")
private Integer isPrimary;
@Schema(description = "权限配置(JSON)")
private String permissions;
@Schema(description = "最大客资分配数(业务员)")
private Integer maxLeads;
@Schema(description = "佣金比例%(业务员)")
private BigDecimal commissionRate;
@Schema(description = "推荐奖金%(推荐人)")
private BigDecimal referralBonus;
@Schema(description = "状态: 0禁用 1启用")
private Integer status;
@Schema(description = "创建时间")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime createTime;
@Schema(description = "更新时间")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime updateTime;
// ========== 关联信息(非数据库字段)==========
@Schema(description = "用户昵称")
private String userName;
@Schema(description = "用户电话")
private String userPhone;
@Schema(description = "当前分配的客资数")
private Integer currentLeads;
// ========== 角色类型常量 ==========
public static final String ROLE_ADMIN = "admin"; // 管理员
public static final String ROLE_SALESMAN = "salesman"; // 业务员
public static final String ROLE_REFERRER = "referrer"; // 推荐人
public String getRoleTypeText() {
if (this.roleType == null) return "";
return switch (this.roleType) {
case ROLE_ADMIN -> "管理员";
case ROLE_SALESMAN -> "业务员";
case ROLE_REFERRER -> "推荐人";
default -> this.roleType;
};
}
}