package cokr.xit.ens.modules.ktsigntalk.direct.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.ktsigntalk.direct.domain.TmpltMngKtSigntalk;
import cokr.xit.ens.modules.ktsigntalk.direct.model.TmpltMngKtSigntalkDTO;
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.ktsigntalk.direct.domain.QTmpltMngKtSigntalk.tmpltMngKtSigntalk;

@RequiredArgsConstructor
public class TmpltMngKtSigntalkRepositoryImpl implements TmpltMngKtSigntalkRepositoryCustom {

    private final JPAQueryFactory query;

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

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

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


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

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

                , tmpltMngKtSigntalk.sndTelNo
                , tmpltMngKtSigntalk.sendTel
                , tmpltMngKtSigntalk.ciTransUseYn
                , tmpltMngKtSigntalk.tmpltMsgUseYn
                , tmpltMngKtSigntalk.tmpltCsInfoUseYn
                , tmpltMngKtSigntalk.pryMessage
        );
    }

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

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

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

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

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


        return builder;
    }

}
