package com.zanxiang.game.data.serve.intercept;

import com.zanxiang.erp.security.util.SecurityUtil;
import com.zanxiang.game.data.serve.mangae.AsyncManager;
import com.zanxiang.game.data.serve.mangae.factory.AsyncFactory;
import com.zanxiang.game.data.serve.pojo.dto.WebLogDTO;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * packageName com.zanxiang.game.data.serve.intercept
 *
 * @author ZhangXianyu
 * @date 2024/3/22
 * @description 接口请求日志记录拦截器
 */
@Component
public class WebVisitLogIntercept implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取当前请求的URL
        String url = request.getRequestURI().toString();
        //获取当前用户id
        Long sysUserId = SecurityUtil.getUserId();
        //标题名称
        WebLogDTO webLogDTO = new WebLogDTO();
        webLogDTO.setTitle(url);
        if(sysUserId!=null){
            webLogDTO.setUserId(sysUserId.toString());
        }
        LocalDateTime now = LocalDateTime.now();
        String dateTime = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        webLogDTO.setVisitTime(dateTime);
        // 发到kafka
        AsyncManager.me().execute(AsyncFactory.webVisitLog(webLogDTO));
        return true;
    }






}