package com.mortals.xhx.base.framework.aspect;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import cn.hutool.core.util.StrUtil;
import com.mortals.framework.model.OperateLogPdu;
import com.mortals.framework.service.IMessageProduceService;
import com.mortals.xhx.common.code.OperTypeEnum;
import com.mortals.xhx.system.MessageProducer;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
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.util.ObjectUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.mortals.framework.service.ILogService;
import com.mortals.framework.service.impl.FileLogServiceImpl;
import com.mortals.xhx.base.system.oper.service.OperLogService;

/**
 * 操作日志记录
 */
@Component
public class OperlogAspect extends FileLogServiceImpl implements ILogService {

    private final static Logger logger = LoggerFactory.getLogger(OperlogAspect.class);

    @Autowired
    private OperLogService operLogService;
    @Autowired
    private IMessageProduceService messageProducer;

    @Override
    public void doHandlerLog(String platformMark, Long userId, String userName, String loginName, String requestUrl,
                             String content, String ip, Date logDate) {
        super.doHandlerLog(platformMark, userId, userName, loginName, requestUrl, content, ip, logDate);
        operLogService.insertOperLog(ip, requestUrl, userId, userName, loginName, content);

        int operType = OperTypeEnum.OTHER.getValue();
        //判断内容包含
        if (StrUtil.contains(content, "查询") || StrUtil.contains(content, "查看")) {
            operType = OperTypeEnum.SEARCH.getValue();
        } else if (StrUtil.contains(content, "保存") || StrUtil.contains(content, "新增") || StrUtil.contains(content, "修改")) {
            operType = OperTypeEnum.SAVE.getValue();
        } else if (StrUtil.contains(content, "删除")) {
            operType = OperTypeEnum.DELETE.getValue();
        }
        OperateLogPdu operateLogPdu = new OperateLogPdu();
        operateLogPdu.initAttrValue();
        operateLogPdu.setIp(ip);
        operateLogPdu.setRequestUrl(requestUrl);
        operateLogPdu.setUserId(userId);
        operateLogPdu.setUserName(userName);
        operateLogPdu.setLoginName(loginName);
        operateLogPdu.setPlatformMark(platformMark);
        operateLogPdu.setLogDate(logDate);
        operateLogPdu.setContent(content);
        operateLogPdu.setOperType(operType);
        messageProducer.syncOperSend(operateLogPdu);
    }

    @Override
    public void doHandlerLog(String platformMark, String loginName, String requestUrl, String content, String ip) {
        this.doHandlerLog(platformMark, null, "", loginName, requestUrl, content, ip, new Date());
    }

    @Pointcut("execution(public  * com.mortals.xhx..*Controller.*(..))")
    public void accessLog() {
    }

    @Before("accessLog()")
    public void doBefore(JoinPoint joinPoint) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        // url
        logger.info("ip[{}]url[{}]", request.getRemoteAddr(), request.getRequestURL());
        // 参数第1和第2个参数为HttpServletRequest request, HttpServletResponse
        if (joinPoint.getArgs().length > 2) {
            logger.info("args={}", joinPoint.getArgs()[2]);
        } else {
            logger.info("args={}", joinPoint.getArgs());
        }
    }


    @AfterReturning(returning = "object", pointcut = "accessLog()")
    public void doAfterReturning(Object object) {
        if (null != object) {
            logger.info("response={}", object.toString());
        }
    }
}
