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

import com.fasterxml.jackson.annotation.JsonInclude;
import com.oqpo.api.entity.oper.AfflEntity;
import com.oqpo.api.entity.oper.AfflSaleInfoEntity;
import com.oqpo.api.entity.oper.FileEntity;
import com.oqpo.api.entity.stinfo.SpplyEntity;
import com.oqpo.api.entity.stinfo.SpplyMgrEntity;
import com.oqpo.api.web.dto.response.stinfo.brand.BrandInfoResponse;
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 SpplyInfoResponse {

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

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

    @ApiModelProperty(value = "상태")
    private String stCd;

    @ApiModelProperty(value = "상태명")
    private String stNm;

    @ApiModelProperty(value = "기업형태")
    private String corpType;

    @ApiModelProperty(value = "기업형태명")
    private String corpTypeNm;

    @ApiModelProperty(value = "사업자번호")
    private String bsnsRegNo;

    @ApiModelProperty(value = "법인번호")
    private String corpRegNo;

    @ApiModelProperty(value = "대표자명")
    private String ownerNm;

    @ApiModelProperty(value = "업종")
    private String ctgrBsns;

    @ApiModelProperty(value = "업태")
    private String bsnsCndt;

    @ApiModelProperty(value = "기업구분코드")
    private String corpDvsn;

    @ApiModelProperty(value = "기업구분명")
    private String corpDvsnNm;

    @ApiModelProperty(value = "담당자명")
    private String mgrNm;

    @ApiModelProperty(value = "지역코드")
    private String areaCd;

    @ApiModelProperty(value = "지역명")
    private String areaNm;

    @ApiModelProperty(value = "부서명")
    private String deptNm;

    @ApiModelProperty(value = "이메일")
    private String mgrEmal;

    @ApiModelProperty(value = "전화번호")
    private String mgrTelNo;

    @ApiModelProperty(value = "담당자핸드폰번호")
    private String mgrMblNo;

    @ApiModelProperty(value = "삭제여부")
    private String delYn;

    @ApiModelProperty(value = "마지막로그인일자")
    private String lstLoginDttm;

    @ApiModelProperty(value = "우편번호")
    private String zipNo;

    @ApiModelProperty(value = "주소1")
    private String addr1;

    @ApiModelProperty(value = "주소2")
    private String addr2;

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

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

    @ApiModelProperty(value = "은행코드")
    private String bnkCd;

    @ApiModelProperty(value = "은행명")
    private String bnkNm;

    @ApiModelProperty(value = "계좌번호")
    private String acctNo;

    @ApiModelProperty(value = "예금주명")
    private String owacNm;

    @ApiModelProperty(value = "통장사본파일번호")
    private String bbookCopyFileNo;

    @ApiModelProperty(value = "사업자등록증파일번호")
    private String bsnsRegFileNo;

    @ApiModelProperty(value = "X좌표")
    private String latX;

    @ApiModelProperty(value = "y좌표")
    private String longY;

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

    @ApiModelProperty(value = "등록자")
    private String sysRegId;

    @ApiModelProperty(value = "수정일")
    private String sysChgDttm;

    @ApiModelProperty(value = "수정자")
    private String sysChgId;

    @ApiModelProperty(value = "통장사본파일 정보")
    private List<SpplyInfoResponse.FileAddInfo> fileBookCopyNoList;

    @ApiModelProperty(value = "사업자등록증파일  정보")
    private List<SpplyInfoResponse.FileAddInfo> fileBsnsRegNoList;

    @ApiModelProperty(value = "공급사 담당자 리스트")
    private List<SpprMgrInfo> spplyMgrList;

    public static SpplyInfoResponse toDTO(SpplyEntity entity, List<SpplyMgrEntity> spplyMgrList, List<FileEntity>  fileBookCopyInfoList, List<FileEntity>  fileBsnsRegNoInfoList) {
        if (entity == null) return null;
        return SpplyInfoResponse.builder()
                .spplyId(entity.getSpplyId())
                .spplyNm(entity.getSpplyNm())
                .stCd(entity.getStCd() == null ? "" :entity.getStCd() )
                .stNm(entity.getStNm() == null ? "" :entity.getStNm() )
                .corpType(entity.getCorpType() == null ? "" : entity.getCorpType())
                .corpTypeNm(entity.getCorpTypeNm() == null ? "" :entity.getCorpTypeNm() )
                .bsnsRegNo(entity.getBsnsRegNo() == null ? "" :entity.getBsnsRegNo() )
                .corpRegNo(entity.getCorpRegNo() == null ? "" :entity.getCorpRegNo() )
                .ownerNm(entity.getOwnerNm() == null ? "" : entity.getOwnerNm())
                .ctgrBsns(entity.getCtgrBsns() == null ? "" : entity.getCtgrBsns() )
                .bsnsCndt(entity.getBsnsCndt() == null ? "" : entity.getBsnsCndt())
                .corpDvsn(entity.getCorpDvsn() == null ? "" :entity.getCorpDvsn()  )
                .corpDvsnNm(entity.getCorpDvsnNm() == null ? "" : entity.getCorpDvsnNm())
                .mgrNm(entity.getMgrNm() == null ? "" : entity.getMgrNm() )
                .areaCd(entity.getAreaCd() == null ? "" :entity.getAreaCd() )
                .areaNm(entity.getAreaNm() == null ? "" :entity.getAreaNm() )
                .deptNm(entity.getDeptNm() == null ? "" :entity.getDeptNm()  )
                .mgrEmal(entity.getMgrEmal() == null ? "" : entity.getMgrEmal())
                .mgrTelNo(entity.getMgrTelNo() == null ? "" : entity.getMgrTelNo())
                .mgrMblNo(entity.getMgrMblNo() == null ? "" :entity.getMgrMblNo() )
                .delYn(entity.getDelYn() == null ? "" : entity.getDelYn())
                .lstLoginDttm(entity.getLstLoginDttm() == null ? "" : entity.getLstLoginDttm())
                .zipNo(entity.getZipNo() == null ? "" : entity.getZipNo())
                .addr1(entity.getAddr1() == null ? "" : entity.getAddr1())
                .addr2(entity.getAddr2() == null ? "" :entity.getAddr2() )
                .brandId(entity.getBrandId() == null ? "" :entity.getBrandId() )
                .brandNm(entity.getBrandNm() == null ? "" :entity.getBrandNm()  )
                .bnkCd(entity.getBnkCd() == null ? "" : entity.getBnkCd())
                .bnkNm(entity.getBnkNm() == null ? "" :entity.getBnkNm()  )
                .acctNo(entity.getAcctNo() == null ? "" : entity.getAcctNo() )
                .owacNm(entity.getOwacNm() == null ? "" :entity.getOwacNm() )
                .bbookCopyFileNo(entity.getBbookCopyFileNo() == null ? "" : entity.getBbookCopyFileNo())
                .fileBookCopyNoList(fileBookCopyInfoList.stream()
                        .map(m -> SpplyInfoResponse.FileAddInfo.builder()
                                .fileNo(m.getFileNo())
                                .fileSeq(m.getFileSeq())
                                .filePath(m.getFilePath())
                                .fileNm(m.getFileNm())
                                .fileOrgNm(m.getFileOrgNm())
                                .build())
                        .collect(Collectors.toList()))
                .bsnsRegFileNo(entity.getBsnsRegFileNo() == null ? "" : entity.getBsnsRegFileNo())
                .fileBsnsRegNoList(fileBsnsRegNoInfoList.stream()
                        .map(m -> SpplyInfoResponse.FileAddInfo.builder()
                                .fileNo(m.getFileNo())
                                .fileSeq(m.getFileSeq())
                                .filePath(m.getFilePath())
                                .fileNm(m.getFileNm())
                                .fileOrgNm(m.getFileOrgNm())
                                .build())
                        .collect(Collectors.toList()))
                .latX(entity.getLatX() == null ? "" :entity.getLatX() )
                .longY(entity.getLongY() == null ? "" : entity.getLongY() )
                .sysRegDttm(entity.getSysRegDttm() == null ? "" :entity.getSysRegDttm() )
                .sysRegId(entity.getSysRegId() == null ? "" : entity.getSysRegId())
                .sysChgDttm(entity.getSysChgDttm() == null ? "" :entity.getSysChgDttm() )
                .sysChgId(entity.getSysChgId() == null ? "" : entity.getSysChgId())
                .spplyMgrList(spplyMgrList.stream()
                        .map(m -> SpprMgrInfo.builder()
                                .spplyId(m.getSpplyId())
                                .spplyMgrId(m.getSpplyMgrId())
                                .rprstTelNo(m.getRprstTelNo() == null ? "" :m.getRprstTelNo())
                                .rprstEmal(m.getRprstEmal() == null ? "" :m.getRprstEmal())
                                .position(m.getPosition() == null ? "" :m.getPosition())
                                .assgnTask(m.getAssgnTask() == null ? "" :m.getAssgnTask())
                                .assgnTaskNm(m.getAssgnTaskNm() == null ? "" :m.getAssgnTaskNm())
                                .build())
                        .collect(Collectors.toList()))
                .build();
    }

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

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

        @ApiModelProperty(value = "영업담당자순번")
        private Integer spplyMgrId;

        @ApiModelProperty(value = "전화번호")
        private String rprstTelNo;

        @ApiModelProperty(value = "이메일")
        private String rprstEmal;

        @ApiModelProperty(value = "직위")
        private String position;

        @ApiModelProperty(value = "담당업무코드")
        private String assgnTask;

        @ApiModelProperty(value = "담당업무명")
        private String assgnTaskNm;

    }

    @Getter
    @Builder
    @AllArgsConstructor(access = AccessLevel.PROTECTED)
    static class FileAddInfo {
        @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;
    }
}
