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

import cn.hutool.core.util.StrUtil;
import com.mortals.framework.model.OperateLogPdu;
import com.mortals.framework.service.ILogService;
import com.mortals.framework.service.IMessageProduceService;
import com.mortals.framework.service.impl.FileLogServiceImpl;
import com.mortals.xhx.base.system.oper.model.OperLogEntity;
import com.mortals.xhx.base.system.oper.service.OperLogService;
import com.mortals.xhx.common.code.OperTypeEnum;
import com.mortals.xhx.module.operate.model.OperateLogEntity;
import com.mortals.xhx.module.operate.service.OperateLogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.checkerframework.checker.units.qual.A;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 操作日志记录
 *
 * @author: zxfei
 * @date: 2021/11/5 13:28
 */
@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;
    @Autowired
    private OperateLogService operateLogService;

    @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);
        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();
        }
        operLogService.insertOperLog(ip, requestUrl, userId, userName, loginName, content);

        OperateLogEntity operLogEntity = new OperateLogEntity();
        operLogEntity.initAttrValue();
        operLogEntity.setIp(ip);
        operLogEntity.setRequestUrl(requestUrl);
        operLogEntity.setUserId(userId);
        operLogEntity.setUserName(userName);
        operLogEntity.setLoginName(loginName);
        operLogEntity.setPlatformMark(platformMark);
        operLogEntity.setLogDate(logDate);
        operLogEntity.setContent(content);
        operLogEntity.setOperType(operType);
        operateLogService.save(operLogEntity, null);
    }

    @Override
    public void doHandlerLog(String platformMark, String loginName, String requestUrl, String content, String ip) {
        // operLogService.insertOperLog(ip, requestUrl, null, "", loginName,
        // content);
        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
        // response
        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());
        }
    }
}
