package com.prolog.eis.dto.buildwave;

import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Getter
@Setter
public class PObOrderCollection {
    @ApiModelProperty("订单总体积")
    private BigDecimal vol = new BigDecimal(0);

    @ApiModelProperty("所有订单集合")
    private List<PObOrderDto> orderList = new ArrayList<>();

    public PObOrderDto findMaxVolYunDan() {
        if (this.getOrderList() == null || this.getOrderList().size() == 0)
            return null;

        PObOrderDto maxValYd = this.getOrderList().get(0);

        for (int i = 1; i < this.getOrderList().size(); i++) {
            PObOrderDto yd = this.getOrderList().get(i);
            if (yd.getVolume().compareTo(maxValYd.getVolume()) > 0)
                maxValYd = yd;
        }

        return maxValYd;
    }

    public PObOrderDto findMinVolYunDan() {
        if (this.getOrderList() == null || this.getOrderList().size() == 0)
            return null;

        PObOrderDto minValYd = this.getOrderList().get(0);

        for (int i = 1; i < this.getOrderList().size(); i++) {
            PObOrderDto yd = this.getOrderList().get(i);
            if (yd.getVolume().compareTo(minValYd.getVolume()) < 0)
                minValYd = yd;
        }

        return minValYd;
    }

    public void swapYunDan(PObOrderDto currentYd, PObOrderDto newYd) {
        this.getOrderList().remove(currentYd);
        this.getOrderList().add(newYd);
        this.setVol(this.getVol().add(newYd.getVolume()).subtract(currentYd.getVolume()));
    }

    public String toString() {
        return String.format("体积：%s，订单数：%s", this.vol, this.orderList.size());
    }
}
