You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
895 lines
44 KiB
895 lines
44 KiB
|
3 months ago
|
package com.example.web.controller;
|
||
|
|
|
||
|
|
import com.example.web.dto.ManagerAuthInfo;
|
||
|
|
import com.example.web.dto.UnifiedCustomerDTO;
|
||
|
|
import com.example.web.dto.UserProductCartDTO;
|
||
|
|
import com.example.web.entity.UsersManagements;
|
||
|
|
import com.example.web.mapper.SupplyUsersManagementsMapper;
|
||
|
|
import com.example.web.service.SupplyCustomerService;
|
||
|
|
import com.example.web.service.SupplyPoolCustomerService;
|
||
|
|
import jakarta.servlet.http.HttpServletRequest;
|
||
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
||
|
|
import org.springframework.http.HttpStatus;
|
||
|
|
import org.springframework.http.ResponseEntity;
|
||
|
|
import org.springframework.web.bind.annotation.*;
|
||
|
|
import org.springframework.web.context.request.RequestContextHolder;
|
||
|
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
||
|
|
|
||
|
|
|
||
|
|
import java.util.HashMap;
|
||
|
|
import java.util.List;
|
||
|
|
import java.util.Map;
|
||
|
|
import java.util.stream.Collectors;
|
||
|
|
|
||
|
|
@RestController
|
||
|
|
@RequestMapping("/supply/pool")
|
||
|
|
@CrossOrigin(origins = "*")
|
||
|
|
public class SupplyCustomerController {
|
||
|
|
|
||
|
|
@Autowired
|
||
|
|
private SupplyCustomerService supplyCustomerService;
|
||
|
|
|
||
|
|
@Autowired
|
||
|
|
private SupplyPoolCustomerService supplyPoolCustomerService;
|
||
|
|
|
||
|
|
@Autowired
|
||
|
|
private SupplyUsersManagementsMapper supplyUsersManagementsMapper;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 根据公司ID查询客户详情 - 优先处理wechat数据源
|
||
|
|
* GET /supply/pool/customers/{id}
|
||
|
|
*/
|
||
|
|
@GetMapping("/customers/{id}")
|
||
|
|
public ResponseEntity<Map<String, Object>> getById(@PathVariable String id, HttpServletRequest request) {
|
||
|
|
System.out.println("====================================================");
|
||
|
|
System.out.println("🔍 查询客户: " + id);
|
||
|
|
System.out.println("====================================================");
|
||
|
|
|
||
|
|
Map<String, Object> response = new HashMap<>();
|
||
|
|
try {
|
||
|
|
// 从URL参数获取用户类型,与前端保持一致
|
||
|
|
String isSupplySideParam = request.getParameter("isSupplySide");
|
||
|
|
boolean isSupplySide = !"false".equalsIgnoreCase(isSupplySideParam);
|
||
|
|
System.out.println("🎯 根据URL参数选择认证类型: " + (isSupplySide ? "采购端" : "销售端"));
|
||
|
|
|
||
|
|
// 从URL参数中获取ManagerAuthInfo
|
||
|
|
ManagerAuthInfo authInfo = getManagerAuthInfoFromRequest(request, isSupplySide);
|
||
|
|
|
||
|
|
if (authInfo == null) {
|
||
|
|
System.out.println("❌ 未找到用户认证信息");
|
||
|
|
response.put("success", false);
|
||
|
|
response.put("message", "用户未登录或认证信息缺失");
|
||
|
|
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(response);
|
||
|
|
}
|
||
|
|
|
||
|
|
System.out.println("✅ 获取到用户认证信息: " + authInfo.getUserName());
|
||
|
|
|
||
|
|
// 🎯 重构:完全基于数据存在性判断,彻底消除对ID格式的依赖
|
||
|
|
System.out.println("🎯 开始查询双数据源(优先wechat)...");
|
||
|
|
|
||
|
|
// 1. 首先尝试查询wechat数据源
|
||
|
|
System.out.println("📊 优先查询 WECHAT 数据源...");
|
||
|
|
UserProductCartDTO userInfo = supplyPoolCustomerService.getPublicSeaCustomerInfo(id);
|
||
|
|
|
||
|
|
if (userInfo != null) {
|
||
|
|
System.out.println("✅ 在wechat数据源中找到客户基础信息");
|
||
|
|
|
||
|
|
// 使用统一的公海池判断逻辑
|
||
|
|
boolean isPublicSea = supplyPoolCustomerService.isPublicSeaCustomerPublic(userInfo, authInfo);
|
||
|
|
System.out.println("📊 公海池判断结果: " + isPublicSea);
|
||
|
|
|
||
|
|
if (isPublicSea) {
|
||
|
|
System.out.println("🎯 识别为公海池客户,返回 WECHAT 数据源数据");
|
||
|
|
UnifiedCustomerDTO customer = convertToUnifiedDTOForPublicSea(userInfo, request);
|
||
|
|
customer.setDataSource("wechat");
|
||
|
|
|
||
|
|
response.put("success", true);
|
||
|
|
response.put("data", customer);
|
||
|
|
return ResponseEntity.ok(response);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 如果不是公海池客户,检查权限并返回wechat数据源的非公海池客户
|
||
|
|
System.out.println("📊 处理 WECHAT 数据源非公海池客户...");
|
||
|
|
boolean hasPermission = supplyCustomerService.hasPermissionToViewWechatCustomer(userInfo, authInfo);
|
||
|
|
|
||
|
|
if (hasPermission) {
|
||
|
|
System.out.println("✅ 有权限查看非公海池客户,返回 WECHAT 数据源数据");
|
||
|
|
UnifiedCustomerDTO customer = convertToUnifiedDTOForPublicSea(userInfo, request);
|
||
|
|
customer.setDataSource("wechat");
|
||
|
|
|
||
|
|
response.put("success", true);
|
||
|
|
response.put("data", customer);
|
||
|
|
return ResponseEntity.ok(response);
|
||
|
|
} else {
|
||
|
|
System.out.println("❌ 无权限查看wechat数据源的非公海池客户");
|
||
|
|
// 继续查询默认数据源
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
System.out.println("ℹ️ 在wechat数据源中未找到客户");
|
||
|
|
}
|
||
|
|
|
||
|
|
// 2. 如果wechat数据源没找到或无权限,再尝试查询默认数据源
|
||
|
|
System.out.println("📊 查询 DEFAULT 数据源...");
|
||
|
|
UnifiedCustomerDTO defaultCustomer = supplyCustomerService.getCustomerById(id, authInfo);
|
||
|
|
|
||
|
|
if (defaultCustomer != null) {
|
||
|
|
System.out.println("✅ 在默认数据源中找到客户");
|
||
|
|
response.put("success", true);
|
||
|
|
response.put("data", defaultCustomer);
|
||
|
|
return ResponseEntity.ok(response);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 3. 如果两个数据源都没找到
|
||
|
|
System.out.println("❌ 未在任何数据源中找到客户信息");
|
||
|
|
response.put("success", false);
|
||
|
|
response.put("message", "未找到对应的客户信息");
|
||
|
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
|
||
|
|
|
||
|
|
} catch (Exception e) {
|
||
|
|
System.err.println("❌ 服务器错误: " + e.getMessage());
|
||
|
|
e.printStackTrace();
|
||
|
|
response.put("success", false);
|
||
|
|
response.put("message", "服务器错误:" + e.getMessage());
|
||
|
|
return ResponseEntity.internalServerError().body(response);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 从请求中获取ManagerAuthInfo - 增强版本,严格区分销售端和采购端
|
||
|
|
*/
|
||
|
|
private ManagerAuthInfo getManagerAuthInfoFromRequest(HttpServletRequest request, boolean isSupplySide) {
|
||
|
|
String managerId = request.getParameter("managerId");
|
||
|
|
String managercompany = request.getParameter("company");
|
||
|
|
String managerdepartment = request.getParameter("department");
|
||
|
|
String organization = request.getParameter("organization");
|
||
|
|
String role = request.getParameter("role");
|
||
|
|
String userName = request.getParameter("userName");
|
||
|
|
String assistant = request.getParameter("assistant");
|
||
|
|
|
||
|
|
// URL解码参数
|
||
|
|
try {
|
||
|
|
if (managerId != null) managerId = java.net.URLDecoder.decode(managerId, "UTF-8");
|
||
|
|
if (managercompany != null) managercompany = java.net.URLDecoder.decode(managercompany, "UTF-8");
|
||
|
|
if (managerdepartment != null) managerdepartment = java.net.URLDecoder.decode(managerdepartment, "UTF-8");
|
||
|
|
if (organization != null) organization = java.net.URLDecoder.decode(organization, "UTF-8");
|
||
|
|
if (role != null) role = java.net.URLDecoder.decode(role, "UTF-8");
|
||
|
|
if (userName != null) userName = java.net.URLDecoder.decode(userName, "UTF-8");
|
||
|
|
if (assistant != null) assistant = java.net.URLDecoder.decode(assistant, "UTF-8");
|
||
|
|
} catch (java.io.UnsupportedEncodingException e) {
|
||
|
|
System.err.println("URL解码失败: " + e.getMessage());
|
||
|
|
}
|
||
|
|
|
||
|
|
// 检查必要参数
|
||
|
|
if (userName == null || userName.trim().isEmpty()) {
|
||
|
|
System.out.println("❌ 用户名为空,无法获取认证信息");
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 部门检查
|
||
|
|
if (managerdepartment == null) {
|
||
|
|
System.out.println("⚠️ 部门信息为空,使用默认值");
|
||
|
|
managerdepartment = "";
|
||
|
|
}
|
||
|
|
|
||
|
|
// 智能判断实际端类型:根据部门信息自动调整isSupplySide值
|
||
|
|
boolean actualIsSupplySide = isSupplySide;
|
||
|
|
if (managerdepartment.contains("销售")) {
|
||
|
|
actualIsSupplySide = false; // 销售部门自动判定为销售端
|
||
|
|
System.out.println("🔄 部门包含'销售',自动调整为销售端");
|
||
|
|
} else if (managerdepartment.contains("采购")) {
|
||
|
|
actualIsSupplySide = true; // 采购部门自动判定为采购端
|
||
|
|
System.out.println("🔄 部门包含'采购',自动调整为采购端");
|
||
|
|
}
|
||
|
|
|
||
|
|
System.out.println("🔍 认证信息检查,实际端类型: " + (actualIsSupplySide ? "采购端" : "销售端") +
|
||
|
|
",部门: '" + managerdepartment + "'");
|
||
|
|
System.out.println("✅ 认证信息检查通过");
|
||
|
|
|
||
|
|
// 验证公司信息一致性
|
||
|
|
if (managercompany == null || managercompany.trim().isEmpty()) {
|
||
|
|
System.out.println("❌ 公司信息为空");
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
ManagerAuthInfo authInfo = new ManagerAuthInfo(
|
||
|
|
managerId != null ? managerId : "",
|
||
|
|
managercompany != null ? managercompany : "",
|
||
|
|
managerdepartment != null ? managerdepartment : "",
|
||
|
|
organization != null ? organization : "",
|
||
|
|
role != null ? role : "",
|
||
|
|
userName,
|
||
|
|
assistant != null ? assistant : "");
|
||
|
|
|
||
|
|
// 设置实际的端类型
|
||
|
|
try {
|
||
|
|
// 尝试通过反射或setter方法设置supplySide属性
|
||
|
|
java.lang.reflect.Field field = authInfo.getClass().getDeclaredField("supplySide");
|
||
|
|
field.setAccessible(true);
|
||
|
|
field.set(authInfo, actualIsSupplySide);
|
||
|
|
} catch (Exception e) {
|
||
|
|
System.out.println("ℹ️ 无法设置supplySide属性: " + e.getMessage());
|
||
|
|
}
|
||
|
|
|
||
|
|
System.out.println("🎯 认证信息详情: " +
|
||
|
|
"系统类型=" + (actualIsSupplySide ? "采购端" : "销售端") +
|
||
|
|
", 公司=" + authInfo.getManagercompany() +
|
||
|
|
", 部门=" + authInfo.getManagerdepartment() +
|
||
|
|
", 组织=" + authInfo.getOrganization() +
|
||
|
|
", 角色=" + authInfo.getRole() +
|
||
|
|
", 负责人=" + authInfo.getUserName() +
|
||
|
|
", 协助人=" + authInfo.getAssistant());
|
||
|
|
|
||
|
|
return authInfo;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 查找选中的产品项 - 修复版本
|
||
|
|
* 优先使用前端传递的选中ID,如果没有则使用第一个
|
||
|
|
*/
|
||
|
|
private UserProductCartDTO.ProductInfo findSelectedProduct(UserProductCartDTO userInfo, String targetProductItemId) {
|
||
|
|
if (userInfo.getProducts() == null || userInfo.getProducts().isEmpty()) {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 🔥 关键修复:优先使用前端传递的选中产品项ID
|
||
|
|
if (targetProductItemId != null && !targetProductItemId.trim().isEmpty()) {
|
||
|
|
for (UserProductCartDTO.ProductInfo product : userInfo.getProducts()) {
|
||
|
|
if (targetProductItemId.equals(product.getProductId())) {
|
||
|
|
System.out.println("✅ 找到前端选中的产品项: " + product.getProductId());
|
||
|
|
return product;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
System.out.println("⚠️ 未找到前端指定的产品项: " + targetProductItemId);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 🔥 其次检查是否有标记为选中的产品项
|
||
|
|
for (UserProductCartDTO.ProductInfo product : userInfo.getProducts()) {
|
||
|
|
if (isProductSelected(product)) {
|
||
|
|
System.out.println("✅ 找到标记为选中的产品项: " + product.getProductId());
|
||
|
|
return product;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 🔥 最后才返回第一个产品项
|
||
|
|
UserProductCartDTO.ProductInfo firstItem = userInfo.getProducts().get(0);
|
||
|
|
System.out.println("🔄 使用第一个产品项作为默认: " + firstItem.getProductId());
|
||
|
|
return firstItem;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 判断产品项是否被选中(根据业务逻辑)
|
||
|
|
*/
|
||
|
|
private boolean isProductSelected(UserProductCartDTO.ProductInfo product) {
|
||
|
|
// 这里可以根据业务需求实现选中逻辑
|
||
|
|
// 例如:通过某个字段标识,或者外部传入的参数
|
||
|
|
|
||
|
|
// 临时方案:默认返回false,使用第一个产品项
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 专门为公海池客户转换DTO的方法 - 采购端使用产品信息(修复版本)
|
||
|
|
* 新增HttpServletRequest参数用于获取选中的产品项ID
|
||
|
|
*/
|
||
|
|
private UnifiedCustomerDTO convertToUnifiedDTOForPublicSea(UserProductCartDTO userInfo, HttpServletRequest request) {
|
||
|
|
UnifiedCustomerDTO dto = new UnifiedCustomerDTO();
|
||
|
|
|
||
|
|
// 设置用户基础信息
|
||
|
|
dto.setId(userInfo.getUserId() != null ? userInfo.getUserId() : "");
|
||
|
|
dto.setPhoneNumber(userInfo.getPhoneNumber() != null ? userInfo.getPhoneNumber() : "");
|
||
|
|
|
||
|
|
// 设置公司信息
|
||
|
|
dto.setCompany(userInfo.getCompany() != null ? userInfo.getCompany() : "");
|
||
|
|
dto.setRegion(userInfo.getRegion() != null ? userInfo.getRegion() : "");
|
||
|
|
|
||
|
|
// 设置需求和规格
|
||
|
|
dto.setDemand(userInfo.getDemand() != null ? userInfo.getDemand() : "");
|
||
|
|
dto.setSpec(userInfo.getSpec() != null ? userInfo.getSpec() : "");
|
||
|
|
|
||
|
|
// 类型转换
|
||
|
|
String frontendType = convertDatabaseTypeToFrontend(userInfo.getType());
|
||
|
|
dto.setType(frontendType != null ? frontendType : "");
|
||
|
|
|
||
|
|
dto.setNickName(userInfo.getNickName() != null ? userInfo.getNickName() : "");
|
||
|
|
dto.setLevel(userInfo.getLevel() != null ? userInfo.getLevel() : "公海池");
|
||
|
|
dto.setCreated_at(userInfo.getCreated_at());
|
||
|
|
dto.setUpdated_at(userInfo.getUpdated_at());
|
||
|
|
|
||
|
|
// 关键:设置数据源标识为wechat
|
||
|
|
dto.setDataSource("wechat");
|
||
|
|
|
||
|
|
// 设置联系人信息 - 从usersContacts获取
|
||
|
|
setContactInfoSafely(dto, userInfo);
|
||
|
|
|
||
|
|
// 设置负责人信息
|
||
|
|
setManagerInfoSafely(dto, userInfo.getUserId());
|
||
|
|
|
||
|
|
// 设置公海需求信息 - 从产品表获取(采购端)
|
||
|
|
if (userInfo.getProducts() != null && !userInfo.getProducts().isEmpty()) {
|
||
|
|
// 🔥 关键修复:从HttpServletRequest中获取目标产品项ID
|
||
|
|
String targetProductItemId = getTargetProductItemIdFromRequest(request);
|
||
|
|
|
||
|
|
UserProductCartDTO.ProductInfo selectedProduct = findSelectedProduct(userInfo, targetProductItemId);
|
||
|
|
|
||
|
|
// 设置到单个字段 - 使用选中的产品项
|
||
|
|
dto.setProductName(selectedProduct.getProductName() != null ? selectedProduct.getProductName() : "");
|
||
|
|
dto.setVariety(selectedProduct.getVariety() != null ? selectedProduct.getVariety() : "");
|
||
|
|
dto.setSpecification(selectedProduct.getSpecification() != null ? selectedProduct.getSpecification() : "");
|
||
|
|
dto.setQuantity(selectedProduct.getQuantity() != null ? selectedProduct.getQuantity() : 0);
|
||
|
|
dto.setGrossWeight(selectedProduct.getGrossWeight() != null ? selectedProduct.getGrossWeight() : "");
|
||
|
|
dto.setYolk(selectedProduct.getYolk() != null ? selectedProduct.getYolk() : "");
|
||
|
|
|
||
|
|
// 🔥 关键修改:设置选中的产品项ID
|
||
|
|
dto.setTargetProductItemId(selectedProduct.getProductId() != null ? selectedProduct.getProductId() : "");
|
||
|
|
|
||
|
|
// 设置customDetails为产品信息
|
||
|
|
dto.setCustomDetails(userInfo.getProducts().toArray());
|
||
|
|
|
||
|
|
System.out.println("✅ 成功设置选中产品项到DTO: " + selectedProduct.getProductName() +
|
||
|
|
", 产品项ID: " + selectedProduct.getProductId() +
|
||
|
|
", 是否前端指定: " + (targetProductItemId != null));
|
||
|
|
} else {
|
||
|
|
// 如果没有产品信息,设置默认值
|
||
|
|
setDefaultProductValues(dto);
|
||
|
|
System.out.println("⚠️ 没有产品信息,使用默认值");
|
||
|
|
}
|
||
|
|
|
||
|
|
// 设置 productItems 到 UnifiedCustomerDTO - 采购端关键修复
|
||
|
|
if (userInfo.getProducts() != null && !userInfo.getProducts().isEmpty()) {
|
||
|
|
List<UnifiedCustomerDTO.ProductItem> unifiedProductItems = userInfo.getProducts().stream()
|
||
|
|
.map(product -> {
|
||
|
|
UnifiedCustomerDTO.ProductItem unifiedItem = new UnifiedCustomerDTO.ProductItem();
|
||
|
|
unifiedItem.setProductId(product.getProductId() != null ? product.getProductId() : "");
|
||
|
|
unifiedItem.setProductName(product.getProductName() != null ? product.getProductName() : "");
|
||
|
|
unifiedItem.setVariety(product.getVariety() != null ? product.getVariety() : "");
|
||
|
|
unifiedItem.setSpecification(product.getSpecification() != null ? product.getSpecification() : "");
|
||
|
|
unifiedItem.setQuantity(product.getQuantity() != null ? product.getQuantity() : 0);
|
||
|
|
unifiedItem.setGrossWeight(product.getGrossWeight() != null ? product.getGrossWeight() : "");
|
||
|
|
unifiedItem.setYolk(product.getYolk() != null ? product.getYolk() : "");
|
||
|
|
return unifiedItem;
|
||
|
|
})
|
||
|
|
.collect(Collectors.toList());
|
||
|
|
dto.setProductItems(unifiedProductItems);
|
||
|
|
System.out.println("✅ 成功设置 productItems 到 UnifiedCustomerDTO,数量: " + unifiedProductItems.size());
|
||
|
|
} else {
|
||
|
|
System.out.println("❌ 没有产品数据可设置到 productItems");
|
||
|
|
}
|
||
|
|
|
||
|
|
return dto;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 从请求参数中获取目标产品项ID - 完整实现
|
||
|
|
*/
|
||
|
|
private String getTargetProductItemIdFromRequest(HttpServletRequest request) {
|
||
|
|
try {
|
||
|
|
if (request == null) {
|
||
|
|
System.out.println("⚠️ HttpServletRequest为null");
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 1. 首先从URL查询参数中获取
|
||
|
|
String targetProductItemId = request.getParameter("targetProductItemId");
|
||
|
|
|
||
|
|
if (targetProductItemId != null && !targetProductItemId.trim().isEmpty()) {
|
||
|
|
System.out.println("🎯 从URL参数获取到目标产品项ID: " + targetProductItemId);
|
||
|
|
return targetProductItemId.trim();
|
||
|
|
}
|
||
|
|
|
||
|
|
// 2. 如果URL参数中没有,尝试从请求属性中获取
|
||
|
|
Object attributeValue = request.getAttribute("targetProductItemId");
|
||
|
|
if (attributeValue != null) {
|
||
|
|
String productItemId = attributeValue.toString();
|
||
|
|
System.out.println("🎯 从请求属性获取到目标产品项ID: " + productItemId);
|
||
|
|
return productItemId;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 3. 尝试从Header中获取
|
||
|
|
String headerValue = request.getHeader("X-Target-ProductItem-Id");
|
||
|
|
if (headerValue != null && !headerValue.trim().isEmpty()) {
|
||
|
|
System.out.println("🎯 从Header获取到目标产品项ID: " + headerValue);
|
||
|
|
return headerValue.trim();
|
||
|
|
}
|
||
|
|
|
||
|
|
System.out.println("⚠️ 未从请求中获取到目标产品项ID");
|
||
|
|
return null;
|
||
|
|
|
||
|
|
} catch (Exception e) {
|
||
|
|
System.err.println("❌ 获取目标产品项ID失败: " + e.getMessage());
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 安全设置负责人信息
|
||
|
|
*/
|
||
|
|
private void setManagerInfoSafely(UnifiedCustomerDTO dto, String userId) {
|
||
|
|
try {
|
||
|
|
System.out.println("🔍 查询用户负责人信息,用户ID: " + userId);
|
||
|
|
UsersManagements userManager = supplyUsersManagementsMapper.findByUserId(userId);
|
||
|
|
|
||
|
|
if (userManager != null) {
|
||
|
|
System.out.println("✅ 找到负责人信息,设置到DTO");
|
||
|
|
// 设置负责人信息到统一的DTO字段
|
||
|
|
dto.setManagercompany(userManager.getManagercompany() != null ? userManager.getManagercompany() : "");
|
||
|
|
dto.setManagerdepartment(userManager.getManagerdepartment() != null ? userManager.getManagerdepartment() : "");
|
||
|
|
dto.setOrganization(userManager.getOrganization() != null ? userManager.getOrganization() : "");
|
||
|
|
dto.setRole(userManager.getRole() != null ? userManager.getRole() : "");
|
||
|
|
dto.setUserName(userManager.getUserName() != null ? userManager.getUserName() : "");
|
||
|
|
dto.setAssistant(userManager.getAssistant() != null ? userManager.getAssistant() : "");
|
||
|
|
|
||
|
|
// 调试日志
|
||
|
|
System.out.println("📝 负责人信息详情: " +
|
||
|
|
"公司=" + dto.getManagercompany() +
|
||
|
|
", 部门=" + dto.getManagerdepartment() +
|
||
|
|
", 组织=" + dto.getOrganization() +
|
||
|
|
", 角色=" + dto.getRole() +
|
||
|
|
", 负责人=" + dto.getUserName() +
|
||
|
|
", 协助人=" + dto.getAssistant());
|
||
|
|
} else {
|
||
|
|
System.out.println("⚠️ 未找到负责人信息,用户可能为公海池客户");
|
||
|
|
// 设置默认值
|
||
|
|
setDefaultManagerValues(dto);
|
||
|
|
}
|
||
|
|
} catch (Exception e) {
|
||
|
|
System.err.println("❌ 查询负责人信息失败: " + e.getMessage());
|
||
|
|
// 发生异常时设置默认值
|
||
|
|
setDefaultManagerValues(dto);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 设置默认负责人值
|
||
|
|
*/
|
||
|
|
private void setDefaultManagerValues(UnifiedCustomerDTO dto) {
|
||
|
|
dto.setManagercompany("");
|
||
|
|
dto.setManagerdepartment("");
|
||
|
|
dto.setOrganization("");
|
||
|
|
dto.setRole("");
|
||
|
|
dto.setUserName("");
|
||
|
|
dto.setAssistant("");
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 设置默认产品值
|
||
|
|
*/
|
||
|
|
private void setDefaultProductValues(UnifiedCustomerDTO dto) {
|
||
|
|
dto.setProductName("");
|
||
|
|
dto.setVariety("");
|
||
|
|
dto.setSpecification("");
|
||
|
|
dto.setQuantity(0);
|
||
|
|
dto.setGrossWeight("");
|
||
|
|
dto.setYolk("");
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 安全设置联系人信息
|
||
|
|
*/
|
||
|
|
private void setContactInfoSafely(UnifiedCustomerDTO dto, UserProductCartDTO userInfo) {
|
||
|
|
if (userInfo.getUsersContacts() != null && !userInfo.getUsersContacts().isEmpty()) {
|
||
|
|
UserProductCartDTO.UsersContacts contact = userInfo.getUsersContacts().get(0);
|
||
|
|
dto.setWechat(contact.getWechat() != null ? contact.getWechat() : "");
|
||
|
|
dto.setAccount(contact.getAccount() != null ? contact.getAccount() : "");
|
||
|
|
dto.setAccountNumber(contact.getAccountNumber() != null ? contact.getAccountNumber() : "");
|
||
|
|
dto.setBank(contact.getBank() != null ? contact.getBank() : "");
|
||
|
|
dto.setAddress(contact.getAddress() != null ? contact.getAddress() : "");
|
||
|
|
} else {
|
||
|
|
setDefaultContactValues(dto);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 设置默认联系人值
|
||
|
|
*/
|
||
|
|
private void setDefaultContactValues(UnifiedCustomerDTO dto) {
|
||
|
|
dto.setWechat("");
|
||
|
|
dto.setAccount("");
|
||
|
|
dto.setAccountNumber("");
|
||
|
|
dto.setBank("");
|
||
|
|
dto.setAddress("");
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 数据库类型转前端类型
|
||
|
|
*/
|
||
|
|
private String convertDatabaseTypeToFrontend(String databaseType) {
|
||
|
|
if (databaseType == null) {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
switch (databaseType) {
|
||
|
|
case "seller":
|
||
|
|
return "供应端";
|
||
|
|
case "buyer":
|
||
|
|
return "客户端";
|
||
|
|
case "both":
|
||
|
|
return "BOTH";
|
||
|
|
default:
|
||
|
|
return databaseType;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 根据手机号查询客户详情
|
||
|
|
*/
|
||
|
|
@GetMapping("/customers/by-phone")
|
||
|
|
public ResponseEntity<Map<String, Object>> getByPhone(@RequestParam String phoneNumber, HttpServletRequest request) {
|
||
|
|
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
|
||
|
|
System.out.println("getByPhone: ---------------------------------------------------" + phoneNumber);
|
||
|
|
Map<String, Object> response = new HashMap<>();
|
||
|
|
try {
|
||
|
|
// 从URL参数获取用户类型,与前端保持一致
|
||
|
|
String isSupplySideParam = request.getParameter("isSupplySide");
|
||
|
|
boolean isSupplySide = !"false".equalsIgnoreCase(isSupplySideParam);
|
||
|
|
System.out.println("🎯 根据URL参数选择认证类型: " + (isSupplySide ? "采购端" : "销售端"));
|
||
|
|
|
||
|
|
// 从URL参数获取认证信息
|
||
|
|
ManagerAuthInfo authInfo = getManagerAuthInfoFromRequest(request, isSupplySide);
|
||
|
|
|
||
|
|
if (authInfo == null) {
|
||
|
|
response.put("success", false);
|
||
|
|
response.put("message", "用户未登录或认证信息缺失");
|
||
|
|
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(response);
|
||
|
|
}
|
||
|
|
|
||
|
|
UnifiedCustomerDTO customer = supplyCustomerService.getCustomerByPhone(phoneNumber, authInfo);
|
||
|
|
|
||
|
|
// 检查客户等级,如果是公海池客户,确保数据源正确
|
||
|
|
if (customer != null && customer.getLevel() != null &&
|
||
|
|
(customer.getLevel().contains("sea-pools") || "公海池".equals(customer.getLevel()))) {
|
||
|
|
customer.setDataSource("wechat");
|
||
|
|
}
|
||
|
|
|
||
|
|
response.put("success", true);
|
||
|
|
response.put("data", customer);
|
||
|
|
return ResponseEntity.ok(response);
|
||
|
|
} catch (IllegalArgumentException e) {
|
||
|
|
response.put("success", false);
|
||
|
|
response.put("message", e.getMessage());
|
||
|
|
return ResponseEntity.badRequest().body(response);
|
||
|
|
} catch (RuntimeException e) {
|
||
|
|
response.put("success", false);
|
||
|
|
response.put("message", e.getMessage());
|
||
|
|
return ResponseEntity.notFound().build();
|
||
|
|
} catch (Exception e) {
|
||
|
|
response.put("success", false);
|
||
|
|
response.put("message", "服务器错误:" + e.getMessage());
|
||
|
|
return ResponseEntity.internalServerError().body(response);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 更新基于电话号码的客户信息(专门用于公海池客户)- 采购员权限:只允许更新为seller和both类型
|
||
|
|
*/
|
||
|
|
@PutMapping("/phone-customers/update")
|
||
|
|
public ResponseEntity<Map<String, Object>> updatePhoneCustomer(@RequestBody UnifiedCustomerDTO updatedDTO, HttpServletRequest request) {
|
||
|
|
Map<String, Object> response = new HashMap<>();
|
||
|
|
|
||
|
|
try {
|
||
|
|
// 从URL参数获取用户类型,与前端保持一致
|
||
|
|
String isSupplySideParam = request.getParameter("isSupplySide");
|
||
|
|
boolean isSupplySide = !"false".equalsIgnoreCase(isSupplySideParam);
|
||
|
|
System.out.println("🎯 根据URL参数选择认证类型: " + (isSupplySide ? "采购端" : "销售端"));
|
||
|
|
|
||
|
|
// 获取认证信息
|
||
|
|
ManagerAuthInfo authInfo = getManagerAuthInfoFromRequest(request, isSupplySide);
|
||
|
|
if (authInfo == null) {
|
||
|
|
response.put("success", false);
|
||
|
|
response.put("message", "用户未登录或认证信息缺失");
|
||
|
|
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(response);
|
||
|
|
}
|
||
|
|
|
||
|
|
System.out.println("======= 开始处理公海池客户更新(wechat数据源)- 采购端 =======");
|
||
|
|
System.out.println("手机号: " + updatedDTO.getPhoneNumber());
|
||
|
|
System.out.println("原始等级: " + updatedDTO.getLevel());
|
||
|
|
System.out.println("负责人信息: " + authInfo.getUserName());
|
||
|
|
|
||
|
|
// 🔥 在Controller层先进行类型转换
|
||
|
|
System.out.println("🔄 Controller层类型转换前: " + updatedDTO.getType());
|
||
|
|
String originalType = updatedDTO.getType();
|
||
|
|
String convertedType = convertCustomerTypeInController(originalType);
|
||
|
|
updatedDTO.setType(convertedType);
|
||
|
|
System.out.println("🔄 Controller层类型转换后: " + originalType + " → " + convertedType);
|
||
|
|
|
||
|
|
System.out.println("🔐 负责人认证信息: " +
|
||
|
|
"公司=" + authInfo.getManagercompany() +
|
||
|
|
", 部门=" + authInfo.getManagerdepartment() +
|
||
|
|
", 组织=" + authInfo.getOrganization() +
|
||
|
|
", 角色=" + authInfo.getRole() +
|
||
|
|
", 负责人=" + authInfo.getUserName() +
|
||
|
|
", 协助人=" + authInfo.getAssistant());
|
||
|
|
|
||
|
|
// 采购员权限:校验客户类型,只允许seller和both
|
||
|
|
if (!"seller".equals(updatedDTO.getType()) && !"both".equals(updatedDTO.getType())) {
|
||
|
|
System.out.println("❌ 权限校验失败:采购员只能更新为seller或both类型,当前类型: " + updatedDTO.getType());
|
||
|
|
response.put("success", false);
|
||
|
|
response.put("message", "采购员权限只能更新为供应端类型客户");
|
||
|
|
return ResponseEntity.badRequest().body(response);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 调用服务层 - 传递authInfo用于负责人信息更新
|
||
|
|
System.out.println("🔄 调用Service更新方法...");
|
||
|
|
boolean success = supplyCustomerService.updatePhoneBasedCustomer(updatedDTO, authInfo);
|
||
|
|
|
||
|
|
System.out.println("✅ Service返回结果: " + success);
|
||
|
|
|
||
|
|
if (success) {
|
||
|
|
System.out.println("🎉 公海池客户信息更新成功");
|
||
|
|
response.put("success", true);
|
||
|
|
response.put("message", "客户信息更新成功");
|
||
|
|
return ResponseEntity.ok(response);
|
||
|
|
} else {
|
||
|
|
System.out.println("❌ 公海池客户信息更新失败");
|
||
|
|
response.put("success", false);
|
||
|
|
response.put("message", "客户信息更新失败");
|
||
|
|
return ResponseEntity.internalServerError().body(response);
|
||
|
|
}
|
||
|
|
} catch (Exception e) {
|
||
|
|
System.err.println("💥 更新公海池客户信息异常: " + e.getMessage());
|
||
|
|
e.printStackTrace();
|
||
|
|
response.put("success", false);
|
||
|
|
response.put("message", "服务器错误:" + e.getMessage());
|
||
|
|
return ResponseEntity.internalServerError().body(response);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Controller层的类型转换方法 - 采购端
|
||
|
|
*/
|
||
|
|
private String convertCustomerTypeInController(String originalType) {
|
||
|
|
if (originalType == null) {
|
||
|
|
return "seller";
|
||
|
|
}
|
||
|
|
|
||
|
|
switch (originalType.toUpperCase()) {
|
||
|
|
case "BOTH":
|
||
|
|
return "both";
|
||
|
|
case "客户端":
|
||
|
|
case "BUYER":
|
||
|
|
return "buyer";
|
||
|
|
case "供应端":
|
||
|
|
case "SELLER":
|
||
|
|
return "seller";
|
||
|
|
default:
|
||
|
|
return originalType;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 更新基于电话号码的客户信息(专门用于公海池客户)- 采购员权限:只允许更新为seller和both类型
|
||
|
|
*/
|
||
|
|
@PutMapping("/customers/update")
|
||
|
|
public ResponseEntity<Map<String, Object>> updateCustomer(
|
||
|
|
@RequestBody UnifiedCustomerDTO updatedDTO, HttpServletRequest request) {
|
||
|
|
|
||
|
|
Map<String, Object> response = new HashMap<>();
|
||
|
|
try {
|
||
|
|
System.out.println("======= 🔄 开始更新非公海池客户信息(采购端) =======");
|
||
|
|
System.out.println("数据源: " + updatedDTO.getDataSource());
|
||
|
|
System.out.println("客户ID: " + updatedDTO.getId());
|
||
|
|
System.out.println("手机号: " + updatedDTO.getPhoneNumber());
|
||
|
|
System.out.println("客户类型(转换前): " + updatedDTO.getType());
|
||
|
|
System.out.println("客户等级: " + updatedDTO.getLevel());
|
||
|
|
System.out.println("公司: " + updatedDTO.getCompany());
|
||
|
|
System.out.println("区域: " + updatedDTO.getRegion());
|
||
|
|
System.out.println("昵称: " + updatedDTO.getNickName());
|
||
|
|
System.out.println("微信: " + updatedDTO.getWechat());
|
||
|
|
System.out.println("地址: " + updatedDTO.getAddress());
|
||
|
|
|
||
|
|
// 打印负责人信息
|
||
|
|
System.out.println("=== 负责人信息 ===");
|
||
|
|
System.out.println("负责人公司: " + updatedDTO.getManagercompany());
|
||
|
|
System.out.println("负责人部门: " + updatedDTO.getManagerdepartment());
|
||
|
|
System.out.println("负责人组织: " + updatedDTO.getOrganization());
|
||
|
|
System.out.println("负责人角色: " + updatedDTO.getRole());
|
||
|
|
System.out.println("负责人姓名: " + updatedDTO.getUserName());
|
||
|
|
System.out.println("协助人: " + updatedDTO.getAssistant());
|
||
|
|
|
||
|
|
// 🎯 关键修复:先进行数据源验证和修正
|
||
|
|
String originalDataSource = updatedDTO.getDataSource();
|
||
|
|
String verifiedDataSource = verifyAndCorrectDataSource(updatedDTO);
|
||
|
|
|
||
|
|
if (!originalDataSource.equals(verifiedDataSource)) {
|
||
|
|
System.out.println("🔄 数据源自动修正: " + originalDataSource + " → " + verifiedDataSource);
|
||
|
|
updatedDTO.setDataSource(verifiedDataSource);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 🎯 关键修复:从URL参数获取用户类型,与前端保持一致
|
||
|
|
String isSupplySideParam = request.getParameter("isSupplySide");
|
||
|
|
boolean isSupplySide = !"false".equalsIgnoreCase(isSupplySideParam);
|
||
|
|
System.out.println("🎯 根据URL参数选择认证类型: " + (isSupplySide ? "采购端" : "销售端"));
|
||
|
|
|
||
|
|
// 获取认证信息 - 根据URL参数选择
|
||
|
|
ManagerAuthInfo authInfo = getManagerAuthInfoFromRequest(request, isSupplySide);
|
||
|
|
if (authInfo == null) {
|
||
|
|
response.put("success", false);
|
||
|
|
response.put("message", "用户未登录或认证信息缺失");
|
||
|
|
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(response);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 关键修复:添加客户类型转换逻辑
|
||
|
|
System.out.println("🔄 Controller层类型转换前: " + updatedDTO.getType());
|
||
|
|
String originalType = updatedDTO.getType();
|
||
|
|
String convertedType = convertCustomerTypeInController(originalType);
|
||
|
|
updatedDTO.setType(convertedType);
|
||
|
|
System.out.println("🔄 Controller层类型转换后: " + originalType + " → " + convertedType);
|
||
|
|
|
||
|
|
System.out.println("=== 数据验证 ===");
|
||
|
|
System.out.println("手机号是否为空: " + (updatedDTO.getPhoneNumber() == null || updatedDTO.getPhoneNumber().trim().isEmpty()));
|
||
|
|
System.out.println("客户类型是否有效: " + ("seller".equals(updatedDTO.getType()) || "both".equals(updatedDTO.getType())));
|
||
|
|
System.out.println("=========================================");
|
||
|
|
|
||
|
|
// 使用修正后的数据源
|
||
|
|
String dataSource = verifiedDataSource;
|
||
|
|
System.out.println("🔄 使用修正后的数据源: " + dataSource);
|
||
|
|
|
||
|
|
// 权限校验
|
||
|
|
System.out.println("🔐 ====== 开始权限校验 ======");
|
||
|
|
System.out.println("🔐 当前类型: " + updatedDTO.getType());
|
||
|
|
if (!"seller".equals(updatedDTO.getType()) && !"both".equals(updatedDTO.getType())) {
|
||
|
|
System.out.println("❌ 权限校验失败:采购员只能更新为seller或both类型,当前类型: " + updatedDTO.getType());
|
||
|
|
response.put("success", false);
|
||
|
|
response.put("message", "采购员权限只能更新为供应端类型客户");
|
||
|
|
return ResponseEntity.badRequest().body(response);
|
||
|
|
}
|
||
|
|
System.out.println("✅ 权限校验通过");
|
||
|
|
|
||
|
|
boolean success;
|
||
|
|
|
||
|
|
// 🎯 关键修复:根据修正后的数据源处理更新
|
||
|
|
if ("wechat".equals(dataSource)) {
|
||
|
|
System.out.println("🎯 使用wechat数据源更新客户信息");
|
||
|
|
if (updatedDTO.getPhoneNumber() == null || updatedDTO.getPhoneNumber().trim().isEmpty()) {
|
||
|
|
throw new IllegalArgumentException("更新微信数据源时手机号不能为空");
|
||
|
|
}
|
||
|
|
|
||
|
|
// 🚨 重要:检查客户是否真的在wechat数据源中
|
||
|
|
UserProductCartDTO wechatCustomer = supplyPoolCustomerService.getWechatCustomerByPhone(updatedDTO.getPhoneNumber());
|
||
|
|
if (wechatCustomer == null) {
|
||
|
|
System.out.println("❌ wechat数据源中未找到该客户,手机号: " + updatedDTO.getPhoneNumber());
|
||
|
|
response.put("success", false);
|
||
|
|
response.put("message", "微信数据源中未找到该客户信息");
|
||
|
|
return ResponseEntity.badRequest().body(response);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 🎯 关键修复:确保传递正确的负责人信息处理标志
|
||
|
|
success = supplyCustomerService.updateWechatCustomer(updatedDTO, authInfo, true);
|
||
|
|
|
||
|
|
} else if ("default".equals(dataSource)) {
|
||
|
|
System.out.println("🎯 使用默认数据源更新");
|
||
|
|
if (updatedDTO.getId() == null || updatedDTO.getId().trim().isEmpty()) {
|
||
|
|
throw new IllegalArgumentException("更新默认数据源时公司ID不能为空");
|
||
|
|
}
|
||
|
|
|
||
|
|
// 检查default数据源中客户是否存在
|
||
|
|
boolean exists = supplyCustomerService.checkDefaultCustomerExists(updatedDTO.getId());
|
||
|
|
if (!exists) {
|
||
|
|
System.out.println("❌ default数据源中不存在ID为" + updatedDTO.getId() + "的客户");
|
||
|
|
|
||
|
|
// 尝试检查该客户是否存在于wechat数据源
|
||
|
|
boolean existsInWechat = false;
|
||
|
|
try {
|
||
|
|
// 先尝试通过手机号查找
|
||
|
|
if (updatedDTO.getPhoneNumber() != null && !updatedDTO.getPhoneNumber().trim().isEmpty()) {
|
||
|
|
System.out.println("🔍 尝试通过手机号在wechat数据源中查找客户: " + updatedDTO.getPhoneNumber());
|
||
|
|
UnifiedCustomerDTO wechatCustomerDTO = supplyCustomerService.getCustomerByPhone(updatedDTO.getPhoneNumber(), authInfo);
|
||
|
|
if (wechatCustomerDTO != null) {
|
||
|
|
existsInWechat = true;
|
||
|
|
System.out.println("✅ 在wechat数据源中找到客户");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
// 如果手机号查找失败,尝试通过ID查找
|
||
|
|
if (!existsInWechat && updatedDTO.getId() != null && !updatedDTO.getId().trim().isEmpty()) {
|
||
|
|
System.out.println("🔍 尝试通过ID在wechat数据源中查找客户: " + updatedDTO.getId());
|
||
|
|
UnifiedCustomerDTO wechatCustomerDTO = supplyCustomerService.getWechatCustomerById(updatedDTO.getId(), authInfo);
|
||
|
|
if (wechatCustomerDTO != null) {
|
||
|
|
existsInWechat = true;
|
||
|
|
System.out.println("✅ 在wechat数据源中找到客户");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
} catch (Exception e) {
|
||
|
|
System.err.println("⚠️ 检查wechat数据源时出错: " + e.getMessage());
|
||
|
|
}
|
||
|
|
|
||
|
|
if (existsInWechat) {
|
||
|
|
System.out.println("🔄 自动切换到wechat数据源进行更新");
|
||
|
|
updatedDTO.setDataSource("wechat");
|
||
|
|
success = supplyCustomerService.updateWechatCustomer(updatedDTO, authInfo, true);
|
||
|
|
response.put("success", true);
|
||
|
|
response.put("message", "客户信息更新成功(自动切换到正确的数据源)");
|
||
|
|
return ResponseEntity.ok(response);
|
||
|
|
} else {
|
||
|
|
response.put("success", false);
|
||
|
|
response.put("message", "默认数据源中未找到该客户,请检查ID或数据源是否正确");
|
||
|
|
return ResponseEntity.badRequest().body(response);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
success = supplyCustomerService.updateDefaultCustomer(updatedDTO, authInfo);
|
||
|
|
|
||
|
|
// 验证更新结果
|
||
|
|
if (!success) {
|
||
|
|
System.out.println("❌ SQL执行成功但未更新任何记录,可能数据未变化或记录不存在");
|
||
|
|
response.put("success", false);
|
||
|
|
response.put("message", "更新失败,未找到可更新的客户记录(可能数据无变化或ID错误)");
|
||
|
|
return ResponseEntity.internalServerError().body(response);
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
System.out.println("❌ 不支持的数据源: " + dataSource);
|
||
|
|
response.put("success", false);
|
||
|
|
response.put("message", "不支持的数据源: " + dataSource);
|
||
|
|
return ResponseEntity.badRequest().body(response);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 🎯 关键修复:返回修正后的数据源信息给前端
|
||
|
|
Map<String, Object> result = new HashMap<>();
|
||
|
|
result.put("success", true);
|
||
|
|
result.put("message", "客户信息更新成功");
|
||
|
|
result.put("correctedDataSource", verifiedDataSource);
|
||
|
|
result.put("originalDataSource", originalDataSource);
|
||
|
|
|
||
|
|
if (!originalDataSource.equals(verifiedDataSource)) {
|
||
|
|
result.put("dataSourceCorrected", true);
|
||
|
|
result.put("message", "客户信息更新成功(数据源已自动修正)");
|
||
|
|
}
|
||
|
|
|
||
|
|
return ResponseEntity.ok(result);
|
||
|
|
|
||
|
|
} catch (IllegalArgumentException e) {
|
||
|
|
response.put("success", false);
|
||
|
|
response.put("message", e.getMessage());
|
||
|
|
return ResponseEntity.badRequest().body(response);
|
||
|
|
} catch (RuntimeException e) {
|
||
|
|
response.put("success", false);
|
||
|
|
response.put("message", e.getMessage());
|
||
|
|
return ResponseEntity.notFound().build();
|
||
|
|
} catch (Exception e) {
|
||
|
|
System.err.println("更新客户信息失败: " + e.getMessage());
|
||
|
|
e.printStackTrace();
|
||
|
|
response.put("success", false);
|
||
|
|
response.put("message", "服务器错误:" + e.getMessage());
|
||
|
|
return ResponseEntity.internalServerError().body(response);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 验证并修正数据源
|
||
|
|
* 根据客户ID和手机号自动检测客户实际所在的数据源
|
||
|
|
*/
|
||
|
|
private String verifyAndCorrectDataSource(UnifiedCustomerDTO updatedDTO) {
|
||
|
|
String dataSource = updatedDTO.getDataSource();
|
||
|
|
String customerId = updatedDTO.getId();
|
||
|
|
String phoneNumber = updatedDTO.getPhoneNumber();
|
||
|
|
|
||
|
|
System.out.println("🔍 开始验证数据源...");
|
||
|
|
System.out.println("📱 手机号: " + phoneNumber);
|
||
|
|
System.out.println("🆔 客户ID: " + customerId);
|
||
|
|
System.out.println("📊 前端指定数据源: " + dataSource);
|
||
|
|
|
||
|
|
// 🔥 关键修复:尊重前端指定的数据源,只在明确错误时修正
|
||
|
|
if ("default".equals(dataSource)) {
|
||
|
|
// 检查default数据源中客户是否存在
|
||
|
|
boolean existsInDefault = supplyCustomerService.checkDefaultCustomerExists(customerId);
|
||
|
|
|
||
|
|
if (existsInDefault) {
|
||
|
|
System.out.println("✅ 客户确实在default数据源中,保持数据源不变");
|
||
|
|
return "default";
|
||
|
|
} else {
|
||
|
|
System.out.println("⚠️ default数据源中未找到客户,尝试检查wechat数据源");
|
||
|
|
// 只有在default数据源不存在时才检查wechat
|
||
|
|
try {
|
||
|
|
UserProductCartDTO wechatCustomer = supplyPoolCustomerService.getWechatCustomerByPhone(phoneNumber);
|
||
|
|
if (wechatCustomer != null) {
|
||
|
|
System.out.println("🔄 检测到客户存在于wechat数据源,自动修正数据源");
|
||
|
|
return "wechat";
|
||
|
|
}
|
||
|
|
} catch (Exception e) {
|
||
|
|
System.err.println("❌ 检查wechat数据源失败: " + e.getMessage());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 保持原数据源
|
||
|
|
System.out.println("✅ 保持原数据源: " + dataSource);
|
||
|
|
return dataSource;
|
||
|
|
}
|
||
|
|
}
|