package com.neusoft.smart.pos.gateway.filters;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.neusoft.smart.pos.framework.task.TaskExecutor;
import com.neusoft.smart.pos.gateway.service.RedisService;
import com.neusoft.smart.pos.gateway.task.UserCacheTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.*;

@Component
public class UserCacheFilter extends ZuulFilter {
	
	@Autowired
	private TaskExecutor taskExecutor;
	
	@Override
	public String filterType() {
		return POST_TYPE;
	}
	
	@Override
	public int filterOrder() {
		return SEND_RESPONSE_FILTER_ORDER;
	}
	
	@Override
	public boolean shouldFilter() {
		RequestContext ctx = RequestContext.getCurrentContext();
		String uri = ctx.getRequest().getRequestURI();
		return uri.startsWith("/service-user/user/createUser") ||
				uri.startsWith("/service-user/user/addUserPermissionGroup") ||
				uri.startsWith("/service-user/user/removeUserPermissionGroup") || uri.startsWith("/unionService/register");
	}
	
	@Override
	public Object run() {
		UserCacheTask userCacheTask = new UserCacheTask();
		taskExecutor.getExecutorService().execute(userCacheTask);
		return null;
	}
	
	
}
