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

import com.google.gson.Gson;
import com.icomsys.main_vm.biz.common.system.vo.SystemCommoncodeReq;
import com.icomsys.main_vm.biz.common.system.vo.SystemCommoncodeRes;
import com.icomsys.main_vm.common.code.enumresource.RoleResource;
import com.icomsys.main_vm.db.jpa.entity.system.QTbBotCommonCode;
import com.icomsys.main_vm.db.jpa.entity.system.QTbIcsOprMngInfo;
import com.icomsys.main_vm.db.jpa.entity.system.QTbServiceGrouop;
import com.icomsys.main_vm.db.jpa.entity.system.QTbUserCust;
import com.querydsl.core.QueryResults;
import com.querydsl.core.types.Projections;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Aspect;

@Aspect
@Slf4j
@RequiredArgsConstructor
public class TbBotCommonCodeRepoDslImpl implements TbBotCommonCodeRepoDsl {
    private final JPAQueryFactory queryFactory;
    private QTbBotCommonCode qTbBotCommonCode = QTbBotCommonCode.tbBotCommonCode;
    private QTbServiceGrouop qTbServiceGrouop = QTbServiceGrouop.tbServiceGrouop;
    private QTbIcsOprMngInfo qTbIcsOprMngInfo = QTbIcsOprMngInfo.tbIcsOprMngInfo;
    private QTbUserCust qTbUserCust = QTbUserCust.tbUserCust;

    @Override
    public QueryResults<SystemCommoncodeRes> SystemCommoncodeList(SystemCommoncodeReq dto) {


        QueryResults<SystemCommoncodeRes> result = null;
        if (dto.getUserVo().getRoleVal().equals(RoleResource.SYSTEMADMIN.getName())) {
            return result = queryFactory.select(
                            Projections.fields(
                                    SystemCommoncodeRes.class,
                                    qTbServiceGrouop.custCode,
                                    qTbServiceGrouop.serviceGroupName,
                                    qTbIcsOprMngInfo.serviceGroup,
                                    qTbBotCommonCode.oprMngCode,
                                    qTbBotCommonCode.minorCode,
                                    qTbBotCommonCode.majorCode,
                                    qTbBotCommonCode.codeName,
                                    qTbBotCommonCode.attribute1,
                                    qTbBotCommonCode.attribute2,
                                    qTbBotCommonCode.attribute3
                            )
                    )
                    .from(qTbBotCommonCode)
                    .innerJoin(qTbIcsOprMngInfo).on(qTbBotCommonCode.oprMngCode.eq(qTbIcsOprMngInfo.oprMngCode))
                    .innerJoin(qTbServiceGrouop).on(qTbServiceGrouop.serviceGroup.eq(qTbIcsOprMngInfo.serviceGroup))
                    .where(
                             qTbServiceGrouopeq(dto)
                            , typeeq(dto)
                    )
                    .limit(dto.getSize())
                    .offset(dto.getPage() * dto.getSize())
                    .orderBy(qTbBotCommonCode.majorCode.asc(), qTbBotCommonCode.registDate.desc())
                    .fetchResults();
        }
        else {
            return result = queryFactory.select(
                            Projections.fields(
                                    SystemCommoncodeRes.class,
                                    qTbServiceGrouop.custCode,
                                    qTbServiceGrouop.serviceGroupName,
                                    qTbIcsOprMngInfo.serviceGroup,
                                    qTbBotCommonCode.oprMngCode,
                                    qTbBotCommonCode.minorCode,
                                    qTbBotCommonCode.majorCode,
                                    qTbBotCommonCode.codeName,
                                    qTbBotCommonCode.attribute1,
                                    qTbBotCommonCode.attribute2,
                                    qTbBotCommonCode.attribute3
                            )
                    )
                    .from(qTbServiceGrouop)
                    .innerJoin(qTbIcsOprMngInfo).on(qTbServiceGrouop.serviceGroup.eq(qTbIcsOprMngInfo.serviceGroup))
                    .innerJoin(qTbBotCommonCode).on(qTbIcsOprMngInfo.oprMngCode.eq(qTbBotCommonCode.oprMngCode))
                    .where(
                             qTbServiceGrouop.serviceGroupUpper.eq(dto.getUserVo().getLastUseServiceGroup())
                            , qTbServiceGrouopeq(dto)
                            , typeeq(dto)
                    )
                    .limit(dto.getSize())
                    .offset(dto.getPage() * dto.getSize())
                    .orderBy(qTbBotCommonCode.majorCode.asc(), qTbBotCommonCode.registDate.desc())
                    .fetchResults();
        }

    }

    private BooleanExpression qTbServiceGrouopeq(SystemCommoncodeReq dto) {
        return dto.getServiceGroup().equals("ALL") ?
                null : qTbServiceGrouop.serviceGroup.eq(dto.getServiceGroup());
    }

