package cn.com.countrygarden.bi.main.interceptor;

import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.dao.experience.WcxExperienceMapper;
import cn.com.countrygarden.bi.main.model.AccessToken;
import cn.com.countrygarden.bi.main.model.experience.WcxEmployeesManage;
import cn.com.countrygarden.bi.main.service.AccessTokenService;
import cn.com.countrygarden.bi.main.service.RoleService;
import cn.com.countrygarden.bi.main.service.experience.WcxCustomerService;
import cn.com.countrygarden.bi.main.service.twerp.hm.TbSysUserService;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.List;

public class TygWxInterceptor extends HandlerInterceptorAdapter {
    protected final Logger logger = LoggerFactory.getLogger(TygWxInterceptor.class);

    @Autowired
    private TbSysUserService tbSysUserService;

    @Autowired
    private RoleService roleService;
    @Autowired
    protected AccessTokenService accessTokenService;
    @Autowired
    private WcxExperienceMapper wcxExperienceMapper;
    /**
     * 前置检查
     *
     * @param request
     * @param response
     * @param handler
     * @return
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (handler instanceof HandlerMethod) {
            String ip = request.getRemoteAddr();
            long startTime = System.currentTimeMillis();
            request.setAttribute("requestStartTime", startTime);
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            // 获取用户token
            Method method = handlerMethod.getMethod();
            logger.info("[" + ip + "] " + method.getDeclaringClass().getName() + "." + method.getName() + " request start");
        }
        String key=request.getParameter("key");
        if(key!=null){
            if("f275cbd642a946e8a509f38748d84771".equals(key)){
                return true;
            }
        }
        String accessToken = request.getParameter("AccessToken");
        List<WcxEmployeesManage> wcxEmployeesManages = wcxExperienceMapper.selectByAccessToken(accessToken);
        if(wcxEmployeesManages.size() ==0){
            Result result=new Result();
            result.setResultCode(10010);
            result.setResultMsg("AccessToken不能为空");
            response.getWriter().write(JSON.toJSONString(result));
            response.setContentType("application/json;charset=UTF-8");
            return false;
        }
        return true;
    }

    /**
     * controller 处理完成
     *
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     * @throws Exception
     */
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
        if (handler instanceof HandlerMethod) {
            String ip = request.getRemoteAddr();
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            Method method = handlerMethod.getMethod();
            long startTime = (Long) request.getAttribute("requestStartTime");
            long endTime = System.currentTimeMillis();
            long executeTime = endTime - startTime;

            logger.info("[" + ip + "] " + method.getDeclaringClass().getName() + "." + method.getName() + " " + executeTime + "ms");
        }
    }
}
