package com.uas.report.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;

import com.uas.report.util.StringUtils;

/**
 * 利用AOP处理Axis Service日志
 * 
 * @author sunyj
 * @since 2017年1月3日 上午9:47:17
 */
@Aspect
@Component
@EnableAspectJAutoProxy
public class AxisServiceLogAspect {

	private Logger logger = LoggerFactory.getLogger(getClass());

	@Pointcut("execution(public * com.uas.report.axis..*Impl.*(..))")
	public void log() {

	}

	@Before("log()")
	public void before(JoinPoint joinPoint) {
		StringBuilder stringBuilder = new StringBuilder("request... ");

		// 类名
		Object target = joinPoint.getTarget();
		Class<?> clazz = target.getClass();
		stringBuilder.append(clazz.getSimpleName());

		// 方法名
		Signature signature = joinPoint.getSignature();
		String methodName = signature.getName();
		stringBuilder.append(".").append(methodName);

		// 方法全名
		String methodString = signature.toString();
		// 取出参数类型
		String substring = methodString.substring(methodString.indexOf("(") + 1, methodString.lastIndexOf(")"));
		if (!StringUtils.isEmpty(substring)) {
			stringBuilder.append(": ");
			String[] strs = substring.split(",");
			Object[] args = joinPoint.getArgs();
			for (int i = 0; i < strs.length; i++) {
				// 参数类型和参数值
				stringBuilder.append(strs[i]).append("=").append(args[i]);
				if (i != strs.length - 1) {
					stringBuilder.append(",");
				}
			}
		}
		logger.info(stringBuilder.toString());
	}

	@AfterReturning(value = "log()", returning = "result")
	public void afterReturning(JoinPoint joinPoint, Object result) {
		logger.info("result... " + result + "\n");
	}

}
