forked from erp-dev/erp
fix: added merchant_id to printing_order and plate_order
This commit is contained in:
0
printing/management/__init__.py
Normal file
0
printing/management/__init__.py
Normal file
0
printing/management/commands/__init__.py
Normal file
0
printing/management/commands/__init__.py
Normal file
79
printing/management/commands/backfill_merchant.py
Normal file
79
printing/management/commands/backfill_merchant.py
Normal file
@@ -0,0 +1,79 @@
|
||||
"""
|
||||
补录 merchant_id 字段的 management command
|
||||
|
||||
用于为历史数据补充 merchant_id 字段值。
|
||||
"""
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
from django.db import transaction
|
||||
|
||||
from basic_info.models import Merchant
|
||||
from printing.models import PlateOrder, PrintingOrder, PrintingJob
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = '为 PlateOrder、PrintingOrder、PrintingJob 补录 merchant_id 字段'
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument(
|
||||
'merchant_id',
|
||||
type=int,
|
||||
help='要设置的商户ID'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--dry-run',
|
||||
action='store_true',
|
||||
help='仅显示将要更新的记录数,不实际执行'
|
||||
)
|
||||
|
||||
def handle(self, *args, **options):
|
||||
merchant_id = options['merchant_id']
|
||||
dry_run = options['dry_run']
|
||||
|
||||
# 验证商户是否存在
|
||||
try:
|
||||
merchant = Merchant.objects.get(id=merchant_id)
|
||||
except Merchant.DoesNotExist:
|
||||
raise CommandError(f'商户 ID {merchant_id} 不存在')
|
||||
|
||||
self.stdout.write(f'目标商户: {merchant.name} (ID: {merchant.id})')
|
||||
self.stdout.write('')
|
||||
|
||||
# 统计需要更新的记录
|
||||
plate_orders_count = PlateOrder.objects.filter(merchant__isnull=True).count()
|
||||
printing_orders_count = PrintingOrder.objects.filter(merchant__isnull=True).count()
|
||||
printing_jobs_count = PrintingJob.objects.filter(merchant__isnull=True).count()
|
||||
|
||||
self.stdout.write(f'待更新记录:')
|
||||
self.stdout.write(f' - PlateOrder: {plate_orders_count} 条')
|
||||
self.stdout.write(f' - PrintingOrder: {printing_orders_count} 条')
|
||||
self.stdout.write(f' - PrintingJob: {printing_jobs_count} 条')
|
||||
self.stdout.write('')
|
||||
|
||||
total = plate_orders_count + printing_orders_count + printing_jobs_count
|
||||
if total == 0:
|
||||
self.stdout.write(self.style.SUCCESS('没有需要更新的记录'))
|
||||
return
|
||||
|
||||
if dry_run:
|
||||
self.stdout.write(self.style.WARNING('--dry-run 模式,未执行实际更新'))
|
||||
return
|
||||
|
||||
# 执行更新
|
||||
with transaction.atomic():
|
||||
updated_plate_orders = PlateOrder.objects.filter(
|
||||
merchant__isnull=True
|
||||
).update(merchant=merchant)
|
||||
|
||||
updated_printing_orders = PrintingOrder.objects.filter(
|
||||
merchant__isnull=True
|
||||
).update(merchant=merchant)
|
||||
|
||||
updated_printing_jobs = PrintingJob.objects.filter(
|
||||
merchant__isnull=True
|
||||
).update(merchant=merchant)
|
||||
|
||||
self.stdout.write('')
|
||||
self.stdout.write(self.style.SUCCESS(f'更新完成:'))
|
||||
self.stdout.write(self.style.SUCCESS(f' - PlateOrder: {updated_plate_orders} 条'))
|
||||
self.stdout.write(self.style.SUCCESS(f' - PrintingOrder: {updated_printing_orders} 条'))
|
||||
self.stdout.write(self.style.SUCCESS(f' - PrintingJob: {updated_printing_jobs} 条'))
|
||||
30
printing/migrations/0029_add_merchant_to_models.py
Normal file
30
printing/migrations/0029_add_merchant_to_models.py
Normal file
@@ -0,0 +1,30 @@
|
||||
# Generated by Django 5.2.8 on 2026-01-14 05:41
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('basic_info', '0024_frontend_page_and_visible_pages'),
|
||||
('printing', '0028_batch_advance_record_only_parameters'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='plateorder',
|
||||
name='merchant',
|
||||
field=models.ForeignKey(blank=True, help_text='所属商户,可空以兼容历史数据', null=True, on_delete=django.db.models.deletion.PROTECT, related_name='plate_orders', to='basic_info.merchant', verbose_name='商户'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='printingjob',
|
||||
name='merchant',
|
||||
field=models.ForeignKey(blank=True, help_text='所属商户,可空以兼容历史数据', null=True, on_delete=django.db.models.deletion.PROTECT, related_name='printing_jobs', to='basic_info.merchant', verbose_name='商户'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='printingorder',
|
||||
name='merchant',
|
||||
field=models.ForeignKey(blank=True, help_text='所属商户,可空以兼容历史数据', null=True, on_delete=django.db.models.deletion.PROTECT, related_name='printing_orders', to='basic_info.merchant', verbose_name='商户'),
|
||||
),
|
||||
]
|
||||
@@ -11,6 +11,17 @@ from stateflow import models as stateflow_models
|
||||
class PlateOrder(ModelBase):
|
||||
"""开版管理订单"""
|
||||
|
||||
# 商户关联(多租户)
|
||||
merchant = models.ForeignKey(
|
||||
basic_models.Merchant,
|
||||
on_delete=models.PROTECT,
|
||||
null=True,
|
||||
blank=True,
|
||||
related_name='plate_orders',
|
||||
verbose_name='商户',
|
||||
help_text='所属商户,可空以兼容历史数据'
|
||||
)
|
||||
|
||||
# 自动编号相关
|
||||
design_code = models.CharField(max_length=50, blank=True, null=True, verbose_name='设计编号')
|
||||
original_id = models.PositiveBigIntegerField(
|
||||
@@ -245,6 +256,18 @@ class PlateOrder(ModelBase):
|
||||
|
||||
class PrintingOrder(ModelBase):
|
||||
"""PrintingOrder model representing a printing order."""
|
||||
|
||||
# 商户关联(多租户)
|
||||
merchant = models.ForeignKey(
|
||||
basic_models.Merchant,
|
||||
on_delete=models.PROTECT,
|
||||
null=True,
|
||||
blank=True,
|
||||
related_name='printing_orders',
|
||||
verbose_name='商户',
|
||||
help_text='所属商户,可空以兼容历史数据'
|
||||
)
|
||||
|
||||
customer = models.ForeignKey(
|
||||
basic_models.Customer,
|
||||
on_delete=models.PROTECT,
|
||||
@@ -327,6 +350,18 @@ class PrintingJobWorkStateEnum(models.IntegerChoices):
|
||||
|
||||
class PrintingJob(ModelBase):
|
||||
"""PrintingJob model representing a printing job associated with an order."""
|
||||
|
||||
# 商户关联(多租户)
|
||||
merchant = models.ForeignKey(
|
||||
basic_models.Merchant,
|
||||
on_delete=models.PROTECT,
|
||||
null=True,
|
||||
blank=True,
|
||||
related_name='printing_jobs',
|
||||
verbose_name='商户',
|
||||
help_text='所属商户,可空以兼容历史数据'
|
||||
)
|
||||
|
||||
printing_order = models.ForeignKey(
|
||||
PrintingOrder,
|
||||
on_delete=models.CASCADE,
|
||||
|
||||
Reference in New Issue
Block a user