package com.red.circle.wallet.infra.database.cache.service.impl;

import com.red.circle.component.redis.service.RedisService;
import com.red.circle.tool.core.date.LocalDateUtils;
import com.red.circle.tool.core.text.StringUtils;
import com.red.circle.wallet.infra.database.cache.key.WalletBankKey;
import com.red.circle.wallet.infra.database.cache.service.WalletBankCacheService;
import java.util.concurrent.TimeUnit;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 钱包银行卡缓存服务 实现.
 *
 * @author pengliang on 2023/2/21
 */
@Service
@RequiredArgsConstructor
public class WalletBankCacheServiceImpl implements WalletBankCacheService {

  private final RedisService redisService;

  @Override
  public boolean checkMonthWithdrawal(Long userId) {
    return StringUtils.isNotBlank(
        redisService.getString(getMonthWithdrawalKey(userId)));
  }

  @Override
  public void removeMonthWithdrawal(Long userId) {
    redisService.delete(getMonthWithdrawalKey(userId));
  }

  @Override
  public void setMonthWithdrawal(Long userId) {
    redisService.setString(getMonthWithdrawalKey(userId), 1, 31, TimeUnit.DAYS);
  }

  private String getMonthWithdrawalKey(Long userId) {
    return WalletBankKey.WITHDRAWAL.getKey(LocalDateUtils.nowThisMonthToInteger(), userId);
  }

}
