package com.ytpm.monitor;

import com.ytpm.agent.enums.UserStatusEnum;
import com.ytpm.app.model.YtDyzUser;
import com.ytpm.dao.dyz.AppUserMapper;
import com.ytpm.dao.nofeeds.NFUserMapper;
import com.ytpm.dao.qnjz.QNUserMapper;
import com.ytpm.dao.qnmjz.QnmUserMapper;
import com.ytpm.risk.enums.BannedTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
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;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * redis 监听器
 */
@Slf4j(topic = "RedisKeyExpire")
@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {

    @Resource
    private AppUserMapper appUserMapper;
    @Resource
    private QNUserMapper qnUserMapper;
    @Resource
    private NFUserMapper nfUserMapper;
    @Resource
    private QnmUserMapper qnmUserMapper;

    /**
     * Creates new {@link } for {@code __keyevent@*__:expired} messages.
     *
     * @param listenerContainer must not be {@literal null}.
     */
    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }
 
    /**
     * 监听redis过期的 key 进行处理
     */
    @Override
    public void onMessage(Message message, byte[] pattern) {
        String key = String.valueOf(message);
        log.error("App-Service 发现预定风控用户：{}",key);
        if(key.startsWith("lock_")){
            String[] arr = key.split("_");
            changeUserStatus(arr[1], UserStatusEnum.RISK.getCode(),"禁止登录");
        }

        if(key.startsWith("unlock_")){
            String[] arr = key.split("_");
            changeUserStatus(arr[1], UserStatusEnum.NORMAL.getCode(),"解除锁定");
        }
    }

     private void changeUserStatus(@RequestParam("userId")String userId, @RequestParam("userStatus")Integer userStatus,@RequestParam("reason")String reason) {
        YtDyzUser dyzUser = appUserMapper.selectPrimaryKey(userId);
        YtDyzUser newUser = new YtDyzUser();
        newUser.setUserId(userId);
        newUser.setUserStatus(userStatus);
        newUser.setRiskReason(BannedTypeEnum.CHANNEL.getDesc()+reason);
        if(Objects.nonNull(dyzUser)){
            appUserMapper.updateUser(newUser);
            return;
        }
        dyzUser = qnUserMapper.selectPrimaryKey(userId);
        if(Objects.nonNull(dyzUser)){
            qnUserMapper.updateUser(newUser);
            return;
        }
        dyzUser = nfUserMapper.selectPrimaryKey(userId);
        if(Objects.nonNull(dyzUser)){
            nfUserMapper.updateUser(newUser);
            return;
        }
        dyzUser = qnmUserMapper.selectPrimaryKey(userId);
        if(Objects.nonNull(dyzUser)){
            qnmUserMapper.updateUser(newUser);
        }
    }
}