package com.zcloud.common.aspect;

import com.zcloud.common.utils.HttpContextUtils;
import com.zcloud.modules.exceptionLog.dao.ErrorLogDao;
import com.zcloud.modules.exceptionLog.entity.ErrorLogEntity;
import com.zcloud.modules.exceptionLog.service.ErrorLogService;
import org.apache.shiro.SecurityUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

@Aspect
@Component
public class ExceptionLoggingAspect {

    public ExceptionLoggingAspect(ErrorLogDao errorLogDao) {
    }

    // 拦截modules包下所有类的所有方法
    @Pointcut("within(com.zcloud.modules..*)")
    public void modulePointCut() {
    }
    // 捕获方法执行异常
    /*@AfterThrowing(pointcut = "modulePointCut()", throwing = "e")
    public void doAfterThrowing(JoinPoint joinPoint, Throwable e) {
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        Map<String, String[]> map = request.getParameterMap();
        Object userId = map.get("userId");
        userId = (userId != null) ? userId : ""; // 使用默认值作为后备
        ErrorLogEntity errorLog = new ErrorLogEntity();
        errorLog.setId(UUID.randomUUID().toString());
        errorLog.setLogTime(new Date());
        errorLog.setUserId(userId.toString());
        errorLog.setExceptionText(e.getMessage().toString());
        // 创建并保存异常日志
        errorLogDao.insert(errorLog);

    }*/
}
