huminmin
4 天以前 934abb1d174772d9e02e46f89587c3df30939c79
客户档案增加客户档案分析相关接口
已修改2个文件
104 ■■■■■ 文件已修改
src/main/java/com/ruoyi/basic/pojo/Customer.java 30 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ruoyi/basic/service/impl/CustomerServiceImpl.java 74 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ruoyi/basic/pojo/Customer.java
@@ -1,7 +1,10 @@
package com.ruoyi.basic.pojo;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
@@ -95,4 +98,31 @@
    @ApiModelProperty(value = "开户行号")
    @Excel(name = "开户行号")
    private String bankCode;
    @ApiModelProperty(value = "客户偏好")
    private String preferences;
    @ApiModelProperty(value = "是否价格敏感")
    @Excel(name = "是否价格敏感")
    private Boolean isPriceSensitive;
    @ApiModelProperty(value = "是否会员")
    @Excel(name = "是否会员")
    private Boolean isVip;
    @TableField(exist = false)
    @ApiModelProperty(value = "购买次数")
    private Integer purchaseCount;
    @TableField(exist = false)
    @ApiModelProperty(value = "平均金额")
    private BigDecimal averageAmount;
    @TableField(exist = false)
    @ApiModelProperty(value = "最近购买时间")
    private LocalDate latestPurchaseTime;
    @TableField(exist = false)
    @ApiModelProperty(value = "常购产品")
    private List<String> topProducts;
}
src/main/java/com/ruoyi/basic/service/impl/CustomerServiceImpl.java
@@ -17,17 +17,17 @@
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.sales.mapper.SalesLedgerMapper;
import com.ruoyi.sales.mapper.SalesLedgerProductMapper;
import com.ruoyi.sales.pojo.SalesLedger;
import com.ruoyi.sales.pojo.SalesLedgerProduct;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
@@ -42,6 +42,7 @@
@Slf4j
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements ICustomerService {
    private final SalesLedgerMapper salesLedgerMapper;
    private final SalesLedgerProductMapper salesLedgerProductMapper;
    private CustomerMapper customerMapper;
    /**
@@ -89,6 +90,71 @@
                    String address = StringUtils.defaultString(c.getCompanyAddress(), "");
                    String phone = StringUtils.defaultString(c.getCompanyPhone(), "");
                    c.setAddressPhone(address + "(" + phone + ")"); // 优化字符串拼接
                    // 查询该客户关联的销售台账中,购买次数、平均金额、最近购买时间
                    List<SalesLedger> salesLedgers = salesLedgerMapper.selectList(new QueryWrapper<SalesLedger>().lambda().eq(SalesLedger::getCustomerId, c.getId()));
                    if (!CollectionUtils.isEmpty(salesLedgers)) {
                        // 计算购买次数
                        int purchaseCount = salesLedgers.size();
                        c.setPurchaseCount(purchaseCount);
                        // 计算平均金额
                        if (purchaseCount > 0) {
                            BigDecimal totalAmount = salesLedgers.stream()
                                    .map(SalesLedger::getContractAmount)
                                    .filter(Objects::nonNull)
                                    .reduce(BigDecimal.ZERO, BigDecimal::add);
                            BigDecimal averageAmount = totalAmount.divide(BigDecimal.valueOf(purchaseCount), 2, BigDecimal.ROUND_HALF_UP);
                            c.setAverageAmount(averageAmount);
                        } else {
                            c.setAverageAmount(BigDecimal.ZERO);
                        }
                        // 计算最近购买时间
                        SalesLedger latestLedger = salesLedgers.stream()
                                .max((l1, l2) -> l1.getExecutionDate().compareTo(l2.getExecutionDate()))
                                .orElse(null);
                        if (latestLedger != null) {
                            c.setLatestPurchaseTime(latestLedger.getExecutionDate());
                        }
                        // 计算常购产品(top 3)
                        List<SalesLedgerProduct> allProducts = new ArrayList<>();
                        for (SalesLedger ledger : salesLedgers) {
                            List<SalesLedgerProduct> products = salesLedgerProductMapper.selectList(
                                    new QueryWrapper<SalesLedgerProduct>().lambda()
                                            .eq(SalesLedgerProduct::getSalesLedgerId, ledger.getId())
                            );
                            if (!CollectionUtils.isEmpty(products)) {
                                allProducts.addAll(products);
                            }
                        }
                        if (!CollectionUtils.isEmpty(allProducts)) {
                            // 按产品类别和规格型号分组,统计购买次数
                            Map<String, Long> productCountMap = allProducts.stream()
                                    .collect(Collectors.groupingBy(
                                            p -> p.getProductCategory() + "-" + p.getSpecificationModel(),
                                            Collectors.counting()
                                    ));
                            // 排序并取前3
                            List<String> topProducts = productCountMap.entrySet().stream()
                                    .sorted(Map.Entry.<String, Long>comparingByValue().reversed())
                                    .limit(3)
                                    .map(Map.Entry::getKey)
                                    .collect(Collectors.toList());
                            c.setTopProducts(topProducts);
                        } else {
                            c.setTopProducts(new ArrayList<>());
                        }
                    } else {
                        // 没有销售记录时设置默认值
                        c.setPurchaseCount(0);
                        c.setAverageAmount(BigDecimal.ZERO);
                        c.setLatestPurchaseTime(null);
                        c.setTopProducts(new ArrayList<>());
                    }
                })
                .collect(Collectors.toList());