package com.oqpo.api.web.dto.response.rtnmng.rtnOrd;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.oqpo.api.entity.pomng.PchOdrBaseInfoEntity;
import com.oqpo.api.entity.pomng.PchOdrDtlPtclEntity;
import com.oqpo.api.entity.rtnmng.RtnOdrBaseInfoEntity;
import com.oqpo.api.entity.rtnmng.RtnOdrDtlPtclEntity;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

import java.util.List;
import java.util.stream.Collectors;


@Getter
@Setter
@Builder
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@JsonInclude(JsonInclude.Include.ALWAYS)
public class RtnOdrInfoResponse {

    @ApiModelProperty(value = "반품발주고유번호") private String rtnPodrUnqNo;
    @ApiModelProperty(value = "브랜드아이디") private String brandId;
    @ApiModelProperty(value = "브랜드명") private String brandNm;
    @ApiModelProperty(value = "반품발주일자") private String rtnOdrRegDt;
    @ApiModelProperty(value = "반품요청구분") private String rtnReqDvsn;
    @ApiModelProperty(value = "반품요청구분명") private String rtnReqDvsnNm;
    @ApiModelProperty(value = "반품발주합계금액") private Long rtnDodrTotalAmt;
    @ApiModelProperty(value = "반품발주품목수량") private Integer rtnDodrItemQty;
    @ApiModelProperty(value = "반품담당자명") private String rtnMgrNm;
    @ApiModelProperty(value = "반품담당자아이디") private String rtnMgrId;
    @ApiModelProperty(value = "반품담당자전화번호") private String rtnMgrTelNo;
    @ApiModelProperty(value = "반품발주상태코드") private String rtnOdrStCd;
    @ApiModelProperty(value = "반품발주상태명") private String rtnOdrStNm;
    @ApiModelProperty(value = "공급사아이디") private String spplyId;
    @ApiModelProperty(value = "공급사명") private String spplyNm;
    @ApiModelProperty(value = "입고창고아이디") private String whsId;
    @ApiModelProperty(value = "입고창고명") private String whsNm;
    @ApiModelProperty(value = "입고LOCATION") private String location;
    @ApiModelProperty(value = "입고LOCATION명") private String locationNm;
    @ApiModelProperty(value = "수거구분") private String colDvsn;
    @ApiModelProperty(value = "수거구분명") private String colDvsnNm;
    @ApiModelProperty(value = "수거요청일자") private String colReqDt;
    @ApiModelProperty(value = "매장아이디") private String storeId;
    @ApiModelProperty(value = "매장명") private String storeNm;
    @ApiModelProperty(value = "반품창고아이디") private String rtnWhsId;
    @ApiModelProperty(value = "반품창고명") private String rtnWhsNm;
    @ApiModelProperty(value = "반품LOCATION") private String rtnLocation;
    @ApiModelProperty(value = "반품LOCATION명") private String rtnLocationNm;


    @ApiModelProperty(value = "구매발주 품목 정보")
    private List<RtnOdrInfoResponse.RtnOrdPoDtlInfo> rtnOdrDtlList;

