package com.sy.coinage.core.aop.aspect;

import com.alibaba.fastjson2.JSON;
import com.sy.coinage.config.CustomConfig;
import com.sy.coinage.core.aop.annotation.Permission;
import com.sy.coinage.core.aop.util.RequestUtil;
import com.sy.coinage.core.constants.Constants;
import com.sy.coinage.core.constants.ResponseEnum;
import com.sy.coinage.core.exception.RetException;
import com.sy.coinage.core.interceptor.TokenVerify;
import com.sy.coinage.core.vo.CacheUserVO;

import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

import java.util.Map;

/**
 * 验证权限切面
 */
@Aspect
@Component
public class PermissionAspect {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private TokenVerify tokenVerify;

    @Autowired
    private CustomConfig customConfig;

    /**
     * 自定义验证权限接口
     *
     * @param @param authRole
     * @return void 返回类型
     * @Title: serviceStatistics
     * @date 2019年5月17日 下午5:22:02
     */
    @Pointcut(value = "@annotation(permission)")
    public void serviceStatistics(Permission permission) {
        System.out.println("切入点");
    }

    /**
     * 前置通知
     *
     * @param @param joinpoint
     * @param @param authRole
     * @return void 返回类型
     * @Title: doBefore
     * @date 2019年5月20日 上午9:26:41
     */
    @Before("serviceStatistics(permission)")
    public void doBefore(JoinPoint joinpoint, Permission permission) {
        if (customConfig.getCheckPermission()) { // 角色id及按钮操作权限
            Map<String, Object> joinPointinfo = RequestUtil.getJoinpointInfoMap(joinpoint);
            Boolean b = false;
            try {
                CacheUserVO user = null;
                String userJson = String.valueOf(joinPointinfo.get(RequestUtil.USER));
                if (StringUtils.isNotBlank(userJson) && !"null".equals(userJson)) {
                    user = JSON.parseObject(userJson, CacheUserVO.class);
                } else {
                    String token = request.getHeader(Constants.TOKEN);
                    user = this.tokenVerify.getCacheUser(token);
                }
                if (user.getTenantId() == null || user.getTenantId().intValue() == 0) {
                    // 租户管理员直接跳过
                    return;
                }
                List<String> roleList = user.getPermissionList();
                String[] value = permission.value();
                if (roleList != null && roleList.size() > 0) {
                    for (int i = 0; i < value.length; i++) {
                        String val = value[i];
                        if (roleList.contains(val)) {
                            b = true;
                            break;
                        }
                    }
                } else {
                    throw new RetException(ResponseEnum.NO_PERMISSION.getMsg());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (!b) {
                throw new RetException(ResponseEnum.NO_PERMISSION.getMsg());
            }
        }
    }

}
