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.
1105 lines
53 KiB
1105 lines
53 KiB
|
3 months ago
|
package com.example.web.controller;
|
||
|
|
|
||
|
|
import com.example.web.dto.EnterpriseInfoDTO;
|
||
|
|
import com.example.web.dto.ManagerAuthInfo;
|
||
|
|
import com.example.web.dto.UnifiedCustomerDTO;
|
||
|
|
import com.example.web.dto.UserProductCartDTO;
|
||
|
|
import com.example.web.entity.Contacts;
|
||
|
|
import com.example.web.entity.Enterprise;
|
||
|
|
import com.example.web.entity.Managers;
|
||
|
|
import com.example.web.entity.UsersManagements;
|
||
|
|
import com.example.web.mapper.SupplyUsersManagementsMapper;
|
||
|
|
import com.example.web.service.*;
|
||
|
|
import jakarta.servlet.http.HttpServletRequest;
|
||
|
|
import jakarta.validation.Valid;
|
||
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
||
|
|
import org.springframework.http.HttpStatus;
|
||
|
|
import org.springframework.http.ResponseEntity;
|
||
|
|
import org.springframework.util.StringUtils;
|
||
|
|
import org.springframework.validation.BindingResult;
|
||
|
|
import org.springframework.web.bind.annotation.*;
|
||
|
|
|
||
|
|
|
||
|
|
import java.util.ArrayList;
|
||
|
|
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 SupplyEnterpriseController {
|
||
|
|
|
||
|
|
@Autowired
|
||
|
|
private SupplyEnterpriseService supplyenterpriseService;
|
||
|
|
@Autowired
|
||
|
|
private SupplyPoolCustomerService supplypoolCustomerService;
|
||
|
|
@Autowired
|
||
|
|
private SupplyCustomerService supplycustomerService;
|
||
|
|
@Autowired
|
||
|
|
private SupplyUsersManagementsMapper supplyUsersManagementsMapper;
|
||
|
|
|
||
|
|
// 修改类型转换方法 - 前端类型转数据库类型
|
||
|
|
private String convertFrontendTypeToDatabase(String frontendType) {
|
||
|
|
if (frontendType == null) {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
switch (frontendType) {
|
||
|
|
case "供应端":
|
||
|
|
return "seller";
|
||
|
|
case "客户端":
|
||
|
|
return "buyer";
|
||
|
|
case "BOTH":
|
||
|
|
return "both";
|
||
|
|
default:
|
||
|
|
return frontendType;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 确保数据库类型转前端类型也正确
|
||
|
|
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;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 添加完整的类型转换方法
|
||
|
|
private Map<String, Object> convertTypesForFrontend(Map<String, Object> response) {
|
||
|
|
if (response.containsKey("data")) {
|
||
|
|
Object data = response.get("data");
|
||
|
|
if (data instanceof Map) {
|
||
|
|
Map<String, UnifiedCustomerDTO> dataMap = (Map<String, UnifiedCustomerDTO>) data;
|
||
|
|
for (UnifiedCustomerDTO dto : dataMap.values()) {
|
||
|
|
String frontendType = convertDatabaseTypeToFrontend(dto.getType());
|
||
|
|
dto.setType(frontendType);
|
||
|
|
// 不再设置固定的数据源,数据源由实际来源决定
|
||
|
|
}
|
||
|
|
} else if (data instanceof List) {
|
||
|
|
List<?> dataList = (List<?>) data;
|
||
|
|
for (Object item : dataList) {
|
||
|
|
if (item instanceof UnifiedCustomerDTO) {
|
||
|
|
UnifiedCustomerDTO dto = (UnifiedCustomerDTO) item;
|
||
|
|
String frontendType = convertDatabaseTypeToFrontend(dto.getType());
|
||
|
|
dto.setType(frontendType);
|
||
|
|
// 不再设置固定的数据源,数据源由实际来源决定
|
||
|
|
} else if (item instanceof EnterpriseInfoDTO) {
|
||
|
|
EnterpriseInfoDTO enterpriseInfo = (EnterpriseInfoDTO) item;
|
||
|
|
if (enterpriseInfo.getEnterprise() != null) {
|
||
|
|
String frontendType = convertDatabaseTypeToFrontend(enterpriseInfo.getEnterprise().getType());
|
||
|
|
enterpriseInfo.getEnterprise().setType(frontendType);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
} else if (data instanceof UnifiedCustomerDTO) {
|
||
|
|
UnifiedCustomerDTO dto = (UnifiedCustomerDTO) data;
|
||
|
|
String frontendType = convertDatabaseTypeToFrontend(dto.getType());
|
||
|
|
dto.setType(frontendType);
|
||
|
|
// 不再设置固定的数据源,数据源由实际来源决定
|
||
|
|
} else if (data instanceof EnterpriseInfoDTO) {
|
||
|
|
EnterpriseInfoDTO enterpriseInfo = (EnterpriseInfoDTO) data;
|
||
|
|
if (enterpriseInfo.getEnterprise() != null) {
|
||
|
|
String frontendType = convertDatabaseTypeToFrontend(enterpriseInfo.getEnterprise().getType());
|
||
|
|
enterpriseInfo.getEnterprise().setType(frontendType);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return response;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 新增:电话号码重复检查接口(供前端预校验)
|
||
|
|
@GetMapping("/customers/check-phone")
|
||
|
|
public ResponseEntity<Map<String, Object>> checkPhoneNumber(@RequestParam String phoneNumber) {
|
||
|
|
System.out.println("电话号码重复检查接口(双数据源):------------"+phoneNumber);
|
||
|
|
Map<String, Object> response = new HashMap<>();
|
||
|
|
|
||
|
|
// 1. 非空校验
|
||
|
|
if (!StringUtils.hasText(phoneNumber)) {
|
||
|
|
response.put("success", false);
|
||
|
|
response.put("message", "电话号码不能为空");
|
||
|
|
response.put("isDuplicate", false);
|
||
|
|
return ResponseEntity.badRequest().body(response);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 2. 格式校验(简单示例:11位数字)
|
||
|
|
if (!phoneNumber.matches("^1[3-9]\\d{9}$")) {
|
||
|
|
response.put("success", false);
|
||
|
|
response.put("message", "请输入有效的11位手机号码");
|
||
|
|
response.put("isDuplicate", false);
|
||
|
|
return ResponseEntity.badRequest().body(response);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 3. 重复校验 - 现在检查双数据源
|
||
|
|
boolean isDuplicate = supplyenterpriseService.isPhoneNumberDuplicate(phoneNumber);
|
||
|
|
response.put("success", true);
|
||
|
|
response.put("isDuplicate", isDuplicate);
|
||
|
|
response.put("message", isDuplicate ? "电话号码已存在" : "电话号码可用");
|
||
|
|
return ResponseEntity.ok(response);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 新增:处理前端提交的客户数据(保存到默认数据源)
|
||
|
|
@PostMapping("/inputcustomers")
|
||
|
|
public ResponseEntity<Map<String, Object>> addCustomer(@Valid UnifiedCustomerDTO dto, BindingResult result) {
|
||
|
|
System.out.println("新增客户信息:------------");
|
||
|
|
System.out.println("原始类型: " + dto.getType());
|
||
|
|
System.out.println(dto.getPhoneNumber()+"----"+dto.getCreated_at()+"----"+dto.getNickName()+"----"+dto.getUserName());
|
||
|
|
Map<String, Object> response = new HashMap<>();
|
||
|
|
|
||
|
|
// 1. 参数校验(包括手机号非空)
|
||
|
|
if (result.hasErrors()) {
|
||
|
|
response.put("success", false);
|
||
|
|
response.put("message", "参数错误:" + result.getFieldError().getDefaultMessage());
|
||
|
|
return ResponseEntity.badRequest().body(response);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 在检查重复之前转换类型
|
||
|
|
String databaseType = convertFrontendTypeToDatabase(dto.getType());
|
||
|
|
dto.setType(databaseType);
|
||
|
|
System.out.println("转换后类型: " + dto.getType());
|
||
|
|
|
||
|
|
// 采购端权限:校验客户类型,只允许seller和both
|
||
|
|
System.out.println("权限校验 - 当前类型: " + dto.getType() + ", 是否seller: " + "seller".equals(dto.getType()) + ", 是否both: " + "both".equals(dto.getType()));
|
||
|
|
|
||
|
|
if (!"seller".equals(dto.getType()) && !"both".equals(dto.getType())) {
|
||
|
|
response.put("success", false);
|
||
|
|
response.put("message", "采购端权限只能新增供应端类型客户和BOTH类型客户");
|
||
|
|
return ResponseEntity.badRequest().body(response);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 2. 检查电话号码是否重复(同时检查双数据源)
|
||
|
|
if (supplyenterpriseService.isPhoneNumberDuplicate(dto.getPhoneNumber())) {
|
||
|
|
response.put("success", false);
|
||
|
|
response.put("message", "电话号码已存在,添加失败");
|
||
|
|
response.put("isDuplicate", true);
|
||
|
|
return ResponseEntity.badRequest().body(response);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 3. 执行新增逻辑
|
||
|
|
try {
|
||
|
|
boolean success = supplyenterpriseService.addCustomer(dto);
|
||
|
|
if (success) {
|
||
|
|
response.put("success", true);
|
||
|
|
response.put("message", "新增客户成功");
|
||
|
|
return ResponseEntity.ok(response);
|
||
|
|
} else {
|
||
|
|
response.put("success", false);
|
||
|
|
response.put("message", "新增客户失败");
|
||
|
|
return ResponseEntity.internalServerError().body(response);
|
||
|
|
}
|
||
|
|
} catch (Exception e) {
|
||
|
|
response.put("success", false);
|
||
|
|
response.put("message", "服务器错误:" + e.getMessage());
|
||
|
|
return ResponseEntity.internalServerError().body(response);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 获取所有客户完整信息 - 融合双数据查询 + 负责人过滤
|
||
|
|
* GET /supply/pool/customers
|
||
|
|
*/
|
||
|
|
@ResponseBody
|
||
|
|
@GetMapping("/customers")
|
||
|
|
public ResponseEntity<Map<String, Object>> getAllCustomers(HttpServletRequest request) {
|
||
|
|
System.out.println("====================================================");
|
||
|
|
System.out.println("🔄 获取所有客户信息 - 双数据源查询 + 负责人过滤");
|
||
|
|
System.out.println("====================================================");
|
||
|
|
|
||
|
|
Map<String, Object> response = new HashMap<>();
|
||
|
|
try {
|
||
|
|
// 从URL参数中获取用户类型,默认为采购端
|
||
|
|
String isSupplySideParam = request.getParameter("isSupplySide");
|
||
|
|
boolean isSupplySide = !"false".equalsIgnoreCase(isSupplySideParam);
|
||
|
|
System.out.println("🔍 根据登录信息判断用户类型: " + (isSupplySide ? "采购端" : "销售端"));
|
||
|
|
|
||
|
|
// 获取认证信息,基于登录信息判断用户类型
|
||
|
|
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());
|
||
|
|
System.out.println("🔐 负责人过滤条件: " +
|
||
|
|
"公司=" + authInfo.getManagercompany() +
|
||
|
|
", 部门=" + authInfo.getManagerdepartment());
|
||
|
|
|
||
|
|
// 1. 创建结果存储容器,优先使用手机号作为键
|
||
|
|
Map<String, UnifiedCustomerDTO> resultMap = new HashMap<>();
|
||
|
|
|
||
|
|
// 2. 处理primary数据源(企业信息)- 添加负责人过滤
|
||
|
|
System.out.println("🏢 开始获取企业数据...");
|
||
|
|
List<EnterpriseInfoDTO> enterpriseData = supplyenterpriseService.getAllEnterpriseInfo();
|
||
|
|
|
||
|
|
// 应用负责人过滤
|
||
|
|
List<EnterpriseInfoDTO> filteredEnterpriseData = enterpriseData.stream()
|
||
|
|
.filter(enterpriseInfo -> hasManagerPermission(enterpriseInfo, authInfo))
|
||
|
|
.collect(Collectors.toList());
|
||
|
|
|
||
|
|
long enterpriseEndTime = System.currentTimeMillis();
|
||
|
|
|
||
|
|
System.out.println("✅ 企业数据查询完成");
|
||
|
|
System.out.println("📊 企业数据条数: " + enterpriseData.size() + " → 过滤后: " + filteredEnterpriseData.size());
|
||
|
|
|
||
|
|
int enterpriseCount = 0;
|
||
|
|
if (filteredEnterpriseData != null) {
|
||
|
|
for (EnterpriseInfoDTO enterpriseInfo : filteredEnterpriseData) {
|
||
|
|
try {
|
||
|
|
UnifiedCustomerDTO dto = convertToUnifiedDTOFromEnterprise(enterpriseInfo);
|
||
|
|
String key = dto.getPhoneNumber() != null && !dto.getPhoneNumber().isEmpty()
|
||
|
|
? dto.getPhoneNumber()
|
||
|
|
: dto.getId();
|
||
|
|
|
||
|
|
if (!StringUtils.hasText(key)) {
|
||
|
|
System.out.println("⚠️ 跳过无有效标识的企业数据");
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
|
||
|
|
resultMap.put(key, dto);
|
||
|
|
enterpriseCount++;
|
||
|
|
|
||
|
|
// 打印前几条数据的详细信息
|
||
|
|
if (enterpriseCount <= 3) {
|
||
|
|
System.out.println("📝 企业客户样例 " + enterpriseCount + ": " +
|
||
|
|
"公司=" + dto.getCompany() +
|
||
|
|
", 联系人=" + dto.getNickName() +
|
||
|
|
", 手机=" + dto.getPhoneNumber() +
|
||
|
|
", 类型=" + dto.getType());
|
||
|
|
}
|
||
|
|
} catch (Exception e) {
|
||
|
|
System.err.println("❌ 处理企业数据时出错: " + e.getMessage());
|
||
|
|
e.printStackTrace();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
System.out.println("✅ 企业数据源成功处理: " + enterpriseCount + " 条记录");
|
||
|
|
}
|
||
|
|
|
||
|
|
// 3. 处理wechat数据源(用户信息)- 添加负责人过滤
|
||
|
|
System.out.println("📱 开始获取wechat数据...");
|
||
|
|
long wechatStartTime = System.currentTimeMillis();
|
||
|
|
|
||
|
|
List<UserProductCartDTO> userData = supplypoolCustomerService.getAllCustomers(authInfo);
|
||
|
|
|
||
|
|
|
||
|
|
// 🔥 新增:在Controller层进行负责人过滤之前,先收集所有用户ID用于批量查询
|
||
|
|
List<String> allUserIds = new ArrayList<>();
|
||
|
|
if (userData != null) {
|
||
|
|
for (UserProductCartDTO userInfo : userData) {
|
||
|
|
if (userInfo.getUserId() != null && !userInfo.getUserId().isEmpty()) {
|
||
|
|
allUserIds.add(userInfo.getUserId());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 🔥 新增:批量查询所有用户的负责人信息
|
||
|
|
Map<String, UsersManagements> managerMap = new HashMap<>();
|
||
|
|
if (!allUserIds.isEmpty()) {
|
||
|
|
try {
|
||
|
|
List<UsersManagements> allManagers = supplyUsersManagementsMapper.findByUserIds(allUserIds);
|
||
|
|
for (UsersManagements manager : allManagers) {
|
||
|
|
if (manager.getUserId() != null) {
|
||
|
|
managerMap.put(manager.getUserId(), manager);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
System.out.println("✅ 批量查询负责人信息完成,共获取 " + allManagers.size() + " 条记录");
|
||
|
|
} catch (Exception e) {
|
||
|
|
System.err.println("❌ 批量查询负责人信息失败: " + e.getMessage());
|
||
|
|
// 即使批量查询失败,也不影响后续逻辑,只是会使用默认值
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 在Controller层进行负责人过滤
|
||
|
|
List<UserProductCartDTO> filteredUserData = new ArrayList<>();
|
||
|
|
if (userData != null) {
|
||
|
|
for (UserProductCartDTO userInfo : userData) {
|
||
|
|
try {
|
||
|
|
if (hasPermissionToViewWechatCustomer(userInfo, authInfo)) {
|
||
|
|
filteredUserData.add(userInfo);
|
||
|
|
}
|
||
|
|
} catch (Exception e) {
|
||
|
|
System.err.println("❌ 检查用户权限时出错: " + e.getMessage());
|
||
|
|
// 单个用户权限检查失败不影响其他用户
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
long wechatEndTime = System.currentTimeMillis();
|
||
|
|
|
||
|
|
System.out.println("✅ Wechat数据查询完成");
|
||
|
|
System.out.println("📊 Wechat数据条数: " + userData.size() + " → 过滤后: " + filteredUserData.size());
|
||
|
|
System.out.println("⏱️ Wechat查询耗时: " + (wechatEndTime - wechatStartTime) + "ms");
|
||
|
|
|
||
|
|
int wechatCount = 0;
|
||
|
|
int updateCount = 0;
|
||
|
|
if (filteredUserData != null) {
|
||
|
|
for (UserProductCartDTO userInfo : filteredUserData) {
|
||
|
|
try {
|
||
|
|
String key = userInfo.getPhoneNumber() != null ? userInfo.getPhoneNumber() : "";
|
||
|
|
if (!StringUtils.hasText(key)) {
|
||
|
|
System.out.println("⚠️ 跳过无手机号的用户数据");
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
// 检查是否已存在该手机号的记录
|
||
|
|
if (resultMap.containsKey(key)) {
|
||
|
|
// 更新现有记录(补充wechat数据源的信息)
|
||
|
|
UnifiedCustomerDTO existingDto = resultMap.get(key);
|
||
|
|
updateUnifiedDTOWithWechatData(existingDto, userInfo);
|
||
|
|
updateCount++;
|
||
|
|
} else {
|
||
|
|
// 🔥 修改:传入managerMap,避免N+1查询
|
||
|
|
UnifiedCustomerDTO dto = convertToUnifiedDTOForPublicSea(userInfo, managerMap);
|
||
|
|
resultMap.put(key, dto);
|
||
|
|
wechatCount++;
|
||
|
|
}
|
||
|
|
} catch (Exception e) {
|
||
|
|
System.err.println("❌ 处理用户数据时出错: " + e.getMessage());
|
||
|
|
e.printStackTrace();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
System.out.println("✅ Wechat数据源成功处理: " + wechatCount + " 条新记录, " + updateCount + " 条更新记录");
|
||
|
|
}
|
||
|
|
|
||
|
|
// 4. 打印最终统计信息和详细信息
|
||
|
|
printFinalStatistics(resultMap, enterpriseCount, wechatCount, updateCount);
|
||
|
|
|
||
|
|
// 5. 构建响应
|
||
|
|
response.put("success", true);
|
||
|
|
response.put("data", resultMap);
|
||
|
|
response.put("message", "获取客户信息成功");
|
||
|
|
response.put("total", resultMap.size());
|
||
|
|
response.put("enterpriseCount", enterpriseCount);
|
||
|
|
response.put("wechatCount", wechatCount);
|
||
|
|
response.put("updateCount", updateCount);
|
||
|
|
|
||
|
|
return ResponseEntity.ok(convertTypesForFrontend(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);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 从请求中获取ManagerAuthInfo - 基于登录信息判断用户类型
|
||
|
|
*/
|
||
|
|
private ManagerAuthInfo getManagerAuthInfoFromRequest(HttpServletRequest request, boolean isSupplySide) {
|
||
|
|
System.out.println("🔍 获取认证信息,用户类型: " + (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 = "";
|
||
|
|
}
|
||
|
|
|
||
|
|
// 记录用户类型,但不进行严格的部门名称验证
|
||
|
|
System.out.println("✅ 用户认证信息获取成功: " + (isSupplySide ? "采购端" : "销售端"));
|
||
|
|
|
||
|
|
// 验证公司信息一致性
|
||
|
|
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 : ""
|
||
|
|
);
|
||
|
|
|
||
|
|
System.out.println("🎯 认证信息详情: " +
|
||
|
|
"系统类型=" + (isSupplySide ? "采购端" : "销售端") +
|
||
|
|
", 公司=" + authInfo.getManagercompany() +
|
||
|
|
", 部门=" + authInfo.getManagerdepartment() +
|
||
|
|
", 组织=" + authInfo.getOrganization() +
|
||
|
|
", 角色=" + authInfo.getRole() +
|
||
|
|
", 负责人=" + authInfo.getUserName() +
|
||
|
|
", 协助人=" + authInfo.getAssistant());
|
||
|
|
|
||
|
|
return authInfo;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 检查企业负责人权限
|
||
|
|
*/
|
||
|
|
private boolean hasManagerPermission(EnterpriseInfoDTO enterpriseInfo, ManagerAuthInfo authInfo) {
|
||
|
|
if (enterpriseInfo.getManagers() == null) {
|
||
|
|
System.out.println("✅ 企业客户无负责人信息,允许查看");
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
return isManagerMatch(enterpriseInfo.getManagers(), authInfo);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 检查负责人信息是否匹配(Managers)
|
||
|
|
*/
|
||
|
|
private boolean isManagerMatch(Managers manager, ManagerAuthInfo authInfo) {
|
||
|
|
boolean match =
|
||
|
|
(authInfo.getManagercompany() == null || authInfo.getManagercompany().equals(manager.getManagercompany())) &&
|
||
|
|
(authInfo.getManagerdepartment() == null || authInfo.getManagerdepartment().equals(manager.getManagerdepartment())) &&
|
||
|
|
(authInfo.getOrganization() == null || authInfo.getOrganization().equals(manager.getOrganization())) &&
|
||
|
|
(authInfo.getRole() == null || authInfo.getRole().equals(manager.getRole())) &&
|
||
|
|
(authInfo.getUserName() == null || authInfo.getUserName().equals(manager.getUserName()));
|
||
|
|
|
||
|
|
System.out.println("🔐 企业负责人权限检查: " + (match ? "✅ 匹配" : "❌ 不匹配"));
|
||
|
|
return match;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 检查是否有权限查看微信客户 - 修复版本
|
||
|
|
*/
|
||
|
|
private boolean hasPermissionToViewWechatCustomer(UserProductCartDTO userInfo, ManagerAuthInfo authInfo) {
|
||
|
|
try {
|
||
|
|
System.out.println("🔐 开始检查微信客户权限,用户ID: " + userInfo.getUserId());
|
||
|
|
|
||
|
|
// 公海池客户对所有人可见
|
||
|
|
if (isPublicSeaCustomer(userInfo)) {
|
||
|
|
System.out.println("✅ 公海池客户,无需负责人权限验证");
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 非公海池客户需要检查负责人权限
|
||
|
|
System.out.println("🔍 非公海池客户,检查负责人权限...");
|
||
|
|
boolean hasPermission = supplycustomerService.hasPermissionToViewWechatCustomer(userInfo, authInfo);
|
||
|
|
System.out.println("🔐 微信客户权限检查结果: " + (hasPermission ? "✅ 有权限" : "❌ 无权限"));
|
||
|
|
return hasPermission;
|
||
|
|
} catch (Exception e) {
|
||
|
|
System.err.println("❌ 检查微信客户权限失败: " + e.getMessage());
|
||
|
|
// 发生异常时,出于安全考虑返回false
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 判断是否为公海池客户
|
||
|
|
*/
|
||
|
|
private boolean isPublicSeaCustomer(UserProductCartDTO userInfo) {
|
||
|
|
// 定义公海池等级
|
||
|
|
java.util.Set<String> publicSeaLevels = java.util.Set.of("company-sea-pools", "organization-sea-pools", "department-sea-pools", "公海池");
|
||
|
|
|
||
|
|
// 检查等级是否为公海池
|
||
|
|
boolean isPublicSeaLevel = publicSeaLevels.contains(userInfo.getLevel());
|
||
|
|
|
||
|
|
System.out.println("🔍 客户等级检查: " + userInfo.getLevel() + " → 是否公海池: " + isPublicSeaLevel);
|
||
|
|
|
||
|
|
// 公海池客户:等级是公海池
|
||
|
|
return isPublicSeaLevel;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 从企业数据源转换DTO
|
||
|
|
*/
|
||
|
|
private UnifiedCustomerDTO convertToUnifiedDTOFromEnterprise(EnterpriseInfoDTO enterpriseInfo) {
|
||
|
|
UnifiedCustomerDTO dto = new UnifiedCustomerDTO();
|
||
|
|
|
||
|
|
// 设置企业信息
|
||
|
|
if (enterpriseInfo.getEnterprise() != null) {
|
||
|
|
dto.setId(enterpriseInfo.getEnterprise().getId() != null ? enterpriseInfo.getEnterprise().getId() : "");
|
||
|
|
dto.setCompany(enterpriseInfo.getEnterprise().getCompany() != null ? enterpriseInfo.getEnterprise().getCompany() : "");
|
||
|
|
dto.setRegion(enterpriseInfo.getEnterprise().getRegion() != null ? enterpriseInfo.getEnterprise().getRegion() : "");
|
||
|
|
dto.setLevel(enterpriseInfo.getEnterprise().getLevel() != null ? enterpriseInfo.getEnterprise().getLevel() : "");
|
||
|
|
String frontendType = convertDatabaseTypeToFrontend(enterpriseInfo.getEnterprise().getType());
|
||
|
|
dto.setType(frontendType != null ? frontendType : "");
|
||
|
|
dto.setDemand(enterpriseInfo.getEnterprise().getDemand() != null ? enterpriseInfo.getEnterprise().getDemand() : "");
|
||
|
|
dto.setSpec(enterpriseInfo.getEnterprise().getSpec() != null ? enterpriseInfo.getEnterprise().getSpec() : "");
|
||
|
|
}
|
||
|
|
|
||
|
|
// 设置联系人信息
|
||
|
|
if (enterpriseInfo.getContacts() != null) {
|
||
|
|
dto.setNickName(enterpriseInfo.getContacts().getNickName() != null ? enterpriseInfo.getContacts().getNickName() : "");
|
||
|
|
dto.setPhoneNumber(enterpriseInfo.getContacts().getPhoneNumber() != null ? enterpriseInfo.getContacts().getPhoneNumber() : "");
|
||
|
|
dto.setWechat(enterpriseInfo.getContacts().getWechat() != null ? enterpriseInfo.getContacts().getWechat() : "");
|
||
|
|
dto.setAccount(enterpriseInfo.getContacts().getAccount() != null ? enterpriseInfo.getContacts().getAccount() : "");
|
||
|
|
dto.setAccountNumber(enterpriseInfo.getContacts().getAccountNumber() != null ? enterpriseInfo.getContacts().getAccountNumber() : "");
|
||
|
|
dto.setBank(enterpriseInfo.getContacts().getBank() != null ? enterpriseInfo.getContacts().getBank() : "");
|
||
|
|
dto.setAddress(enterpriseInfo.getContacts().getAddress() != null ? enterpriseInfo.getContacts().getAddress() : "");
|
||
|
|
dto.setCreated_at(enterpriseInfo.getContacts().getCreated_at());
|
||
|
|
dto.setUpdated_at(enterpriseInfo.getContacts().getUpdated_at());
|
||
|
|
}
|
||
|
|
|
||
|
|
// 设置负责人信息(根据新的表结构)
|
||
|
|
if (enterpriseInfo.getManagers() != null) {
|
||
|
|
// 使用新的字段名
|
||
|
|
dto.setManagercompany(enterpriseInfo.getManagers().getManagercompany() != null ? enterpriseInfo.getManagers().getManagercompany() : "");
|
||
|
|
dto.setManagerdepartment(enterpriseInfo.getManagers().getManagerdepartment() != null ? enterpriseInfo.getManagers().getManagerdepartment() : "");
|
||
|
|
dto.setOrganization(enterpriseInfo.getManagers().getOrganization() != null ? enterpriseInfo.getManagers().getOrganization() : "");
|
||
|
|
dto.setRole(enterpriseInfo.getManagers().getRole() != null ? enterpriseInfo.getManagers().getRole() : "");
|
||
|
|
dto.setUserName(enterpriseInfo.getManagers().getUserName() != null ? enterpriseInfo.getManagers().getUserName() : "");
|
||
|
|
dto.setAssistant(enterpriseInfo.getManagers().getAssistant() != null ? enterpriseInfo.getManagers().getAssistant() : "");
|
||
|
|
}
|
||
|
|
|
||
|
|
// 设置数据源标识
|
||
|
|
dto.setDataSource("enterprise");
|
||
|
|
|
||
|
|
return dto;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 专门为公海池客户转换DTO的方法 - 补充负责人信息(优化版本)
|
||
|
|
*/
|
||
|
|
private UnifiedCustomerDTO convertToUnifiedDTOForPublicSea(
|
||
|
|
UserProductCartDTO userInfo,
|
||
|
|
Map<String, UsersManagements> managerMap) { // 🔥 新增参数:批量查询的负责人信息
|
||
|
|
|
||
|
|
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);
|
||
|
|
|
||
|
|
// 🔥 关键修改:从批量查询的managerMap中获取负责人信息,避免N+1查询
|
||
|
|
setManagerInfoFromMap(dto, userInfo.getUserId(), managerMap);
|
||
|
|
|
||
|
|
// 设置公海需求信息 - 从产品表获取(采购端特有逻辑)
|
||
|
|
if (userInfo.getProducts() != null && !userInfo.getProducts().isEmpty()) {
|
||
|
|
UserProductCartDTO.ProductInfo selectedProduct = userInfo.getProducts().get(0);
|
||
|
|
|
||
|
|
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());
|
||
|
|
} else {
|
||
|
|
// 如果没有产品信息,设置默认值
|
||
|
|
setDefaultProductValues(dto);
|
||
|
|
}
|
||
|
|
|
||
|
|
return dto;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 从批量查询的Map中设置负责人信息 - 避免N+1查询(与销售端保持一致)
|
||
|
|
*/
|
||
|
|
private void setManagerInfoFromMap(UnifiedCustomerDTO dto, String userId, Map<String, UsersManagements> managerMap) {
|
||
|
|
try {
|
||
|
|
System.out.println("🔍 从批量Map中查询用户负责人信息,用户ID: " + userId);
|
||
|
|
UsersManagements userManager = managerMap.get(userId);
|
||
|
|
|
||
|
|
if (userManager != null) {
|
||
|
|
System.out.println("✅ 从批量Map中找到负责人信息,设置到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("⚠️ 未在批量Map中找到负责人信息,用户可能为公海池客户");
|
||
|
|
// 设置默认值
|
||
|
|
setDefaultManagerValues(dto);
|
||
|
|
}
|
||
|
|
} catch (Exception e) {
|
||
|
|
System.err.println("❌ 从Map设置负责人信息失败: " + e.getMessage());
|
||
|
|
// 发生异常时设置默认值
|
||
|
|
setDefaultManagerValues(dto);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 设置默认负责人信息(与销售端保持一致)
|
||
|
|
*/
|
||
|
|
private void setDefaultManagerValues(UnifiedCustomerDTO dto) {
|
||
|
|
dto.setManagercompany("");
|
||
|
|
dto.setManagerdepartment("");
|
||
|
|
dto.setOrganization("");
|
||
|
|
dto.setRole("");
|
||
|
|
dto.setUserName("");
|
||
|
|
dto.setAssistant("");
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 用wechat数据更新现有的DTO(用于数据合并)- 采购端使用产品信息
|
||
|
|
*/
|
||
|
|
private void updateUnifiedDTOWithWechatData(UnifiedCustomerDTO existingDto, UserProductCartDTO userInfo) {
|
||
|
|
// 补充产品信息(如果企业数据中缺少)
|
||
|
|
if ((existingDto.getProductName() == null || existingDto.getProductName().isEmpty()) &&
|
||
|
|
userInfo.getProducts() != null && !userInfo.getProducts().isEmpty()) {
|
||
|
|
|
||
|
|
UserProductCartDTO.ProductInfo product = userInfo.getProducts().get(0);
|
||
|
|
existingDto.setProductName(product.getProductName() != null ? product.getProductName() : "");
|
||
|
|
existingDto.setVariety(product.getVariety() != null ? product.getVariety() : "");
|
||
|
|
existingDto.setSpecification(product.getSpecification() != null ? product.getSpecification() : "");
|
||
|
|
existingDto.setQuantity(product.getQuantity() != null ? product.getQuantity() : 0);
|
||
|
|
existingDto.setGrossWeight(product.getGrossWeight() != null ? product.getGrossWeight() : "");
|
||
|
|
existingDto.setYolk(product.getYolk() != null ? product.getYolk() : "");
|
||
|
|
|
||
|
|
// 设置customDetails
|
||
|
|
existingDto.setCustomDetails(userInfo.getProducts().toArray());
|
||
|
|
}
|
||
|
|
|
||
|
|
// 补充其他可能缺失的字段
|
||
|
|
if (existingDto.getLevel() == null || existingDto.getLevel().isEmpty()) {
|
||
|
|
existingDto.setLevel(userInfo.getLevel() != null ? userInfo.getLevel() : "公海池");
|
||
|
|
}
|
||
|
|
|
||
|
|
// 标记为混合数据源
|
||
|
|
existingDto.setDataSource("mixed");
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 打印最终统计信息 - 采购端版本
|
||
|
|
*/
|
||
|
|
private void printFinalStatistics(Map<String, UnifiedCustomerDTO> resultMap, int enterpriseCount, int wechatCount, int updateCount) {
|
||
|
|
System.out.println("====================================================");
|
||
|
|
System.out.println("📈 [采购端] 数据源查询统计:");
|
||
|
|
System.out.println("🏢 企业数据源: " + enterpriseCount + " 条记录");
|
||
|
|
System.out.println("📱 Wechat数据源: " + (wechatCount + updateCount) + " 条记录 (" + wechatCount + " 新增, " + updateCount + " 更新)");
|
||
|
|
System.out.println("📊 合并后总客户数: " + resultMap.size() + " 条记录");
|
||
|
|
System.out.println("====================================================");
|
||
|
|
|
||
|
|
// 打印详细样例(限制数量避免日志过多)
|
||
|
|
int sampleCount = 0;
|
||
|
|
System.out.println("📋 [采购端] 最终数据样例:");
|
||
|
|
for (Map.Entry<String, UnifiedCustomerDTO> entry : resultMap.entrySet()) {
|
||
|
|
if (sampleCount >= 5) break;
|
||
|
|
|
||
|
|
String key = entry.getKey();
|
||
|
|
UnifiedCustomerDTO dto = entry.getValue();
|
||
|
|
|
||
|
|
System.out.println("--- 客户 " + (sampleCount + 1) + " ---");
|
||
|
|
System.out.println(" 键: " + key);
|
||
|
|
System.out.println(" 数据源: " + dto.getDataSource());
|
||
|
|
System.out.println(" ID: " + dto.getId());
|
||
|
|
System.out.println(" 公司: " + dto.getCompany());
|
||
|
|
System.out.println(" 联系人: " + dto.getNickName());
|
||
|
|
System.out.println(" 手机: " + dto.getPhoneNumber());
|
||
|
|
System.out.println(" 微信号: " + dto.getWechat());
|
||
|
|
System.out.println(" 类型: " + dto.getType());
|
||
|
|
System.out.println(" 等级: " + dto.getLevel());
|
||
|
|
System.out.println(" 地区: " + dto.getRegion());
|
||
|
|
System.out.println(" 需求: " + dto.getDemand());
|
||
|
|
System.out.println(" 规格: " + dto.getSpec());
|
||
|
|
|
||
|
|
// 产品信息
|
||
|
|
System.out.println(" 产品名称: " + dto.getProductName());
|
||
|
|
System.out.println(" 品种: " + dto.getVariety());
|
||
|
|
System.out.println(" 规格说明: " + dto.getSpecification());
|
||
|
|
System.out.println(" 数量: " + dto.getQuantity());
|
||
|
|
System.out.println(" 毛重: " + dto.getGrossWeight());
|
||
|
|
System.out.println(" 蛋黄: " + dto.getYolk());
|
||
|
|
|
||
|
|
// 联系人详细信息
|
||
|
|
System.out.println(" 账户: " + dto.getAccount());
|
||
|
|
System.out.println(" 账号: " + dto.getAccountNumber());
|
||
|
|
System.out.println(" 银行: " + dto.getBank());
|
||
|
|
System.out.println(" 地址: " + dto.getAddress());
|
||
|
|
|
||
|
|
// 负责人信息
|
||
|
|
System.out.println(" 负责公司: " + dto.getManagercompany());
|
||
|
|
System.out.println(" 负责部门: " + dto.getManagerdepartment());
|
||
|
|
System.out.println(" 组织: " + dto.getOrganization());
|
||
|
|
System.out.println(" 角色: " + dto.getRole());
|
||
|
|
System.out.println(" 负责人: " + dto.getUserName());
|
||
|
|
System.out.println(" 协助人: " + dto.getAssistant());
|
||
|
|
|
||
|
|
// 时间信息
|
||
|
|
System.out.println(" 创建时间: " + dto.getCreated_at());
|
||
|
|
System.out.println(" 更新时间: " + dto.getUpdated_at());
|
||
|
|
|
||
|
|
sampleCount++;
|
||
|
|
System.out.println(); // 空行分隔
|
||
|
|
}
|
||
|
|
System.out.println("====================================================");
|
||
|
|
}
|
||
|
|
|
||
|
|
// 保留原有的辅助方法 - 采购端版本
|
||
|
|
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()) {
|
||
|
|
List<UserProductCartDTO.UsersContacts> validContacts = userInfo.getUsersContacts().stream()
|
||
|
|
.filter(contact -> contact != null)
|
||
|
|
.collect(Collectors.toList());
|
||
|
|
|
||
|
|
if (!validContacts.isEmpty()) {
|
||
|
|
UserProductCartDTO.UsersContacts contact = validContacts.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);
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
setDefaultContactValues(dto);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private void setDefaultContactValues(UnifiedCustomerDTO dto) {
|
||
|
|
dto.setWechat("");
|
||
|
|
dto.setAccount("");
|
||
|
|
dto.setAccountNumber("");
|
||
|
|
dto.setBank("");
|
||
|
|
dto.setAddress("");
|
||
|
|
}
|
||
|
|
|
||
|
|
// 其他方法保持不变...
|
||
|
|
/**
|
||
|
|
* 根据ID获取客户详细信息
|
||
|
|
* GET /supply/pool/customers/{id}
|
||
|
|
*/
|
||
|
|
@GetMapping("/{id}")
|
||
|
|
public ResponseEntity<Map<String, Object>> getCustomerById(@PathVariable String id) {
|
||
|
|
Map<String, Object> response = new HashMap<>();
|
||
|
|
try {
|
||
|
|
EnterpriseInfoDTO customer = supplyenterpriseService.getEnterpriseInfoById(id);
|
||
|
|
if (customer != null) {
|
||
|
|
response.put("success", true);
|
||
|
|
response.put("data", customer);
|
||
|
|
response.put("message", "获取客户信息成功");
|
||
|
|
return ResponseEntity.ok(response);
|
||
|
|
} else {
|
||
|
|
response.put("success", false);
|
||
|
|
response.put("message", "未找到对应的客户信息");
|
||
|
|
return ResponseEntity.notFound().build();
|
||
|
|
}
|
||
|
|
} catch (Exception e) {
|
||
|
|
response.put("success", false);
|
||
|
|
response.put("message", "获取客户信息失败: " + e.getMessage());
|
||
|
|
return ResponseEntity.internalServerError().body(response);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 搜索客户信息
|
||
|
|
* GET /supply/pool/customers/search?keyword=xxx
|
||
|
|
*/
|
||
|
|
@GetMapping("/search")
|
||
|
|
public ResponseEntity<Map<String, Object>> searchCustomers(@RequestParam(required = false) String keyword) {
|
||
|
|
Map<String, Object> response = new HashMap<>();
|
||
|
|
try {
|
||
|
|
List<EnterpriseInfoDTO> customers = supplyenterpriseService.searchEnterprises(keyword);
|
||
|
|
// 类型转换:将每个企业类型从数据库类型转换为前端类型
|
||
|
|
for (EnterpriseInfoDTO customer : customers) {
|
||
|
|
if (customer.getEnterprise() != null) {
|
||
|
|
String frontendType = convertDatabaseTypeToFrontend(customer.getEnterprise().getType());
|
||
|
|
customer.getEnterprise().setType(frontendType);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
response.put("success", true);
|
||
|
|
response.put("data", customers);
|
||
|
|
response.put("message", "搜索客户信息成功");
|
||
|
|
response.put("total", customers.size());
|
||
|
|
return ResponseEntity.ok(response);
|
||
|
|
} catch (Exception e) {
|
||
|
|
response.put("success", false);
|
||
|
|
response.put("message", "搜索客户信息失败: " + e.getMessage());
|
||
|
|
return ResponseEntity.internalServerError().body(response);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 获取所有企业基本信息
|
||
|
|
* GET /supply/pool/customers/enterprises
|
||
|
|
*/
|
||
|
|
@GetMapping("/enterprises")
|
||
|
|
public ResponseEntity<Map<String, Object>> getAllEnterprises() {
|
||
|
|
Map<String, Object> response = new HashMap<>();
|
||
|
|
try {
|
||
|
|
List<Enterprise> enterprises = supplyenterpriseService.getAllEnterprises();
|
||
|
|
// 类型转换:将每个企业类型从数据库类型转换为前端类型
|
||
|
|
for (Enterprise enterprise : enterprises) {
|
||
|
|
String frontendType = convertDatabaseTypeToFrontend(enterprise.getType());
|
||
|
|
enterprise.setType(frontendType);
|
||
|
|
}
|
||
|
|
response.put("success", true);
|
||
|
|
response.put("data", enterprises);
|
||
|
|
response.put("message", "获取企业信息成功");
|
||
|
|
response.put("total", enterprises.size());
|
||
|
|
return ResponseEntity.ok(response);
|
||
|
|
} catch (Exception e) {
|
||
|
|
response.put("success", false);
|
||
|
|
response.put("message", "获取企业信息失败: " + e.getMessage());
|
||
|
|
return ResponseEntity.internalServerError().body(response);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 获取指定企业的联系人信息
|
||
|
|
* GET /supply/pool/customers/{id}/contacts
|
||
|
|
*/
|
||
|
|
@GetMapping("/{id}/contacts")
|
||
|
|
public ResponseEntity<Map<String, Object>> getContactsByEnterpriseId(@PathVariable String id) {
|
||
|
|
Map<String, Object> response = new HashMap<>();
|
||
|
|
try {
|
||
|
|
List<Contacts> contacts = supplyenterpriseService.getContactsByEnterpriseId(id);
|
||
|
|
response.put("success", true);
|
||
|
|
response.put("data", contacts);
|
||
|
|
response.put("message", "获取联系人信息成功");
|
||
|
|
response.put("total", contacts.size());
|
||
|
|
return ResponseEntity.ok(response);
|
||
|
|
} catch (Exception e) {
|
||
|
|
response.put("success", false);
|
||
|
|
response.put("message", "获取联系人信息失败: " + e.getMessage());
|
||
|
|
return ResponseEntity.internalServerError().body(response);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 获取指定企业的负责人信息
|
||
|
|
* GET /supply/pool/customers/{id}/managers
|
||
|
|
*/
|
||
|
|
@GetMapping("/{id}/managers")
|
||
|
|
public ResponseEntity<Map<String, Object>> getManagersByEnterpriseId(@PathVariable String id) {
|
||
|
|
Map<String, Object> response = new HashMap<>();
|
||
|
|
try {
|
||
|
|
List<Managers> managers = supplyenterpriseService.getManagersByEnterpriseId(id);
|
||
|
|
response.put("success", true);
|
||
|
|
response.put("data", managers);
|
||
|
|
response.put("message", "获取负责人信息成功");
|
||
|
|
response.put("total", managers.size());
|
||
|
|
return ResponseEntity.ok(response);
|
||
|
|
} catch (Exception e) {
|
||
|
|
response.put("success", false);
|
||
|
|
response.put("message", "获取负责人信息失败: " + e.getMessage());
|
||
|
|
return ResponseEntity.internalServerError().body(response);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 健康检查接口
|
||
|
|
* GET /supply/pool/customers/health
|
||
|
|
*/
|
||
|
|
@GetMapping("/health")
|
||
|
|
public ResponseEntity<Map<String, Object>> healthCheck() {
|
||
|
|
Map<String, Object> response = new HashMap<>();
|
||
|
|
response.put("success", true);
|
||
|
|
response.put("message", "客户信息服务运行正常");
|
||
|
|
response.put("timestamp", System.currentTimeMillis());
|
||
|
|
return ResponseEntity.ok(response);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 获取所有客户信息 - 不使用前端参数,直接查询双数据源所有数据
|
||
|
|
* GET /pool/all-customers
|
||
|
|
*/
|
||
|
|
@ResponseBody
|
||
|
|
@GetMapping("/all-customers")
|
||
|
|
public ResponseEntity<Map<String, Object>> getAllCustomersWithoutFilter() {
|
||
|
|
System.out.println("====================================================");
|
||
|
|
System.out.println("🔄 开始获取所有客户信息 - 双数据源查询(无负责人过滤)");
|
||
|
|
System.out.println("====================================================");
|
||
|
|
|
||
|
|
Map<String, Object> response = new HashMap<>();
|
||
|
|
try {
|
||
|
|
// 1. 创建结果存储容器,优先使用手机号作为键
|
||
|
|
Map<String, UnifiedCustomerDTO> resultMap = new HashMap<>();
|
||
|
|
|
||
|
|
// 2. 处理primary数据源(企业信息)- 不进行负责人过滤
|
||
|
|
System.out.println("🏢 开始获取企业数据...");
|
||
|
|
List<EnterpriseInfoDTO> enterpriseData = supplyenterpriseService.getAllEnterpriseInfo();
|
||
|
|
|
||
|
|
System.out.println("✅ 企业数据查询完成");
|
||
|
|
System.out.println("📊 企业数据条数: " + enterpriseData.size());
|
||
|
|
|
||
|
|
int enterpriseCount = 0;
|
||
|
|
if (enterpriseData != null) {
|
||
|
|
for (EnterpriseInfoDTO enterpriseInfo : enterpriseData) {
|
||
|
|
try {
|
||
|
|
UnifiedCustomerDTO dto = convertToUnifiedDTOFromEnterprise(enterpriseInfo);
|
||
|
|
String key = dto.getPhoneNumber() != null && !dto.getPhoneNumber().isEmpty()
|
||
|
|
? dto.getPhoneNumber()
|
||
|
|
: dto.getId();
|
||
|
|
|
||
|
|
if (!StringUtils.hasText(key)) {
|
||
|
|
System.out.println("⚠️ 跳过无有效标识的企业数据");
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
|
||
|
|
resultMap.put(key, dto);
|
||
|
|
enterpriseCount++;
|
||
|
|
|
||
|
|
// 打印前几条数据的详细信息
|
||
|
|
if (enterpriseCount <= 3) {
|
||
|
|
System.out.println("📝 企业客户样例 " + enterpriseCount + ": " +
|
||
|
|
"公司=" + dto.getCompany() +
|
||
|
|
", 联系人=" + dto.getNickName() +
|
||
|
|
", 手机=" + dto.getPhoneNumber() +
|
||
|
|
", 类型=" + dto.getType());
|
||
|
|
}
|
||
|
|
} catch (Exception e) {
|
||
|
|
System.err.println("❌ 处理企业数据时出错: " + e.getMessage());
|
||
|
|
e.printStackTrace();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
System.out.println("✅ 企业数据源成功处理: " + enterpriseCount + " 条记录");
|
||
|
|
}
|
||
|
|
|
||
|
|
// 3. 处理wechat数据源(用户信息)- 不进行负责人过滤
|
||
|
|
System.out.println("📱 开始获取wechat数据...");
|
||
|
|
List<UserProductCartDTO> userData = supplypoolCustomerService.getAllCustomersWithoutFilter();
|
||
|
|
|
||
|
|
// 🔥 新增:批量查询负责人信息(与getAllCustomers方法相同)
|
||
|
|
List<String> allUserIds = new ArrayList<>();
|
||
|
|
if (userData != null) {
|
||
|
|
for (UserProductCartDTO userInfo : userData) {
|
||
|
|
if (userInfo.getUserId() != null && !userInfo.getUserId().isEmpty()) {
|
||
|
|
allUserIds.add(userInfo.getUserId());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
Map<String, UsersManagements> managerMap = new HashMap<>();
|
||
|
|
if (!allUserIds.isEmpty()) {
|
||
|
|
try {
|
||
|
|
List<UsersManagements> allManagers = supplyUsersManagementsMapper.findByUserIds(allUserIds);
|
||
|
|
for (UsersManagements manager : allManagers) {
|
||
|
|
if (manager.getUserId() != null) {
|
||
|
|
managerMap.put(manager.getUserId(), manager);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
System.out.println("✅ 批量查询负责人信息完成,共获取 " + allManagers.size() + " 条记录");
|
||
|
|
} catch (Exception e) {
|
||
|
|
System.err.println("❌ 批量查询负责人信息失败: " + e.getMessage());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
System.out.println("✅ Wechat数据查询完成");
|
||
|
|
System.out.println("📊 Wechat数据条数: " + userData.size());
|
||
|
|
|
||
|
|
int wechatCount = 0;
|
||
|
|
int updateCount = 0;
|
||
|
|
if (userData != null) {
|
||
|
|
for (UserProductCartDTO userInfo : userData) {
|
||
|
|
try {
|
||
|
|
String key = userInfo.getPhoneNumber() != null ? userInfo.getPhoneNumber() : "";
|
||
|
|
if (!StringUtils.hasText(key)) {
|
||
|
|
System.out.println("⚠️ 跳过无手机号的用户数据");
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 检查是否已存在该手机号的记录
|
||
|
|
if (resultMap.containsKey(key)) {
|
||
|
|
UnifiedCustomerDTO existingDto = resultMap.get(key);
|
||
|
|
updateUnifiedDTOWithWechatData(existingDto, userInfo);
|
||
|
|
updateCount++;
|
||
|
|
} else {
|
||
|
|
// 🔥 修改:传入managerMap
|
||
|
|
UnifiedCustomerDTO dto = convertToUnifiedDTOForPublicSea(userInfo, managerMap);
|
||
|
|
resultMap.put(key, dto);
|
||
|
|
wechatCount++;
|
||
|
|
}
|
||
|
|
} catch (Exception e) {
|
||
|
|
System.err.println("❌ 处理用户数据时出错: " + e.getMessage());
|
||
|
|
e.printStackTrace();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
System.out.println("✅ Wechat数据源成功处理: " + wechatCount + " 条新记录, " + updateCount + " 条更新记录");
|
||
|
|
}
|
||
|
|
|
||
|
|
// 4. 打印最终统计信息和详细信息
|
||
|
|
printFinalStatistics(resultMap, enterpriseCount, wechatCount, updateCount);
|
||
|
|
|
||
|
|
// 5. 构建响应
|
||
|
|
response.put("success", true);
|
||
|
|
response.put("data", resultMap);
|
||
|
|
response.put("message", "获取客户信息成功");
|
||
|
|
response.put("total", resultMap.size());
|
||
|
|
response.put("enterpriseCount", enterpriseCount);
|
||
|
|
response.put("wechatCount", wechatCount);
|
||
|
|
response.put("updateCount", updateCount);
|
||
|
|
|
||
|
|
return ResponseEntity.ok(convertTypesForFrontend(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);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|