package com.prolog.cs.biz.outbound.stage;

import com.prolog.cs.basic.warehouse.model.vo.StageInfo;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


public class WorkStage
{
    private StageInfo info;
    private List<WorkStageBillInfo> bills;
    private BigDecimal caseQty;//当前数量
    private BigDecimal numOfClients;//当前客户数量
    private BigDecimal numOfBills; //当前订单数量
    private BigDecimal numOfVol; //当前体积
    private BigDecimal result;//被选中后本次存放数量
    private long defaultMaxClientValue = 20;
    //最多放100箱
    private long defaultMaxPieceNum = 100;
    private long defaultBillNum = 20;

    private BigDecimal remain;
    private BigDecimal remainVol;
    private BigDecimal remainNumOfBills;
    private BigDecimal remainNumOfClients;

    public WorkStage(StageInfo infoTemp, List<WorkStageBillInfo> billInfos)

    {
        info = infoTemp;

        if (billInfos == null || billInfos.isEmpty())
        {
            caseQty = BigDecimal.ZERO;
            numOfBills = BigDecimal.ZERO;
            numOfClients = BigDecimal.ZERO;
        } else
        {
            info = infoTemp;
            bills = billInfos;


            //集货区按订单维度存放
            this.numOfBills = BigDecimal.valueOf(bills.size());
            Map<String, List<WorkStageBillInfo>> groupByClientId =
                    bills.stream().filter(x -> x.getShipperId() != null).
                            collect(Collectors.groupingBy(WorkStageBillInfo::getShipperId));

            numOfClients = BigDecimal.valueOf(groupByClientId.size());

            caseQty = bills.stream().map(WorkStageBillInfo::getCaseQty).reduce((a, b) -> a.add(b)).orElse(BigDecimal.ZERO);
        }

    }

    public StageInfo getInfo()
    {
        return info;
    }

    public void setInfo(StageInfo info)
    {
        this.info = info;
    }

    public List<WorkStageBillInfo> getBills()
    {
        return bills;
    }

    public BigDecimal getNumOfVol() {
        return numOfVol;
    }

    public void setNumOfVol(BigDecimal numOfVol) {
        this.numOfVol = numOfVol;
    }

    public void setBills(List<WorkStageBillInfo> bills)
    {
        this.bills = bills;
    }

    public BigDecimal getNumOfClients()
    {
        return numOfClients;
    }

    public void setNumOfClients(BigDecimal numOfClients)
    {
        this.numOfClients = numOfClients;
    }

    public BigDecimal getNumOfBills()
    {
        return numOfBills;
    }

    public void setNumOfBills(BigDecimal numOfBills)
    {
        this.numOfBills = numOfBills;
    }

    public BigDecimal getResult()
    {
        return result;
    }

    public void setResult(BigDecimal result)
    {
        this.result = result;
    }

    public BigDecimal getCaseQty()
    {
        return caseQty;
    }

    public void setCaseQty(BigDecimal caseQty)
    {
        this.caseQty = caseQty;
    }

    public String getCode()
    {
        return info.getStageCode();
    }


    public BigDecimal getRemain()
    {
        if (remain != null)
        {
            return remain;
        }

//        BigDecimal maxClientNum = info.getMaxClientNum();
//        if (maxClientNum == null)
//        {
//            maxClientNum = BigDecimal.valueOf(defaultMaxClientValue);
//        }
//        if (maxClientNum.compareTo(numOfClients) <= 0)
//        {
//            return BigDecimal.valueOf(0);
//
//        }
//        BigDecimal maxBillNum = info.getMaxBillNum();
//        if (maxBillNum == null || (maxBillNum.compareTo(BigDecimal.ZERO) == 0))
//        {
//            maxBillNum = BigDecimal.valueOf(defaultBillNum);
//        }
//
//        if (maxBillNum.compareTo(numOfBills) <= 0)
//        {
//            return BigDecimal.valueOf(0);
//        }

        BigDecimal maxPieceNum = info.getMaxPieceNum();

        if (maxPieceNum == null)
        {
            maxPieceNum = BigDecimal.valueOf(defaultMaxPieceNum);
        }

        remain = maxPieceNum.subtract(caseQty);

        if (remain.compareTo(BigDecimal.valueOf(0)) <= 0)
        {
            remain = BigDecimal.ZERO;
        }
        return remain;

    }

    public BigDecimal numberOfBills()
    {
        return numOfBills;
    }

    public String getStageId()
    {
        return info.getStageId();
    }

    public BigDecimal getRemainVol() {
        if(numOfVol==null){
            return info.getMaxVolNum(); 
        }
        return info.getMaxVolNum().subtract(numOfVol);
    }

    public void setRemainVol(BigDecimal remainVol) {
        this.remainVol = remainVol;
    }

    public BigDecimal getRemainNumOfBills() {
        if(numOfBills==null){
            return info.getMaxBillNum();
        }
        return info.getMaxBillNum().subtract(numOfBills);
    }

    public void setRemainNumOfBills(BigDecimal remainNumOfBills) {
        this.remainNumOfBills = remainNumOfBills;
    }

    public BigDecimal getRemainNumOfClients() {
        if(numOfClients==null){
            return info.getMaxClientNum();
        }
        return info.getMaxClientNum().subtract(numOfClients);
    }

    public void setRemainNumOfClients(BigDecimal remainNumOfClients) {
        this.remainNumOfClients = remainNumOfClients;
    }
}
