Files
ucvl-zonghengjia-open-source/backend/control/management/commands/prepare_monthly_1024g.py
2026-10-11 23:23:20 +00:00

33 lines
1.9 KiB
Python

from decimal import Decimal
from django.core.management.base import BaseCommand, CommandError
from django.db import transaction
from control.models import Plan
class Command(BaseCommand):
help = "准备1024G/月、8元套餐;--list 上架展示与申请,内部员工免费;不启用收款,不修改现有订阅。"
def add_arguments(self, parser):
parser.add_argument("--list", action="store_true", dest="list_plan", help="开放展示和申请;外部付费权限仍关闭。")
def handle(self, *args, **options):
expected = dict(name="1024G 月度套餐", audience=Plan.Audience.ALL,
employee_benefit=False, price_cny=Decimal("8.00"), traffic_limit_gb=1024,
billing_months=1, duration_days=0, device_limit=3,
online_payment_required=True)
defaults = dict(expected, active=options["list_plan"],
description="外部人员8元/月、1024G;内部员工免费。仅计算代理流量。按开通日起一个月,到期不自动扣款。暂未开放付款,仅登记申请;内部员工核实身份并审核后免费开通。")
with transaction.atomic():
plan, created = Plan.objects.get_or_create(slug="monthly-1024g", defaults=defaults)
plan = Plan.objects.select_for_update().get(pk=plan.pk)
if not created and any(getattr(plan, field) != value for field, value in expected.items()):
raise CommandError("已有同标识套餐但配置不同,未覆盖;请先人工核对。")
if options["list_plan"] and not plan.active:
plan.active = True
plan.full_clean()
plan.save(update_fields=["active", "updated_at"])
state = "已上架展示与申请;暂未开放付款和外部付费权限" if plan.active else "草稿已准备,尚未上架"
self.stdout.write(f"1024G / 月 / 8元套餐:{state}。内部员工免费,现有订阅不变。")