Files
Django/config/management/commands/restore_finance_stats.py

65 lines
2.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
一键恢复财务「每日收入」统计repair_wechat_income 搞乱后执行)。
只重建:
- daily_income_stat每日收入统计表财务看板读这个
- platform_income_log支付幂等日志
绝不碰:
- daily_payout_stat每日支出repair 没动过)
- szjilu收支记录表已废弃
- 订单 / 充值业务表
用法:
预览python manage.py restore_finance_stats
执行python manage.py restore_finance_stats --yes
"""
from datetime import date
from django.core.management.base import BaseCommand
from jituan.services.szjilu_accounting import restore_finance_daily_stats
class Command(BaseCommand):
help = '从订单/充值重建 daily_income_stat恢复 repair 搞乱的每日收入)'
def add_arguments(self, parser):
parser.add_argument('--since-year', type=int, default=2020, help='从哪年开始重算')
parser.add_argument('--until-year', type=int, default=0, help='到哪年,默认今年')
parser.add_argument('--yes', action='store_true', help='确认写入数据库')
def handle(self, *args, **options):
until_year = options['until_year'] or date.today().year
since_year = options['since_year'] or 2020
dry_run = not options['yes']
if dry_run:
self.stdout.write(self.style.WARNING(
'【预览】未改数据库。下面「恢复后」应是正确每日收入合计。\n'
))
else:
self.stdout.write(self.style.WARNING(
'【执行】正在从订单/充值重建 daily_income_stat...\n'
))
r = restore_finance_daily_stats(since_year, until_year, dry_run=dry_run)
self.stdout.write(f'范围: {r["since_year"]} ~ {r["until_year"]}')
self.stdout.write(f' 支付笔数: {r["after_count"]}')
self.stdout.write(f' 恢复前 daily 合计: {r["before_daily_total"]:.2f}')
self.stdout.write(self.style.SUCCESS(
f' 恢复后 daily 合计: {r.get("after_daily_total", r["after_total"]):.2f}'
))
self.stdout.write(f' 其中今日收入: {r["today_amount"]:.2f} 元 / {r["today_count"]}')
if dry_run:
self.stdout.write(self.style.WARNING(
'\n数字对的话执行:\n'
' python manage.py restore_finance_stats --yes'
))
else:
self.stdout.write(self.style.SUCCESS(
'\n完成。daily_payout_stat 未动。请 push 代码后刷新财务页。'
))