package com.mzl.flower.schedule; import com.mzl.flower.dto.response.warehouse.WarehouseLocationDTO; import com.mzl.flower.entity.payment.Order; import com.mzl.flower.entity.warehouse.WarehouseLocation; import com.mzl.flower.service.payment.OrderService; import com.mzl.flower.service.warehouse.WarehouseService; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.time.DateFormatUtils; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.time.LocalDateTime; import java.util.*; /** * 库位相关定时任务 */ @Component @Slf4j public class WarehouseScheduleService { private final WarehouseService warehouseService; private final OrderService orderService; public WarehouseScheduleService(WarehouseService warehouseService, OrderService orderService) { this.warehouseService = warehouseService; this.orderService = orderService; } /** * 每天17点,定时分配订单库位(查询前一天17点到今天17点支付完成且未分配库位的订单) */ @Scheduled(cron = "0 0 17 * * ?") public void allocatedWarehouseLocation() { log.info("定时分配订单库位开始:" + DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss")); //查询当日待分配的订单 List orders = orderService.selectPaymentOrderList(); if(orders!=null && orders.size()>0){ //订单按照合伙人分组排序 Map> partnerOrders = new HashMap<>(); List customerOrders = new ArrayList<>(); for (Order order : orders) { Long partnerId = order.getPartnerId(); if(partnerId==null || partnerId==0){ //非合伙人订单 customerOrders.add(order); }else{ //合伙人订单 if(partnerOrders.containsKey(partnerId)){ partnerOrders.get(partnerId).add(order); }else{ List orderIds = new ArrayList<>(); orderIds.add(order); partnerOrders.put(partnerId,orderIds); } } } if((partnerOrders!=null && partnerOrders.size()>0) || (customerOrders!=null && customerOrders.size()>0)){ //查询库位队列信息 Queue locations = warehouseService.selectAllocatedWarehouseLocation(); if(locations!=null && locations.size()>0){ Map partnerLocationMap = new HashMap<>(); if(partnerOrders!=null && partnerOrders.size()>0 ){ //合伙人订单 for (Long partnerId : partnerOrders.keySet()) { List orderIds = partnerOrders.get(partnerId); for (Order order : orderIds) { if(locations!=null && locations.size()>0){ String key = order.getCreateBy()+"||"+order.getCustomerAddress(); WarehouseLocationDTO location = null; if(partnerLocationMap.containsKey(key)){ location = partnerLocationMap.get(key); } else { location = locations.poll(); partnerLocationMap.put(key,location); } orderService.allocatedWarehouseLocation(order.getId(), location.getWarehouseId(), location.getWarehouseName(), location.getId(), location.getCode()); } } if(locations!=null && locations.size()>0){ //两个合伙人之间空一个库位 locations.poll(); } } } if(customerOrders!=null && customerOrders.size()>0){ //散户订单 Map customerLocationMap = new HashMap<>(); for (Order order : customerOrders) { if(locations!=null && locations.size()>0){ String key = order.getCreateBy()+"||"+order.getCustomerAddress(); WarehouseLocationDTO location = null; if(customerLocationMap.containsKey(key)){ location = customerLocationMap.get(key); } else { location = locations.poll(); customerLocationMap.put(key,location); } orderService.allocatedWarehouseLocation(order.getId(), location.getWarehouseId(), location.getWarehouseName(),location.getId(), location.getCode()); } } } } } } log.info("定时分配订单库位结束:" + DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss")); } /** * 每天凌晨2点,合伙人发货 */ @Scheduled(cron = "0 0 4 * * ?") public void clearWarehouseLocation() { log.info("定时合伙人发货开始:" + DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss")); orderService.setPartnerOrderSend(); log.info("定时合伙人发货开始:" + DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss")); } }