|
|
@@ -15,6 +15,7 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
+import java.util.Collections;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
@@ -59,8 +60,8 @@ public class TransportStatisticsService {
|
|
|
|
|
|
log.info("查询顶部运输统计数据,todayStart={}, tomorrowStart={}", todayStart, tomorrowStart);
|
|
|
|
|
|
- List<KwtWaybillOrder> completedWaybillOrders =
|
|
|
- waybillOrderRepository.queryCompletedWaybillOrders(CarWaybillV1Enum.COMPLETED.getCode());
|
|
|
+ List<KwtWaybillOrder> completedWaybillOrders = defaultList(
|
|
|
+ waybillOrderRepository.queryCompletedWaybillOrders(CarWaybillV1Enum.COMPLETED.getCode()));
|
|
|
if (completedWaybillOrders.isEmpty()) {
|
|
|
log.info("未查询到已完成运单,返回默认零值");
|
|
|
return buildEmptyStatistics();
|
|
|
@@ -68,11 +69,13 @@ public class TransportStatisticsService {
|
|
|
|
|
|
// 提取已完成运单的ID列表,用于查询关联的子任务数据
|
|
|
List<Long> waybillOrderIds = completedWaybillOrders.stream()
|
|
|
+ .filter(Objects::nonNull)
|
|
|
.map(KwtWaybillOrder::getId)
|
|
|
.filter(Objects::nonNull)
|
|
|
.toList();
|
|
|
// 提取已完成运单关联的物流订单ID,用于查询计费方式
|
|
|
List<Long> logisticsOrderIds = completedWaybillOrders.stream()
|
|
|
+ .filter(Objects::nonNull)
|
|
|
.map(KwtWaybillOrder::getLOrderId)
|
|
|
.filter(Objects::nonNull)
|
|
|
.distinct()
|
|
|
@@ -80,22 +83,28 @@ public class TransportStatisticsService {
|
|
|
log.debug("提取到 {} 个唯一的物流订单ID", logisticsOrderIds.size());
|
|
|
|
|
|
// 批量查询物流订单信息
|
|
|
- List<KwtLogisticsOrder> logisticsOrders = logisticsOrderRepository.queryByLogisticsOrderIds(logisticsOrderIds);
|
|
|
+ List<KwtLogisticsOrder> logisticsOrders = logisticsOrderIds.isEmpty()
|
|
|
+ ? Collections.emptyList()
|
|
|
+ : defaultList(logisticsOrderRepository.queryByLogisticsOrderIds(logisticsOrderIds));
|
|
|
log.debug("查询到 {} 条物流订单记录", logisticsOrders.size());
|
|
|
|
|
|
// 构建物流订单ID到计费方式的映射关系
|
|
|
Map<Long, String> billingModeMap = logisticsOrders.stream()
|
|
|
+ .filter(Objects::nonNull)
|
|
|
.filter(logisticsOrder -> Objects.nonNull(logisticsOrder.getId()))
|
|
|
.collect(Collectors.toMap(KwtLogisticsOrder::getId, KwtLogisticsOrder::getBillingMode, (left, right) -> left));
|
|
|
log.debug("构建计费方式映射完成,共 {} 条记录", billingModeMap.size());
|
|
|
|
|
|
// 根据运单ID列表批量查询子任务信息,用于后续统计净重
|
|
|
- List<KwtWaybillOrderSubtask> subtasks = waybillOrderSubtaskRepository.queryByWOrderIds(waybillOrderIds);
|
|
|
+ List<KwtWaybillOrderSubtask> subtasks = waybillOrderIds.isEmpty()
|
|
|
+ ? Collections.emptyList()
|
|
|
+ : defaultList(waybillOrderSubtaskRepository.queryByWOrderIds(waybillOrderIds));
|
|
|
|
|
|
// 先按运单维度汇总净重,后续累计和今日统计都直接复用,避免重复遍历明细数据。
|
|
|
// 其中净重按物流订单计费方式动态取值:
|
|
|
// billingMode=1 取装货量,billingMode=2 取卸货量,其它情况默认按卸货量处理。
|
|
|
Map<Long, BigDecimal> transportAmountMap = subtasks.stream()
|
|
|
+ .filter(Objects::nonNull)
|
|
|
.filter(subtask -> Objects.nonNull(subtask.getWOrderId()))
|
|
|
.collect(Collectors.groupingBy(
|
|
|
KwtWaybillOrderSubtask::getWOrderId,
|
|
|
@@ -107,12 +116,14 @@ public class TransportStatisticsService {
|
|
|
|
|
|
// 计算累计运量:遍历所有已完成运单,从预计算的运量映射中获取各运单净重并累加
|
|
|
BigDecimal totalTransportAmount = completedWaybillOrders.stream()
|
|
|
+ .filter(Objects::nonNull)
|
|
|
.map(KwtWaybillOrder::getId)
|
|
|
.map(id -> transportAmountMap.getOrDefault(id, BigDecimal.ZERO))
|
|
|
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
|
|
|
// 筛选今日完成的运单:根据运单更新时间判断是否落在当天时间范围内
|
|
|
List<KwtWaybillOrder> todayCompletedWaybillOrders = completedWaybillOrders.stream()
|
|
|
+ .filter(Objects::nonNull)
|
|
|
.filter(waybillOrder -> isBetween(waybillOrder.getUpdateTime(), todayStart, tomorrowStart))
|
|
|
.toList();
|
|
|
|
|
|
@@ -143,6 +154,10 @@ public class TransportStatisticsService {
|
|
|
.build();
|
|
|
}
|
|
|
|
|
|
+ private <T> List<T> defaultList(List<T> list) {
|
|
|
+ return list == null ? Collections.emptyList() : list;
|
|
|
+ }
|
|
|
+
|
|
|
private BigDecimal defaultAmount(BigDecimal amount) {
|
|
|
return amount == null ? BigDecimal.ZERO : amount;
|
|
|
}
|
|
|
@@ -155,6 +170,9 @@ public class TransportStatisticsService {
|
|
|
* @return 当前子单应参与统计的净重
|
|
|
*/
|
|
|
private BigDecimal resolveTransportAmount(KwtWaybillOrderSubtask subtask, String billingMode) {
|
|
|
+ if (billingMode == null) {
|
|
|
+ return BigDecimal.ZERO;
|
|
|
+ }
|
|
|
if (DictEnum.CHARGING_TYPE_1.getValue().equals(billingMode)) {
|
|
|
return defaultAmount(subtask.getLoadAmount());
|
|
|
}
|