|
|
@@ -202,6 +202,7 @@ public class KwcContractTradeService {
|
|
|
queryTradeReq.setSupplementCode(reqVo.getSupplementCode());
|
|
|
queryTradeReq.setPurchaseEntId(reqVo.getPurchaseEntId());
|
|
|
queryTradeReq.setSupplyEntId(reqVo.getSupplyEntId());
|
|
|
+ queryTradeReq.setAgentEntId(reqVo.getAgentEntId());
|
|
|
queryTradeReq.setDispatchWay(reqVo.getDispatchWay());
|
|
|
queryTradeReq.setStatus(reqVo.getStatus());
|
|
|
queryTradeReq.setAgentFlag(reqVo.getAgentFlag());
|
|
|
@@ -359,6 +360,7 @@ public class KwcContractTradeService {
|
|
|
|
|
|
private void checkParam(TradeBaseInfoReqVo baseInfo, List<TradeGoodsInfoReqVo> goodsInfo) {
|
|
|
if (Objects.nonNull(baseInfo)) {
|
|
|
+ validateAgentUnitParam(baseInfo);
|
|
|
if (StringUtils.isNotBlank(baseInfo.getContractCode()) && baseInfo.getContractCode().length() > 20) {
|
|
|
throw new SystemException("合同编号超长!");
|
|
|
}
|
|
|
@@ -428,6 +430,28 @@ public class KwcContractTradeService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 校验代理单位参数,兼容不传代理单位的旧合同新增请求。
|
|
|
+ *
|
|
|
+ * @param baseInfo 贸易合同基础信息
|
|
|
+ */
|
|
|
+ static void validateAgentUnitParam(TradeBaseInfoReqVo baseInfo) {
|
|
|
+ if (Objects.isNull(baseInfo)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ boolean hasAgentEnt = Objects.nonNull(baseInfo.getAgentEntId());
|
|
|
+ boolean hasAgentPhone = StringUtils.isNotBlank(baseInfo.getAgentPhone());
|
|
|
+ if (!hasAgentEnt && !hasAgentPhone) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (!hasAgentEnt) {
|
|
|
+ throw new SystemException("代理单位不能为空");
|
|
|
+ }
|
|
|
+ if (!hasAgentPhone) {
|
|
|
+ throw new SystemException("代理单位联系电话不能为空");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private static boolean hasDuplicateGoodsId(List<TradeGoodsInfoReqVo> goodsInfo) {
|
|
|
if (goodsInfo == null || goodsInfo.isEmpty()) {
|
|
|
return false;
|
|
|
@@ -1738,6 +1762,9 @@ public class KwcContractTradeService {
|
|
|
entIds.add(Long.valueOf(req.getSupplyEntId()));
|
|
|
//type = CooperateTypeEnum.SUPPLIER.getCode();
|
|
|
}
|
|
|
+ if (org.apache.commons.lang3.StringUtils.isNotBlank(req.getAgentEntId())) {
|
|
|
+ entIds.add(Long.valueOf(req.getAgentEntId()));
|
|
|
+ }
|
|
|
Set<Long> contractIds = buildTradeQueryContractIds(entId, req, entIds);
|
|
|
if (org.apache.commons.collections4.CollectionUtils.isEmpty(contractIds)) {
|
|
|
return Collections.emptyList();
|
|
|
@@ -1874,6 +1901,11 @@ public class KwcContractTradeService {
|
|
|
.filter(x -> org.apache.commons.lang3.StringUtils.equals(x.getProvideEntId(), req.getSupplyEntId()))
|
|
|
.collect(Collectors.toList());
|
|
|
}
|
|
|
+ if (org.apache.commons.lang3.StringUtils.isNotBlank(req.getAgentEntId())) {
|
|
|
+ queryListResVos = queryListResVos.stream()
|
|
|
+ .filter(x -> org.apache.commons.lang3.StringUtils.equals(x.getAgentEntId(), req.getAgentEntId()))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ }
|
|
|
return queryListResVos.stream()
|
|
|
.sorted(Comparator.comparing(QueryListResVo::getUpdateTime).reversed()
|
|
|
.thenComparing(Comparator.comparing(QueryListResVo::getId).reversed()))
|
|
|
@@ -1959,6 +1991,22 @@ public class KwcContractTradeService {
|
|
|
log.debug("供应方过滤后剩余合同数量: {} (原数量: {})", contractIds.size(), beforeSize);
|
|
|
}
|
|
|
|
|
|
+ if (org.apache.commons.lang3.StringUtils.isNotBlank(req.getAgentEntId())) {
|
|
|
+ Long agentEntId = Long.valueOf(req.getAgentEntId());
|
|
|
+ log.debug("应用代理单位过滤条件: agentEntId = {}", agentEntId);
|
|
|
+
|
|
|
+ Set<Long> agentContractIds = units.stream()
|
|
|
+ .filter(unit -> Objects.equals(unit.getEntId(), agentEntId))
|
|
|
+ .filter(unit -> Objects.equals(unit.getUnitType(), CooperateTypeEnum.SUPPLIER.getCode()))
|
|
|
+ .map(KwcContractTradeUnit::getContractId)
|
|
|
+ .filter(Objects::nonNull)
|
|
|
+ .collect(Collectors.toSet());
|
|
|
+
|
|
|
+ int beforeSize = contractIds.size();
|
|
|
+ contractIds.retainAll(agentContractIds);
|
|
|
+ log.debug("代理单位过滤后剩余合同数量: {} (原数量: {})", contractIds.size(), beforeSize);
|
|
|
+ }
|
|
|
+
|
|
|
log.info("最终构建的合同ID集合大小: {}, entId: {}, purchaseEntId: {}, supplyEntId: {}",
|
|
|
contractIds.size(), entId, req.getPurchaseEntId(), req.getSupplyEntId());
|
|
|
return contractIds;
|
|
|
@@ -1979,6 +2027,9 @@ public class KwcContractTradeService {
|
|
|
if (org.apache.commons.lang3.StringUtils.isNotBlank(req.getSupplyEntId())) {
|
|
|
entIds.add(Long.valueOf(req.getSupplyEntId()));
|
|
|
}
|
|
|
+ if (org.apache.commons.lang3.StringUtils.isNotBlank(req.getAgentEntId())) {
|
|
|
+ entIds.add(Long.valueOf(req.getAgentEntId()));
|
|
|
+ }
|
|
|
Set<Long> contractIds = buildTradeQueryContractIds(entId, req, entIds);
|
|
|
if (org.apache.commons.collections4.CollectionUtils.isEmpty(contractIds)) {
|
|
|
return 0L;
|
|
|
@@ -2038,7 +2089,8 @@ public class KwcContractTradeService {
|
|
|
}
|
|
|
|
|
|
if (org.apache.commons.lang3.StringUtils.isNotBlank(req.getPurchaseEntId())
|
|
|
- || org.apache.commons.lang3.StringUtils.isNotBlank(req.getSupplyEntId())) {
|
|
|
+ || org.apache.commons.lang3.StringUtils.isNotBlank(req.getSupplyEntId())
|
|
|
+ || org.apache.commons.lang3.StringUtils.isNotBlank(req.getAgentEntId())) {
|
|
|
Set<Long> tradeContractIds = records.stream().map(KwcContractTrade::getId).collect(Collectors.toSet());
|
|
|
List<KwcContractTradeUnit> tradeUnits = kwcContractTradeUnitRepository.queryByContractIds(tradeContractIds);
|
|
|
Map<String, KwcContractTradeUnit> contractUnitTypeKeyAndUnitMap = Maps.newHashMap();
|
|
|
@@ -2065,6 +2117,14 @@ public class KwcContractTradeService {
|
|
|
return org.apache.commons.lang3.StringUtils.equals(String.valueOf(provideEnt.getEntId()), req.getSupplyEntId());
|
|
|
}).collect(Collectors.toList());
|
|
|
}
|
|
|
+ if (org.apache.commons.lang3.StringUtils.isNotBlank(req.getAgentEntId())) {
|
|
|
+ records = records.stream().filter(record -> {
|
|
|
+ KwcContractTradeUnit provideEnt =
|
|
|
+ finalContractUnitTypeKeyAndUnitMap.getOrDefault(record.getId() + "-" + CooperateTypeEnum.SUPPLIER.getCode(),
|
|
|
+ new KwcContractTradeUnit());
|
|
|
+ return org.apache.commons.lang3.StringUtils.equals(String.valueOf(provideEnt.getEntId()), req.getAgentEntId());
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+ }
|
|
|
}
|
|
|
return records.size();
|
|
|
}
|
|
|
@@ -2123,6 +2183,10 @@ public class KwcContractTradeService {
|
|
|
new KwcContractTradeUnit());
|
|
|
queryListResVo.setProvideEntId(String.valueOf(provideEnt.getEntId()));
|
|
|
queryListResVo.setProvideEntName(provideEnt.getFirmName());
|
|
|
+ if (Objects.equals(t.getAgentFlag(), Global.YES)) {
|
|
|
+ queryListResVo.setAgentEntId(String.valueOf(provideEnt.getEntId()));
|
|
|
+ queryListResVo.setAgentEntName(provideEnt.getFirmName());
|
|
|
+ }
|
|
|
//queryListResVo.setCheckedEntName();
|
|
|
queryListResVo.setContractNo(t.getContractNo());
|
|
|
queryListResVo.setContractName(t.getName());
|
|
|
@@ -2191,72 +2255,133 @@ public class KwcContractTradeService {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
+ * 查询符合条件的物流合同订单列表。
|
|
|
+ * <p>
|
|
|
+ * 主要逻辑:
|
|
|
+ * 1. 校验入参(商品ID、托运方企业ID)。
|
|
|
+ * 2. 根据托运方企业ID查询其关联的物流合同单位,获取合同ID集合。
|
|
|
+ * 3. 根据商品ID筛选出包含该商品的物流合同ID集合(用于后续过滤非通用价格合同)。
|
|
|
+ * 4. 查询物流合同主表信息,并进行过滤:
|
|
|
+ * - 保留“指定商品关联的合同”或“拥有通用价格的合同”。
|
|
|
+ * - 状态必须为“已签约”。
|
|
|
+ * - 当前时间必须在合同有效期内(处理结束时间为空的情况,视为永久有效)。
|
|
|
+ * 5. 组装返回结果,填充托运方、承运方信息及商品价格(优先使用特定商品价格,其次使用通用价格)。
|
|
|
+ * 6. 特殊处理:如果传入了贸易合同ID,且该贸易合同满足“买家托运”且“按装货量结算”的条件,则在结果列表首位插入一条“平台默认物流”记录。
|
|
|
+ * </p>
|
|
|
+ *
|
|
|
+ * @param logisticsOrderDto 查询条件DTO,包含 goodsId, entId, tradeContractId
|
|
|
+ * @return 物流合同订单响应列表
|
|
|
+ */
|
|
|
public List<ContractLogisticsOrderResDto> queryContractLogisticsOrder(LogisticsOrderDto logisticsOrderDto) {
|
|
|
- log.info("查询物流合同请求:{}", JSON.toJSONString(logisticsOrderDto));
|
|
|
+ log.info("开始查询物流合同订单,请求参数:{}", JSON.toJSONString(logisticsOrderDto));
|
|
|
|
|
|
+ // 1. 参数校验
|
|
|
if (Objects.isNull(logisticsOrderDto.getGoodsId())) {
|
|
|
+ log.warn("查询物流合同订单失败:商品ID为空");
|
|
|
throw new BusinessException("商品id不能为空!");
|
|
|
}
|
|
|
if (Objects.isNull(logisticsOrderDto.getEntId())) {
|
|
|
+ log.warn("查询物流合同订单失败:托运方企业ID为空");
|
|
|
throw new BusinessException("托运方企业id不能为空!");
|
|
|
}
|
|
|
- //通过托运企业查询物流公司
|
|
|
- List<KwcContractLogisticsUnit> contractLogistics =
|
|
|
+
|
|
|
+ // 2. 根据托运方企业ID查询关联的物流合同单位
|
|
|
+ List<KwcContractLogisticsUnit> contractLogisticsUnits =
|
|
|
kwcContractLogisticsUnitRepository.queryByEntId(logisticsOrderDto.getEntId());
|
|
|
- if (org.apache.commons.collections4.CollectionUtils.isEmpty(contractLogistics)) {
|
|
|
+ if (org.apache.commons.collections4.CollectionUtils.isEmpty(contractLogisticsUnits)) {
|
|
|
+ log.debug("未找到企业[{}]关联的物流合同单位,返回空列表", logisticsOrderDto.getEntId());
|
|
|
return Collections.emptyList();
|
|
|
}
|
|
|
- //物流订单合id
|
|
|
- Set<Long> logTradeContractIds = contractLogistics.stream()
|
|
|
- .map(KwcContractLogisticsUnit::getContractId).collect(Collectors.toSet());
|
|
|
-
|
|
|
- //查询商品信息
|
|
|
- List<KwcContractLogisticsGoods> kwcContractLogisticsGoods = kwcContractLogisticsGoodsRepository.queryByLogIdsAndGoodsId(logTradeContractIds,
|
|
|
- logisticsOrderDto.getGoodsId());
|
|
|
+
|
|
|
+ // 提取物流合同ID集合
|
|
|
+ Set<Long> logTradeContractIds = contractLogisticsUnits.stream()
|
|
|
+ .map(KwcContractLogisticsUnit::getContractId)
|
|
|
+ .filter(Objects::nonNull)
|
|
|
+ .collect(Collectors.toSet());
|
|
|
+ log.debug("企业[{}]关联的物流合同ID数量: {}", logisticsOrderDto.getEntId(), logTradeContractIds.size());
|
|
|
+
|
|
|
+ // 3. 根据商品ID筛选包含该商品的物流合同
|
|
|
+ // 目的:识别哪些合同是明确约定了该商品价格的,这些合同即使没有设置通用价格也应被保留
|
|
|
+ List<KwcContractLogisticsGoods> kwcContractLogisticsGoods = kwcContractLogisticsGoodsRepository.queryByLogIdsAndGoodsId(
|
|
|
+ logTradeContractIds,
|
|
|
+ logisticsOrderDto.getGoodsId()
|
|
|
+ );
|
|
|
+ log.debug("包含商品ID[{}]的物流合同数量: {}", logisticsOrderDto.getGoodsId(), kwcContractLogisticsGoods.size());
|
|
|
Map<Long, KwcContractLogisticsGoods> contractLogisticsGoodsMap = Maps.newHashMap();
|
|
|
Set<Long> goodsContractIds = new HashSet<>();
|
|
|
if (org.apache.commons.collections4.CollectionUtils.isNotEmpty(kwcContractLogisticsGoods)) {
|
|
|
+ // 构建合同ID -> 商品信息的映射
|
|
|
contractLogisticsGoodsMap = kwcContractLogisticsGoods.stream()
|
|
|
- .collect(Collectors.toMap(KwcContractLogisticsGoods::getContractId,
|
|
|
- Function.identity(), (x, y) -> x));
|
|
|
+ .collect(Collectors.toMap(KwcContractLogisticsGoods::getContractId, Function.identity(), (x, y) -> x));
|
|
|
+ // 提取包含该商品的合同ID集合
|
|
|
goodsContractIds = kwcContractLogisticsGoods.stream()
|
|
|
.map(KwcContractLogisticsGoods::getContractId)
|
|
|
.collect(Collectors.toSet());
|
|
|
+ log.debug("包含商品ID[{}]的物流合同数量: {}", logisticsOrderDto.getGoodsId(), goodsContractIds.size());
|
|
|
}
|
|
|
|
|
|
- //查询物流订单 如果是通用价格不为空的不能过滤掉
|
|
|
+ // 4. 查询物流合同主表信息
|
|
|
List<KwcContractLogistics> logistics = contractLogisticsRepository.queryByLogisticContractIds(logTradeContractIds);
|
|
|
+ if (CollectionUtils.isEmpty(logistics)) {
|
|
|
+ log.debug("未查询到任何物流合同主表信息,返回空列表");
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 5. 初步过滤合同:
|
|
|
+ // 规则:合同必须包含指定商品(在 goodsContractIds 中) 或者 合同设置了通用价格(commonPrice 不为空)
|
|
|
Set<Long> finalGoodsContractIds = goodsContractIds;
|
|
|
- logistics = logistics.stream().filter(x-> finalGoodsContractIds.contains(x.getId()) || Objects.nonNull(x.getCommonPrice()))
|
|
|
- .distinct().collect(Collectors.toList());
|
|
|
+ logistics = logistics.stream()
|
|
|
+ .filter(x -> finalGoodsContractIds.contains(x.getId()) || Objects.nonNull(x.getCommonPrice()))
|
|
|
+ .distinct()
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
if (CollectionUtils.isEmpty(logistics)) {
|
|
|
+ log.debug("经过商品/通用价格过滤后,剩余物流合同数量为0,返回空列表");
|
|
|
return Collections.emptyList();
|
|
|
}
|
|
|
+ log.debug("经过商品/通用价格过滤后,剩余物流合同数量: {}", logistics.size());
|
|
|
+
|
|
|
+ // 6. 进一步过滤:状态校验与有效期校验
|
|
|
Date date = new Date();
|
|
|
logistics = logistics.stream()
|
|
|
.peek(log -> {
|
|
|
+ // 如果结束时间为空,视为永久有效,设置为一个极大的日期以便后续比较
|
|
|
if (Objects.isNull(log.getEndTime())) {
|
|
|
LocalDate localDate = LocalDate.of(9999, 12, 30);
|
|
|
- Date date1 = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
|
|
|
- log.setEndTime(date1);
|
|
|
+ Date maxDate = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
|
|
|
+ log.setEndTime(maxDate);
|
|
|
}
|
|
|
})
|
|
|
.filter(log -> Objects.nonNull(log.getStartTime()) && Objects.nonNull(log.getEndTime()))
|
|
|
- .filter(log -> Objects.equals(log.getStatus(), ContractStatusEnum.SIGNED.getCode())
|
|
|
- && log.getStartTime().before(date) && log.getEndTime().after(date))
|
|
|
+ .filter(log ->
|
|
|
+ // 状态必须为已签约
|
|
|
+ Objects.equals(log.getStatus(), ContractStatusEnum.SIGNED.getCode())
|
|
|
+ // 当前时间必须在开始时间之后(或等于)
|
|
|
+ && !log.getStartTime().after(date) && !log.getEndTime().before(date)
|
|
|
+ )
|
|
|
.collect(Collectors.toList());
|
|
|
+
|
|
|
if (CollectionUtils.isEmpty(logistics)) {
|
|
|
+ log.debug("经过状态和有效期过滤后,剩余物流合同数量为0,返回空列表");
|
|
|
return Collections.emptyList();
|
|
|
}
|
|
|
- //查询企业
|
|
|
+ log.debug("经过状态和有效期过滤后,最终符合条件的物流合同数量: {}", logistics.size());
|
|
|
+
|
|
|
+ // 7. 查询合同关联的单位信息(托运方、承运方等)
|
|
|
List<KwcContractLogisticsUnit> units = kwcContractLogisticsUnitRepository.queryByContractIds(logTradeContractIds);
|
|
|
- //物流订单和类型组成唯一key
|
|
|
Map<String, KwcContractLogisticsUnit> contractLogisticsMap = Maps.newHashMap();
|
|
|
if (org.apache.commons.collections4.CollectionUtils.isNotEmpty(units)) {
|
|
|
+ // Key格式: contractId_unitType
|
|
|
contractLogisticsMap = units.stream()
|
|
|
- .collect(Collectors.toMap(c -> c.getContractId() + "_" + c.getUnitType(),
|
|
|
- Function.identity(), (x, y) -> x));
|
|
|
+ .collect(Collectors.toMap(
|
|
|
+ c -> c.getContractId() + "_" + c.getUnitType(),
|
|
|
+ Function.identity(),
|
|
|
+ (x, y) -> x // 冲突时保留第一个
|
|
|
+ ));
|
|
|
}
|
|
|
+
|
|
|
+ // 8. 组装返回结果
|
|
|
Map<String, KwcContractLogisticsUnit> finalContractLogisticsMap = contractLogisticsMap;
|
|
|
Map<Long, KwcContractLogisticsGoods> finalContractLogisticsGoodsMap = contractLogisticsGoodsMap;
|
|
|
List<ContractLogisticsOrderResDto> collect = logistics.stream()
|
|
|
@@ -2325,63 +2450,157 @@ public class KwcContractTradeService {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
+ * 查询贸易合同订单信息(主要用于下拉框选择等场景)。
|
|
|
+ * <p>
|
|
|
+ * 根据传入的企业ID、企业类型和商品ID,查询符合条件的已签约贸易合同列表。
|
|
|
+ * 针对供应商类型(Supplier),会进一步过滤为当前登录用户作为销售员的合同;
|
|
|
+ * 针对其他类型(如采购商),则基于当前登录企业进行过滤。
|
|
|
+ * </p>
|
|
|
+ *
|
|
|
+ * @param contractTradeOrderDto 查询条件DTO,包含 goodsId(商品ID)、entId(企业ID)、entType(企业类型)、keyword(搜索关键字)
|
|
|
+ * @return 贸易合同订单信息列表 {@link ContractTradeOrderInfo}
|
|
|
+ */
|
|
|
public List<ContractTradeOrderInfo> queryContractTradeOrder(ContractTradeOrderDto contractTradeOrderDto) {
|
|
|
- log.info("查询贸易合同订单,请求:{}", JSON.toJSONString(contractTradeOrderDto));
|
|
|
+ log.info("开始查询贸易合同订单,请求参数:{}", JSON.toJSONString(contractTradeOrderDto));
|
|
|
+
|
|
|
+ // 1. 参数校验:确保商品ID、企业ID和企业类型不为空
|
|
|
checkParam(contractTradeOrderDto.getGoodsId(), contractTradeOrderDto.getEntId(), contractTradeOrderDto.getEntType());
|
|
|
- //查询贸易合同企业信息
|
|
|
- List<KwcContractTradeUnit> tradeUnits = kwcContractTradeUnitRepository.queryByEntIdAndUnitType(contractTradeOrderDto.getEntId(),
|
|
|
- contractTradeOrderDto.getEntType());
|
|
|
+
|
|
|
+ // 2. 根据企业和类型查询关联的合同单位信息
|
|
|
+ // 例如:查询某供应商参与的所有作为“供应方”的合同,或某采购商参与的所有作为“采购方”的合同
|
|
|
+ List<KwcContractTradeUnit> tradeUnits = kwcContractTradeUnitRepository.queryByEntIdAndUnitType(
|
|
|
+ contractTradeOrderDto.getEntId(),
|
|
|
+ contractTradeOrderDto.getEntType()
|
|
|
+ );
|
|
|
+
|
|
|
if (org.apache.commons.collections4.CollectionUtils.isEmpty(tradeUnits)) {
|
|
|
+ log.debug("未找到指定企业[{}]在类型[{}]下的合同单位信息,返回空列表", contractTradeOrderDto.getEntId(), contractTradeOrderDto.getEntType());
|
|
|
+ return List.of();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 提取这些单位关联的所有合同ID
|
|
|
+ Set<Long> contractIds = tradeUnits.stream()
|
|
|
+ .map(KwcContractTradeUnit::getContractId)
|
|
|
+ .filter(Objects::nonNull)
|
|
|
+ .collect(Collectors.toSet());
|
|
|
+
|
|
|
+ if (org.apache.commons.collections4.CollectionUtils.isEmpty(contractIds)) {
|
|
|
+ log.debug("提取到的合同ID集合为空,返回空列表");
|
|
|
return List.of();
|
|
|
}
|
|
|
- Set<Long> contractIds =
|
|
|
- tradeUnits.stream().map(KwcContractTradeUnit::getContractId).collect(Collectors.toSet());
|
|
|
+ log.debug("初步筛选出的合同ID数量: {}", contractIds.size());
|
|
|
|
|
|
- List<KwcContractTradeUnit> tradeUnits1 = kwcContractTradeUnitRepository.queryByContractIds(contractIds);
|
|
|
- if (org.apache.commons.collections4.CollectionUtils.isEmpty(tradeUnits1)) {
|
|
|
+ // 3. 批量查询这些合同的所有单位信息,用于后续构建返回对象中的企业名称等字段
|
|
|
+ List<KwcContractTradeUnit> allTradeUnits = kwcContractTradeUnitRepository.queryByContractIds(contractIds);
|
|
|
+ if (org.apache.commons.collections4.CollectionUtils.isEmpty(allTradeUnits)) {
|
|
|
+ log.warn("根据合同ID集合查询合同单位信息为空,contractIds: {}", contractIds);
|
|
|
return List.of();
|
|
|
}
|
|
|
- Map<String, KwcContractTradeUnit> conTractTradeUnitMap =
|
|
|
- tradeUnits1.stream().collect(Collectors.toMap(x -> x.getContractId() +
|
|
|
- "_" + x.getUnitType(), Function.identity(),
|
|
|
- (x, y) -> x));
|
|
|
- // 判断当前查询的企业类型是否为供应方(供应商)
|
|
|
- if (Objects.equals(contractTradeOrderDto.getEntType(), CooperateTypeEnum.SUPPLIER.getCode())) {
|
|
|
- log.debug("当前查询类型为供应方,执行销售员签约贸易合同查询逻辑。entId: {}, goodsId: {}",
|
|
|
+
|
|
|
+ // 构建映射:Key为 "contractId_unitType",Value为单位对象
|
|
|
+ // 方便后续通过合同ID和单位类型快速获取对应的企业信息(如采购方名称、供应方名称)
|
|
|
+ Map<String, KwcContractTradeUnit> contractTradeUnitMap = allTradeUnits.stream()
|
|
|
+ .collect(Collectors.toMap(
|
|
|
+ unit -> unit.getContractId() + "_" + unit.getUnitType(),
|
|
|
+ Function.identity(),
|
|
|
+ (existing, replacement) -> existing // 处理Key冲突,保留第一个
|
|
|
+ ));
|
|
|
+
|
|
|
+ // 4. 分支处理:根据企业类型执行不同的查询逻辑
|
|
|
+ Integer entType = contractTradeOrderDto.getEntType();
|
|
|
+
|
|
|
+ // 场景A:如果查询的是供应方(供应商)
|
|
|
+ // 业务规则:供应商查看合同时,通常只能看到自己作为销售员负责的、且已签约的合同
|
|
|
+ if (Objects.equals(entType, CooperateTypeEnum.SUPPLIER.getCode())) {
|
|
|
+ log.debug("当前查询类型为供应方(Supplier),进入销售员专属查询逻辑。entId: {}, goodsId: {}",
|
|
|
contractTradeOrderDto.getEntId(), contractTradeOrderDto.getGoodsId());
|
|
|
- // 调用专用方法查询当前登录用户作为销售员且已签约的贸易合同信息
|
|
|
- return querySalesmanSignedTradeOrder(contractTradeOrderDto, contractIds, conTractTradeUnitMap);
|
|
|
- }
|
|
|
- contractIds = tradeUnits1.stream()
|
|
|
- .filter(x -> Objects.equals(x.getEntId(), LoginUserHolder.getEntId()) && Objects.equals(x.getUnitType(),
|
|
|
- 2))
|
|
|
+
|
|
|
+ // 调用专用方法,内部会过滤 salesmanId = 当前登录用户ID 且状态为已签约的合同
|
|
|
+ return querySalesmanSignedTradeOrder(contractTradeOrderDto, contractIds, contractTradeUnitMap);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 场景B:其他类型(主要是采购方 Purchaser)
|
|
|
+ // 业务规则:采购商查看合同时,需限制在当前登录企业范围内,且角色为采购方(unitType=2)
|
|
|
+ log.debug("当前查询类型非供应方,执行通用采购方查询逻辑。当前登录企业ID: {}", LoginUserHolder.getEntId());
|
|
|
+
|
|
|
+ // 再次过滤合同ID:只保留当前登录企业作为“采购方”参与的合同
|
|
|
+ // 注意:这里硬编码了 unitType=2 (假设 2 代表 PURCHASER/采购方,需与 CooperateTypeEnum.PURCHASER.getCode() 保持一致)
|
|
|
+ Set<Long> filteredContractIds = allTradeUnits.stream()
|
|
|
+ .filter(unit -> Objects.equals(unit.getEntId(), LoginUserHolder.getEntId())
|
|
|
+ && Objects.equals(unit.getUnitType(), CooperateTypeEnum.PURCHASER.getCode()))
|
|
|
.map(KwcContractTradeUnit::getContractId)
|
|
|
.collect(Collectors.toSet());
|
|
|
- //查询商品信息
|
|
|
- List<KwcContractTradeGoods> kwcContractGoods = kwcContractTradeGoodsRepository.queryByContractIdsAndGoodsId(contractIds,
|
|
|
- contractTradeOrderDto.getGoodsId());
|
|
|
- if (org.apache.commons.collections4.CollectionUtils.isEmpty(kwcContractGoods)) {
|
|
|
+
|
|
|
+ if (org.apache.commons.collections4.CollectionUtils.isEmpty(filteredContractIds)) {
|
|
|
+ log.debug("当前登录企业[{}]未作为采购方参与任何上述合同,返回空列表", LoginUserHolder.getEntId());
|
|
|
return Collections.emptyList();
|
|
|
}
|
|
|
- Map<Long, KwcContractTradeGoods> contractGoodsMap = kwcContractGoods.stream()
|
|
|
- .collect(Collectors.toMap(KwcContractTradeGoods::getContractId, Function.identity(), (x, y) -> x));
|
|
|
- //只查询已签约的
|
|
|
- Set<Long> contractIdList = kwcContractGoods.stream()
|
|
|
- .filter(x -> Objects.equals(x.getStatus(), ContractStatusEnum.SIGNED.getCode()))
|
|
|
+ log.debug("过滤后剩余合同ID数量: {}", filteredContractIds.size());
|
|
|
+
|
|
|
+ // 5. 根据商品ID进一步筛选合同
|
|
|
+ // 查询上述合同中,包含指定商品ID的合同商品信息
|
|
|
+ List<KwcContractTradeGoods> contractGoodsList = kwcContractTradeGoodsRepository.queryByContractIdsAndGoodsId(
|
|
|
+ filteredContractIds,
|
|
|
+ contractTradeOrderDto.getGoodsId()
|
|
|
+ );
|
|
|
+
|
|
|
+ if (org.apache.commons.collections4.CollectionUtils.isEmpty(contractGoodsList)) {
|
|
|
+ log.debug("在合同[{}]中未找到包含商品ID[{}]的商品信息,返回空列表", filteredContractIds, contractTradeOrderDto.getGoodsId());
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 构建映射:Key为 contractId,Value为合同商品信息(用于获取价格、数量等)
|
|
|
+ Map<Long, KwcContractTradeGoods> contractGoodsMap = contractGoodsList.stream()
|
|
|
+ .collect(Collectors.toMap(
|
|
|
+ KwcContractTradeGoods::getContractId,
|
|
|
+ Function.identity(),
|
|
|
+ (x, y) -> x
|
|
|
+ ));
|
|
|
+
|
|
|
+ // 6. 状态过滤:只保留“已签约”状态的合同
|
|
|
+ Set<Long> signedContractIds = contractGoodsList.stream()
|
|
|
+ .filter(goods -> Objects.equals(goods.getStatus(), ContractStatusEnum.SIGNED.getCode()))
|
|
|
.map(KwcContractTradeGoods::getContractId)
|
|
|
.collect(Collectors.toSet());
|
|
|
- //查询贸易合同
|
|
|
- List<KwcContractTrade> kwcContractTrades = kwcContractTradeRepository.findByContractIds(contractIdList);
|
|
|
- if (org.apache.commons.collections4.CollectionUtils.isEmpty(kwcContractTrades)) {
|
|
|
+
|
|
|
+ if (org.apache.commons.collections4.CollectionUtils.isEmpty(signedContractIds)) {
|
|
|
+ log.debug("未找到状态为“已签约”且包含指定商品的合同,返回空列表");
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ log.debug("符合条件的已签约合同ID数量: {}", signedContractIds.size());
|
|
|
+
|
|
|
+ // 7. 查询合同主表信息
|
|
|
+ List<KwcContractTrade> contractTrades = kwcContractTradeRepository.findByContractIds(signedContractIds);
|
|
|
+ if (org.apache.commons.collections4.CollectionUtils.isEmpty(contractTrades)) {
|
|
|
+ log.warn("根据合同ID查询合同主表信息为空,signedContractIds: {}", signedContractIds);
|
|
|
return Collections.emptyList();
|
|
|
}
|
|
|
- kwcContractTrades = kwcContractTrades.stream()
|
|
|
- .filter(x -> Objects.equals(x.getStatus(), ContractStatusEnum.SIGNED.getCode()))
|
|
|
+
|
|
|
+ // 双重保险:再次在内存中过滤状态,确保数据一致性
|
|
|
+ List<KwcContractTrade> validTrades = contractTrades.stream()
|
|
|
+ .filter(trade -> Objects.equals(trade.getStatus(), ContractStatusEnum.SIGNED.getCode()))
|
|
|
.collect(Collectors.toList());
|
|
|
- List<ContractTradeOrderInfo> result = kwcContractTrades.stream()
|
|
|
- .map(x -> getContractTradeOrderInfo(x, conTractTradeUnitMap, contractTradeOrderDto.getEntType(), contractGoodsMap))
|
|
|
+
|
|
|
+ if (org.apache.commons.collections4.CollectionUtils.isEmpty(validTrades)) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 8. 组装返回结果
|
|
|
+ List<ContractTradeOrderInfo> resultList = validTrades.stream()
|
|
|
+ .map(trade -> getContractTradeOrderInfo(
|
|
|
+ trade,
|
|
|
+ contractTradeUnitMap,
|
|
|
+ contractTradeOrderDto.getEntType(),
|
|
|
+ contractGoodsMap
|
|
|
+ ))
|
|
|
.collect(Collectors.toList());
|
|
|
- return filterContractTradeOrderInfo(result, contractTradeOrderDto.getKeyword());
|
|
|
+
|
|
|
+ // 9. 关键字模糊匹配过滤(如有)
|
|
|
+ List<ContractTradeOrderInfo> finalResult = filterContractTradeOrderInfo(resultList, contractTradeOrderDto.getKeyword());
|
|
|
+
|
|
|
+ log.info("查询贸易合同订单完成,最终返回记录数: {}", finalResult.size());
|
|
|
+ return finalResult;
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -2449,14 +2668,14 @@ public class KwcContractTradeService {
|
|
|
// - 组装返回对象:利用之前构建的 map 填充详细信息
|
|
|
List<ContractTradeOrderInfo> result = kwcContractTrades.stream()
|
|
|
.filter(trade -> Objects.equals(trade.getStatus(), ContractStatusEnum.SIGNED.getCode()))
|
|
|
-// .filter(trade -> {
|
|
|
-// boolean isMySale = Objects.equals(trade.getSalesmanId(), currentUserId);
|
|
|
-// if (!isMySale) {
|
|
|
-// log.trace("合同ID: {} 被过滤,因为销售员ID: {} 不匹配当前用户: {}",
|
|
|
-// trade.getId(), trade.getSalesmanId(), currentUserId);
|
|
|
-// }
|
|
|
-// return isMySale;
|
|
|
-// })
|
|
|
+ .filter(trade -> {
|
|
|
+ boolean isMySale = Objects.equals(trade.getSalesmanId(), currentUserId);
|
|
|
+ if (!isMySale) {
|
|
|
+ log.trace("合同ID: {} 被过滤,因为销售员ID: {} 不匹配当前用户: {}",
|
|
|
+ trade.getId(), trade.getSalesmanId(), currentUserId);
|
|
|
+ }
|
|
|
+ return isMySale;
|
|
|
+ })
|
|
|
.map(trade -> getContractTradeOrderInfo(trade, conTractTradeUnitMap, contractTradeOrderDto.getEntType(), contractGoodsMap))
|
|
|
.collect(Collectors.toList());
|
|
|
|