package cn.com.countrygarden.bi.main.controller.performance.monthly;

import cn.com.countrygarden.bi.main.core.Response;
import cn.com.countrygarden.bi.main.core.ResultCode;
import cn.com.countrygarden.bi.main.exception.ServiceException;
import cn.com.countrygarden.bi.main.model.AccessToken;
import cn.com.countrygarden.bi.main.service.AccessTokenService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * 身份验证切面
 */
@Aspect
@Component
public class AccessTokenAspect {

    @Autowired
    HttpServletRequest request;

    @Autowired
    private AccessTokenService accessTokenService;


    @Around("execution(* cn.com.countrygarden.bi.main.service.performance.impl.PerformanceMonthServiceImpl.*(..))")
    public Object checkAccessToekn(ProceedingJoinPoint jp) throws Throwable {
        //      根据授权获取用户信息
        String accessToken = this.request.getParameter("AccessToken");
        AccessToken token = accessTokenService.findByToken(accessToken);
        if (token == null || token.getUserCode() == null) {
          return Response.response(ResultCode.UNAUTHORIZED);
        }
        Object[] agrs = jp.getArgs();
        if(agrs.length > 2)
             agrs[2] = token.getUserCode();

        Object obj = jp.proceed(agrs);
        return obj;

    }
}
