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

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.PchReqBaseInfoEntity;
import com.oqpo.api.entity.pomng.PchReqDtlPtclEntity;
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 PchReqInfoResponse {

    @ApiModelProperty(value = "구매요청고유번호") private String pchReqUnqNo;
    @ApiModelProperty(value = "브랜드아이디") private String brandId;
    @ApiModelProperty(value = "브랜드명") private String brandNm;
    @ApiModelProperty(value = "매장아이디") private String storeId;
    @ApiModelProperty(value = "구매요청명") private String pchReqNm;
    @ApiModelProperty(value = "구매요청부서") private String pchReqDept;
    @ApiModelProperty(value = "구매요청자명") private String pchReqMgrNm;
    @ApiModelProperty(value = "구매요청일자") private String pchReqDt;
    @ApiModelProperty(value = "구매요청상태코드") private String pchReqStCd;
    @ApiModelProperty(value = "구매요청상태명") private String pchReqStNm;
    @ApiModelProperty(value = "구매요청구분") private String pchReqDvsn;
    @ApiModelProperty(value = "비고") private String note;
    @ApiModelProperty(value = "구매요청품목수량") private Integer pchReqItemQty;
    @ApiModelProperty(value = "구매요청합계금액") private Integer pchReqTotalAmt;
    @ApiModelProperty(value = "구매요청반려일자") private String pchReqRjctDt;
    @ApiModelProperty(value = "구매요청반려자명") private String pchReqRjctNm;
    @ApiModelProperty(value = "구매요청반려사유") private String pchReqRjctRsn;
    @ApiModelProperty(value = "구매요청자아이디") private String pchReqMgrId;
    @ApiModelProperty(value = "구매요청반려자아이디") private String pchReqRjctId;
    @ApiModelProperty(value = "납기요청일") private String dlvReqDt;
    @ApiModelProperty(value = "창고아이디") private String whsId;
    @ApiModelProperty(value = "창고명") private String whsNm;
    @ApiModelProperty(value = "창고상세위치") private String location;
    @ApiModelProperty(value = "창고상세위치명") private String locationNm;
    @ApiModelProperty(value = "선결재금액") private Integer pchApprAmt;
    @ApiModelProperty(value = "선결재금액") private String pchApprYn;
    @ApiModelProperty(value = "결재상태") private String pchApprNm;
    @ApiModelProperty(value = "시스템등록일시") private String sysRegDttm;
    @ApiModelProperty(value = "시스템등록아이디") private String sysRegId;
    @ApiModelProperty(value = "시스템변경일시") private String sysChgDttm;
    @ApiModelProperty(value = "시스템변경아이디") private String sysChgId;

    @ApiModelProperty(value = "매장명") private String storeNm;
    @ApiModelProperty(value = "권한타입코드") private String authTpCd;
    @ApiModelProperty(value = "정산타입") private String loanDvsn;
    @ApiModelProperty(value = "정산타입명") private String loanDvsnNm;
    @ApiModelProperty(value = "구매가능금액") private long ordUseAmt;
    @ApiModelProperty(value = "매장고유여신번호") private String loanMgntUnqNo;


    @ApiModelProperty(value = "구매요청 품목 정보")
    private List<PchReqInfoResponse.PchReqDtlInfo> pchReqDtlList;

    public static PchReqInfoResponse toDTO(PchReqBaseInfoEntity entity, UserMngEntity uEntity, LoanMgntBaseInfoEntity loanEntity, List<PchReqDtlPtclEntity>  dtlEntityList) {
        if (entity == null) return null;
        return PchReqInfoResponse.builder()
                .pchReqUnqNo(entity.getPchReqUnqNo())
                .brandId(entity.getBrandId() == null ? "" :entity.getBrandId())
                .brandNm(entity.getBrandNm() == null ? "" :entity.getBrandNm())
                .storeId(entity.getStoreId() == null ? "" :entity.getStoreId())
                .pchReqNm(entity.getPchReqNm() == null ? "" :entity.getPchReqNm())
                .pchReqDept(entity.getPchReqDept() == null ? "" :entity.getPchReqDept())
                .pchReqMgrNm(entity.getPchReqMgrNm() == null ? "" :entity.getPchReqMgrNm())
                .pchReqDt(entity.getPchReqDt() == null ? "" :entity.getPchReqDt())
                .pchReqStCd(entity.getPchReqStCd() == null ? "" :entity.getPchReqStCd())
                .pchReqStNm(entity.getPchReqStNm() == null ? "" :entity.getPchReqStNm())
                .pchReqDvsn(entity.getPchReqDvsn() == null ? "" :entity.getPchReqDvsn())
                .note(entity.getNote() == null ? "" :entity.getNote())
                .pchReqItemQty(entity.getPchReqItemQty() == null ? 0 :entity.getPchReqItemQty())
                .pchReqTotalAmt(entity.getPchReqTotalAmt() == null ? 0 :entity.getPchReqTotalAmt())
                .pchReqRjctDt(entity.getPchReqRjctDt() == null ? "" :entity.getPchReqRjctDt())
                .pchReqRjctNm(entity.getPchReqRjctNm() == null ? "" :entity.getPchReqRjctNm())
                .pchReqRjctRsn(entity.getPchReqRjctRsn() == null ? "" :entity.getPchReqRjctRsn() )
                .pchReqMgrId(entity.getPchReqMgrId() == null ? "" :entity.getPchReqMgrId())
                .pchReqRjctId(entity.getPchReqRjctId() == null ? "" :entity.getPchReqRjctId())
                .dlvReqDt(entity.getDlvReqDt() == null ? "" :entity.getDlvReqDt())
                .whsId(entity.getWhsId() == null ? "" :entity.getWhsId())
                .whsNm(entity.getWhsNm() == null ? "" :entity.getWhsNm())
                .location(entity.getLocation() == null ? "" :entity.getLocation())
                .locationNm(entity.getLocationNm() == null ? "" :entity.getLocationNm())
                .sysRegDttm(entity.getSysRegDttm() == null ? "" :entity.getSysRegDttm())
                .sysRegId(entity.getSysRegId() == null ? "" :entity.getSysRegId())
                .sysChgDttm(entity.getSysChgDttm() == null ? "" :entity.getSysChgDttm())
                .sysChgId(entity.getSysChgId() == null ? "" :entity.getSysChgId())
                .storeNm(uEntity.getStoreNm() == null ? "" :uEntity.getStoreNm())
                .authTpCd(uEntity.getAuthTpCd() == null ? "" : uEntity.getAuthTpCd())
                .ordUseAmt(loanEntity == null ? 0 : loanEntity.getOrdUseAmt())
                .loanDvsn(loanEntity == null ?  "" : loanEntity.getLoanDvsn() == null ? "" : loanEntity.getLoanDvsn())
                .loanDvsnNm(loanEntity == null ?  "" : loanEntity.getLoanDvsnNm() == null ? "" : loanEntity.getLoanDvsnNm())
                .loanMgntUnqNo(loanEntity == null ?  "" : loanEntity.getLoanMgntUnqNo() == null ? "" : loanEntity.getLoanMgntUnqNo())
                .pchApprAmt(entity.getPchApprAmt())
                .pchApprYn(entity.getPchApprYn())
                .pchApprNm(entity.getPchApprYn() == "Y" ? "선결재" : "미납")
                .pchReqDtlList(dtlEntityList.stream()
                        .map(m -> PchReqDtlInfo.builder()
                                .pchReqUnqNo(m.getPchReqUnqNo())
                                .pchReqDtlNo(m.getPchReqDtlNo())
                                .itemId(m.getItemId() == null ? "" :m.getItemId())
                                .itemNm(m.getItemNm() == null ? "" :m.getItemNm())
                                .unit(m.getUnit() == null ? "" :m.getUnit())
                                .unitNm(m.getUnitNm() == null ? "" :m.getUnitNm())
                                .pchReqQty(m.getPchReqQty() == null ? 0 :m.getPchReqQty())
                                .pchReqAmt(m.getPchReqAmt() == null ? 0 :m.getPchReqAmt())
                                .unitAmt(m.getUnitAmt() == null ? 0 :m.getUnitAmt())
                                .brandUnitUnqNo(m.getBrandUnitUnqNo() == null ? "" : m.getBrandUnitUnqNo())
                                .storeUnitUnqNo(m.getStoreUnitUnqNo() == null ? "" : m.getStoreUnitUnqNo())
                                .unitGubun(m.getUnitGubun() == null ? "" : m.getUnitGubun())
                                .podrDtlStCd(m.getPodrDtlStCd() == null ? "" : m.getPodrDtlStCd())
                                .podrDtlStNm(m.getPodrDtlStNm() == null ? "" : m.getPodrDtlStNm())
                                .build())
                        .collect(Collectors.toList()))
                .build();
    }

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

        @ApiModelProperty(value = "구매요청고유번호") private String pchReqUnqNo;
        @ApiModelProperty(value = "구매요청상세번호") private String pchReqDtlNo;
        @ApiModelProperty(value = "품목아이디") private String itemId;
        @ApiModelProperty(value = "품목명") private String itemNm;
        @ApiModelProperty(value = "단위") private String unit;
        @ApiModelProperty(value = "단위명") private String unitNm;
        @ApiModelProperty(value = "구매요청수량") private Integer pchReqQty;
        @ApiModelProperty(value = "구매요청금액") private Integer pchReqAmt;
        @ApiModelProperty(value = "단가금액") private Integer unitAmt;
        @ApiModelProperty(value = "브랜드단가고유번호") private String brandUnitUnqNo;
        @ApiModelProperty(value = "매장단가고유번호") private String storeUnitUnqNo;
        @ApiModelProperty(value = "단가구분") private String unitGubun;
        @ApiModelProperty(value = "발주상태코드") private String podrDtlStCd;
        @ApiModelProperty(value = "발주상태명") private String podrDtlStNm;


    }
}