    public static RtnOdrInfoResponse toDTO(RtnOdrBaseInfoEntity entity, List<RtnOdrDtlPtclEntity>  dtlEntityList) {
        if (entity == null) return null;
        return RtnOdrInfoResponse.builder()
                .rtnPodrUnqNo(entity.getRtnPodrUnqNo() == null ? "" : entity.getRtnPodrUnqNo() )
                .brandId(entity.getBrandId() == null ? "" : entity.getBrandId())
                .brandNm(entity.getBrandNm() == null ? "" : entity.getBrandNm())
                .rtnOdrRegDt(entity.getRtnOdrRegDt() == null ? "" : entity.getRtnOdrRegDt())
                .rtnReqDvsn(entity.getRtnReqDvsn() == null ? "" : entity.getRtnReqDvsn())
                .rtnReqDvsnNm(entity.getRtnReqDvsnNm() == null ? "" : entity.getRtnReqDvsnNm())
                .rtnDodrTotalAmt(entity.getRtnDodrTotalAmt() == null ? 0 : entity.getRtnDodrTotalAmt())
                .rtnDodrItemQty(entity.getRtnDodrItemQty() == null ? 0 : entity.getRtnDodrItemQty())
                .rtnMgrNm(entity.getRtnMgrNm() == null ? "" : entity.getRtnMgrNm())
                .rtnMgrId(entity.getRtnMgrId() == null ? "" : entity.getRtnMgrId())
                .rtnMgrTelNo(entity.getRtnMgrTelNo() == null ? "" : entity.getRtnMgrTelNo())
                .rtnOdrStCd(entity.getRtnOdrStCd() == null ? "" : entity.getRtnOdrStCd())
                .rtnOdrStNm(entity.getRtnOdrStNm() == null ? "" : entity.getRtnOdrStNm())
                .spplyId(entity.getSpplyId() == null ? "" : entity.getSpplyId())
                .spplyNm(entity.getSpplyNm() == null ? "" : entity.getSpplyNm())
                .whsId(entity.getWhsId() == null ? "" : entity.getWhsId())
                .whsNm(entity.getWhsNm() == null ? "" : entity.getWhsNm())
                .location(entity.getLocation() == null ? "" : entity.getLocation())
                .locationNm(entity.getLocationNm() == null ? "" : entity.getLocationNm())
                .colDvsn(entity.getColDvsn() == null ? "" : entity.getColDvsn())
                .colDvsnNm(entity.getColDvsnNm() == null ? "" : entity.getColDvsnNm())
                .colReqDt(entity.getColReqDt() == null ? "" : entity.getColReqDt())
                .storeId(entity.getStoreId() == null ? "" : entity.getStoreId())
                .storeNm(entity.getStoreNm() == null ? "" : entity.getStoreNm())
                .rtnWhsId(entity.getRtnWhsId() == null ? "" : entity.getRtnWhsId())
                .rtnWhsNm(entity.getRtnWhsNm() == null ? "" : entity.getRtnWhsNm())
                .rtnLocation(entity.getRtnLocation() == null ? "" : entity.getRtnLocation())
                .rtnLocationNm(entity.getRtnLocationNm() == null ? "" : entity.getRtnLocationNm())
                .rtnOdrDtlList(dtlEntityList.stream()
                        .map(m -> RtnOrdPoDtlInfo.builder()
                                .rtnPodrUnqNo(m.getRtnPodrUnqNo() == null ? "" : m.getRtnPodrUnqNo())
                                .rtnPodrDtlNo(m.getRtnPodrDtlNo() == null ? "" : m.getRtnPodrDtlNo())
                                .rtnReqUnqNo(m.getRtnReqUnqNo() == null ? "" : m.getRtnReqUnqNo())
                                .rtnReqDtlNo(m.getRtnReqDtlNo() == null ? "" : m.getRtnReqDtlNo())
                                .itemId(m.getItemId() == null ? "" : m.getItemId())
                                .itemNm(m.getItemNm() == null ? "" : m.getItemNm())
                                .unit(m.getUnit() == null ? "" : m.getUnit())
                                .unitAmt(m.getUnitAmt() == null ? 0 : m.getUnitAmt())
                                .colReqDt(m.getColReqDt() == null ? "" : m.getColReqDt())
                                .rtnReqQty(m.getRtnReqQty() == null ? 0 : m.getRtnReqQty())
                                .rtnDvsn(m.getRtnDvsn() == null ? "" : m.getRtnDvsn())
                                .rtnDvsnNm(m.getRtnDvsnNm() == null ? "" : m.getRtnDvsnNm())
                                .rtnQty(m.getRtnQty() == null ? 0 : m.getRtnQty())
                                .rtnAmt(m.getRtnAmt() == null ? 0 : m.getRtnAmt())
                                .rtnRsnDvsn(m.getRtnRsnDvsn() == null ? "" : m.getRtnRsnDvsn())
                                .rtnRsnDvsnNm(m.getRtnRsnDvsnNm() == null ? "" : m.getRtnRsnDvsnNm())
                                .rtnRsn(m.getRtnRsn() == null ? "" : m.getRtnRsn())
                                .rtnOdrQty(m.getRtnOdrQty() == null ? 0 : m.getRtnOdrQty())
                                .rtnOdrAmt(m.getRtnOdrAmt() == null ? 0 : m.getRtnOdrAmt())
                                .rodrDtlStCd(m.getRodrDtlStCd() == null ? "" : m.getRodrDtlStCd())
                                .rodrDtlStNm(m.getRodrDtlStNm() == null ? "" : m.getRodrDtlStNm())
                                .build())
                        .collect(Collectors.toList()))
                .build();
    }

    @Getter
    @Builder
    @AllArgsConstructor(access = AccessLevel.PROTECTED)
    static class RtnOrdPoDtlInfo {
        @ApiModelProperty(value = "반품발주고유번호") private String rtnPodrUnqNo;
        @ApiModelProperty(value = "반품발주상세번호") private String rtnPodrDtlNo;
        @ApiModelProperty(value = "반품요청고유번호") private String rtnReqUnqNo;
        @ApiModelProperty(value = "반품요청상세번호") private String rtnReqDtlNo;
        @ApiModelProperty(value = "품목아이디") private String itemId;
        @ApiModelProperty(value = "품목명") private String itemNm;
        @ApiModelProperty(value = "단위") private String unit;
        @ApiModelProperty(value = "단가금액") private Long unitAmt;
        @ApiModelProperty(value = "수거요청일자") private String colReqDt;
        @ApiModelProperty(value = "반품요청수량") private Integer rtnReqQty;
        @ApiModelProperty(value = "반품구분") private String rtnDvsn;
        @ApiModelProperty(value = "반품구분명") private String rtnDvsnNm;
        @ApiModelProperty(value = "반품수량") private Integer rtnQty;
        @ApiModelProperty(value = "반품금액") private Long rtnAmt;
        @ApiModelProperty(value = "반품사유구분") private String rtnRsnDvsn;
        @ApiModelProperty(value = "반품사유구분명") private String rtnRsnDvsnNm;
        @ApiModelProperty(value = "반품사유") private String rtnRsn;
        @ApiModelProperty(value = "반품발주수량") private Integer rtnOdrQty;
        @ApiModelProperty(value = "반품발주금액") private Long rtnOdrAmt;
        @ApiModelProperty(value = "반품발주상세상태코드") private String rodrDtlStCd;
        @ApiModelProperty(value = "반품발주상세상태명") private String rodrDtlStNm;

    }
}
