package org.springblade.app.listener;


import io.lettuce.core.RedisClient;
import org.apache.commons.lang.StringUtils;
import org.springblade.estate.feign.IGuestAuthorizeClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {

    private RedisClient redisClient;

    @Autowired
    private IGuestAuthorizeClient guestAuthorizeClient;


    //密码访客授权的缓存名字
    private final static String GUEST_AUTHORIZE_FOR_PASSWORD = "sptg:guest:authorize:password";

    /**
     * Creates new {@link MessageListener} for {@code __keyevent@*__:expired} messages.
     *
     * @param listenerContainer must not be {@literal null}.
     */
    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {
        // 用户做自己的业务处理即可,注意message.toString()可以获取失效的key
        String expiredKey = message.toString();
        if(expiredKey.startsWith(GUEST_AUTHORIZE_FOR_PASSWORD)){
            //下发remove指令到设备
            String param[] = expiredKey.split("[:]");
            Long id = Long.parseLong(param[param.length-1]);
//            GuestAuthorize guestAuthorize = guestAuthorizeFeign.findGuestAuthorizeById(id);
//            guestAuthorize.setEnableType(OperationConstant.ONE);
//            guestAuthorizeFeign.updateGuestAuthorizeEnableType(guestAuthorize);
//            //下发指令
//            DispatchTask dispatchTask = new DispatchTask();
//            dispatchTask.setXqId(guestAuthorize.getResidentialId());
//            dispatchTask.setUnitId(guestAuthorize.getUnitId());
//            dispatchTask.setData(guestAuthorize);
//            Map<String,String> logMap = new HashMap<>();
//            //小区
//            if (!StringUtils.isEmpty(guestAuthorize.getResidentialName())){
//                logMap.put("xqName",guestAuthorize.getResidentialName());
//            }else {
//                logMap.put("xqName",null);
//            }
//            //单元
//            if (!StringUtils.isEmpty(guestAuthorize.getUnitName())){
//                logMap.put("unitName",guestAuthorize.getUnitName());
//            }else {
//                logMap.put("unitName",null);
//            }
//            //房间
//            if (!StringUtils.isEmpty(guestAuthorize.getRoomName())){
//                logMap.put("roomName",guestAuthorize.getRoomName());
//            }else {
//                logMap.put("roomName",null);
//            }
//            //用户名
//            if (!StringUtils.isEmpty(guestAuthorize.getUserName())){
//                logMap.put("userName",guestAuthorize.getUserName());
//            }else {
//                logMap.put("userName",null);
//            }
//            //密码
//            if (null != guestAuthorize.getPassword()){
//                logMap.put("password",guestAuthorize.getPassword().toString());
//            }else {
//                logMap.put("password",null);
//            }
//            //有效期开始时间
//            SimpleDateFormat format = new SimpleDateFormat();
//            if (null != guestAuthorize.getBeginDate()){
//                logMap.put("beginTime",format.format(guestAuthorize.getBeginDate()));
//            }else {
//                logMap.put("beginTime",null);
//            }
//            //有效期结束时间
//            if (null != guestAuthorize.getEndDate()){
//                logMap.put("endTime",format.format(guestAuthorize.getEndDate()));
//            }else {
//                logMap.put("endTime",null);
//            }
//            deviceFeign.removeOpenDoorPwd(dispatchTask,logMap);
        }
    }
}
