
package com.unis.detectionnum.server.service;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import com.unis.detectionnum.server.feign.GetUser;
import com.unis.detectionnum.server.feign.UserDTO;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
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 org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.unis.detectionnum.common.vo.LogVO;
import com.unis.detectionnum.server.domain.LogDomain;
import com.unis.detectionnum.server.utils.CommonUtil;


/**
 * *
 * * @ClassName: LogAspect
 *  * @Description: 日志管理
 *  * @author LiuFeng
 *  * @date 2019年10月22日 下午5:49:01
 */


@Aspect
@Component
public class LogAspect {

    @Autowired
    GetUser getUser;

    @Autowired
    private LogDomain logDomain;
    // 切点的注解 是指那些方法需要被执行"AOP"
    @Pointcut("execution(* com.unis.detectionnum.server.service..*.*(..))")
    public void logPoinCut() {
    }// 定义一个切入点

    // 返回后通知value="logPoinCut()"是指通知是在logPoinCut()切点返回后通知的
    // returning="rtv"是返回值
    @AfterReturning(value = "logPoinCut()", returning = "rtv")
    // JoinPoint是连接点的意思我们要获取到的如类名，方法名，请求参数等都是从连接点中取出来的
    public void afterLog(JoinPoint joinpoint, Object rtv) {

        LogVO logBean = new LogVO();
        logBean.setCreateDate(new Date());
        // 获取类名
        String classname = joinpoint.getTarget().getClass().getSimpleName();

        String module = "数据查询";
        if(classname.contains("alarm")) {
            module = "告警信息";
        }else if(classname.contains("DCfxxsjy")) {
            module = "仓房信息";
        }else if(classname.contains("DKdxxsjy")) {
            module = "库点信息";
        }else if(classname.contains("Device")) {
            module = "参数管理";
        }else if(classname.contains("Scan")) {
            module = "数据查询";
        }
        logBean.setModule(module);

        // 获取方法名
        String method = joinpoint.getSignature().getName().toLowerCase();
        String action = "查询";
        if(method.contains("insert") || method.contains("save") || method.contains("add")) {
            action = "添加";
        }if(method.contains("update") || method.contains("edit")) {
            action = "修改";
        }if(method.contains("get") || method.contains("find") || method.contains("list") || method.contains("page")) {
            action = "查询";
        }if(method.contains("del") || method.contains("delete")) {
            action = "删除";
        }
        logBean.setAction(action);
        // 获取请求参数
        String reqparam = "请求参数:";
        Object[] args = joinpoint.getArgs();
        for (Object object : args) {
            if(object != null)
                reqparam += "||" + object.toString();
        }
        String resdata = "返回数据:";
        // 返回值
        if(rtv!=null){
            resdata+=rtv.toString();
        }
        logBean.setOperateData(reqparam+"***"+resdata);
        // 获取request对象
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes();
        if(requestAttributes != null && requestAttributes.getRequest() != null) {
            HttpServletRequest request = requestAttributes.getRequest();

            UserDTO currUserinfo = getUser.getCurrUserinfo();
            logBean.setUserName(currUserinfo.getUserName());
            logBean.setUserId(currUserinfo.getUserCode());

            // 获取ip地址是封装好的一个类
            String ip = CommonUtil.getIp(request);
            logBean.setIp(ip);
            logDomain.save(logBean);
        }

    }
}
