package com.philisense.www.aspect;

import com.philisense.www.core.text.Convert;
import com.philisense.www.utils.ActiveUserUtils;
import com.philisense.www.utils.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 数据权限
 *
 * @author
 */
@Component
@Aspect
public class DataPermissionAspect {

    private static final Logger log = LoggerFactory.getLogger(DataPermissionAspect.class);

    /**
     * 配置切入点
     */
    @Pointcut("@annotation(com.philisense.www.aspect.DataQueryPermission) " +
            "|| execution(* com.philisense.www.controller..*.query*(..)) " +
            "|| execution(* com.philisense.www.controller..*.count*(..)) ")
    public void dataPermissionPointcut() {}

    /**
     * 配置前置通知,使用在方法dataPermissionPointcut()上注册的切入点
     *
     * @param joinPoint join point for advice
     */
    @Around("dataPermissionPointcut()")
    public Object logBefore(ProceedingJoinPoint joinPoint) throws Throwable {
        Map<String, Object> activeUserMap = ActiveUserUtils.getActiveUserMap();
        log.info("DataPermissionAspect.logBefore() 当前用户：{}", activeUserMap);

        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        String[] parameterNames = methodSignature.getParameterNames();
        Object[] param = joinPoint.getArgs();
        
        Map<String, Object> tempParamMap = new HashMap<>();

        String userId = Convert.toStr(activeUserMap.get("user_id"));
        String userNameCn = Convert.toStr(activeUserMap.get("user_name_cn"));
        boolean systemUser = "0".equals(Convert.toStr(activeUserMap.get("is_system_user")));

        if (null != parameterNames) {
            for (int i = 0; i < parameterNames.length; i++) {
                if (parameterNames[i].equalsIgnoreCase("map")) {
                    tempParamMap.putAll((Map) param[i]);
                    tempParamMap = (Map) param[i];
                    // 系统用户systemUser和自定义权限标识authFlag为true的可以查看全部信息
                    boolean authFlag = Convert.toBool(tempParamMap.get("authFlag"),false);
                    tempParamMap.put("message", StringUtils.format("{}-{}" , Convert.toStr(tempParamMap.get("message"), ""), userNameCn));
                    tempParamMap.put("apply_user_id", systemUser || authFlag ? "" : userId);
                    // 考勤管理查询
                    tempParamMap.put("usercode", (systemUser || authFlag) && StringUtils.isNull(tempParamMap.get("usercode")) ? "" : (StringUtils.isNull(tempParamMap.get("usercode")) ? userId : tempParamMap.get("usercode")));
                    tempParamMap.put("user_id", systemUser || authFlag ? "" : userId);
                    tempParamMap.put(Convert.toStr(tempParamMap.get("authField"), "create_user_id"), systemUser || authFlag ? "" : userId);
                    param[i] = tempParamMap;
                }
            }
        }
        
        


        long start = System.currentTimeMillis();
        Object result = joinPoint.proceed(param);
        long end = System.currentTimeMillis();
        return result;
        
    }


}
