package com.prolog.eis.utils;

import com.prolog.eis.vo.GoodsInfoVO;
import com.prolog.eis.vo.GoodsShelvesVO;
import jodd.util.StringUtil;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


public class ABCSurplusLocation {


    public static  List<GoodsShelvesVO> queryGoodsShelf(List<GoodsShelvesVO> shelves, String goodsAbcType, GoodsInfoVO param, String planes){

        List<GoodsShelvesVO> collect = new ArrayList<>();
        boolean flag = false;
        //查询同一面有同goodsId的情况
        List<GoodsShelvesVO> collect1 = shelves.stream().filter(
                p -> param.getProperty().equals(p.getProperty()) &&  p.getPlanes().equals(planes)
        ).collect(Collectors.toList());
        if(!collect1.isEmpty()){
            collect = collect1.stream().sorted(Comparator.comparing(GoodsShelvesVO::getPalletLocation)).collect(Collectors.toList());
            if(!collect.isEmpty()){
                flag = true;
            }
        }
        //查询同一面有对应ABC空货格的情况
        List<GoodsShelvesVO> collect2 = shelves.stream().filter(
                p ->  getProperty(p) &&  p.getPlanes().equals(planes)
        ).collect(Collectors.toList());
        if(!flag && !collect2.isEmpty()){
            collect = collect2.stream().filter(x -> x.getAbcType().equals(goodsAbcType)).sorted(Comparator.comparing(GoodsShelvesVO::getPalletLocation)).collect(Collectors.toList());
            if(!collect.isEmpty()){
                flag = true;
            }
        }
        //查询反面同goodsId有容量的货位
        List<GoodsShelvesVO> collect3 = shelves.stream().filter(
                p ->  param.getProperty().equals(p.getProperty()) &&  !p.getPlanes().equals(planes)
        ).collect(Collectors.toList());
        if(!flag && !collect3.isEmpty()){
            collect = collect3.stream().sorted(Comparator.comparing(GoodsShelvesVO::getPalletLocation)).collect(Collectors.toList());
            if(!collect.isEmpty()){
                flag = true;
            }
        }

        //查询反面有对应ABC空的货位
        List<GoodsShelvesVO> collect4 = shelves.stream().filter(
                p ->  getProperty(p)  &&  !p.getPlanes().equals(planes)
        ).collect(Collectors.toList());

        if(!flag && !collect4.isEmpty()){
            collect = collect4.stream().filter(x -> x.getAbcType().equals(goodsAbcType)).sorted(Comparator.comparing(GoodsShelvesVO::getPalletLocation)).collect(Collectors.toList());
            if(!collect.isEmpty()){
                flag = true;
            }
        }
        return  flag ? collect : new ArrayList<>();
    }

    private static Boolean getProperty(GoodsShelvesVO param){
        Boolean flg = false;
        if(StringUtil.isEmpty(param.getItemId()) && StringUtil.isEmpty(param.getLotId()) &&StringUtil.isEmpty(param.getOwnerId())){
            flg = true;
        }
        return flg;
    }

}
