package cokr.xit.ens.biz.traffic.chuncheon.mydoc.domain.repository;

import cokr.xit.ens.biz.traffic.chuncheon.mydoc.domain.ElctrnNticSndng;
import cokr.xit.ens.biz.traffic.chuncheon.mydoc.domain.ElctrnNticSndngDetail;
import com.querydsl.core.types.Projections;
import com.querydsl.core.types.dsl.Expressions;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;

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

import static cokr.xit.ens.biz.traffic.chuncheon.mydoc.domain.QElctrnNticSndng.elctrnNticSndng;
import static cokr.xit.ens.biz.traffic.chuncheon.mydoc.domain.QElctrnNticSndngDetail.elctrnNticSndngDetail;
import static cokr.xit.ens.biz.traffic.chuncheon.mydoc.domain.QNhtTmplatManage.nhtTmplatManage;
import static cokr.xit.ens.modules.kkomydoc.domain.QSendDetailKkoMydoc.sendDetailKkoMydoc;

@RequiredArgsConstructor
public class ElctrnNticSndngDetailRepositoryImpl implements ElctrnNticSndngDetailRepositoryCustom {

    private final JPAQueryFactory query;

    @Override
    public List<ElctrnNticSndngDetail> findAllDecByElctrnNticSndng(ElctrnNticSndng elctrnNticSndng) {

        return query.select(Projections.fields(ElctrnNticSndngDetail.class
                        , elctrnNticSndngDetail.elctrnNticSndngDetailId
                        , elctrnNticSndngDetail.elctrnNticSndng
                        , elctrnNticSndngDetail.signguCode
                        , elctrnNticSndngDetail.mainCode
                        , Expressions.stringTemplate("ECL_DECRYPT({0})", elctrnNticSndngDetail.ihidnum).as("ihidnum")
                        , elctrnNticSndngDetail.cnDetail
                        , elctrnNticSndngDetail.mobilePageCn
                        , elctrnNticSndngDetail.create
                        , elctrnNticSndngDetail.update
                        , elctrnNticSndngDetail.externalDocumentUuid))
                .from(elctrnNticSndngDetail)
                .where(elctrnNticSndngDetail.elctrnNticSndng.eq(elctrnNticSndng))
                .fetch();
    }


    @Override
    public List<ElctrnNticSndngDetail> findAllFetchByElctrnNticSndngId(String elctrnNticSndngId) {
        return query.selectFrom(elctrnNticSndngDetail)
                .innerJoin(elctrnNticSndngDetail.elctrnNticSndng, elctrnNticSndng)
                .fetchJoin()
                .innerJoin(elctrnNticSndng.nhtTmplatManage, nhtTmplatManage)
                .fetchJoin()
                .where(elctrnNticSndngDetail.elctrnNticSndng.elctrnNticSndngId.eq(elctrnNticSndngId))
                .fetch();

    }


    @Override
    public Optional<Long> findLastSendMastIdByElctrnNticSndngId(String elctrnNticSndngId) {
        return Optional.ofNullable(query.select(sendDetailKkoMydoc.sendMast.sendMastId)
                .from(sendDetailKkoMydoc)
//                .where(sendDetailKkoMydoc.propExternalDocumentUuid.in(findExternalDocumentUuidsByLnkInputId(elctrnNticSndngId)))
                .innerJoin(elctrnNticSndngDetail).on(sendDetailKkoMydoc.propExternalDocumentUuid.eq(elctrnNticSndngDetail.externalDocumentUuid))
                .where(elctrnNticSndngDetail.elctrnNticSndng.elctrnNticSndngId.eq(elctrnNticSndngId))
                .groupBy(sendDetailKkoMydoc.sendMast.sendMastId)
                .orderBy(sendDetailKkoMydoc.sendMast.sendMastId.desc())
                .fetchFirst());
    }

    private List<String> findExternalDocumentUuidsByLnkInputId(String elctrnNticSndngId) {
        return query.select(elctrnNticSndngDetail.externalDocumentUuid)
                .from(elctrnNticSndngDetail)
                .where(elctrnNticSndngDetail.elctrnNticSndng.elctrnNticSndngId.eq(elctrnNticSndngId))
                .fetch();
    }


    @Override
    public String findIhidnumDecByElctrnNticSndngDetailId(String elctrnNticSndngDetailId) {
        return query.select(Expressions.stringTemplate("ECL_DECRYPT({0})", elctrnNticSndngDetail.ihidnum).as("ihidnum"))
                .from(elctrnNticSndngDetail)
                .where(elctrnNticSndngDetail.elctrnNticSndngDetailId.eq(elctrnNticSndngDetailId))
                .fetchOne();
    }
}
