package com.oqpo.api.web.dto.request.rtnmng;

import com.oqpo.api.entity.pomng.PchReqDtlPtclEntity;
import com.oqpo.api.entity.rtnmng.RtnReqDtlPtclEntity;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

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

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

    @ApiModelProperty(value = "반품요청고유번호")
    private String rtnReqUnqNo;

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

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

    @ApiModelProperty(value = "구매요청상태")
    private String rtnReqStCd;  // RR00  임시저장, RR20 구매요청

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

    @NotBlank
    @ApiModelProperty(value = "수거요청일")
    private String colReqDt ;

    @ApiModelProperty(value = "반품 창고구분")
    private String whsDvsn;

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

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


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

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

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


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

        gridData.forEach(x -> {
            RtnReqDtlPtclEntity entity = new RtnReqDtlPtclEntity();
            entity.setRtnReqUnqNo(x.getRtnReqUnqNo());
            entity.setRtnReqDtlNo(x.getRtnReqDtlNo());
            entity.setItemId(x.getItemId());
            entity.setItemNm(x.getItemNm());
            entity.setUnit(x.getUnit());
            entity.setRtnQty(x.getRtnQty());
            entity.setRtnAmt(x.getRtnAmt());
            entity.setUnitAmt(x.getUnitAmt());
            entity.setRtnDvsn(x.getRtnDvsn());
            entity.setRtnRsn(x.getRtnRsn());
            entity.setRtnRsnDvsn(x.getRtnRsnDvsn());
            entity.setSpplyId(x.getSpplyId());
            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 SaveRtnItemList {

        @ApiModelProperty(value = "반품요청번호")
        private String rtnReqUnqNo;

        @ApiModelProperty(value = "반품품청상세번호")
        private String rtnReqDtlNo;

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

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

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

        @NotBlank
        @ApiModelProperty(value = "반품요청수량")
        private Integer rtnQty;

        @NotBlank
        @ApiModelProperty(value = "반품요청금액")
        private Long rtnAmt;

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

        @NotBlank
        @ApiModelProperty(value = "반품구분")
        private String rtnDvsn;

        @ApiModelProperty(value = "반품사유")
        private String rtnRsn;

        @ApiModelProperty(value = "반품사유구분")
        private String rtnRsnDvsn;

        @ApiModelProperty(value = "공급사아이디")
        private String spplyId;

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

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

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