"""只修正今日 daily_income_stat.total_count（不动金额、不动其它天）。"""
from datetime import date

from django.core.management.base import BaseCommand

from jituan.services.szjilu_accounting import fix_income_stat_count_for_date


class Command(BaseCommand):
    help = '修正今日收入笔数（repair 会把 total_count 虚增，本命令只改笔数列）'

    def add_arguments(self, parser):
        parser.add_argument('--date', type=str, default='', help='YYYY-MM-DD，默认今天')
        parser.add_argument('--yes', action='store_true')

    def handle(self, *args, **options):
        stat_date = date.fromisoformat(options['date']) if options['date'] else date.today()
        dry_run = not options['yes']
        r = fix_income_stat_count_for_date(stat_date, dry_run=dry_run)
        self.stdout.write(f'日期: {r["date"]}')
        self.stdout.write(f'  表内笔数合计(错): {r["before_count"]}')
        self.stdout.write(self.style.SUCCESS(f'  修正后笔数:       {r["after_count"]}'))
        if r['clubs']:
            for cid, cnt in r['clubs'].items():
                self.stdout.write(f'    club={cid}: {cnt} 笔')
        if dry_run:
            self.stdout.write(self.style.WARNING('\n确认: python manage.py fix_today_income_count --yes'))
        else:
            self.stdout.write(self.style.SUCCESS('\n已写入 daily_income_stat.total_count，请刷新财务页。'))
