package com.qmth.themis.exam.aspect;

import com.qmth.themis.business.util.JacksonUtil;
import com.qmth.themis.business.util.ServletUtil;
import com.qmth.themis.common.util.ResultUtil;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * @Description: api aspect
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2020/5/12
 */
@Aspect
@Component
public class ApiControllerAspect {
    private final static Logger log = LoggerFactory.getLogger(ApiControllerAspect.class);

    /**
     * api切入点
     */
    @Pointcut("execution(public * com.qmth.themis.exam.api.*.*(..))")
    public void apiAspect() {
    }

    /**
     * 考试环绕切入
     *
     * @param joinPoint
     * @return
     * @throws Throwable
     */
    @Around(value = "apiAspect()")
    public Object aroundApiPoint(ProceedingJoinPoint joinPoint) throws Throwable {
        try {
            long start = System.currentTimeMillis();
            MethodSignature msig = (MethodSignature) joinPoint.getSignature();
            String className = msig.getDeclaringTypeName();
            String methodName = msig.getName();
            Object[] args = joinPoint.getArgs();
            String[] paramsName = msig.getParameterNames();
            HttpServletRequest request = ServletUtil.getRequest();
            log.info("============请求地址========:{}", request.getRequestURL());
            log.info("============类=============:{}", className);
            log.info("============方法===========:{}", methodName);
            if (Objects.nonNull(args) && args.length > 0) {
                boolean jsonOut = true;
                for (Object o : args) {
                    if (o instanceof CommonsMultipartFile) {
                        jsonOut = false;
                        break;
                    }
                }
                if (jsonOut) {
                    log.info("============参数key:{},参数value===========:{}", JacksonUtil.parseJson(paramsName), JacksonUtil.parseJson(args));
                } else {
                    for (int i = 0; i < args.length; i++) {
                        log.info("============参数key:{},参数value===========:{}", paramsName[i], args[i]);
                    }
                }
            }
            log.info("============platform===========:{}", ServletUtil.getRequestPlatform());
            log.info("============deviceId===========:{}", ServletUtil.getRequestDeviceId());
            log.info("============Authorization===========:{}", ServletUtil.getRequestAuthorizationForAspect());
            log.info("============time===========:{}", ServletUtil.getRequestTimeForTime());
            Object proceed = joinPoint.proceed();
            long end = System.currentTimeMillis();
            log.info("============耗时============:{}秒", (end - start) / 1000);
            return proceed;
        } catch (Exception e) {
            log.error("请求出错", e);
            return ResultUtil.error(e.getMessage());
        }
    }
}
