package com.backendsys.modules.common.aspect;

import com.backendsys.modules.common.config.security.utils.HttpRequestUtil;
import com.backendsys.modules.system.dao.SysLogDao;
import com.backendsys.modules.system.entity.SysLogEntity;
import com.google.gson.Gson;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * 系统日志，切面处理类
 */
@Aspect
@Component
public class SysLogAspect {

	@Autowired
	private HttpRequestUtil httpRequestUtil;

	@Autowired
	private SysLogDao sysLogDao;
	
	@Pointcut("@annotation(com.backendsys.modules.common.aspect.SysLog)")
	public void logPointCut() {}

	@Around("logPointCut()")
	public Object around(ProceedingJoinPoint point) throws Throwable {
		long beginTime = System.currentTimeMillis();
		Object result = point.proceed();
		// 执行时长(毫秒)
		long time = System.currentTimeMillis() - beginTime;
		// 保存日志
		saveSysLog(point, time);
		return result;
	}

	private void saveSysLog(ProceedingJoinPoint joinPoint, long time) {
		MethodSignature signature = (MethodSignature) joinPoint.getSignature();
		Method method = signature.getMethod();

		SysLogEntity sysLogEntity = new SysLogEntity();

		// 注解上的描述
		SysLog syslog = method.getAnnotation(SysLog.class);
		if(syslog != null){
			sysLogEntity.setAction(syslog.value());
		}

		// 请求方法名
		String className = joinPoint.getTarget().getClass().getName();
		String methodName = signature.getName();
		sysLogEntity.setClassname(className);
		sysLogEntity.setMethod(methodName);

		// 请求参数
		Object[] args = joinPoint.getArgs();
//		String params = new Gson().toJson(args);
		String params = getParams(joinPoint.getArgs());
		sysLogEntity.setParams(params);

		sysLogEntity.setIp(httpRequestUtil.getIpAddr());
		sysLogEntity.setUsername(httpRequestUtil.getUserName());
		sysLogEntity.setTime(time);
		// 保存系统日志
		sysLogDao.insert(sysLogEntity);
	}

	// 过滤 MultipartFile 类型
	private String getParams(Object[] args) {
		if (args == null || args.length == 0) {
			return "";
		}
		List<Object> filteredArgs = new ArrayList<>();
		for (Object arg : args) {
			if (arg instanceof MultipartFile) {
				// 对于 MultipartFile，只记录文件名和大小
				MultipartFile file = (MultipartFile) arg;
				filteredArgs.add(new MultipartFileInfo(file.getOriginalFilename(), file.getSize()));
			} else {
				filteredArgs.add(arg);
			}
		}
		return new Gson().toJson(filteredArgs);
	}

	// 辅助类，用于记录 MultipartFile 的基本信息
	private static class MultipartFileInfo {
		private String originalFilename;
		private long size;
		public MultipartFileInfo(String originalFilename, long size) {
			this.originalFilename = originalFilename;
			this.size = size;
		}
		public String getOriginalFilename() {
			return originalFilename;
		}
		public long getSize() {
			return size;
		}
	}
}
