package com.red.circle.other.infra.database.cache.service.user.impl;

import com.red.circle.component.redis.service.RedisService;
import com.red.circle.other.infra.database.cache.key.user.CountAnchorKey;
import com.red.circle.other.infra.database.cache.service.user.CountUserActiveIndexService;
import com.red.circle.tool.core.date.DateFormatConstant;
import com.red.circle.tool.core.date.LocalDateTimeUtils;
import java.util.concurrent.TimeUnit;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 统计运营活跃指标(用户活跃).
 *
 * @author pengliang on 2022/3/29
 */
@Service
@RequiredArgsConstructor
public class CountUserActiveIndexServiceImpl implements CountUserActiveIndexService {

  private final RedisService redisService;

  @Override
  public void countAnchorActiveUser(String sysOrigin, Long account) {
    redisService.setBitmaps(getCountKey(sysOrigin), account, 2,
        TimeUnit.DAYS);
  }

  @Override
  public void countAnchorActiveUser(Long account) {
    redisService.setBitmaps(getCountAllKey(), account, 2,
        TimeUnit.DAYS);
  }

  @Override
  public Long getActiveAnchorCount(String sysOrigin, String date) {
    return redisService.getBitmapsCount(
        CountAnchorKey.ACTIVE_COUNT.getKey(sysOrigin, date));
  }

  @Override
  public Long getActiveAnchorCount(String date) {
    return redisService.getBitmapsCount(CountAnchorKey.ACTIVE_COUNT.getKey(date));
  }

  @Override
  public void countOrdinaryActiveUser(String sysOrigin, Long account) {
    redisService.setBitmaps(getCountKey(sysOrigin), account, 2, TimeUnit.DAYS);
  }

  @Override
  public void countOrdinaryActiveUser(Long account) {
    redisService.setBitmaps(getCountAllKey(), account, 2, TimeUnit.DAYS);
  }

  @Override
  public Long getActiveOrdinaryCount(String sysOrigin, String date) {
    return redisService.getBitmapsCount(
        CountAnchorKey.ACTIVE_COUNT.getKey(sysOrigin, date));
  }

  @Override
  public Long getActiveOrdinaryCount(String date) {
    return redisService.getBitmapsCount(CountAnchorKey.ACTIVE_COUNT.getKey(date));
  }

  private String getCountAllKey() {
    return CountAnchorKey.ACTIVE_COUNT.getKey(
        LocalDateTimeUtils.nowFormat(DateFormatConstant.yyyyMMdd));
  }

  private String getCountKey(String sysOrigin) {
    return CountAnchorKey.ACTIVE_COUNT.getKey(
        LocalDateTimeUtils.nowFormat(DateFormatConstant.yyyyMMdd), sysOrigin);
  }

}
