
package org.tio.sitexxx.service.topic;

import cn.hutool.core.util.StrUtil;
import org.tio.sitexxx.service.service.base.UserService;
import org.tio.sitexxx.service.service.ConfService;
import org.redisson.api.listener.MessageListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.sitexxx.service.vo.topic.TopicVo;

import java.util.Map;
import java.util.Objects;

/**
 * @author tanyaowu 
 * 2016年11月8日 下午4:05:52
 */
public class TopicCommonListener implements MessageListener<TopicVo> {
	private static Logger log = LoggerFactory.getLogger(TopicCommonListener.class);

	public static TopicCommonListener ME = new TopicCommonListener();

	@Override
	public void onMessage(CharSequence channel, TopicVo msg) {
		String clientid = msg.getClientId();
		if (StrUtil.isBlank(clientid)) {
			log.error("clientid is null");
			return;
		}
		if (!msg.isForceRun() && Objects.equals(TopicVo.CLIENTID, clientid)) {
			log.info("自己发布的消息,{}", clientid);
			return;
		}
		Short type = msg.getType();
		if (Objects.equals(type, TopicVo.Type.CLEAR_CONF)) {
			//清空系统参数
			ConfService.clearCache();
		} else if (Objects.equals(type, TopicVo.Type.CLEAR_USER)) {
			//清空用户缓存，后台修改用户相关信息
			Integer uid = (Integer) msg.getValue();
			UserService.ME._clearCache(uid);
		} else if (Objects.equals(type, TopicVo.Type.CLEAR_ALL_USER)) {
			UserService.ME.notifyClearCache(null);
		} else if (Objects.equals(type, TopicVo.Type.CLEAR_ALL_CONF)) {
			ConfService.clearAll();
		} else if (Objects.equals(type, TopicVo.Type.SET_SYSTEM_PARAM)) {
			Map<String, Object> params = msg.getParams();
			for (Map.Entry<String, Object> entry : params.entrySet()) {
				String key = entry.getKey();
				String val = (String) entry.getValue();
				System.setProperty(key, val);
			}
		}
	}
}
