package org.springblade.utils.listener;


import org.springblade.common.Constants;
import org.springblade.estate.guest.entity.Guest;
import org.springblade.mq.enums.Instruct;
import org.springblade.mq.service.MsgPublishService;
import org.springblade.openinterface.facedevice.qinlin.util.QinLinHttpUtil;
import org.springblade.openinterface.facedevice.qinlin.vo.QLinUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;



@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {

	@Autowired
	private  QinLinHttpUtil qinLinHttpUtil;

	@Autowired
	private MsgPublishService msgPublishService;

    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {
        String expiredKey = message.toString();
        if(expiredKey.startsWith(Constants.REDIS_GUEST_EFFECTIVE_TIME_JVT)){//园区访客信息在巨龙设备中过了有效期后删除
            //下发删除指令到巨龙设备
            String param[] = expiredKey.split("[:]");
            String macAddress = param[param.length-1];
            String guestTel = param[param.length-2];
			Guest guest = new Guest();
			guest.setId(Long.valueOf(Constants.GUEST_PRE+guestTel));
            msgPublishService.publishJVT(guest,macAddress, Instruct.delete_user_face);
        }else if(expiredKey.startsWith(Constants.REDIS_GUEST_EFFECTIVE_TIME_QINLIN)){//园区访客信息在亲邻设备中过了有效期后删除
			//下发删除指令到亲邻设备
			String param[] = expiredKey.split("[:]");
			String guestTel = param[param.length-1];
			QLinUser qLinUser = new QLinUser();
			qLinUser.setUserId(Constants.GUEST_PRE);
			qinLinHttpUtil.deleteUser(qLinUser);
		}
    }
}
