package com.sptg.gateway.receiver;

import com.sptg.common.security.service.SecurityServiceInterface;
import com.sptg.permissions.api.entity.Account;
import com.sptg.permissions.api.stream.AccountDisableReceiverInterface;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * title: AccountDisableReceiver
 * description: 账号禁用消费者
 *
 *                  
 *            
 */

@Component
@Slf4j
@RequiredArgsConstructor
@EnableBinding(value = { AccountDisableReceiverInterface.class})
public class AccountDisableReceiver {

    private final SecurityServiceInterface<Account> securityServiceInterface;

    @StreamListener(value = AccountDisableReceiverInterface.INPUT)
    public void input(Long accountId){
        if (null == accountId){
            return;
        }
        //  获取需要删除的账号缓存
        List<Account> onLineUserList = this.securityServiceInterface.getOnLineUserList();
        for (Account onLineUser : onLineUserList) {
            if (onLineUser.getId().equals(accountId)) {
                //  用户已经被删除或不可用，强制退出
                this.securityServiceInterface.removeUserToken(onLineUser.getLoginToken());
                break;
            }
        }
    }

}
