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

import com.google.gson.Gson;
import com.icomsys.main_vm.biz.rcp.conversation.vo.SynonymWordListVo;
import com.icomsys.main_vm.db.jpa.entity.conversation.QTbSynonymMaster;
import com.icomsys.main_vm.db.jpa.entity.conversation.QTbSynonymWord;
import com.icomsys.main_vm.db.jpa.entity.conversation.TbSynonymWord;
import com.icomsys.main_vm.db.mybatis.alias.LoginVO;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.JPAExpressions;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Aspect;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

@Aspect
@Slf4j
@RequiredArgsConstructor
public class TbSynonymWordRepoDslImpl implements TbSynonymWordRepoDsl {

    private final JPAQueryFactory queryFactory;
    private QTbSynonymMaster qTbSynonymMaster = QTbSynonymMaster.tbSynonymMaster;
    private QTbSynonymWord qTbSynonymWord = QTbSynonymWord.tbSynonymWord;
    @Override
    public List<SynonymWordListVo> insertSynonymWord(HashMap eMap) {
        var query = queryFactory.select(
                        Projections.fields(SynonymWordListVo.class,
                                qTbSynonymMaster.seq))
                .from(qTbSynonymMaster)
                .where(qTbSynonymMaster.idivWord.eq(eMap.get("idivWord").toString())
                        .and(qTbSynonymMaster.oprMngCode.eq(eMap.get("serviceCode").toString())))
                .fetch();
        return query;
    }


    @Override
    public void updateSynonymWord(HashMap eMap) {
//        var query = queryFactory.select(
//                        Projections.fields(SynonymWordListVo.class,
//                                qTbSynonymMaster.seq))
//                .from(qTbSynonymMaster)
//                .where(qTbSynonymMaster.idivWord.eq(eMap.get("idivWord").toString())
//                        .and(qTbSynonymMaster.oprMngCode.eq(eMap.get("serviceCode").toString()))
//                        .and(qTbSynonymMaster.useYn.eq("Y")))
//                .fetch();
//        return query;
        try {
            var query = queryFactory.update(qTbSynonymWord)
                    .set(qTbSynonymWord.masterSeq, (
                            ExpressionUtils.as(JPAExpressions
                                    .select(qTbSynonymMaster.seq).from(qTbSynonymMaster)
                                    .where(qTbSynonymMaster.idivWord.eq(eMap.get("idivWord").toString())
                                            .and(qTbSynonymMaster.oprMngCode.eq(eMap.get("serviceCode").toString())
                                                    .and(qTbSynonymMaster.useYn.eq("Y")))), "seq")))
                    .set(qTbSynonymWord.useYn, "Y")
                    .set(qTbSynonymWord.updateId, eMap.get("userId").toString())
                    .set(qTbSynonymWord.updateDate, LocalDateTime.now())
                    .where(qTbSynonymWord.synonymWord.eq(eMap.get("synonymWord").toString())
                            .and(qTbSynonymWord.oprMngCode.eq(eMap.get("serviceCode").toString())))
                    .execute();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void deleteSynonymWord(HashMap eMap) {
            var query = queryFactory.update(qTbSynonymWord)
                    .set(qTbSynonymWord.useYn, "N")
                    .where(qTbSynonymWord.masterSeq.eq(Long.valueOf(String.valueOf(eMap.get("seq")))))
                    .execute();
    }
}
