From 92112e68ee3707ed8437671d0164f036a2836c5d Mon Sep 17 00:00:00 2001
From: huminmin <mac@MacBook-Pro.local>
Date: 星期六, 28 三月 2026 10:41:28 +0800
Subject: [PATCH] 客户档案增加客户画像分析数据接口

---
 src/main/java/com/ruoyi/basic/pojo/Customer.java                    |   25 +++++++++++++++++++++++++
 src/main/java/com/ruoyi/basic/service/impl/CustomerServiceImpl.java |   33 +++++++++++++++++++++++++++++++++
 2 files changed, 58 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/ruoyi/basic/pojo/Customer.java b/src/main/java/com/ruoyi/basic/pojo/Customer.java
index 511e691..f97cb0a 100644
--- a/src/main/java/com/ruoyi/basic/pojo/Customer.java
+++ b/src/main/java/com/ruoyi/basic/pojo/Customer.java
@@ -1,6 +1,8 @@
 package com.ruoyi.basic.pojo;
 
 import java.io.Serializable;
+import java.math.BigDecimal;
+import java.time.LocalDate;
 import java.util.Date;
 
 import com.baomidou.mybatisplus.annotation.*;
@@ -99,4 +101,27 @@
     @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;
 }
diff --git a/src/main/java/com/ruoyi/basic/service/impl/CustomerServiceImpl.java b/src/main/java/com/ruoyi/basic/service/impl/CustomerServiceImpl.java
index f24a2d2..3d4f0ae 100644
--- a/src/main/java/com/ruoyi/basic/service/impl/CustomerServiceImpl.java
+++ b/src/main/java/com/ruoyi/basic/service/impl/CustomerServiceImpl.java
@@ -24,6 +24,7 @@
 import org.springframework.util.CollectionUtils;
 import org.springframework.web.multipart.MultipartFile;
 
+import java.math.BigDecimal;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
@@ -93,6 +94,38 @@
                     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());
+                        }
+                    } else {
+                        // 娌℃湁閿�鍞褰曟椂璁剧疆榛樿鍊�
+                        c.setPurchaseCount(0);
+                        c.setAverageAmount(BigDecimal.ZERO);
+                        c.setLatestPurchaseTime(null);
+                    }
                 })
                 .collect(Collectors.toList());
 

--
Gitblit v1.9.3