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

import com.fasterxml.jackson.annotation.JsonInclude;
import com.oqpo.api.entity.loanmng.LoanMgntBaseInfoEntity;
import com.oqpo.api.entity.oper.UserMngEntity;
import com.oqpo.api.entity.pomng.PchOdrBaseInfoEntity;
import com.oqpo.api.entity.pomng.PchOdrDtlPtclEntity;
import com.oqpo.api.entity.pomng.PchReqDtlPtclEntity;
import com.oqpo.api.web.dto.response.oper.affl.AfflShopInfoResponse;
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 PchOrdInitInfoResponse {

    @ApiModelProperty(value = "브랜드아이디") private String brandId;
    @ApiModelProperty(value = "브랜드명") private String brandNm;
    @ApiModelProperty(value = "매장아이디") private String storeId;
    @ApiModelProperty(value = "공급사아이디") private String spplyId;
    @ApiModelProperty(value = "공급사명") private String spplyNm;
    @ApiModelProperty(value = "발주일자") private String podrDt;
    @ApiModelProperty(value = "납품장소") private String dlvPlace;
    @ApiModelProperty(value = "납품창고 아이디") private String whsId;
    @ApiModelProperty(value = "납품창고 location") private String location;
    @ApiModelProperty(value = "발주담당자아이디") private String podrMgrId;
    @ApiModelProperty(value = "발주담당자명") private String podrMgrNm;
    @ApiModelProperty(value = "발주담당자연락처") private String podrMgrTelNo;
    @ApiModelProperty(value = "배송구분코드") private String dlvDvsn;
    @ApiModelProperty(value = "배송구분명") private String dlvDvsnNm;
    @ApiModelProperty(value = "발주총금액") private long podrTotalAmt;
    @ApiModelProperty(value = "납품요청일") private String dlvReqDt;
    @ApiModelProperty(value = "출하창고아이디") private String shmtWhsId;
    @ApiModelProperty(value = "출하창고명") private String shmtWhsNm;
    @ApiModelProperty(value = "츨히location") private String shmtLocation;
    @ApiModelProperty(value = "츨히location명") private String shmtLocationNm;

    @ApiModelProperty(value = "발주품목리스트") private List<PchOrdDtlInfo> itemList;


    public static PchOrdInitInfoResponse toDTO(UserMngEntity uEntity, PchOdrBaseInfoEntity baseEntity, List<PchOdrDtlPtclEntity> dtlEntity ) {
        if (uEntity == null) return null;
        return PchOrdInitInfoResponse.builder()
                .brandId(baseEntity.getBrandId() == null ? "" : baseEntity.getBrandId())
                .brandNm(baseEntity.getBrandNm() == null ? "" : baseEntity.getBrandNm())
                .storeId(baseEntity.getStoreId() == null ? "" : baseEntity.getStoreId())
                .spplyId(baseEntity.getSpplyId() == null ? "" : baseEntity.getSpplyId())
                .spplyNm(baseEntity.getSpplyNm() == null ? "" : baseEntity.getSpplyNm())
                .podrDt(baseEntity.getPodrDt() == null ? "" : baseEntity.getPodrDt())
                .dlvPlace(baseEntity.getDlvPlace() == null ? "" : baseEntity.getDlvPlace())
                .whsId(baseEntity.getWhsId() == null ? "" : baseEntity.getWhsId())
                .location(baseEntity.getLocation() == null ? "" : baseEntity.getLocation())
                .podrMgrId(uEntity.getUserId() == null ? "" : uEntity.getUserId())
                .podrMgrNm(uEntity.getUserNm() == null ? "" : uEntity.getUserNm())
                .podrMgrTelNo(uEntity.getTelNo() == null ? "" : uEntity.getTelNo())
                .dlvDvsn(baseEntity.getDlvDvsn() == null ? "" : baseEntity.getDlvDvsn())
                .dlvDvsnNm(baseEntity.getDlvDvsnNm() == null ? "" : baseEntity.getDlvDvsnNm())
                .podrTotalAmt(baseEntity.getPodrTotalAmt() == null ? 0 : baseEntity.getPodrTotalAmt())
                .dlvReqDt(baseEntity.getDlvReqDt() == null ? "" : baseEntity.getDlvReqDt())
                .shmtWhsId(baseEntity.getShmtWhsId() == null ? "" : baseEntity.getShmtWhsId())
                .shmtWhsNm(baseEntity.getShmtWhsNm() == null ? "" : baseEntity.getShmtWhsNm())
                .shmtLocation(baseEntity.getShmtLocation() == null ? "" : baseEntity.getShmtLocation())
                .shmtLocationNm(baseEntity.getShmtLocationNm() == null ? "" : baseEntity.getShmtLocationNm())
                .itemList(dtlEntity.stream()
                        .map(m -> PchOrdDtlInfo.builder()
                                .pchReqUnqNo(m.getPchReqUnqNo() == null ? "" : m.getPchReqUnqNo() )
                                .pchReqDtlNo(m.getPchReqDtlNo() == null ? "" : m.getPchReqDtlNo() )
                                .itemId(m.getItemId() == null ? "" : m.getItemId())
                                .itemNm(m.getItemNm() == null ? "" : m.getItemNm())
                                .unitAmt(m.getUnitAmt() == null ? 0 : m.getUnitAmt())
                                .unit(m.getUnit() == null ? "" : m.getUnit())
                                .pchReqQty(m.getPchReqQty() == null ? 0 : m.getPchReqQty())
                                .podrQty(m.getPodrQty() == null ? 0 : m.getPodrQty())
                                .podrAmt(m.getPodrAmt() == null ? 0 : m.getPodrAmt())
                                .dlvReqDt(m.getDlvReqDt() == null ? "" : m.getDlvReqDt())
                                .whsPassQty(m.getWhsPassQty() == null ? 0 : m.getWhsPassQty())
                                .brandUnitUnqNo(m.getBrandUnitUnqNo() == null ? "" : m.getBrandUnitUnqNo())
                                .storeUnitUnqNo(m.getStoreUnitUnqNo() == null ? "" : m.getStoreUnitUnqNo())
                                .spplyId(m.getSpplyId() == null ? "" : m.getSpplyId())
                                .spplyNm(m.getSpplyNm() == null ? "" : m.getSpplyNm())
                                .build())
                        .collect(Collectors.toList()))
                .build();
    }


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

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

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

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

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

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

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

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

        @ApiModelProperty(value = "발주수량")
        private Integer podrQty;

        @ApiModelProperty(value = "발주금액")
        private Long podrAmt;

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

        @ApiModelProperty(value = "출고가능수량")
        private Integer whsPassQty;

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

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

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

        @ApiModelProperty(value = "공급업체명")
        private String spplyNm;

    }
}
