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

import com.google.gson.Gson;
import com.icomsys.main_vm.biz.common.login.service.LoginService;
import com.icomsys.main_vm.biz.rcp.conversation.vo.LongtextManagerVO;
import com.icomsys.main_vm.db.jpa.entity.scenario.QTbApiNode;
import com.icomsys.main_vm.db.jpa.entity.scenario.QTbLongtextManager;
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.util.HashMap;
import java.util.List;

/**
 * Name : TbLongTextManagerDslImpl 
 * date : 2023-02-09 
 * author :  Mong
 * description :
 * ===========================================================
 * DATE                 AUTHOR                NOTE
 * -----------------------------------------------------------
 * 2023-02-09                Mong             최초 생성
 */
@Aspect
@Slf4j
@RequiredArgsConstructor
public class TbLongTextManagerDslImpl implements TbLongTextManagerDsl{

    private final JPAQueryFactory queryFactory;

    private QTbLongtextManager qTbLongtextManager = QTbLongtextManager.tbLongtextManager;

    private QTbApiNode qTbApiNode = QTbApiNode.tbApiNode;

    @Override
    public void updateApiLongTextManager(LongtextManagerVO vo) {
        var query = queryFactory.update(qTbLongtextManager)
                .set(qTbLongtextManager.textContents, vo.getTextContents())
                .set(qTbLongtextManager.checksum, vo.getChecksum())
                .where(qTbLongtextManager.rfSeq.eq(vo.getRfSeq())
                   .and(qTbLongtextManager.tableCode.eq(vo.getTableCode())
                   .and(qTbLongtextManager.columnCode.eq(vo.getColumnCode())))
                );
        query.execute();
    }

    @Override
    public List<LongtextManagerVO> selectLongTextManager(LongtextManagerVO vo) {
        var query = queryFactory.select(
                Projections.fields(
                        LongtextManagerVO.class,
                        qTbLongtextManager.textContents
                )
        ).from(qTbLongtextManager)
                .leftJoin(qTbLongtextManager).on(qTbApiNode.seq.eq(qTbLongtextManager.rfSeq))
        .where(qTbLongtextManager.rfSeq.eq(vo.getRfSeq())
           .and(qTbLongtextManager.columnCode.eq(vo.getColumnCode()))
           .and(qTbLongtextManager.tableCode.eq(vo.getTableCode()))
        ).fetch();

        return query;

    }

}
