package com.qmth.distributed.print.aspect;

import com.qmth.boot.api.exception.ApiException;
import com.qmth.teachcloud.common.util.JacksonUtil;
import com.qmth.teachcloud.common.util.ResultUtil;
import com.qmth.teachcloud.common.util.ServletUtil;
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.validation.BeanPropertyBindingResult;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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.distributed.print.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();
            if (Objects.nonNull(request)) {
                log.info("============请求地址========:{}", request.getServletPath());
                log.info("============类=============:{}", className);
                log.info("============方法===========:{}", methodName);
                if (Objects.nonNull(args) && args.length > 0) {
                    for (int i = 0; i < args.length; i++) {
                        if (Objects.nonNull(args[i]) && (args[i] instanceof HttpServletRequest || args[i] instanceof HttpServletResponse || args[i] instanceof CommonsMultipartFile || args[i] instanceof MultipartFile || args[i] instanceof BeanPropertyBindingResult)) {
                            continue;
                        } else {
                            log.info("============参数key:{},参数value===========:{}", JacksonUtil.parseJson(paramsName[i]), JacksonUtil.parseJson(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);
            if (e instanceof ApiException) {
                return ResultUtil.error((ApiException) e, e.getMessage());
            } else {
                return ResultUtil.error(e.getMessage());
            }
        }
    }
}