
package com.corewing.app.common.aspect;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.json.JSONUtil;
import com.corewing.app.common.annotation.CommonLog;
import com.corewing.app.entity.User;
import com.corewing.app.service.UserService;
import com.corewing.app.util.DevLogUtil;
import com.corewing.app.util.RedisUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;


import javax.annotation.Resource;
import java.lang.reflect.Method;

/**
 * 业务日志aop切面
 *
 */
@Aspect
@Order
@Component
public class DevLogAop {


    @Resource
    private RedisUtil redisUtil;

    @Resource
    private UserService userService;

    /**
     * 日志切入点
     *
     * @author xuyuxiang
     * @date 2020/3/23 17:10
     */
    @Pointcut("@annotation(com.corewing.app.common.annotation.CommonLog)")
    private void getLogPointCut() {
    }

    /**
     * 操作成功返回结果记录日志
     *
     */
    @AfterReturning(pointcut = "getLogPointCut()", returning = "result")
    public void doAfterReturning(JoinPoint joinPoint, Object result) {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = methodSignature.getMethod();
        CommonLog commonLog = method.getAnnotation(CommonLog.class);
        String userName = "未知";
        String userId = "-1";
        try {
            try {
               if( StpUtil.isLogin()) {
                   User user = (User) redisUtil.get("APP_" + StpUtil.getLoginId());
                   userName = user.getUsername();
                   userId = user.getId().toString();
               }
            } catch (Exception e) {
                User user = userService.getById(StpUtil.getLoginIdAsLong());
                userName = user.getUsername();
                userId = user.getId().toString();
            }
        } catch (Exception ignored) {
        }
        // 异步记录日志
        DevLogUtil.executeOperationLog(commonLog, userName, userId, joinPoint, JSONUtil.toJsonStr(result));
    }

    /**
     * 操作发生异常记录日志
     *
     */
    @AfterThrowing(pointcut = "getLogPointCut()", throwing = "exception")
    public void doAfterThrowing(JoinPoint joinPoint, Exception exception) {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = methodSignature.getMethod();
        CommonLog commonLog = method.getAnnotation(CommonLog.class);
        String userName = "未知";
        String userId = "-1";
        try {
            if( StpUtil.isLogin()) {
                User user = (User) redisUtil.get("APP_" + StpUtil.getLoginId());
                userName = user.getUsername();
                userId = user.getId().toString();
            }
        } catch (Exception e) {
            User user = userService.getById(StpUtil.getLoginIdAsLong());
            userName = user.getUsername();
            userId = user.getId().toString();
        }
        //异步记录日志
        DevLogUtil.executeExceptionLog(commonLog, userName, userId, joinPoint, exception);
    }
}
