package com.ho.business.config;

import com.ho.common.tools.annotation.NotSportEnOperation;
import com.ho.common.tools.constant.CommonConstant;
import com.ho.common.tools.constant.RedisKeyConstant;
import com.ho.common.tools.exception.BaseResponseCode;
import com.ho.common.tools.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
/**
 * @author kerwin
 */
@Aspect
@Component
@Slf4j
public class NotSportEnOperationAspect {

    @Pointcut("execution(* com.ho.*.controller.*.*(..))")
    public void pointCut() {

    }
    @Around("pointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        ServletRequestAttributes servletAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletAttributes.getRequest();
        MethodSignature signature = (MethodSignature) point.getSignature();
        Method method = signature.getMethod();
        NotSportEnOperation notSportEnOperation = method.getAnnotation(NotSportEnOperation.class);
        if (null != notSportEnOperation) {
            String lang = request.getHeader(CommonConstant.LANG);
            if(!CommonConstant.langTemp.ZH_CN.equals(lang)){
                throw new BusinessException(BaseResponseCode.EN_US_NOT_SUPPORT_OPERATION);
            }
        }
        //执行方法
        Object result = point.proceed();
        //方法执行结果
        return result;
    }
}
