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

import com.icomsys.main_vm.biz.rcp.conversation.vo.ProjectSettingVO;
import com.icomsys.main_vm.biz.rcp.conversation.vo.SelectSimilarityCriteriaVO;
import com.icomsys.main_vm.biz.rcp.learningTest.vo.SelectProjectSettingInfoVO;
import com.icomsys.main_vm.db.jpa.entity.conversation.QTbProjectSetting;
import com.icomsys.main_vm.db.jpa.entity.system.QTbBotUser;
import com.icomsys.main_vm.db.jpa.entity.system.QTbIcsOprMngInfo;
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 TbProjectSettingRepoDslImpl implements TbProjectSettingRepoDsl{

    private final JPAQueryFactory queryFactory;
    private QTbProjectSetting qTbProjectSetting = QTbProjectSetting.tbProjectSetting;

    @Override
    public List<SelectSimilarityCriteriaVO> selectSimilarityCriteria(String serviceGroup) {
        var result = queryFactory.select(Projections.fields(
                SelectSimilarityCriteriaVO.class,
                        qTbProjectSetting.oprMngCode,
                qTbProjectSetting.similarityCriteria,
                qTbProjectSetting.classCriteria,
                qTbProjectSetting.classResultCriteria,
                qTbProjectSetting.fallbackMsg,
                qTbProjectSetting.greetingMsg
            )
        ).from(qTbProjectSetting)
        .where(qTbProjectSetting.oprMngCode.eq(serviceGroup))
        .fetch();

        return result;
    }

    public void updateServiceInfo(ProjectSettingVO vo){
        com.querydsl.jpa.impl.JPAUpdateClause query = null;

            query = queryFactory.update(qTbProjectSetting)
                    .set(qTbProjectSetting.similarityCriteria, vo.getSimilarityCriteria())
                    .set(qTbProjectSetting.classCriteria, vo.getClassCriteria())
                    .set(qTbProjectSetting.classResultCriteria, vo.getClassResultCriteria())
                    .set(qTbProjectSetting.fallbackMsg, vo.getFallbackMsg())
                    .set(qTbProjectSetting.greetingMsg, vo.getGreetingMsg())
                    .set(qTbProjectSetting.updateId, vo.getUpdateId())
                    .set(qTbProjectSetting.updateDate, LocalDateTime.now())
                    .where(qTbProjectSetting.oprMngCode.eq(vo.getOprMngCode()));
            query.execute();

    }

    @Override
    public SelectProjectSettingInfoVO selectProjectSettingInfo(String serviceGroup) {
        var result = queryFactory.select(Projections.fields(
                                SelectProjectSettingInfoVO.class,
                                qTbProjectSetting.oprMngCode,
                                qTbProjectSetting.similarityCriteria,
                                qTbProjectSetting.classCriteria,
                                qTbProjectSetting.classResultCriteria,
                                qTbProjectSetting.fallbackMsg,
                                qTbProjectSetting.greetingMsg,
                                qTbProjectSetting.ifMappingId,
                                qTbProjectSetting.registId,
                                qTbProjectSetting.registDate,
                                qTbProjectSetting.updateId,
                                qTbProjectSetting.updateDate
                        )
                ).from(qTbProjectSetting)
                .where(qTbProjectSetting.oprMngCode.eq(serviceGroup))
                .fetchOne();
        return result;
    }

}
