package com.zt.plat.module.qms.core.aspect;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.security.core.LoginUser;
import com.zt.plat.module.qms.core.aspect.annotation.Dict;
import com.zt.plat.module.qms.core.aspect.annotation.QmsPermission;
import com.zt.plat.module.qms.core.constant.CommonConstant;
import com.zt.plat.module.qms.core.legend.LegendApi;
import com.zt.plat.module.qms.core.legend.LegendConvertUtils;
import com.zt.plat.module.qms.core.legend.vo.DictModel;
import com.zt.plat.module.qms.framework.datapermission.QMSPermissionContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import static com.zt.plat.framework.security.core.util.SecurityFrameworkUtils.getLoginUser;
import static com.zt.plat.module.qms.core.constant.CacheConstant.QMS_DICT_BIZ_CACHE;
import static com.zt.plat.module.qms.core.constant.CacheConstant.QMS_DICT_TABLE_CACHE;
import static com.zt.plat.module.qms.core.constant.DataTypeConstant.DICT_ANNOTATION_SPLIT;

@Aspect
@Component
@Slf4j
public class QmsPermissionAspect {
    /**
     * 定义切点Pointcut
     */
    @Pointcut("@annotation(com.zt.plat.module.qms.core.aspect.annotation.QmsPermission)")
    public void executeService() {
    }

    @Before("executeService()")
    public void doBefore(JoinPoint point) throws Throwable {
        handleDataScope(point);
    }

    private void handleDataScope(JoinPoint joinPoint){
        QmsPermission annotation = getAnnotationByJoinPoint(joinPoint);
        if(annotation == null)
            return;
        QMSPermissionContextHolder.setContext(true,  annotation.deptDataRoleCodes(), annotation.moduleDataRoleCodes(),
                annotation.deptIdColumn(), annotation.userIdColumn(), annotation.self(), annotation.custom());
    }

    private QmsPermission getAnnotationByJoinPoint(JoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();

        if (method != null) {
            return method.getAnnotation(QmsPermission.class);
        }
        return null;
    }

}
