Browse Source

销售看板接口优化

donglang 1 month ago
parent
commit
3bb73c5453

+ 7 - 0
sckw-modules-api/sckw-order-api/src/main/java/com/sckw/order/api/dubbo/TradeOrderInfoService.java

@@ -146,6 +146,13 @@ public interface TradeOrderInfoService {
      */
     List<OrderUnitInfoDetailVO> queryOrderUnitInfByEntName(String entName);
 
+    /**
+     * 通过企业查询贸易订单id
+     *
+     * @return
+     */
+    Set<Long> queryOrderIdByEntId(Long entId, String unitType);
+
 
     /**
      * 查询近期的贸易订单,计算商品的近期销量

+ 8 - 0
sckw-modules/sckw-order/src/main/java/com/sckw/order/dubbo/TradeOrderInfoServiceImpl.java

@@ -991,5 +991,13 @@ public class TradeOrderInfoServiceImpl implements TradeOrderInfoService {
 
     }
 
+    @Override
+    public Set<Long> queryOrderIdByEntId(Long entId, String unitType) {
+        return kwoTradeOrderUnitRepository.queryByEntIdAndUnitType(entId, unitType)
+                .stream()
+                .map(KwoTradeOrderUnit::getTOrderId)
+                .filter(Objects::nonNull)
+                .collect(Collectors.toSet());
+    }
 
 }

+ 4 - 2
sckw-modules/sckw-transport/src/main/java/com/sckw/transport/repository/KwtGatekeeperWaybillOrderRepository.java

@@ -7,6 +7,8 @@ import com.sckw.transport.model.KwtGatekeeperWaybillOrder;
 import org.springframework.stereotype.Repository;
 
 import java.util.List;
+import java.util.Objects;
+import java.util.Set;
 
 /**
  * @Author: 门卫车辆订单
@@ -42,10 +44,10 @@ public class KwtGatekeeperWaybillOrderRepository extends ServiceImpl<KwtGatekeep
                         .last("limit 1"));
     }
 
-    public List<KwtGatekeeperWaybillOrder> queryGatekeeperWaybillOrderByEntId(Long entId, List<Integer> status) {
+    public List<KwtGatekeeperWaybillOrder> queryGatekeeperWaybillOrderByEntId(Set<Long> waybillOrderIds, List<Integer> status) {
         return list(
                 Wrappers.<KwtGatekeeperWaybillOrder>lambdaQuery()
-                        .eq(KwtGatekeeperWaybillOrder::getEntId, entId)
+                        .in(Objects.nonNull(waybillOrderIds), KwtGatekeeperWaybillOrder::getEntId, waybillOrderIds)
                         .in(KwtGatekeeperWaybillOrder::getStatus, status)
                         .orderByDesc(KwtGatekeeperWaybillOrder::getId));
     }

+ 2 - 2
sckw-modules/sckw-transport/src/main/java/com/sckw/transport/repository/KwtWaybillOrderSubtaskRepository.java

@@ -97,10 +97,10 @@ public class KwtWaybillOrderSubtaskRepository extends ServiceImpl<KwtWaybillOrde
         );
     }
 
-    public List<KwtWaybillOrderSubtask> queryWaybillOrderSubByEntIdAndStatus(Long entId, Integer status, LocalDateTime startTime, LocalDateTime endTime) {
+    public List<KwtWaybillOrderSubtask> queryWaybillOrderSubByEntIdAndStatus(Set<Long> logOrderIds, Integer status, LocalDateTime startTime, LocalDateTime endTime) {
         return list(Wrappers.<KwtWaybillOrderSubtask>lambdaQuery()
                 .eq(KwtWaybillOrderSubtask::getDelFlag,0)
-                .eq(Objects.nonNull(entId),KwtWaybillOrderSubtask::getEntId,entId)
+                .in(Objects.nonNull(logOrderIds),KwtWaybillOrderSubtask::getLOrderId,logOrderIds)
                 .eq(Objects.nonNull(status),KwtWaybillOrderSubtask::getStatus,status)
                 .ge(Objects.nonNull(startTime), KwtWaybillOrderSubtask::getUpdateTime, startTime)
                 .le(Objects.nonNull(endTime), KwtWaybillOrderSubtask::getUpdateTime, endTime)

+ 76 - 31
sckw-modules/sckw-transport/src/main/java/com/sckw/transport/service/app/SalesWaybillOrderService.java

@@ -5,24 +5,21 @@ 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.KwtLogisticsOrder;
 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;
 import com.sckw.transport.model.vo.StatisticsWaybillResp;
-import com.sckw.transport.repository.*;
+import com.sckw.transport.repository.KwtGatekeeperWaybillOrderRepository;
+import com.sckw.transport.repository.KwtLogisticsOrderRepository;
+import com.sckw.transport.repository.KwtWaybillOrderSubtaskRepository;
 import com.sckw.transport.service.KwtWaybillOrderV1Service;
-import jakarta.annotation.Resource;
 import lombok.RequiredArgsConstructor;
 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;
 
@@ -30,10 +27,7 @@ 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.*;
 import java.util.stream.Collectors;
 
 /**
@@ -49,6 +43,10 @@ public class SalesWaybillOrderService {
     private final KwtWaybillOrderSubtaskRepository waybillOrderSubtaskRepository;
     private final KwtWaybillOrderV1Service waybillOrderV1Service;
     private final KwtGatekeeperWaybillOrderRepository gatekeeperWaybillOrderRepository;
+    private final KwtLogisticsOrderRepository logisticsOrderRepository;
+
+    @DubboReference(version = "1.0.0", group = "design", check = false, timeout = 80000)
+    TradeOrderInfoService tradeOrderInfoService;
 
     /**
      * 查询销售数据
@@ -58,21 +56,62 @@ public class SalesWaybillOrderService {
     public SalesWaybillOrderResp querySalesStatistics(SalesWaybillOrderQueryParam param) {
         log.info("查询销售数据:{}", JSON.toJSONString(param));
         SalesWaybillOrderResp salesResp = new SalesWaybillOrderResp();
+        //查询销售企业的贸易订单id
+        Set<Long> orderIds = tradeOrderInfoService.queryOrderIdByEntId(param.getEntId(), "2");
+        if (CollectionUtils.isEmpty(orderIds)) {
+            return buildSalesWaybillOrderResp();
+        }
+        // 查询销售企业的物流订单id
+        List<KwtLogisticsOrder> logisticsOrders = logisticsOrderRepository.queryByTradeOrderIds(orderIds);
+        Set<Long> logOrderIds = Optional.ofNullable(logisticsOrders).orElse(Collections.emptyList())
+                .stream().map(KwtLogisticsOrder::getId).filter(Objects::nonNull)
+                .collect(Collectors.toSet());
+        if (CollectionUtils.isEmpty(logOrderIds)) {
+            return buildSalesWaybillOrderResp();
+        }
+
         // 1. 计算执行中订单(物流订单)
         calOngoingLogOrderCount(param, salesResp);
 
         // 2.计算今日车次
-        calTodayTruckCount(param, salesResp);
+        calTodayTruckCount(logOrderIds, salesResp);
 
         // 3.计算场内车辆
-        calInsideVehicleCount(param, salesResp);
+        calInsideVehicleCount(logOrderIds, salesResp);
 
         log.info("查询销售数据成功,结果:{}", JSON.toJSONString(salesResp));
-
         return salesResp;
 
     }
 
+    /**
+     * 构建默认返回数据
+     * @return
+     */
+    private SalesWaybillOrderResp buildSalesWaybillOrderResp() {
+        SalesWaybillOrderResp resp = new SalesWaybillOrderResp();
+        resp.setOngoingLogOrderCount(0);
+        resp.setTodayTruckCount(0);
+        resp.setInsideVehicleCount(0);
+        resp.setTodayShipmentWeightCount(BigDecimal.ZERO);
+        resp.setMonthShipmentWeightCount(BigDecimal.ZERO);
+
+        List<SalesWaybillOrderResp.RecentSevenDayShipmentVO> resultVOs = new ArrayList<>();
+        for (int i = 6; i >= 0; i--) {
+            LocalDate date = LocalDate.now().minusDays(i);
+            String dateStr = date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
+
+            SalesWaybillOrderResp.RecentSevenDayShipmentVO vo = new SalesWaybillOrderResp.RecentSevenDayShipmentVO();
+            vo.setDate(dateStr);
+            vo.setShipmentWeight(BigDecimal.ZERO);
+            resultVOs.add(vo);
+        }
+
+        resp.setRecentSevenDayShipmentCountVOs(resultVOs);
+
+        return resp;
+    }
+
     /**
      *  计算执行中订单
      * @param param
@@ -102,30 +141,30 @@ public class SalesWaybillOrderService {
 
     /**
      *  计算今日车次
-     * @param param
+     * @param logOrderIds
      * @param salesResp
      */
-    private void calTodayTruckCount(SalesWaybillOrderQueryParam param, SalesWaybillOrderResp salesResp) {
+    private void calTodayTruckCount(Set<Long> logOrderIds, SalesWaybillOrderResp salesResp) {
         //今日车次/出货量
-        todayTruckCount(param, salesResp);
+        todayTruckCount(logOrderIds, salesResp);
         //本月出货量
-        monthShipmentWeightCount(param, salesResp);
+        monthShipmentWeightCount(logOrderIds, salesResp);
         //近7天出货量
-        recentSevenDayShipmentCount(param, salesResp);
+        recentSevenDayShipmentCount(logOrderIds, salesResp);
     }
 
     /**
      * 今日车次/出货量
-     * @param param
+     * @param logOrderIds
      * @param salesResp
      */
-    private void todayTruckCount(SalesWaybillOrderQueryParam param, SalesWaybillOrderResp salesResp) {
+    private void todayTruckCount(Set<Long> logOrderIds, SalesWaybillOrderResp salesResp) {
         // 今天 00:00:00
         LocalDateTime todayStart = LocalDate.now().atStartOfDay();
         // 今天 23:59:59
         LocalDateTime todayEnd = LocalDate.now().atTime(23, 59, 59);
         List<KwtWaybillOrderSubtask> todayWaybillOrderSub = waybillOrderSubtaskRepository
-                .queryWaybillOrderSubByEntIdAndStatus(param.getEntId(), CarWaybillV1Enum.COMPLETED.getCode(), todayStart, todayEnd);
+                .queryWaybillOrderSubByEntIdAndStatus(logOrderIds, CarWaybillV1Enum.COMPLETED.getCode(), todayStart, todayEnd);
         if (CollectionUtils.isEmpty(todayWaybillOrderSub)) {
             salesResp.setTodayTruckCount(Global.NUMERICAL_ZERO);
             salesResp.setTodayShipmentWeightCount(BigDecimal.ZERO);
@@ -142,16 +181,16 @@ public class SalesWaybillOrderService {
 
     /**
      * 本月出货量
-     * @param param
+     * @param logOrderIds
      * @param salesResp
      */
-    private void monthShipmentWeightCount(SalesWaybillOrderQueryParam param, SalesWaybillOrderResp salesResp) {
+    private void monthShipmentWeightCount(Set<Long> logOrderIds, SalesWaybillOrderResp salesResp) {
         // 本月第一天 00:00:00
         LocalDateTime monthStart = LocalDate.now().withDayOfMonth(1).atStartOfDay();
         // 本月最后一天 23:59:59
         LocalDateTime monthEnd = LocalDate.now().withDayOfMonth(LocalDate.now().lengthOfMonth()).atTime(23, 59, 59);
         List<KwtWaybillOrderSubtask> monthWaybillOrderSub = waybillOrderSubtaskRepository
-                .queryWaybillOrderSubByEntIdAndStatus(param.getEntId(), CarWaybillV1Enum.COMPLETED.getCode(), monthStart, monthEnd);
+                .queryWaybillOrderSubByEntIdAndStatus(logOrderIds, CarWaybillV1Enum.COMPLETED.getCode(), monthStart, monthEnd);
         if (CollectionUtils.isEmpty(monthWaybillOrderSub)) {
             salesResp.setMonthShipmentWeightCount(BigDecimal.ZERO);
         }
@@ -165,10 +204,10 @@ public class SalesWaybillOrderService {
 
     /**
      * 近7天出货量
-     * @param param
+     * @param logOrderIds
      * @param salesResp
      */
-    private void recentSevenDayShipmentCount(SalesWaybillOrderQueryParam param, SalesWaybillOrderResp salesResp) {
+    private void recentSevenDayShipmentCount(Set<Long> logOrderIds, SalesWaybillOrderResp salesResp) {
         // 7天前 00:00:00
         LocalDate today = LocalDate.now();
         LocalDateTime sevenDaysStartTime = today.minusDays(6).atStartOfDay();
@@ -177,7 +216,7 @@ public class SalesWaybillOrderService {
 
         // 查询数据库,获取近7天的所有已完成子运单数据
         List<KwtWaybillOrderSubtask> sevenDayWaybillOrderSubtask = waybillOrderSubtaskRepository
-                .queryWaybillOrderSubByEntIdAndStatus(param.getEntId(), CarWaybillV1Enum.COMPLETED.getCode(), sevenDaysStartTime, sevenDaysEndTime);
+                .queryWaybillOrderSubByEntIdAndStatus(logOrderIds, CarWaybillV1Enum.COMPLETED.getCode(), sevenDaysStartTime, sevenDaysEndTime);
 
         // 分组统计,按日期聚合求和
         Map<String, BigDecimal> dailyWeightMap = sevenDayWaybillOrderSubtask.stream()
@@ -206,12 +245,18 @@ public class SalesWaybillOrderService {
 
     /**
      * 计算场内车辆
-     * @param param
+     * @param logOrderIds
      * @param salesResp
      */
-    private void calInsideVehicleCount(SalesWaybillOrderQueryParam param, SalesWaybillOrderResp salesResp) {
+    private void calInsideVehicleCount(Set<Long> logOrderIds, SalesWaybillOrderResp salesResp) {
+        // 查询运单
+        List<KwtWaybillOrderSubtask> waybillOrderSubtasks = waybillOrderSubtaskRepository.queryByLogIds(new ArrayList<>(logOrderIds));
+        Set<Long> waybillOrderIds = Optional.ofNullable(waybillOrderSubtasks).orElse(Collections.emptyList())
+                .stream().map(KwtWaybillOrderSubtask::getWOrderId)
+                .collect(Collectors.toSet());
+        // 查询门卫
         List<KwtGatekeeperWaybillOrder> kwtGatekeeperWaybillOrders = gatekeeperWaybillOrderRepository
-                .queryGatekeeperWaybillOrderByEntId(param.getEntId(),
+                .queryGatekeeperWaybillOrderByEntId(waybillOrderIds,
                         List.of(GatekeeperStatusEnum.IN_YARD.getCode(),
                                 GatekeeperStatusEnum.PENDING_RELEASE.getCode(),
                                 GatekeeperStatusEnum.READY_RELEASE.getCode()));