|
|
@@ -2,11 +2,16 @@ package com.sckw.transport.service.app;
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
+import com.sckw.core.model.constant.Global;
|
|
|
+import com.sckw.core.model.enums.CarWaybillV1Enum;
|
|
|
+import com.sckw.core.model.enums.GatekeeperStatusEnum;
|
|
|
import com.sckw.fleet.api.RemoteFleetService;
|
|
|
import com.sckw.order.api.dubbo.TradeOrderInfoService;
|
|
|
import com.sckw.product.api.dubbo.GoodsInfoService;
|
|
|
import com.sckw.system.api.RemoteSystemService;
|
|
|
import com.sckw.transport.handler.*;
|
|
|
+import com.sckw.transport.model.KwtGatekeeperWaybillOrder;
|
|
|
+import com.sckw.transport.model.KwtWaybillOrderSubtask;
|
|
|
import com.sckw.transport.model.param.SalesWaybillOrderQueryParam;
|
|
|
import com.sckw.transport.model.param.SalesWaybillOrderResp;
|
|
|
import com.sckw.transport.model.param.WaybillOrderReq;
|
|
|
@@ -19,8 +24,17 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.dubbo.config.annotation.DubboReference;
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* @author zk
|
|
|
@@ -93,12 +107,14 @@ public class SalesWaybillOrderService {
|
|
|
public SalesWaybillOrderResp querySalesStatistics(SalesWaybillOrderQueryParam param) {
|
|
|
log.info("查询销售数据:{}", JSON.toJSONString(param));
|
|
|
SalesWaybillOrderResp salesResp = new SalesWaybillOrderResp();
|
|
|
- // 查询执行中订单(物流订单)
|
|
|
- WaybillOrderReq req = new WaybillOrderReq();
|
|
|
- req.setEntId(String.valueOf(param.getEntId()));
|
|
|
+ // 1. 计算执行中订单(物流订单)
|
|
|
+ calOngoingLogOrder(param, salesResp);
|
|
|
|
|
|
- StatisticsWaybillResp statisticsWaybillResp = waybillOrderV1Service.statisticsWaybillOrder(req);
|
|
|
+ // 2.计算今日车次
|
|
|
+ calTodayTruckCount(param, salesResp);
|
|
|
|
|
|
+ // 3.计算场内车辆
|
|
|
+ calInsideVehicleCount(param, salesResp);
|
|
|
|
|
|
log.info("查询销售数据成功,结果:{}", JSON.toJSONString(salesResp));
|
|
|
|
|
|
@@ -106,4 +122,132 @@ public class SalesWaybillOrderService {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 计算执行中订单
|
|
|
+ * @param param
|
|
|
+ * @param salesResp
|
|
|
+ */
|
|
|
+ private void calOngoingLogOrder(SalesWaybillOrderQueryParam param, SalesWaybillOrderResp salesResp) {
|
|
|
+ WaybillOrderReq req = new WaybillOrderReq();
|
|
|
+ req.setEntId(String.valueOf(param.getEntId()));
|
|
|
+ StatisticsWaybillResp statisticsWaybillResp = waybillOrderV1Service.statisticsWaybillOrder(req);
|
|
|
+ if (statisticsWaybillResp == null) {
|
|
|
+ salesResp.setOngoingLogOrder(Global.NUMERICAL_ZERO);
|
|
|
+ }
|
|
|
+ List<String> targetStatus = List.of("5", "10", "15");
|
|
|
+ List<StatisticsWaybillResp.OrderBillStatusStatistics> orderStatusStatistics = statisticsWaybillResp.getOrderStatusStatistics();
|
|
|
+ int ongoingLogOrder = orderStatusStatistics.stream().filter(Objects::nonNull)
|
|
|
+ .filter(stat -> targetStatus.contains(stat.getOrderStatus()))
|
|
|
+ .mapToInt(s -> {
|
|
|
+ try {
|
|
|
+ return Integer.parseInt(s.getOrderNum());
|
|
|
+ } catch (Exception e) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .sum();
|
|
|
+ salesResp.setOngoingLogOrder(ongoingLogOrder);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 计算今日车次
|
|
|
+ * @param param
|
|
|
+ * @param salesResp
|
|
|
+ */
|
|
|
+ private void calTodayTruckCount(SalesWaybillOrderQueryParam param, SalesWaybillOrderResp salesResp) {
|
|
|
+ //今日车次
|
|
|
+ LocalDateTime todayStart = LocalDate.now().atStartOfDay(); // 今天 00:00:00
|
|
|
+ LocalDateTime todayEnd = LocalDate.now().atTime(23, 59, 59);
|
|
|
+ List<KwtWaybillOrderSubtask> todayTruckCount = waybillOrderSubtaskRepository
|
|
|
+ .queryWaybillOrderSubByEntIdAndStatus(param.getEntId(), CarWaybillV1Enum.COMPLETED.getCode(), todayStart, todayEnd);
|
|
|
+ if (CollectionUtils.isEmpty(todayTruckCount)) {
|
|
|
+ salesResp.setTodayTruckCount(Global.NUMERICAL_ZERO);
|
|
|
+ salesResp.setTodayShipmentWeight(BigDecimal.ZERO);
|
|
|
+ }
|
|
|
+ //今日车次
|
|
|
+ salesResp.setTodayTruckCount(todayTruckCount.size());
|
|
|
+ //今日出货量
|
|
|
+ BigDecimal todayLoadAmount = todayTruckCount.stream()
|
|
|
+ .filter(sub -> sub.getLoadAmount() != null)
|
|
|
+ .map(KwtWaybillOrderSubtask::getLoadAmount)
|
|
|
+ .reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
+ salesResp.setTodayShipmentWeight(todayLoadAmount);
|
|
|
+
|
|
|
+ //本月出货量
|
|
|
+ LocalDateTime monthStart = LocalDate.now().withDayOfMonth(1).atStartOfDay();
|
|
|
+ LocalDateTime monthEnd = LocalDate.now().withDayOfMonth(LocalDate.now().lengthOfMonth()).atTime(23, 59, 59);
|
|
|
+ List<KwtWaybillOrderSubtask> monthShipmentWeight = waybillOrderSubtaskRepository
|
|
|
+ .queryWaybillOrderSubByEntIdAndStatus(param.getEntId(), CarWaybillV1Enum.COMPLETED.getCode(), monthStart, monthEnd);
|
|
|
+ if (CollectionUtils.isEmpty(todayTruckCount)) {
|
|
|
+ salesResp.setMonthShipmentWeight(BigDecimal.ZERO);
|
|
|
+ }
|
|
|
+ //本月出货量
|
|
|
+ BigDecimal monthLoadAmount = todayTruckCount.stream()
|
|
|
+ .filter(sub -> sub.getLoadAmount() != null)
|
|
|
+ .map(KwtWaybillOrderSubtask::getLoadAmount)
|
|
|
+ .reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
+ salesResp.setTodayShipmentWeight(monthLoadAmount);
|
|
|
+
|
|
|
+ // 1. 定义时间范围:近7天(包含今天)
|
|
|
+ LocalDate today = LocalDate.now();
|
|
|
+ LocalDateTime sevenDaysStartTime = today.minusDays(6).atStartOfDay(); // 7天前 00:00:00
|
|
|
+ LocalDateTime sevenDaysEndTime = today.atTime(23, 59, 59); // 今天 23:59:59
|
|
|
+
|
|
|
+ // 2. 查询数据库:获取近7天的所有已完成子运单数据
|
|
|
+ List<KwtWaybillOrderSubtask> subtaskList = waybillOrderSubtaskRepository
|
|
|
+ .queryWaybillOrderSubByEntIdAndStatus(param.getEntId(), CarWaybillV1Enum.COMPLETED.getCode(), sevenDaysStartTime, sevenDaysEndTime);
|
|
|
+
|
|
|
+ // 3. 内存分组统计:按“日期”聚合求和
|
|
|
+ // 结果格式:Map<"2026-04-23", 100.5>
|
|
|
+ Map<String, BigDecimal> dailyWeightMap = subtaskList.stream()
|
|
|
+ .filter(task -> task.getLoadAmount() != null) // 过滤空值
|
|
|
+ .collect(Collectors.groupingBy(
|
|
|
+ // 关键:按创建时间的日期部分分组 (yyyy-MM-dd)
|
|
|
+ task -> java.time.LocalDateTime.ofInstant(task.getCreateTime().toInstant(),
|
|
|
+ java.time.ZoneId.systemDefault()).toLocalDate().toString(),
|
|
|
+ // 求和
|
|
|
+ Collectors.mapping(KwtWaybillOrderSubtask::getLoadAmount, Collectors.reducing(BigDecimal.ZERO, BigDecimal::add))
|
|
|
+ ));
|
|
|
+
|
|
|
+ // 4. 构建前端需要的 VO 列表(按时间顺序,并补全缺失日期的0值)
|
|
|
+ List<SalesWaybillOrderResp.RecentSevenDaysShipmentVO> resultVOs = new ArrayList<>();
|
|
|
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
|
|
+
|
|
|
+ for (int i = 6; i >= 0; i--) {
|
|
|
+ // 倒序循环:从6天前遍历到今天,保证列表是按时间正序排列
|
|
|
+ LocalDate date = today.minusDays(i);
|
|
|
+ String dateStr = date.format(formatter);
|
|
|
+
|
|
|
+ // 从 Map 中获取当天的重量,如果没有数据则默认为 0
|
|
|
+ BigDecimal weight = dailyWeightMap.getOrDefault(dateStr, BigDecimal.ZERO);
|
|
|
+
|
|
|
+ SalesWaybillOrderResp.RecentSevenDaysShipmentVO vo = new SalesWaybillOrderResp.RecentSevenDaysShipmentVO();
|
|
|
+ vo.setDate(dateStr);
|
|
|
+ vo.setShipmentWeight(weight);
|
|
|
+
|
|
|
+ resultVOs.add(vo);
|
|
|
+ }
|
|
|
+ salesResp.setRecentSevenDaysShipmentVOs(resultVOs);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 计算场内车辆
|
|
|
+ * @param param
|
|
|
+ * @param salesResp
|
|
|
+ */
|
|
|
+ private void calInsideVehicleCount(SalesWaybillOrderQueryParam param, SalesWaybillOrderResp salesResp) {
|
|
|
+ List<KwtGatekeeperWaybillOrder> kwtGatekeeperWaybillOrders = gatekeeperWaybillOrderRepository
|
|
|
+ .queryGatekeeperWaybillOrderByEntId(param.getEntId(),
|
|
|
+ List.of(GatekeeperStatusEnum.IN_YARD.getCode(),
|
|
|
+ GatekeeperStatusEnum.PENDING_RELEASE.getCode(),
|
|
|
+ GatekeeperStatusEnum.READY_RELEASE.getCode()));
|
|
|
+ if (kwtGatekeeperWaybillOrders.isEmpty()) {
|
|
|
+ salesResp.setInsideVehicleCount(Global.NUMERICAL_ZERO);
|
|
|
+ }
|
|
|
+ salesResp.setInsideVehicleCount(kwtGatekeeperWaybillOrders.size());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
}
|