package com.xisc.pm.api.dto;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import io.choerodon.core.exception.CommonException;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * 组合板信息DTO
 * @author jun.zeng
 *
 */
@Data
public class PmCompositeBoardDTO implements Serializable {
    
    private static final long serialVersionUID = -2050706044232142434L;

    @ApiModelProperty("组合板长度,前端输入")
    private BigDecimal szLength;
    
    @ApiModelProperty("组合板宽度,前端输入")
    private BigDecimal szWidth;
    
    @ApiModelProperty("小板总数,前端输入")
    private Integer spCount ;
    
    @ApiModelProperty("合同总数,前端输入")
    private Integer orderCount ;
    
    @ApiModelProperty("BLOCK个数,前端输入")
    private Integer blkCount;
    
    @ApiModelProperty("取板方式_长度方向,前端输入")
    private Integer pickModeLenDir;
    
    @ApiModelProperty("取板方式_宽度方向,前端输入")
    private Integer pickModeWidDir;
    
    @ApiModelProperty("组合子板（Block）,前端输入")
    private List<PmCompositeBlockDTO> pmCompositeBlockDTOs;
    
    @ApiModelProperty("组合子板（Block）,合同组板小板数")
    private Map<String , BigDecimal> orderSpCount ;
    
    public void check() {
        if(this.szLength == null) {
            throw new CommonException("pm.pm_pro_cont_009", "组合板长");
        }
        if(this.szWidth == null) {
            throw new CommonException("pm.pm_pro_cont_009", "组合板宽");
        }
        if(this.spCount == null || spCount == 0) {
            throw new CommonException("pm.pm_pro_cont_009", "小板数");
        }
        if(this.orderCount == null || orderCount == 0) {
            throw new CommonException("pm.pm_pro_cont_009", "合同数");
        }
        if(this.blkCount == null || blkCount == 0) {
            throw new CommonException("pm.pm_pro_cont_009", "BLOCK数");
        }
        if(this.pickModeLenDir == null || pickModeLenDir == 0) {
            throw new CommonException("pm.pm_pro_cont_009", "取板方式_长度方向");
        }
        if(this.pickModeWidDir == null || pickModeWidDir == 0) {
            throw new CommonException("pm.pm_pro_cont_009", "取板方式_宽度方向");
        }
        if(this.pmCompositeBlockDTOs == null) {
            throw new CommonException("pm.pm_pro_cont_009", "组合子板");
        }
        if(this.pmCompositeBlockDTOs.size() != blkCount) {
            throw new CommonException("pm.pm_pro_cont_046", "组合子板");
        }
        // 汇总统计合同总小板数
        this.orderSpCount = new HashMap<>();
        for (PmCompositeBlockDTO pmCompositeBlockDTO : pmCompositeBlockDTOs) {
            Map<String , BigDecimal> map = pmCompositeBlockDTO.check();
            for (Entry<String, BigDecimal> entry : map.entrySet()) {
                if(orderSpCount.containsKey(entry.getKey())) {
                    orderSpCount.put(entry.getKey(), entry.getValue().add(orderSpCount.get(entry.getKey())));
                } else {
                    orderSpCount.put(entry.getKey(), entry.getValue());
                }
            }
        }
    }
    
}
