|
|
@@ -2255,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());
|
|
|
+
|
|
|
+ // 提取物流合同ID集合
|
|
|
+ Set<Long> logTradeContractIds = contractLogisticsUnits.stream()
|
|
|
+ .map(KwcContractLogisticsUnit::getContractId)
|
|
|
+ .filter(Objects::nonNull)
|
|
|
+ .collect(Collectors.toSet());
|
|
|
+ log.debug("企业[{}]关联的物流合同ID数量: {}", logisticsOrderDto.getEntId(), logTradeContractIds.size());
|
|
|
|
|
|
- //查询商品信息
|
|
|
- List<KwcContractLogisticsGoods> kwcContractLogisticsGoods = kwcContractLogisticsGoodsRepository.queryByLogIdsAndGoodsId(logTradeContractIds,
|
|
|
- logisticsOrderDto.getGoodsId());
|
|
|
+ // 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()
|
|
|
@@ -2607,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());
|
|
|
|