package com.prolog.eis.strategy.outbound;

import com.prolog.eis.constants.base.OrderStatusConstants;
import com.prolog.eis.dto.strategy.*;
import com.prolog.eis.model.outbound.BizOutboundTask;
import com.prolog.eis.model.outbound.BizOutboundTaskDetail;
import com.prolog.eis.outbound_dispatch.DispatchStrategy;
import com.prolog.eis.service.container.ContainerTaskService;
import com.prolog.eis.service.outbound.BizOutboundTaskDetailService;
import com.prolog.eis.service.outbound.BizOutboundTaskService;
import com.prolog.eis.store_init.StoreInitStrategy;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author wk
 */
@Component
@RequiredArgsConstructor
public class AssignOrderStrategy extends DispatchStrategy {

    private final BizOutboundTaskService bizOutboundTaskService;

    private final BizOutboundTaskDetailService bizOutboundTaskDetailService;

    private final ContainerTaskService containerTaskService;

    @Override
    public void doDispatch(String billType, String warehouseArea, StoreInitStrategy storeInitStrategy) throws Exception {
        System.out.println("指定订单出库策略");
        InitDto initDto = storeInitStrategy.getInitDto(billType, warehouseArea, this);

        List<OrderDto> orderList = initDto.getOrderList();
        orderList.sort(Comparator.comparing(OrderDto::getPriority).thenComparing(OrderDto::getCreateTime));
        Map<String, BigDecimal> storeMap =
                initDto.getContainerInfoDtoList().stream().collect(Collectors.toMap(ContainerInfoDto::getBusinessProperty, ContainerInfoDto::getQty, (k1, k2) -> k1.add(k2)));
        for (OrderDto orderDto : orderList) {
            if (computeStore(orderDto, storeMap)) {
                List<ContainerTaskDto> containerTaskDtoList = computeStore(orderDto, initDto.getContainerInfoDtoList(), initDto.getStrategyMap());
                handleOrder(containerTaskDtoList, null,null);
                containerTaskService.createContainerTaskBatch(containerTaskDtoList);
                return ;
            }
        }
        throw new Exception("未找到对应托盘");
    }

    @Override
    public void handleOrder(List<ContainerTaskDto> containerTaskDtoList, String stationId,List<OrderDto> orderDtoList) {
        System.out.println("指定订单订单处理");
        Integer orderId = containerTaskDtoList.get(0).getOrderId();
        List<BizOutboundTaskDetail> bizOutboundTaskDetailList = bizOutboundTaskDetailService.findByOrderId(orderId);
        BizOutboundTask bizOutboundTask = bizOutboundTaskService.findById(orderId);
        bizOutboundTaskDetailList.forEach(k -> {
            k.setStatus(OrderStatusConstants.START_STATUS);
            k.setUpdateTime(LocalDateTime.now());
        });
        bizOutboundTaskDetailService.batchUpdate(bizOutboundTaskDetailList);
        bizOutboundTask.setStatus(OrderStatusConstants.START_STATUS);
        bizOutboundTask.setUpdateTime(LocalDateTime.now());
        bizOutboundTaskService.update(bizOutboundTask);

    }

    @Override
    public void delUseContainer(List<ContainerInfoDto> collect, OrderDto orderDto) {

    }

    private List<ContainerTaskDto> computeStore(OrderDto orderDto, List<ContainerInfoDto> containerInfoDtoList, Map<Integer, String> strategyMap) throws Exception {
        List<ContainerTaskDto> containerTaskDtoList = new ArrayList<>();
        List<OrderDetailDto> orderDetailDtoList = orderDto.getOrderDetailDtoList();
        List<OrderDto> orderDtos = Arrays.asList(orderDto);
        getAllTask(orderDtos, containerInfoDtoList, strategyMap, containerTaskDtoList, orderDetailDtoList);
        return containerTaskDtoList;
    }
}
