package com.xunhong.erp.turbo.rpc.filter;

import cn.dev33.satoken.same.SaSameUtil;
import com.xunhong.erp.turbo.base.dto.UserSession;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;

/**
 * Sa-Token 整合 Dubbo Consumer端过滤器
 */
@Activate(group = {CommonConstants.CONSUMER}, order = -10000)
public class DubboConsumerFilter implements Filter {

	@Override
	public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {

		// 追加 Same-Token 参数
		String token = SaSameUtil.getToken();
		RpcContext.getClientAttachment().setAttachment(SaSameUtil.SAME_TOKEN, token);

		// 如果有其他自定义附加数据，如租户
		RpcContext.getClientAttachment().setAttachment("USER-SESSION", UserSession.USER_THREAD_LOCAL.get());

		// 开始调用
		return invoker.invoke(invocation);
	}

}
