package com.prolog.eis.service.out;

import com.prolog.eis.constants.sys.SysParamConstants;
import com.prolog.eis.core.dto.route.AreaInOutCountDto;
import com.prolog.eis.core.model.biz.route.ContainerLocation;
import com.prolog.eis.dto.strategy.ContainerInfoDto;
import com.prolog.eis.router.vo.ContainerLocationVo;
import com.prolog.eis.service.feign.RouteFeignService;
import com.prolog.eis.service.sys.SysParamService;
import com.prolog.framework.core.exception.PrologException;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author dengj
 * @version 1.0
 * @date 2023/3/6 10:21
 */
@Service
@RequiredArgsConstructor
public class BasicStoreStrategyService {

    private final RouteFeignService routeFeignService;

    private final SysParamService sysParamService;

    public void getAreaTask(List<ContainerInfoDto> containerInfoDtoList) throws Exception {
        List<String> collect = containerInfoDtoList.stream().map(ContainerInfoDto::getAreaNo).filter(x -> Objects.nonNull(x)).distinct().collect(Collectors.toList());
        if (CollectionUtils.isEmpty(collect)) {
            throw new PrologException("容器集合所在区域不能为空");
        }
        List<AreaInOutCountDto> list = routeFeignService.queryAreaInOutCount(collect);
        for (ContainerInfoDto containerInfoDto : containerInfoDtoList) {

            AreaInOutCountDto inOutCountDto = list.stream().filter(x -> x.getAreaNo().equals(containerInfoDto.getAreaNo())).findFirst().orElse(null);
            if (Objects.isNull(inOutCountDto)) {
                containerInfoDto.setTaskNum(0);
            } else {
                containerInfoDto.setTaskNum(inOutCountDto.getInCount() + inOutCountDto.getOutCount());
            }
        }
    }


    public void getContainerLocation(List<ContainerInfoDto> containerInfoDtoList, String warehouseArea) throws Exception {
        List<String> containerNoList = containerInfoDtoList.stream().map(ContainerInfoDto::getContainerNo).collect(Collectors.toList());
        List<ContainerLocationVo> data = routeFeignService.findLocationByContainerList(containerNoList);

        // 判断四向库在途库存开关是否打开
        if (sysParamService.findVal(SysParamConstants.SXK_TO_AGV_STOCK_SWITCH, "0").equals("0")) {
            System.out.println("AGV拆零四向库库存预占开关关闭");
            // 查询四向库发往AGV库的在途库存
            List<ContainerLocationVo> sxkContainerLocations = data.stream().filter(containerLocationVo -> containerLocationVo.getSourceArea().contains("SX")).collect(Collectors.toList());
            if(org.apache.commons.collections.CollectionUtils.isNotEmpty(sxkContainerLocations)){
                // 按照容器号分组
                Map<String, List<ContainerLocationVo>> collect = sxkContainerLocations.stream().collect(Collectors.groupingBy(ContainerLocationVo :: getContainerNo));
                containerInfoDtoList.removeIf(t -> collect.containsKey(t.getContainerNo()));
            }
        }
        Iterator<ContainerInfoDto> iterator = containerInfoDtoList.iterator();
        Map<String, ContainerLocationVo> dataMap = data.stream().collect(Collectors.toMap(ContainerLocation::getContainerNo, t -> t));
        while (iterator.hasNext()) {
            ContainerInfoDto next = iterator.next();
            if (dataMap.containsKey(next.getContainerNo())) {
                ContainerLocationVo containerLocationVo = dataMap.get(next.getContainerNo());
                next.setAreaNo(containerLocationVo.getCurrentArea());
                next.setDepth(containerLocationVo.getDeptNum());
            } else {
                iterator.remove();
            }
        }
    }
}
