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/service/impl/CustomerServiceImpl.java | 75 +++++++++++++++++++++++++++++++++++--
1 files changed, 71 insertions(+), 4 deletions(-)
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 afc1c30..3d4f0ae 100644
--- a/src/main/java/com/ruoyi/basic/service/impl/CustomerServiceImpl.java
+++ b/src/main/java/com/ruoyi/basic/service/impl/CustomerServiceImpl.java
@@ -2,6 +2,7 @@
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@@ -11,11 +12,19 @@
import com.ruoyi.basic.service.ICustomerService;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
+import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.security.LoginUser;
+import com.ruoyi.framework.web.domain.AjaxResult;
+import com.ruoyi.project.system.domain.SysUser;
+import com.ruoyi.sales.mapper.SalesLedgerMapper;
+import com.ruoyi.sales.pojo.SalesLedger;
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.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
@@ -33,6 +42,7 @@
@AllArgsConstructor
@Slf4j
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements ICustomerService {
+ private final SalesLedgerMapper salesLedgerMapper;
private CustomerMapper customerMapper;
/**
@@ -65,8 +75,12 @@
// 2. 鏋勫缓鏌ヨ鏉′欢锛堝寮虹┖鍊煎畨鍏級
LambdaQueryWrapper<Customer> queryWrapper = new LambdaQueryWrapper<>();
String customerName = customer.getCustomerName();
+ String customerType = customer.getCustomerType();
if (StringUtils.isNotBlank(customerName)) {
queryWrapper.like(Customer::getCustomerName, customerName);
+ }
+ if (StringUtils.isNotBlank(customerType)) {
+ queryWrapper.like(Customer::getCustomerType, customerType);
}
// 3. 鎵ц鍒嗛〉鏌ヨ锛堜繚鐣欏垎椤靛厓鏁版嵁锛�
@@ -80,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());
@@ -98,8 +144,8 @@
@Override
public int insertCustomer(Customer customer) {
LoginUser loginUser = SecurityUtils.getLoginUser();
- Integer tenantId = loginUser.getTenantId();
- customer.setTenantId(Long.valueOf(tenantId));
+ Long tenantId = loginUser.getTenantId();
+ customer.setTenantId(tenantId);
return customerMapper.insert(customer);
}
@@ -112,8 +158,8 @@
@Override
public int updateCustomer(Customer customer) {
LoginUser loginUser = SecurityUtils.getLoginUser();
- Integer tenantId = loginUser.getTenantId();
- customer.setTenantId(Long.valueOf(tenantId));
+ Long tenantId = loginUser.getTenantId();
+ customer.setTenantId(tenantId);
return customerMapper.updateById(customer);
}
@@ -126,6 +172,10 @@
@Override
public int deleteCustomerByIds(Long[] ids) {
List<Long> idList = Arrays.asList(ids);
+ List<SalesLedger> salesLedgers = salesLedgerMapper.selectList(new QueryWrapper<SalesLedger>().lambda().in(SalesLedger::getCustomerId, idList));
+ if (!salesLedgers.isEmpty()) {
+ throw new RuntimeException("瀹㈡埛妗f涓嬫湁閿�鍞悎鍚岋紝璇峰厛鍒犻櫎閿�鍞悎鍚�");
+ }
return customerMapper.deleteBatchIds(idList);
}
@@ -142,6 +192,23 @@
}
@Override
+ public AjaxResult importData(MultipartFile file) {
+ try {
+ ExcelUtil<Customer> util = new ExcelUtil<Customer>(Customer.class);
+ List<Customer> userList = util.importExcel(file.getInputStream());
+ if(CollectionUtils.isEmpty(userList)){
+ return AjaxResult.warn("妯℃澘閿欒鎴栧鍏ユ暟鎹负绌�");
+ }
+ this.saveOrUpdateBatch(userList);
+ return AjaxResult.success(true);
+ }catch (Exception e){
+ e.printStackTrace();
+ return AjaxResult.error("瀵煎叆澶辫触");
+ }
+
+ }
+
+ @Override
public List<Map<String, Object>> customerList(Customer customer) {
LambdaQueryWrapper<Customer> queryWrapper = Wrappers.lambdaQuery();
queryWrapper.select(Customer::getId, Customer::getCustomerName, Customer::getTaxpayerIdentificationNumber);
--
Gitblit v1.9.3