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

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

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

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

    @NotBlank
    @ApiModelProperty(value = "코드")
    private String codeCd;

    @NotBlank
    @ApiModelProperty(value = "코드타입")
    private String codeTpCd;

    @NotBlank
    @ApiModelProperty(value = "코드명")
    private String codeNm;

    @NotBlank
    @ApiModelProperty(value = "사용여부")
    private String useYn;

    @ApiModelProperty(value = "등록상세리스트")
    private List<SaveCodeDtl> gridInsertData;

    @ApiModelProperty(value = "수정상세리스트")
    private List<SaveCodeDtl> gridUpdateData;

    @ApiModelProperty(value = "삭제상세리스트")
    private List<SaveCodeDtl> gridDeleteData;

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

        gridData.forEach(x -> {
            CodeEntity entity = new CodeEntity();
            entity.setCodeCd(x.getCodeCd());
            entity.setCodeDtlCd(x.getCodeDtlCd());
            entity.setCodeDtlNm(x.getCodeDtlNm());
            entity.setUseYn(x.getUseYn());
            entity.setViewNum(x.getViewNum());
            entity.setCodeDesc(x.getCodeDesc());
            entities.add(entity);
        });
        return entities;
    }

    @Getter
    @Builder
    @AllArgsConstructor(access = AccessLevel.PROTECTED)
    static class SaveCodeDtl {
        @NotBlank
        @ApiModelProperty(value = "코드")
        private String codeCd;

        @NotBlank
        @ApiModelProperty(value = "상세코드")
        private String codeDtlCd;

        @NotBlank
        @ApiModelProperty(value = "상세코드명")
        private String codeDtlNm;

        @NotBlank
        @ApiModelProperty(value = "사용여부")
        private String useYn;

        @ApiModelProperty(value = "출력순번")
        private Integer viewNum;

        @ApiModelProperty(value = "상세비고")
        private String codeDesc;
    }
}
