package com.oqpo.api.web.dto.request.pomng.pchOrd;

import com.oqpo.api.entity.pomng.PchOdrDtlPtclEntity;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;

@Getter
@Setter
public class SavePchOrdRequest {
    @NotBlank
    @ApiModelProperty(value = "CRUD")
    private String viewCd;

    @NotBlank
    @ApiModelProperty(value = "JOB  구매요청건 발주 - R  , 신규발주 - P")
    private String jobCd;

    @ApiModelProperty(value = "구매발주고유번호") private String pchPodrUnqNo;

    @NotBlank
    @ApiModelProperty(value = "브랜드아이디") private String brandId;

    @NotBlank
    @ApiModelProperty(value = "배송구분") private String dlvDvsn;

    @NotBlank
    @ApiModelProperty(value = "납품요청일") private String dlvReqDt;

    @ApiModelProperty(value = "비고") private String note;
    @ApiModelProperty(value = "공급사아이디") private String spplyId;
    @ApiModelProperty(value = "창고아이디") private String whsId;
    @ApiModelProperty(value = "LOCATION") private String location;
    @ApiModelProperty(value = "출하창고아이디") private String shmtWhsId;
    @ApiModelProperty(value = "출하LOCATION") private String shmtLocation;
    @ApiModelProperty(value = "매장아이디") private String storeId;


    @ApiModelProperty(value = "등록 품목리스트")
    private List<SavePoItemList> gridInsertData;


    public List<PchOdrDtlPtclEntity> toCodeEntities(List<SavePoItemList> gridData) {
        List<PchOdrDtlPtclEntity> entities = new ArrayList<>();
        if (gridData == null || gridData.size() == 0)
            return entities;

        gridData.forEach(x -> {
            PchOdrDtlPtclEntity entity = new PchOdrDtlPtclEntity();
            entity.setPchReqUnqNo(x.getPchReqUnqNo());
            entity.setPchReqDtlNo(x.getPchReqDtlNo());
            entity.setItemId(x.getItemId());
            entity.setPchReqQty(x.getPchReqQty());
            entity.setPodrQty(x.getPodrQty());
            entity.setBrandUnitUnqNo(x.getBrandUnitUnqNo());
            entity.setStoreUnitUnqNo(x.getStoreUnitUnqNo());
            entities.add(entity);
        });
        return entities;
    }

    @Getter
    @Builder
    @AllArgsConstructor(access = AccessLevel.PROTECTED)
    static class SavePoItemList {

        @ApiModelProperty(value = "구매요청고유번호") private String pchReqUnqNo;
        @ApiModelProperty(value = "구매요청상세번호") private String pchReqDtlNo;
        @ApiModelProperty(value = "품목아이디") private String itemId;
        @ApiModelProperty(value = "구매요청수량") private Integer pchReqQty;
        @ApiModelProperty(value = "발주수량") private Integer podrQty;
        @ApiModelProperty(value = "브랜드단가고유번호") private String brandUnitUnqNo;
        @ApiModelProperty(value = "매장단가고유번호") private String storeUnitUnqNo;
    }
}
