From 551c6188203d5dead51bd448a8a13b989619e377 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=A6=E9=B8=BF=E5=B1=95?= <18031053041@163.com> Date: Wed, 15 Apr 2026 17:47:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=87=BA=E5=BA=93=E5=AF=BC=E5=85=A5=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../listener/ExcelParseService.java | 223 +++++++++--------- 1 file changed, 115 insertions(+), 108 deletions(-) diff --git a/mhd_wms/src/main/java/com/mhd/wms/domain/stockOutOrder/repository/listener/ExcelParseService.java b/mhd_wms/src/main/java/com/mhd/wms/domain/stockOutOrder/repository/listener/ExcelParseService.java index a8f73c3c3..378ae5270 100644 --- a/mhd_wms/src/main/java/com/mhd/wms/domain/stockOutOrder/repository/listener/ExcelParseService.java +++ b/mhd_wms/src/main/java/com/mhd/wms/domain/stockOutOrder/repository/listener/ExcelParseService.java @@ -7,6 +7,7 @@ import com.alibaba.excel.read.builder.ExcelReaderBuilder; import com.alibaba.excel.read.metadata.ReadSheet; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; +import com.alibaba.fastjson.TypeReference; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.mhd.common.core.domain.po.SysDictDataVo; import com.mhd.common.core.domain.po.UserPo; @@ -35,6 +36,7 @@ import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import java.io.IOException; import java.io.InputStream; +import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; @@ -155,10 +157,20 @@ public class ExcelParseService { } private void initParam(StockOutOrderDO stockOutOrderDO) { - //设置货主信息 - shipperParam(stockOutOrderDO); - //设置客户信息 - customerParam(stockOutOrderDO); + String customerName = stockOutOrderDO.getCustomerName(); + if (StringUtils.isEmpty(customerName)) { + return; + } + UserPo userPo = resolveCustomerShipperUserPoForImport(customerName); + if (userPo == null) { + throw new ServiceException("获取客户信息失败:未在名称与货主列表中匹配到当前组织下的客户,请核对名称或改用客户编号/ID"); + } + stockOutOrderDO.setShipperId(userPo.getUserId()); + stockOutOrderDO.setShipperCode(userPo.getUserMemberCode()); + stockOutOrderDO.setShipperName(userPo.getUserName()); + stockOutOrderDO.setCustomerId(userPo.getUserId()); + stockOutOrderDO.setCustomerCode(userPo.getUserMemberCode()); + stockOutOrderDO.setCustomerName(userPo.getUserName()); } /** @@ -168,88 +180,6 @@ public class ExcelParseService { return stockOutOrderDomainService.insert(stockOutOrderDO); } - /** - * @description 封装货主信息 - * @author ZhouGY - * @date 2024/5/9 20:43 - * @param stockOutOrderDO - */ - private void shipperParam(StockOutOrderDO stockOutOrderDO){ - String customerName = stockOutOrderDO.getCustomerName(); - if (StringUtils.isEmpty(customerName)) return; - Long loginOrganizationId = null; - LoginUser loginUser = SecurityUtils.getLoginUser(); - if (loginUser != null && loginUser.getUserPo() != null) { - loginOrganizationId = loginUser.getUserPo().getOrganizationId(); - } - if (!customerName.matches("\\d+")) { - AjaxResult ajaxResult = userServiceFeign.getInfoByName(customerName); - if(!"200".equals(String.valueOf(ajaxResult.get("code")))){ - throw new ServiceException("获取客户信息失败"); - } - UserPo userPo = JSON.parseObject(com.alibaba.fastjson2.JSONObject.toJSONString(ajaxResult.get("data")), UserPo.class); - if (!shipperMatchesImporterOrganization(userPo, loginOrganizationId)) { - throw new ServiceException("当前登录用户组织下不存在该货主信息或客户名称与组织不匹配"); - } - stockOutOrderDO.setShipperId(userPo.getUserId()); - stockOutOrderDO.setShipperCode(userPo.getUserMemberCode()); - stockOutOrderDO.setShipperName(userPo.getUserName()); - } else { - AjaxResult ajaxResult = userServiceFeign.getInfo(Long.valueOf(customerName)); - if(!"200".equals(String.valueOf(ajaxResult.get("code")))){ - throw new ServiceException("获取货主信息失败"); - } - UserPo userPo = JSON.parseObject(com.alibaba.fastjson2.JSONObject.toJSONString(ajaxResult.get("data")), UserPo.class); - if (!shipperMatchesImporterOrganization(userPo, loginOrganizationId)) { - throw new ServiceException("当前登录用户组织下不存在该货主信息或客户名称与组织不匹配"); - } - stockOutOrderDO.setShipperId(userPo.getUserId()); - stockOutOrderDO.setShipperCode(userPo.getUserMemberCode()); - stockOutOrderDO.setShipperName(userPo.getUserName()); - } - } - - /** - * @description 封装客户信息 - * @author ZhouGY - * @date 2024/5/9 20:43 - * @param stockOutOrderDO - */ - private void customerParam(StockOutOrderDO stockOutOrderDO){ - String customerName = stockOutOrderDO.getCustomerName(); - if (StringUtils.isEmpty(customerName)) return; - Long loginOrganizationId = null; - LoginUser loginUser = SecurityUtils.getLoginUser(); - if (loginUser != null && loginUser.getUserPo() != null) { - loginOrganizationId = loginUser.getUserPo().getOrganizationId(); - } - if (!customerName.matches("\\d+")) { - AjaxResult ajaxResult = userServiceFeign.getInfoByName(customerName); - if(!"200".equals(String.valueOf(ajaxResult.get("code")))){ - throw new ServiceException("获取客户信息失败"); - } - UserPo userPo = JSON.parseObject(com.alibaba.fastjson2.JSONObject.toJSONString(ajaxResult.get("data")), UserPo.class); - if (!shipperMatchesImporterOrganization(userPo, loginOrganizationId)) { - throw new ServiceException("当前登录用户组织下不存在该客户信息或客户名称与组织不匹配"); - } - stockOutOrderDO.setCustomerId(userPo.getUserId()); - stockOutOrderDO.setCustomerCode(userPo.getUserMemberCode()); - stockOutOrderDO.setCustomerName(userPo.getUserName()); - } else { - AjaxResult ajaxResult = userServiceFeign.getInfo(Long.valueOf(customerName)); - if(!"200".equals(String.valueOf(ajaxResult.get("code")))){ - throw new ServiceException("获取客户信息失败"); - } - UserPo userPo = JSON.parseObject(com.alibaba.fastjson2.JSONObject.toJSONString(ajaxResult.get("data")), UserPo.class); - if (!shipperMatchesImporterOrganization(userPo, loginOrganizationId)) { - throw new ServiceException("当前登录用户组织下不存在该客户信息或客户名称与组织不匹配"); - } - stockOutOrderDO.setCustomerId(userPo.getUserId()); - stockOutOrderDO.setCustomerCode(userPo.getUserMemberCode()); - stockOutOrderDO.setCustomerName(userPo.getUserName()); - } - } - /** * 校验导入明细是否已在当前货主下维护物料基础信息 */ @@ -324,11 +254,6 @@ public class ExcelParseService { return errorMsg; } - /** - * @description 根据客户名称获取货主ID(用于导入时提前获取货主ID) - * @param customerName 客户名称(在出库单中,客户名称就是货主名称) - * @return 货主ID,如果找不到则返回null - */ /** * 导入出库单:解析出的货主用户是否属于当前登录用户同一组织(organizationId)。 * 仅当登录用户带有 organizationId 时启用过滤;否则保持与旧行为一致。 @@ -344,44 +269,126 @@ public class ExcelParseService { return oid != null && loginOrganizationId.equals(oid); } - private Long getShipperIdByCustomerName(String customerName) { + /** + * 按客户名称或数字ID解析货主用户,逻辑与入库单导入一致:getInfoByName → 货主列表兜底,均做 organizationId 校验。 + */ + private UserPo resolveCustomerShipperUserPoForImport(String customerName) { if (StringUtils.isEmpty(customerName)) { return null; } - Long loginOrganizationId = null; LoginUser loginUser = SecurityUtils.getLoginUser(); if (loginUser != null && loginUser.getUserPo() != null) { loginOrganizationId = loginUser.getUserPo().getOrganizationId(); } + String shipperName = customerName.trim(); + if (StringUtils.isEmpty(shipperName)) { + return null; + } - try { - if (!customerName.matches("\\d+")) { - // 按名称查询 - AjaxResult ajaxResult = userServiceFeign.getInfoByName(customerName); - if ("200".equals(String.valueOf(ajaxResult.get("code")))) { + // 纯数字:按用户ID查询 + if (shipperName.matches("\\d+")) { + try { + Long possibleId = Long.valueOf(shipperName); + AjaxResult ajaxResult = userServiceFeign.getInfo(possibleId); + if ("200".equals(String.valueOf(ajaxResult.get("code"))) && ajaxResult.get("data") != null) { UserPo userPo = JSON.parseObject(com.alibaba.fastjson2.JSONObject.toJSONString(ajaxResult.get("data")), UserPo.class); if (userPo != null && shipperMatchesImporterOrganization(userPo, loginOrganizationId)) { - return userPo.getUserId(); + return userPo; } } - } else { - // 按ID查询 - AjaxResult ajaxResult = userServiceFeign.getInfo(Long.valueOf(customerName)); - if ("200".equals(String.valueOf(ajaxResult.get("code")))) { - UserPo userPo = JSON.parseObject(com.alibaba.fastjson2.JSONObject.toJSONString(ajaxResult.get("data")), UserPo.class); - if (userPo != null && shipperMatchesImporterOrganization(userPo, loginOrganizationId)) { - return userPo.getUserId(); + } catch (Exception e) { + // ignore + } + return null; + } + + // 按名称查询(用户中心) + try { + AjaxResult ajaxResult = userServiceFeign.getInfoByName(shipperName); + if ("200".equals(String.valueOf(ajaxResult.get("code"))) && ajaxResult.get("data") != null) { + UserPo userPo = JSON.parseObject(com.alibaba.fastjson2.JSONObject.toJSONString(ajaxResult.get("data")), UserPo.class); + if (userPo != null && shipperMatchesImporterOrganization(userPo, loginOrganizationId)) { + return userPo; + } + } + } catch (Exception e) { + // 继续货主列表兜底 + } + + // 货主列表兜底(与入库单 getShipperIdByNameOrCode 一致) + try { + AjaxResult shipperListResult = userServiceFeign.userShipperListAll(); + if ("200".equals(String.valueOf(shipperListResult.get("code"))) && shipperListResult.get("data") != null) { + Object data = shipperListResult.get("data"); + List> shipperList = null; + if (data instanceof List) { + @SuppressWarnings("unchecked") + List rawList = (List) data; + shipperList = new ArrayList<>(); + for (Object item : rawList) { + if (item instanceof Map) { + @SuppressWarnings("unchecked") + Map mapItem = (Map) item; + shipperList.add(mapItem); + } else { + Map mapItem = JSON.parseObject(JSONObject.toJSONString(item), Map.class); + shipperList.add(mapItem); + } + } + } else { + String jsonStr = JSONObject.toJSONString(data); + shipperList = JSON.parseObject(jsonStr, new TypeReference>>(){}); + } + + if (shipperList != null && !shipperList.isEmpty()) { + String normalizedShipperName = shipperName; + for (Map shipper : shipperList) { + Object userId = shipper.get("userId"); + Object userName = shipper.get("userName"); + if (userName == null) { + userName = shipper.get("name"); + } + if (userName == null) { + userName = shipper.get("shipperName"); + } + boolean matched = false; + if (StringUtils.isNotEmpty(normalizedShipperName) && userName != null) { + String normalizedUserName = String.valueOf(userName).trim(); + if (normalizedShipperName.equals(normalizedUserName) + || normalizedUserName.contains(normalizedShipperName)) { + matched = true; + } + } + if (matched && userId != null) { + Long candidateId = Long.valueOf(String.valueOf(userId)); + AjaxResult infoRes = userServiceFeign.getInfo(candidateId); + if ("200".equals(String.valueOf(infoRes.get("code"))) && infoRes.get("data") != null) { + UserPo up = JSON.parseObject(com.alibaba.fastjson2.JSONObject.toJSONString(infoRes.get("data")), UserPo.class); + if (shipperMatchesImporterOrganization(up, loginOrganizationId)) { + return up; + } + } + } } } } } catch (Exception e) { - // 如果查询失败,返回null,后续在shipperParam方法中会再次尝试 + // ignore } - return null; } + /** + * @description 根据客户名称获取货主ID(用于导入时提前获取货主ID) + * @param customerName 客户名称(在出库单中,客户名称就是货主名称) + * @return 货主ID,如果找不到则返回null + */ + private Long getShipperIdByCustomerName(String customerName) { + UserPo userPo = resolveCustomerShipperUserPoForImport(customerName); + return userPo != null ? userPo.getUserId() : null; + } + /** * @description 初始化物料明细列表,查询物料基础信息并设置物料基础信息ID(改为与入库单相同的逻辑) * @param outMaterialDetailDOList 出库物料明细列表