Эх сурвалжийг харах

定时统计近7天的订单销售出库数据

fgd 1 жил өмнө
parent
commit
ad2784fa0c

+ 33 - 19
sd-business/src/main/java/com/sd/business/service/order/impl/OrderSalesShipmentStatisticsServiceImpl.java

@@ -145,27 +145,41 @@ public class OrderSalesShipmentStatisticsServiceImpl extends ServiceImpl<OrderSa
 
     @Override
     public void salesShipmentStatistics() {
-        Date yesterday = DateUtil.yesterday();
-        String date = DateUtil.formatDate(yesterday);
-        List<OrderInfo> list = orderService.list(q -> q
-                .eq(OrderInfo::getStatus, OrderStatusEnum.COMPLETION_PRODUCTION.getKey())
-                .ne(OrderInfo::getClassify, OrderClassifyEnum.OUTSOURCE_ORDER.getKey())
-                .likeRight(OrderInfo::getShippingTime, date)
-                .select(BaseIdPo::getId));
-        if (list.isEmpty()) {
+        Date date = new Date();
+        List<OrderSalesShipmentStatistics> salesShipmentStatisticsList = new ArrayList<>();
+        // 统计近7天数据
+        for (int i = 7; i >= 1; i--) {
+            Date offsetDay = DateUtil.offsetDay(date, -i);
+            String formatDate = DateUtil.formatDate(offsetDay);
+            List<OrderInfo> list = orderService.list(q -> q
+                    .eq(OrderInfo::getStatus, OrderStatusEnum.COMPLETION_PRODUCTION.getKey())
+                    .ne(OrderInfo::getClassify, OrderClassifyEnum.OUTSOURCE_ORDER.getKey())
+                    .likeRight(OrderInfo::getShippingTime, formatDate)
+                    .select(BaseIdPo::getId));
+            if (list.isEmpty()) {
+                return;
+            }
+            Map<Long, OrderSalesShipmentStatistics> statisticsMap = this.mapKEntity(OrderSalesShipmentStatistics::getBomSpecId, q -> q.eq(OrderSalesShipmentStatistics::getTotalDate, formatDate));
+
+            List<Long> orderIds = list.stream().map(BaseIdPo::getId).collect(Collectors.toList());
+            List<OrderSku> orderSkuList = orderSkuService.list(q -> q.in(OrderSku::getOrderId, orderIds));
+            Map<Long, BigDecimal> map = orderSkuList.stream().collect(Collectors.toMap(OrderSku::getBomSpecId, OrderSku::getQuantity, BigDecimal::add));
+            List<OrderSalesShipmentStatistics> statisticsDtoList = map.keySet().stream().map(item -> {
+                OrderSalesShipmentStatistics statistics = statisticsMap.get(item);
+                if (statistics == null) {
+                    statistics = new OrderSalesShipmentStatistics();
+                }
+                statistics.setTotalDate(offsetDay);
+                statistics.setBomSpecId(item);
+                statistics.setQuantity(map.get(item));
+                return statistics;
+            }).collect(Collectors.toList());
+            salesShipmentStatisticsList.addAll(statisticsDtoList);
+        }
+        if (salesShipmentStatisticsList.isEmpty()) {
             return;
         }
-        List<Long> orderIds = list.stream().map(BaseIdPo::getId).collect(Collectors.toList());
-        List<OrderSku> orderSkuList = orderSkuService.list(q -> q.in(OrderSku::getOrderId, orderIds));
-        Map<Long, BigDecimal> map = orderSkuList.stream().collect(Collectors.toMap(OrderSku::getBomSpecId, OrderSku::getQuantity, BigDecimal::add));
-        List<OrderSalesShipmentStatistics> statisticsDtoList = map.keySet().stream().map(item -> {
-            OrderSalesShipmentStatistics dto = new OrderSalesShipmentStatistics();
-            dto.setTotalDate(yesterday);
-            dto.setBomSpecId(item);
-            dto.setQuantity(map.get(item));
-            return dto;
-        }).collect(Collectors.toList());
-        this.saveBatch(statisticsDtoList);
+        this.saveOrUpdateBatch(salesShipmentStatisticsList);
     }
 
     @Override