package cokr.xit.ens.modules.kkoalimtalk.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.kkoalimtalk.domain.TmpltMngKkoAlimtalk;
import cokr.xit.ens.modules.kkoalimtalk.model.TmpltMngKkoAlimtalkDTO;
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.kkoalimtalk.domain.QTmpltMngKkoAlimtalk.tmpltMngKkoAlimtalk;

@RequiredArgsConstructor
public class TmpltMngKkoAlimtalkRepositoryImpl implements TmpltMngKkoAlimtalkRepositoryCustom {

    private final JPAQueryFactory query;

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

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

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

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

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

                , tmpltMngKkoAlimtalk.biztalkOrgCd
                , tmpltMngKkoAlimtalk.biztalkTmpltCode
                , tmpltMngKkoAlimtalk.pryMessage
        );
    }

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

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

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

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

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


        return builder;
    }
}
