import { Injectable, Logger } from '@nestjs/common';
import { Cron } from '@nestjs/schedule';
import { SettlementService } from '@/modules/shared/finance/settlement.service';

@Injectable()
export class SettlementSchedule {
  private readonly logger = new Logger(SettlementSchedule.name);

  constructor(
    private readonly settlementService: SettlementService,
  ) {}

  // 每天凌晨2点执行自动结算
  @Cron('0 2 * * *')
  async autoSettle() {
    try {
      this.logger.log('开始执行自动结算任务');
      // 调用 SettlementService 的周结算方法
      await this.settlementService.handleWeeklySettlement();
      this.logger.log('自动结算任务执行完成');
    } catch (error) {
      this.logger.error('自动结算任务执行失败', error.stack);
    }
  }
}
