package cokr.xit.ens.modules.nvsigntalk.domain.repository;

import cokr.xit.ens.core.utils.CmmnUtil;
import cokr.xit.ens.modules.common.ctgy.sys.mng.model.TmpltMngSearchDTO;
import cokr.xit.ens.modules.nvsigntalk.domain.TmpltMngNvSigntalk;
import cokr.xit.ens.modules.nvsigntalk.model.TmpltMngNvSigntalkDTO;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Projections;
import com.querydsl.core.types.QBean;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;

import java.util.List;
import java.util.Optional;

import static cokr.xit.ens.modules.common.ctgy.sys.mng.domain.QOrgMng.orgMng;
import static cokr.xit.ens.modules.nvsigntalk.domain.QTmpltMngNvSigntalk.tmpltMngNvSigntalk;

@RequiredArgsConstructor
public class TmpltMngNvSigntalkRepositoryImpl implements TmpltMngNvSigntalkRepositoryCustom {

    private final JPAQueryFactory query;


    @Override
    public Optional<TmpltMngNvSigntalk> findFetchByOrgCdAndTmpltCd(String orgCd, String tmpltCd) {
        return Optional.ofNullable(query.selectFrom(tmpltMngNvSigntalk)
                .where(tmpltMngNvSigntalk.orgMng.orgCd.eq(orgCd)
                        .and(tmpltMngNvSigntalk.tmpltCd.eq(tmpltCd)))
                .fetchOne());
    }

    @Override
    public List<TmpltMngNvSigntalk> findAllFetchBySearchDTO(TmpltMngSearchDTO searchDTO) {
        return query.selectFrom(tmpltMngNvSigntalk)
                .innerJoin(tmpltMngNvSigntalk.orgMng, orgMng)
                .fetchJoin()
                .where(eqSearchDTO(searchDTO))
                .fetch();
    }

    @Override
    public Optional<TmpltMngNvSigntalkDTO> findDtoByOrgCdAndTmpltCd(String orgCd, String tmpltCd) {
        return Optional.ofNullable(query.select(mappedDto())
                .from(tmpltMngNvSigntalk)
                .where(tmpltMngNvSigntalk.orgMng.orgCd.eq(orgCd)
                        .and(tmpltMngNvSigntalk.tmpltCd.eq(tmpltCd)))
                .fetchOne());
    }

    @Override
    public List<TmpltMngNvSigntalkDTO> findAllDtoBySearchDTO(TmpltMngSearchDTO searchDTO) {
        return query.select(mappedDto())
                .from(tmpltMngNvSigntalk)
                .innerJoin(tmpltMngNvSigntalk.orgMng, orgMng)
                .where(eqSearchDTO(searchDTO))
                .fetch();
    }

    private QBean<TmpltMngNvSigntalkDTO> mappedDto() {
        return Projections.fields(TmpltMngNvSigntalkDTO.class
                , tmpltMngNvSigntalk.dtype
                , tmpltMngNvSigntalk.orgMng.orgCd
                , tmpltMngNvSigntalk.orgMng.orgNm
                , tmpltMngNvSigntalk.tmpltCd
                , tmpltMngNvSigntalk.title
                , tmpltMngNvSigntalk.message
                , tmpltMngNvSigntalk.useYn

                , tmpltMngNvSigntalk.callCenterNo
                , tmpltMngNvSigntalk.ciTransUseYn
                , tmpltMngNvSigntalk.tmpltMsgUseYn
                , tmpltMngNvSigntalk.tmpltCsInfoUseYn
                , tmpltMngNvSigntalk.messageDataFmt
        );
    }

    @Override
    public Optional<TmpltMngNvSigntalk> findFetchByOrgCdAndTmpltCdAndUseYn(String orgCd, String tmpltCd, String useYn) {
        return Optional.ofNullable(
                query.selectFrom(tmpltMngNvSigntalk)
                        .innerJoin(tmpltMngNvSigntalk.orgMng, orgMng)
                        .fetchJoin()
                        .where(
                                tmpltMngNvSigntalk.orgMng.orgCd.eq(orgCd)
                                        .and(tmpltMngNvSigntalk.tmpltCd.eq(tmpltCd))
                                        .and(tmpltMngNvSigntalk.useYn.eq(useYn))
                        )
                        .fetchOne());
    }

    private BooleanBuilder eqSearchDTO(TmpltMngSearchDTO searchDTO) {
        BooleanBuilder builder = new BooleanBuilder();

        if (!CmmnUtil.isEmpty(searchDTO.getSchOrgCd()))
            builder.and(tmpltMngNvSigntalk.orgMng.orgCd.eq(searchDTO.getSchOrgCd()));

        if (!CmmnUtil.isEmpty(searchDTO.getSchTmpltCd()))
            builder.and(tmpltMngNvSigntalk.tmpltCd.eq(searchDTO.getSchTmpltCd()));

        if (!CmmnUtil.isEmpty(searchDTO.getSchTitle()))
            builder.and(tmpltMngNvSigntalk.title.like(searchDTO.getSchTitle() + "%"));


        return builder;
    }
}
