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

import com.google.gson.Gson;
import com.icomsys.main_vm.biz.common.system.vo.SystemBotUserRes;
import com.icomsys.main_vm.biz.common.system.vo.SystemPolicyMenuReq;
import com.icomsys.main_vm.biz.common.system.vo.SystemPolicyMenuRes;
import com.icomsys.main_vm.common.code.enumresource.RoleResource;
import com.icomsys.main_vm.db.jpa.entity.system.QTbCustPolicy;
import com.icomsys.main_vm.db.jpa.entity.system.QTbIcsCustInfo;
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.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Aspect;

import java.util.List;

@Aspect
@Slf4j
@RequiredArgsConstructor
public class TbCustPolicyRepoDslImpl implements TbCustPolicyRepoDsl {

    private final JPAQueryFactory queryFactory;
    private QTbCustPolicy qTbCustPolicy = QTbCustPolicy.tbCustPolicy;
    private QTbIcsCustInfo qTbIcsCustInfo = QTbIcsCustInfo.tbIcsCustInfo;
    private QTbServiceGrouop qTbServiceGrouop = QTbServiceGrouop.tbServiceGrouop;
    private QTbUserCust qTbUserCust = QTbUserCust.tbUserCust;

    @Override
    public QueryResults<SystemPolicyMenuRes> PolicyMenuListQueryResult(SystemPolicyMenuReq dto) {
        log.info("PolicyMenuList - {}", new Gson().toJson(dto));
        QueryResults<SystemPolicyMenuRes> result = null;

        if (dto.getUserVo().getRoleVal().equals(RoleResource.SYSTEMADMIN.getName())) {
            result = queryFactory.select(
                            Projections.fields(
                                    SystemPolicyMenuRes.class,
                                    qTbCustPolicy.custCode,
                                    qTbIcsCustInfo.custName,
                                    qTbCustPolicy.serviceGroup,
                                    qTbServiceGrouop.serviceGroupName,
                                    qTbCustPolicy.policyVal
                            )
                    )
                    .from(qTbCustPolicy)
                    .innerJoin(qTbIcsCustInfo).on(qTbCustPolicy.custCode.eq(qTbIcsCustInfo.custCode))
                    .innerJoin(qTbServiceGrouop).on(qTbCustPolicy.serviceGroup.eq(qTbServiceGrouop.serviceGroup))
                    .orderBy(qTbCustPolicy.policyVal.asc())
                    .limit(dto.getSize())
                    .offset(dto.getSize() * dto.getPage())
                    .fetchResults();
            return result;
        } else {
            log.info("dsl init -{} ", new Gson().toJson(dto) );
            result = queryFactory.select(Projections.fields(
                                    SystemPolicyMenuRes.class,
                                    qTbCustPolicy.custCode,
                                    qTbIcsCustInfo.custName,
                                    qTbCustPolicy.serviceGroup,
                                    qTbServiceGrouop.serviceGroupName,
                                    qTbCustPolicy.policyVal
                            )
                    )
                    .from(qTbUserCust)
                    .innerJoin(qTbServiceGrouop).on(qTbUserCust.serviceGroup.eq(qTbServiceGrouop.serviceGroup))
                    .innerJoin(qTbIcsCustInfo).on(qTbUserCust.custCode.eq(qTbIcsCustInfo.custCode))
                    .innerJoin(qTbCustPolicy).on(qTbServiceGrouop.serviceGroup.eq(qTbCustPolicy.serviceGroup))
                    .where(qTbUserCust.userSeq.eq(dto.getUserVo().getUserSeq())
                            , qTbUserCust.serviceGroup.eq(qTbServiceGrouop.serviceGroup)
                            , qTbServiceGrouop.serviceGroupUpper.eq(dto.getUserVo().getLastUseServiceGroup())
                    )
                    .limit(dto.getSize())
                    .offset(dto.getSize() * dto.getPage())
                    .orderBy(qTbCustPolicy.policyVal.asc())
                    .groupBy(
                            qTbCustPolicy.custCode,
                            qTbIcsCustInfo.custName,
                            qTbCustPolicy.serviceGroup,
                            qTbServiceGrouop.serviceGroupName,
                            qTbCustPolicy.policyVal)
                    .fetchResults();
            return result;
        }

    }

    @Override
    public List<SystemPolicyMenuRes> PolicyMenuListQueryList(SystemPolicyMenuReq dto) {
        log.info("PolicyMenuList - {}", new Gson().toJson(dto));
        List<SystemPolicyMenuRes> result = null;

        if (dto.getUserVo().getRoleVal().equals(RoleResource.SYSTEMADMIN.getName())) {
            result = queryFactory.select(
                            Projections.fields(
                                    SystemPolicyMenuRes.class,
                                    qTbCustPolicy.custCode,
                                    qTbIcsCustInfo.custName,
                                    qTbCustPolicy.serviceGroup,
                                    qTbServiceGrouop.serviceGroupName,
                                    qTbCustPolicy.policyVal
                            )
                    )
                    .from(qTbCustPolicy)
                    .innerJoin(qTbIcsCustInfo).on(qTbCustPolicy.custCode.eq(qTbIcsCustInfo.custCode))
                    .innerJoin(qTbServiceGrouop).on(qTbCustPolicy.serviceGroup.eq(qTbServiceGrouop.serviceGroup))
                    .orderBy(qTbCustPolicy.policyVal.asc())
                    .fetch();
            return result;
        } else {
            result = queryFactory.select(Projections.fields(
                                    SystemPolicyMenuRes.class,
                                    qTbCustPolicy.custCode,
                                    qTbIcsCustInfo.custName,
                                    qTbCustPolicy.serviceGroup,
                                    qTbServiceGrouop.serviceGroupName,
                                    qTbCustPolicy.policyVal
                            )
                    )
                    .from(qTbUserCust)
                    .innerJoin(qTbServiceGrouop).on(qTbUserCust.serviceGroup.eq(qTbServiceGrouop.serviceGroup))
                    .innerJoin(qTbIcsCustInfo).on(qTbUserCust.custCode.eq(qTbIcsCustInfo.custCode))
                    .innerJoin(qTbCustPolicy).on(qTbServiceGrouop.serviceGroup.eq(qTbCustPolicy.serviceGroup))
                    .where(qTbUserCust.userSeq.eq(dto.getUserVo().getUserSeq())
                            , qTbUserCust.serviceGroup.eq(qTbServiceGrouop.serviceGroup)
                            , qTbServiceGrouop.serviceGroupUpper.eq(dto.getUserVo().getLastUseServiceGroup())
                    )
                    .orderBy(qTbCustPolicy.policyVal.asc())
                    .groupBy(
                            qTbCustPolicy.custCode,
                            qTbIcsCustInfo.custName,
                            qTbCustPolicy.serviceGroup,
                            qTbServiceGrouop.serviceGroupName,
                            qTbCustPolicy.policyVal)
                    .fetch();
            return result;
        }
    }


}
