package com.ruoyi.web.modules.category.support;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import com.ruoyi.common.utils.StringUtils;

/**
 * 解析商家端当前店铺 ID（请求头 X-Shop-Id）
 */
@Component
public class SellerShopContextInterceptor implements HandlerInterceptor
{
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
    {
        String shopIdHeader = request.getHeader(com.ruoyi.web.modules.category.constant.CategoryConstants.HEADER_SHOP_ID);
        if (StringUtils.isNotEmpty(shopIdHeader))
        {
            SellerShopContext.setShopId(Long.valueOf(shopIdHeader));
        }
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
    {
        SellerShopContext.clear();
    }
}
