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

import com.icomsys.main_vm.biz.common.system.vo.SystemServiceAuthGroupReq;
import com.icomsys.main_vm.biz.common.system.vo.SystemServiceAuthGroupRes;
import com.icomsys.main_vm.common.code.enumresource.RoleResource;
import com.icomsys.main_vm.db.jpa.entity.system.QTbIcsCustInfo;
import com.icomsys.main_vm.db.jpa.entity.system.QTbServiceAuthGroup;
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 TbServiceAuthGroupRepoDslImpl implements TbServiceAuthGroupRepoDsl {

    private final JPAQueryFactory queryFactory;
    private QTbServiceAuthGroup qTbServiceAuthGroup = QTbServiceAuthGroup.tbServiceAuthGroup;
    private QTbServiceGrouop qTbServiceGrouop = QTbServiceGrouop.tbServiceGrouop;
    private QTbIcsCustInfo qTbIcsCustInfo = QTbIcsCustInfo.tbIcsCustInfo;
    private QTbUserCust qTbUserCust = QTbUserCust.tbUserCust;

    @Override
    public QueryResults<SystemServiceAuthGroupRes> ServiceAuthGroupListView(SystemServiceAuthGroupReq dto) {
//
//        QueryResults<SystemServiceAuthGroupRes> result = null;

        if (dto.getUserVo().getRoleVal().equals(RoleResource.SYSTEMADMIN.getName())) {
            return queryFactory.select(
                            Projections.fields(
                                    SystemServiceAuthGroupRes.class,
                                    qTbServiceGrouop.serviceGroup,
                                    qTbServiceGrouop.serviceGroupName,
                                    qTbServiceAuthGroup.authGroupSeq,
                                    qTbServiceAuthGroup.policyGroupName,
                                    qTbServiceAuthGroup.policyGroupDesc,
                                    qTbServiceGrouop.custCode,
                                    qTbServiceAuthGroup.registDate
                            )
                    )
//                .from(qTbUserCust)
//                //내가사용할 수 있는 UserCust의 서비스그룹
//                .innerJoin(qTbServiceGrouop).on(qTbUserCust.serviceGroup.eq(qTbServiceGrouop.serviceGroup))
//                .innerJoin(qTbServiceAuthGroup).on(qTbServiceGrouop.serviceGroup.eq(qTbServiceAuthGroup.serviceGroup))
                    .from(qTbServiceGrouop)
                    .innerJoin(qTbServiceAuthGroup).on(qTbServiceGrouop.serviceGroup.eq(qTbServiceAuthGroup.serviceGroup))
                    .where(
//                        qTbUserCust.userSeq.eq(dto.getUserVo().getUserSeq())
//                        현재 사용중인 ServiceTypeM = lastUseServiceGroup로
//                            qTbServiceGrouop.serviceGroupUpper.eq(dto.getUserVo().getLastUseServiceGroup())
                             qTbServiceGrouopeq(dto)
                            , qTbServiceAuthGroupNameeq(dto)
                    )
                    .limit(dto.getSize())
                    .offset(dto.getPage() * dto.getSize())
                    .orderBy(qTbServiceAuthGroup.registDate.desc())
                    .groupBy(qTbServiceGrouop.serviceGroup,
                            qTbServiceGrouop.serviceGroupName,
                            qTbServiceAuthGroup.authGroupSeq,
                            qTbServiceAuthGroup.policyGroupName,
                            qTbServiceAuthGroup.policyGroupDesc,
                            qTbServiceGrouop.custCode,
                            qTbServiceAuthGroup.registDate)
                    .fetchResults();
        } else {
            return queryFactory.select(
                            Projections.fields(
                                    SystemServiceAuthGroupRes.class,
                                    qTbServiceGrouop.serviceGroup,
                                    qTbServiceGrouop.serviceGroupName,
                                    qTbServiceAuthGroup.authGroupSeq,
                                    qTbServiceAuthGroup.policyGroupName,
                                    qTbServiceAuthGroup.policyGroupDesc,
                                    qTbServiceGrouop.custCode,
                                    qTbServiceAuthGroup.registDate
                            )
                    )
//                .from(qTbUserCust)
//                //내가사용할 수 있는 UserCust의 서비스그룹
//                .innerJoin(qTbServiceGrouop).on(qTbUserCust.serviceGroup.eq(qTbServiceGrouop.serviceGroup))
//                .innerJoin(qTbServiceAuthGroup).on(qTbServiceGrouop.serviceGroup.eq(qTbServiceAuthGroup.serviceGroup))
                    .from(qTbServiceGrouop)
                    .innerJoin(qTbServiceAuthGroup).on(qTbServiceGrouop.serviceGroup.eq(qTbServiceAuthGroup.serviceGroup))
                    .where(
//                        qTbUserCust.userSeq.eq(dto.getUserVo().getUserSeq())
//                        현재 사용중인 ServiceTypeM = lastUseServiceGroup로
                            qTbServiceGrouop.serviceGroupUpper.eq(dto.getUserVo().getLastUseServiceGroup())
                            , qTbServiceGrouopeq(dto)
                            , qTbServiceAuthGroupNameeq(dto)
//                            , qTbServiceGrouop.serviceType.eq("S")
                    )
                    .limit(dto.getSize())
                    .offset(dto.getPage() * dto.getSize())
                    .orderBy(qTbServiceAuthGroup.registDate.desc())
                    .groupBy(qTbServiceGrouop.serviceGroup,
                            qTbServiceGrouop.serviceGroupName,
                            qTbServiceAuthGroup.authGroupSeq,
                            qTbServiceAuthGroup.policyGroupName,
                            qTbServiceAuthGroup.policyGroupDesc,
                            qTbServiceGrouop.custCode,
                            qTbServiceAuthGroup.registDate)
                    .fetchResults();
        }

//        return result;
    }

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

    private BooleanExpression qTbServiceAuthGroupNameeq(SystemServiceAuthGroupReq dto) {
        if (dto.getCodeText() == null || dto.getCodeText().equals("")) {
            return null;
        } else {
            return qTbServiceAuthGroup.policyGroupName.upper().contains(dto.getCodeText().toUpperCase());
        }
    }

}
