package com.zcloud.util.logAop;

import com.alibaba.fastjson.JSON;
import com.zcloud.controller.base.BaseController;
import com.zcloud.entity.PageData;
import com.zcloud.service.system.FHlogService;
import com.zcloud.util.DateUtil;
import com.zcloud.util.Jurisdiction;
import com.zcloud.util.UuidUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
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.lang.reflect.Method;
import java.util.Date;
import java.util.Enumeration;

/**
 * 说明：日志切面
 * 作者：wangxuan
 * 官网：www.zcloudchina.com
 */
@Aspect
@Component
public class LogOperationAspect {
    @Autowired
    private FHlogService fHlogService;
    private static final Logger logger = LoggerFactory.getLogger(LogOperationAspect.class);

    @Around("execution(* com.zcloud.controller..*.*(..))")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        long begintimeMillis = System.currentTimeMillis();
        try {
            Object result = point.proceed();

            long endtimeMillis = System.currentTimeMillis();
            saveLog(point, 0, endtimeMillis - begintimeMillis, null);
            return result;
        } catch (Exception e) {
            long endtimeMillis = System.currentTimeMillis();
            saveLog(point, 1, endtimeMillis - begintimeMillis, e);
            throw e;
        }

    }

    private void saveLog(ProceedingJoinPoint joinPoint, int success, long TIME_CONSUMING, Object e) throws Exception {
//        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//        Method method = joinPoint.getTarget().getClass().getDeclaredMethod(signature.getName(), signature.getParameterTypes());
        PageData parameterPageData = getPageData();
        PageData pageData = new PageData();
        pageData.put("HTTP_URL", getRequest().getServletPath());
        if (Jurisdiction.getSession() != null) {
            pageData.put("CREATOR", Jurisdiction.getSession().getAttribute("USER_ID"));
        } else {
            pageData.put("CREATOR", parameterPageData.getString("USER_ID"));
        }
        pageData.put("TIME_CONSUMING", TIME_CONSUMING);
        pageData.put("HTTP_UR2L", DateUtil.getYear());
        pageData.put("PARAMETER", JSON.toJSONString(parameterPageData));
        pageData.put("CREATED_TIME", DateUtil.date2Str(new Date()));
        pageData.put("SUCCESS", success);
        pageData.put("RESULT_INFO", e == null ? null : JSON.toJSONString(e));
        fHlogService.saveGobleLog(pageData);
    }

    private PageData getPageData() {
        return new PageData(this.getRequest());
    }

    /**
     * 得到request对象
     *
     * @return
     */
    private HttpServletRequest getRequest() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return request;
    }

    public String get32UUID() {
        return UuidUtil.get32UUID();
    }

}
