package com.red.circle.wallet.app.scheduler;

import com.red.circle.component.redis.annotation.TaskCacheLock;
import com.red.circle.tool.core.date.DateFormatConstant;
import com.red.circle.tool.core.date.DateUtils;
import com.red.circle.wallet.infra.database.rds.service.WalletGoldAssetRecordService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 清理历史流水， 数据.
 *
 * @author pengliang on 2023/11/10
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class ClearHistoryDataTask {

  private final WalletGoldAssetRecordService walletGoldAssetRecordService;

  /**
   * 2分钟检查一次.
   */
//  @Scheduled(cron = "0 */2 * * * ?")
//  @TaskCacheLock(key = "CLEAR_HISTORY_DATA_TASK", expireSecond = 60 * 2)
  public void execute() {
    Integer shardingMonth = Integer.parseInt(
        DateUtils.formatUtc(DateUtils.nowMinusDays(62), DateFormatConstant.MM));
    walletGoldAssetRecordService.deleteShardingData(shardingMonth, 50000);
  }


  /**
   * 1天检查一次.
   */
//  @Scheduled(cron = "0 0 0 1/1 * ?")
//  @TaskCacheLock(key = "CLEAR_OPTIMIZE_TABLE", expireSecond = 60 * 2)
  public void clearOptimizeTable() {
    Integer shardingMonth = Integer.parseInt(
        DateUtils.formatUtc(DateUtils.nowMinusDays(30 * 3 + 2), DateFormatConstant.MM));

    if (walletGoldAssetRecordService.existsData(shardingMonth)) {
      return;
    }

    walletGoldAssetRecordService.executeOptimizeTable(shardingMonth);
  }

}
