package com.icomsys.main_vm.db.jpa.repo.oprManage.dsl;

import com.icomsys.main_vm.biz.common.system.vo.SystemBotTtsTagRes;
import com.icomsys.main_vm.biz.common.system.vo.SystemBotTtsTagVo;
import com.icomsys.main_vm.biz.common.system.vo.SystemCommoncodeRes;
import com.icomsys.main_vm.biz.rcp.conversation.vo.ProjectSettingVO;
import com.icomsys.main_vm.db.jpa.entity.conversation.TbIcsLog;
import com.icomsys.main_vm.db.jpa.entity.oprManage.QTbBotTtsTag;
import com.icomsys.main_vm.db.jpa.entity.system.QTbBotCommonCode;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Aspect;

import java.time.LocalDateTime;
import java.util.List;

@Aspect
@Slf4j
@RequiredArgsConstructor
public class TbBotTtsTagRepoDslImpl implements TbBotTtsTagRepoDsl {

    private final JPAQueryFactory queryFactory;
    private QTbBotCommonCode qTbBotCommonCode = QTbBotCommonCode.tbBotCommonCode;
    private QTbBotTtsTag qTbBotTtsTag = QTbBotTtsTag.tbBotTtsTag;

    @Override
    public List<SystemCommoncodeRes> selectTtsProdCodeList (String majorCode) {
        List<SystemCommoncodeRes> result = null;
        result = queryFactory.select(
                Projections.fields(
                        SystemCommoncodeRes.class,
                        qTbBotCommonCode.minorCode,
                        qTbBotCommonCode.codeName
                )
        )
                .from(qTbBotCommonCode)
                .where(qTbBotCommonCode.majorCode.eq(majorCode))
                .orderBy(qTbBotCommonCode.updateDate.desc())
                .fetch();

        return result;
    }
    @Override
    public List<SystemBotTtsTagRes> selectTtsTagList (String custCode, String majorCode, String prodCode) {
        List<SystemBotTtsTagRes> result = null;
        result = queryFactory.select(
                        Projections.fields(
                                SystemBotTtsTagRes.class,
                                qTbBotCommonCode.codeName,
                                qTbBotCommonCode.attribute1,
                                qTbBotCommonCode.attribute2,
                                qTbBotTtsTag.ttsStartTag,
                                qTbBotTtsTag.ttsEndTag,
                                qTbBotTtsTag.updateDate
                        )
                )
                .from(qTbBotCommonCode)
                .leftJoin(qTbBotTtsTag)
                .on(qTbBotCommonCode.minorCode.eq(qTbBotTtsTag.tagCode).and(qTbBotTtsTag.ttsProdCode.eq(prodCode)))
                .where(qTbBotCommonCode.custCode.eq(custCode).and(qTbBotCommonCode.majorCode.eq(majorCode)))
                .orderBy(qTbBotCommonCode.updateDate.desc())
                .fetch();

        return result;
    }

    @Override
    public SystemBotTtsTagVo existsById(SystemBotTtsTagVo vo) {
        return queryFactory.select(
                Projections.fields(SystemBotTtsTagVo.class,
                        qTbBotTtsTag.registId,
                        qTbBotTtsTag.registDate
                        )
                )
                .from(qTbBotTtsTag)
                .where(qTbBotTtsTag.ttsProdCode.eq(vo.getTtsProdCode())
                .and(qTbBotTtsTag.tagCode.eq(vo.getTagCode())))
                .fetchFirst();
    }

}
