package com.oqpo.api.web.dto.response.stinfo.itemUnit;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.oqpo.api.entity.oper.FileEntity;
import com.oqpo.api.entity.stinfo.ItemEntity;
import com.oqpo.api.entity.stinfo.ItemUnitEntity;
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 ItemUnitInfoResponse {

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

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

    @ApiModelProperty(value = "브랜드명")
    private String brandNm;

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

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

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

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

    @ApiModelProperty(value = "계약시작일자")
    private String contSttDt;

    @ApiModelProperty(value = "계약종료일자")
    private String contEndDt;

    @ApiModelProperty(value = "통화")
    private String curr;

    @ApiModelProperty(value = "통화명")
    private String currNm;

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

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

    @ApiModelProperty(value = "단가금액")
    private Integer unitAmt;

    @ApiModelProperty(value = "리드타임")
    private Integer leadTime;

    @ApiModelProperty(value = "배송조건")
    private String dlvCndt;

    @ApiModelProperty(value = "배송조건명")
    private String dlvCndtNm;

    @ApiModelProperty(value = "단기계약서파일번호")
    private String shortContFileNo;

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

    @ApiModelProperty(value = "단가상태코드")
    private String unitStCd;

    @ApiModelProperty(value = "단가상태코드명")
    private String unitStNm;

    @ApiModelProperty(value = "기준수량")
    private Integer stddQty;

    @ApiModelProperty(value = "매장판매기준수량")
    private Integer storeSaleStddQty;

    @ApiModelProperty(value = "매장판매단가금액")
    private Integer storeSaleUnitAmt;

    @ApiModelProperty(value = "시스템등록일시")
    private String sysRegDttm;

    @ApiModelProperty(value = "시스템등록아이디")
    private String sysRegId;

    @ApiModelProperty(value = "시스템변경일시")
    private String sysChgDttm;

    @ApiModelProperty(value = "시스템변경아이디")
    private String sysChgId;


    @ApiModelProperty(value = "품목이지미 파일 정보")
    private List<ItemUnitInfoResponse.FileContInfo> imageFileNoList;

    public static ItemUnitInfoResponse toDTO(ItemUnitEntity entity, List<FileEntity>  fileImageInfoList) {
        if (entity == null) return null;
        return ItemUnitInfoResponse.builder()
                .brandUnitUnqNo(entity.getBrandUnitUnqNo())
                .brandId(entity.getBrandId())
                .brandNm(entity.getBrandNm())
                .itemId(entity.getItemId())
                .itemNm(entity.getItemNm())
                .spplyId(entity.getSpplyId())
                .spplyNm(entity.getSpplyNm())
                .contSttDt(entity.getContSttDt() == null ? "" :entity.getContSttDt())
                .contEndDt(entity.getContEndDt() == null ? "" :entity.getContEndDt())
                .curr(entity.getCurr() == null ? "" :entity.getCurr())
                .currNm(entity.getCurrNm() == null ? "" :entity.getCurrNm())
                .unit(entity.getUnit() == null ? "" :entity.getUnit())
                .unitNm(entity.getUnitNm() == null ? "" :entity.getUnitNm())
                .unitAmt(entity.getUnitAmt() == null ? 0 :entity.getUnitAmt())
                .leadTime(entity.getLeadTime() == null ? 0 :entity.getLeadTime())
                .dlvCndt(entity.getDlvCndt() == null ? "" :entity.getDlvCndt())
                .dlvCndtNm(entity.getDlvCndtNm() == null ? "" :entity.getDlvCndtNm())
                .shortContFileNo(entity.getShortContFileNo() == null ? "" :entity.getShortContFileNo())
                .imageFileNoList(fileImageInfoList.stream()
                        .map(m -> FileContInfo.builder()
                                .fileNo(m.getFileNo())
                                .fileSeq(m.getFileSeq())
                                .filePath(m.getFilePath())
                                .fileNm(m.getFileNm())
                                .fileOrgNm(m.getFileOrgNm())
                                .build())
                        .collect(Collectors.toList()))
                .spplyItemId(entity.getSpplyItemId() == null ? "" :entity.getSpplyItemId())
                .unitStCd(entity.getUnitStCd() == null ? "" :entity.getUnitStCd())
                .unitStNm(entity.getUnitStNm() == null ? "" :entity.getUnitStNm())
                .stddQty(entity.getStddQty() == null ? 0 :entity.getStddQty())
                .storeSaleStddQty(entity.getStoreSaleStddQty() == null ? 0 :entity.getStoreSaleStddQty())
                .storeSaleUnitAmt(entity.getStoreSaleUnitAmt() == null ? 0 :entity.getStoreSaleUnitAmt())
                .sysRegDttm(entity.getSysRegDttm())
                .sysRegId(entity.getSysRegId())
                .sysChgDttm(entity.getSysChgDttm())
                .sysChgId(entity.getSysChgId())
                .build();
    }

    @Getter
    @Builder
    @AllArgsConstructor(access = AccessLevel.PROTECTED)
    static class FileContInfo {
        @ApiModelProperty(value = "파일번호")
        private String fileNo;

        @ApiModelProperty(value = "파일순번")
        private Integer fileSeq;

        @ApiModelProperty(value = "파일경로")
        private String filePath;

        @ApiModelProperty(value = "파일명")
        private String fileNm;

        @ApiModelProperty(value = "원파일명")
        private String fileOrgNm;
    }
}