    private BooleanExpression typeeq(SystemCommoncodeReq dto) {
        log.info(" typeeq dto- {}", new Gson().toJson(dto));
        if (dto.getCodeText() == null || dto.getCodeText().equals("")) {
            return null;
        } else {
            if (dto.getCodeType().equals("ALL")) {
                return qTbBotCommonCode.minorCode.upper().contains(dto.getCodeText().toUpperCase()).or(qTbBotCommonCode.majorCode.upper().contains(dto.getCodeText().toUpperCase()));
            } else if (dto.getCodeType().equals("major")) {
                return qTbBotCommonCode.majorCode.upper().contains(dto.getCodeText().toUpperCase());
            } else if (dto.getCodeType().equals("minor")) {
                return qTbBotCommonCode.minorCode.upper().contains(dto.getCodeText().toUpperCase());
            } else {
                return null;
            }
        }

    }

    @Override
    public QueryResults<SystemCommoncodeRes> SystemCommoncodeCallNumList(SystemCommoncodeReq dto) {
        QueryResults<SystemCommoncodeRes> result = null;
        if (dto.getUserVo().getRoleVal().equals(RoleResource.SYSTEMADMIN.getName())) {
            return result = queryFactory.select(
                            Projections.fields(
                                    SystemCommoncodeRes.class,
                                    qTbBotCommonCode.codeName,
                                    qTbBotCommonCode.attribute1
                            )
                    )
                    .from(qTbBotCommonCode)
                    .innerJoin(qTbIcsOprMngInfo).on(qTbIcsOprMngInfo.serviceGroup.eq(dto.getServiceGroup()).and(qTbBotCommonCode.oprMngCode.eq(qTbIcsOprMngInfo.oprMngCode)))
                    .where(qTbBotCommonCode.majorCode.eq("CUST_CALL_NUM"))
                    .orderBy(qTbBotCommonCode.codeName.asc())
                    .fetchResults();
        } else {
            return result = queryFactory.select(
                            Projections.fields(
                                    SystemCommoncodeRes.class,
                                    qTbBotCommonCode.codeName,
                                    qTbBotCommonCode.attribute1
                            )
                    )
                    .from(qTbBotCommonCode)
                    .innerJoin(qTbIcsOprMngInfo).on(qTbIcsOprMngInfo.serviceGroup.eq(dto.getServiceGroup()).and(qTbBotCommonCode.oprMngCode.eq(qTbIcsOprMngInfo.oprMngCode)))
                    .where(qTbBotCommonCode.majorCode.eq("CUST_CALL_NUM").and(qTbBotCommonCode.oprMngCode.eq(dto.getOprmngCode())))
                    .orderBy(qTbBotCommonCode.codeName.asc())
                    .fetchResults();
        }
    }

    @Override
    public QueryResults<SystemCommoncodeRes> SystemCommoncodeProductList(SystemCommoncodeReq dto) {
        QueryResults<SystemCommoncodeRes> result = null;
        if (dto.getUserVo().getRoleVal().equals(RoleResource.SYSTEMADMIN.getName())) {
            return result = queryFactory.select(
                            Projections.fields(
                                    SystemCommoncodeRes.class,
                                    qTbBotCommonCode.minorCode,
                                    qTbBotCommonCode.codeName,
                                    qTbBotCommonCode.attribute1,
                                    qTbBotCommonCode.attribute2,
                                    qTbBotCommonCode.attribute3
                            )
                    )
                    .from(qTbBotCommonCode)
                    .innerJoin(qTbIcsOprMngInfo).on(qTbIcsOprMngInfo.serviceGroup.eq(dto.getServiceGroup()).and(qTbBotCommonCode.oprMngCode.eq(qTbIcsOprMngInfo.oprMngCode)))
                    .where(qTbBotCommonCode.majorCode.eq(dto.getCodeType()))
                    .orderBy(qTbBotCommonCode.codeName.asc())
                    .fetchResults();
        } else {
            return result = queryFactory.select(
                            Projections.fields(
                                    SystemCommoncodeRes.class,
                                    qTbBotCommonCode.minorCode,
                                    qTbBotCommonCode.codeName,
                                    qTbBotCommonCode.attribute1,
                                    qTbBotCommonCode.attribute2,
                                    qTbBotCommonCode.attribute3
                            )
                    )
                    .from(qTbBotCommonCode)
                    .innerJoin(qTbIcsOprMngInfo).on(qTbIcsOprMngInfo.serviceGroup.eq(dto.getServiceGroup()).and(qTbBotCommonCode.oprMngCode.eq(qTbIcsOprMngInfo.oprMngCode)))
                    .where(qTbBotCommonCode.majorCode.eq(dto.getCodeType()).and(qTbBotCommonCode.oprMngCode.eq(dto.getOprmngCode())))
                    .orderBy(qTbBotCommonCode.codeName.asc())
                    .fetchResults();
        }
    }

}
