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

import static cokr.xit.ens.modules.common.ctgy.sys.mng.domain.QOrgMng.*;
import static cokr.xit.ens.modules.kkotalk.domain.QTmpltMngKkoTalk.*;

import java.util.*;

import com.querydsl.core.*;
import com.querydsl.core.types.*;
import com.querydsl.jpa.impl.*;

import cokr.xit.ens.core.utils.*;
import cokr.xit.ens.modules.common.ctgy.sys.mng.model.*;
import cokr.xit.ens.modules.kkotalk.domain.*;
import cokr.xit.ens.modules.kkotalk.model.*;
import lombok.*;

@RequiredArgsConstructor
public class TmpltMngKkoTalkRepositoryImpl implements TmpltMngKkoTalkRepositoryCustom {

    private final JPAQueryFactory query;

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

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

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


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

    private QBean<TmpltMngKkoTalkDTO> mappedDto() {
        return Projections.fields(TmpltMngKkoTalkDTO.class
                , tmpltMngKkoTalk.dtype
                , tmpltMngKkoTalk.orgMng.orgCd
                , tmpltMngKkoTalk.orgMng.orgNm
                , tmpltMngKkoTalk.tmpltCd
                , tmpltMngKkoTalk.title
                , tmpltMngKkoTalk.message
                , tmpltMngKkoTalk.useYn
                , tmpltMngKkoTalk.csNumber
                , tmpltMngKkoTalk.csName
                , tmpltMngKkoTalk.ciTransUseYn
                , tmpltMngKkoTalk.tmpltMsgUseYn
                , tmpltMngKkoTalk.tmpltCsInfoUseYn
                , tmpltMngKkoTalk.pryMessage
        );
    }

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

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

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

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

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


        return builder;
    }

}
