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

import com.oqpo.api.entity.oper.AfflSaleInfoEntity;
import com.oqpo.api.entity.pomng.PchReqDtlPtclEntity;
import com.oqpo.api.web.dto.request.oper.affl.SaveAfflRequest;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

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

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

    @ApiModelProperty(value = "구매요청고유번호")
    private String pchReqUnqNo;

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

    @ApiModelProperty(value = "매장아이디")
    private String storeId;

    @ApiModelProperty(value = "구매요청명")
    private String pchReqNm;

    @ApiModelProperty(value = "구매요청상태")
    private String pchReqStCd;  // PR00  임시저장, PR20 구매요청

    @ApiModelProperty(value = "비고")
    private String note;

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

    @NotBlank
    @ApiModelProperty(value = "창고아이디")
    private String whsId;

    @NotBlank
    @ApiModelProperty(value = "창고상세위치정보")
    private String location;

    @ApiModelProperty(value = "매장고유여신번호")
    private String loanMgntUnqNo;

    @NotBlank
    @ApiModelProperty(value = "구매요청구분")
    private String pchReqDvsn;


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

    @ApiModelProperty(value = "수정 품목리스트")
    private List<SavePchReqlRequest.SaveItemList> gridUpdateData;

    @ApiModelProperty(value = "삭제 품목리스트")
    private List<SavePchReqlRequest.SaveItemList> gridDeleteData;


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

        gridData.forEach(x -> {
            PchReqDtlPtclEntity entity = new PchReqDtlPtclEntity();
            entity.setPchReqUnqNo(x.getPchReqUnqNo());
            entity.setPchReqDtlNo(x.getPchReqDtlNo());
            entity.setItemId(x.getItemId());
            entity.setItemNm(x.getItemNm());
            entity.setUnit(x.getUnit());
            entity.setPchReqQty(x.getPchReqQty());
            entity.setPchReqAmt(x.getPchReqAmt());
            entity.setUnitAmt(x.getUnitAmt());
            entity.setBrandUnitUnqNo(x.getBrandUnitUnqNo());
            entity.setStoreUnitUnqNo(x.getStoreUnitUnqNo());
            entity.setUnitGubun(x.getUnitGubun());
            entities.add(entity);
        });
        return entities;
    }

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

        @ApiModelProperty(value = "구매요청번호")
        private String pchReqUnqNo;

        @ApiModelProperty(value = "구매요청상세번호")
        private String pchReqDtlNo;

        @NotBlank
        @ApiModelProperty(value = "품목아이디")
        private String itemId;

        @NotBlank
        @ApiModelProperty(value = "품목명")
        private String itemNm;

        @NotBlank
        @ApiModelProperty(value = "단위")
        private String unit;

        @NotBlank
        @ApiModelProperty(value = "구매요청수량")
        private Integer pchReqQty;

        @NotBlank
        @ApiModelProperty(value = "구매요청금액")
        private Integer pchReqAmt;

        @NotBlank
        @ApiModelProperty(value = "단가")
        private Integer unitAmt;

        @ApiModelProperty(value = "브랜드단가고유번호")
        private String brandUnitUnqNo;

        @ApiModelProperty(value = "매장단가고유번호")
        private String storeUnitUnqNo;

        @ApiModelProperty(value = "단가구분")
        private String unitGubun;
    }
}
