package com.ibeetl.admin.core.aop;

import com.ibeetl.admin.core.annotation.LogController;
import com.ibeetl.admin.core.entity.CoreAudit;
import com.ibeetl.admin.core.service.CorePlatformService;
import com.ibeetl.admin.core.service.LogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
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;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Aspect
@Component
public class LogAspect {
	private static final Logger log = LoggerFactory.getLogger(LogAspect.class);

	@Autowired
	private LogService logService;
	
	@Autowired
	private CorePlatformService cps;

	// controller层切点
	@Pointcut("@annotation(com.ibeetl.admin.core.annotation.LogController)")
	public void serviceAspect() {
	}

	@After("serviceAspect()")
	public void doServiceAfter(JoinPoint joinPoint) {
		CoreAudit ca=new CoreAudit();
		ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes();
		HttpServletRequest request = requestAttributes.getRequest();
		// 请求的IP
		String ip = request.getRemoteAddr();
		// 请求的参数
		String param = getParam(joinPoint);
		// 请求的参数
		String methodName = (joinPoint.getSignature().getName());
		Map<String,String> map=getServiceMthodType(joinPoint);
		try {
			ca.setIp(ip);
			ca.setMessage(map.get("content"));
			ca.setUserName(cps.getCurrentUser().getName());
			ca.setSuccess(1);
			ca.setFunctionName(methodName);
			ca.setUserId(cps.getCurrentUser().getId());
			ca.setCreateTime(new Date());
			logService.writeManageOptLog(ca);
		}catch (Exception e){
			e.printStackTrace();
		}
	}
	/**
	 * 方法参数
	 * 
	 * @param joinPoint
	 * @return
	 */
	private String getParam(JoinPoint joinPoint) {
		StringBuilder params = new StringBuilder();
		if (joinPoint.getArgs() != null && joinPoint.getArgs().length > 0) {
			for (int i = 0; i < joinPoint.getArgs().length; i++) {
					params.append(joinPoint.getArgs()[i]).append(";");
			}
		}
		return params.toString();
	}

	// 操作类型，用于区分操作
	private static Map<String,String> getServiceMthodType(JoinPoint joinPoint) {
		String targetName = joinPoint.getTarget().getClass().getName();
		String methodName = joinPoint.getSignature().getName();
		Object[] arguments = joinPoint.getArgs();
		Class targetClass = null;
		try {
			targetClass = Class.forName(targetName);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		assert targetClass != null;
		Method[] methods = targetClass.getMethods();
		String type = "";
		String content="";
		String desc="";
		Map<String,String> map=new HashMap<String,String>();
		for (Method method : methods) {
			if (method.getName().equals(methodName)) {
				Class[] clazzs = method.getParameterTypes();
				if (clazzs.length == arguments.length) {
					type = method.getAnnotation(LogController.class).type();
					content=method.getAnnotation(LogController.class).content();
					desc=method.getAnnotation(LogController.class).description();
					map.put("content",content);
					map.put("desc",desc);
					map.put("type",type);
					break;
				}
			}
		}
		return map;
	}
}
