feat: 批次关系链时创建新批次支持使用已有批次号

This commit is contained in:
caoqianming 2025-04-22 14:29:33 +08:00
parent ea00a38406
commit f22ba2752f
1 changed files with 12 additions and 6 deletions

View File

@ -12,6 +12,7 @@ from django.utils.translation import gettext_lazy as _
from rest_framework.exceptions import ParseError
from django.db.models import Count
from django.db import transaction
from django.db.models import Max
# Create your models here.
class SfLog(CommonADModel):
@ -599,7 +600,8 @@ class BatchSt(BaseModel):
"""
TN: 产品批次统计
"""
batch = models.TextField("批次号", unique=True, db_index=True)
batch = models.TextField("批次号", db_index=True)
version = models.IntegerField("版本号", default=1, db_index=True)
last_time = models.DateTimeField("最后操作时间", null=True, blank=True)
data = models.JSONField("数据", default=list, blank=True)
material_start = models.ForeignKey(Material, verbose_name="起始物料", on_delete=models.SET_NULL, null=True, blank=True)
@ -607,22 +609,26 @@ class BatchSt(BaseModel):
handover = models.ForeignKey(Handover, verbose_name='由何交接记录创建', on_delete=models.CASCADE, null=True, blank=True)
mlog = models.ForeignKey(Mlog, verbose_name='由何日志创建', on_delete=models.CASCADE, null=True, blank=True)
class Meta:
unique_together = [("batch", "version")]
@classmethod
def g_create(cls, batch:str, mio=None, handover=None, mlog=None, material_start=None):
"""
创建新的批次
"""
if mio is None and handover is None and mlog is None and material_start is None:
if mio is None and handover is None and mlog is None:
return cls.objects.get_or_create(batch=batch)
else:
latest_version = 0
# 带有来源的批次获取,需检查批次号是否可用
if cls.objects.filter(batch=batch).exists():
raise ParseError(f"{batch}-该批次号不可用")
if cls.objects.filter(batch=batch, version=0).exists():
latest_version = BatchSt.objects.filter(batch=batch).aggregate(Max("version"))["version__max"]
if mio is None and handover is None and mlog is None:
raise ParseError("mio or handover or mlog must be provided")
ins = cls.objects.create(batch=batch, mio=mio, handover=handover, mlog=mlog, material_start=material_start)
ins = cls.objects.create(batch=batch, mio=mio, handover=handover, mlog=mlog, material_start=material_start, version=latest_version+1)
return ins, True
@classmethod
@transaction.atomic
def init_dag(cls, batch:str):