package com.ruoyi.framework.interceptor;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.quotation.domain.TSeapriceQueryLog;
import com.ruoyi.quotation.service.ITSeapriceQueryLogService;
import com.ruoyi.system.service.ISysOperLogService;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

@Component
public class TSeapriceQueryLogInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        //返回 false 则请求中断
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        String uri = request.getRequestURI();
        if("/warehouse/seaprice/queryApp".equals(uri)){
                TSeapriceQueryLog tSeapriceQueryLog = new TSeapriceQueryLog();
                tSeapriceQueryLog.setfPortDestinationId(Long.valueOf(request.getParameter("fPortDestinationId")));
                tSeapriceQueryLog.setfPortOriginId(Long.valueOf(request.getParameter("fPortOriginId")));
                tSeapriceQueryLog.setfPortTransitId(Long.valueOf(request.getParameter("fPortTransitId")));
                tSeapriceQueryLog.setfValiddateBegin(DateUtil.parse(request.getParameter("fValiddateBegin"),"yyyy-MM-dd HH:mm:ss"));
                tSeapriceQueryLog.setfValiddateEnd(DateUtil.parse(request.getParameter("fValiddateEnd"),"yyyy-MM-dd HH:mm:ss"));
                tSeapriceQueryLog.setfUserId(SecurityUtils.getLoginUser().getUser().getUserId());
                tSeapriceQueryLog.setfQueryDatetime(new Date());
                SpringUtils.getBean(ITSeapriceQueryLogService.class).insertTSeapriceQueryLog(tSeapriceQueryLog);
        }
    }
}
