package com.rrtx.services.common.aspect;

import cn.hutool.json.JSONUtil;
import com.rrtx.services.common.properties.ConfigProperties;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * ClassName: LogAspect<br>
 * Description: 记录用户操作日志 <br>
 * Company: rrtx
 *
 * @author rrtx
 * @version v1.0.0    2019/7/24 4:40 PM  xugz    文件初始创建
 */
@Slf4j
@Aspect
@Component
public class LogAspect {

	@Autowired
	private ConfigProperties configProperties;


	@Pointcut("@annotation(com.rrtx.services.common.annotation.Log)")
	public void pointcut() {
		// do nothing
	}

	@Around("pointcut()")
	public Object around(ProceedingJoinPoint point) throws Throwable {
		Object result;
		long beginTime = System.currentTimeMillis();
		Object[] args = point.getArgs();
		boolean isOpenAopLog = configProperties.isOpenAopLog();
		if (isOpenAopLog){
			int i = 0;
			for(Object object : args){
				log.info("{}第{}个入参为{}", point.getSignature(), i++, object);
			}
		}
		// 执行方法
		result = point.proceed();
		if (configProperties.isOpenAopLog()) {
			// 执行时长(毫秒)
			long time = System.currentTimeMillis() - beginTime;

			log.info("{}结果{}----耗时----{}ms", point.getSignature(),  JSONUtil.toJsonStr(result), time);
		}

		return result;
	}
}
