MemberDailyReport.php 1.0 KB

123456789101112131415161718192021222324252627282930
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Services\MemberDailyReportService;
  4. use Carbon\Carbon;
  5. use Illuminate\Console\Command;
  6. class MemberDailyReport extends Command
  7. {
  8. protected $signature = 'report:member-daily {date? : Y-m-d,默认今天} {--days=1 : 从 date 往前共重算几天}';
  9. protected $description = '生成会员每日盈亏报表';
  10. public function handle(MemberDailyReportService $reports): int
  11. {
  12. $date = (string) ($this->argument('date') ?: now()->toDateString());
  13. $parsed = Carbon::createFromFormat('!Y-m-d', $date);
  14. if (!$parsed || $parsed->format('Y-m-d') !== $date) {
  15. $this->error('日期必须是有效的 Y-m-d');
  16. return self::FAILURE;
  17. }
  18. $days = max(1, min(31, (int) $this->option('days')));
  19. for ($i = 0; $i < $days; $i++) {
  20. $day = $parsed->copy()->subDays($i)->toDateString();
  21. $this->info($day . ':' . $reports->refresh($day) . ' 行');
  22. }
  23. return self::SUCCESS;
  24. }
  25. }