
package com.ssj.aijia.config;

import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.ssj.framework.core.security.manager.TokenManager;
import com.ssj.framework.core.util.StringUtil;
import com.ssj.framework.core.util.WebUtil;
import com.ssj.request.config.HttpHelper;
import com.ssj.service.conch.conch.service.ConchDerverService;
import com.ssj.service.sys.analysis.service.AnalysisService;
import com.ssj.service.sys.homework.service.HomeworkService;

import io.swagger.annotations.ApiOperation;


/**
 *
 * 类名称：ApiAccessVerifyInterceptor<br>
 * 类描述：API访问权限验证<br>
 * @version v1.0
 *
 */
@Component
public class RequestApiLogInterceptor extends HandlerInterceptorAdapter {
    private static final Logger logger = LoggerFactory.getLogger(RequestApiLogInterceptor.class);
    @Autowired
    private TokenManager tokenManager;
    @Reference(version = "1.0" ,timeout = 5000)
    private ConchDerverService conchDerverService;
    @Reference(version = "1.0" ,timeout = 5000)
    private AnalysisService analysisService;
    @Reference(version = "1.0" ,timeout = 5000)
    private HomeworkService homeworkService;


    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        ServletWebRequest webRequest = new ServletWebRequest(request, response);
        try {
            if (!(handler instanceof HandlerMethod)) {
                return;
            }
            HandlerMethod method = (HandlerMethod) handler;
            String methodName = method.getMethod().getName();
            if (!"error".equals(methodName)) {
                //获取请求api的设备信息
                MDC.clear();
                MDC.put("phoneimei",webRequest.getHeader("Phoneimei"));
                MDC.put("phonesys",webRequest.getHeader("phonesys"));
                MDC.put("phonetype",webRequest.getHeader("phonetype"));
                MDC.put("version",webRequest.getHeader("version"));
                MDC.put("platform",webRequest.getHeader("platform"));
                MDC.put("build",webRequest.getHeader("build"));
                MDC.put("apiUrl",request.getRequestURI().replaceAll(request.getContextPath(), ""));
                MDC.put("methodName",methodName);
                MDC.put("ipAddress",WebUtil.getRemoteAddr(request));
                String accessToken =webRequest.getParameter("accessToken");
                MDC.put("accessToken",accessToken);
                String userId ="";
                if(StringUtil.isNotEmpty(accessToken)){
                    userId=tokenManager.getUserId(accessToken);
                }
                MDC.put("userId",userId);
                MDC.put("requestMap",JSON.toJSONString(request.getParameterMap()));

                try {
                    ApiOperation apiOperation = method.getMethod().getAnnotation(ApiOperation.class);
                    MDC.put("apiName",apiOperation.value());

                    String postBody = "";
                    RequestMapping requestMapping = method.getMethod().getAnnotation(RequestMapping.class);
                    if (requestMapping != null && Arrays.asList(requestMapping.method()).contains(RequestMethod.POST)) {
                        postBody = HttpHelper.getBodyString(request);
                    }
                    MDC.put("postBody", postBody);
                } catch (Exception ignored) {

                }
                logger.error(MDC.getCopyOfContextMap().toString());

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
