From f22ba2752f2ef16fb47bd643dd576700697b1dac Mon Sep 17 00:00:00 2001 From: caoqianming Date: Tue, 22 Apr 2025 14:29:33 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=89=B9=E6=AC=A1=E5=85=B3=E7=B3=BB?= =?UTF-8?q?=E9=93=BE=E6=97=B6=E5=88=9B=E5=BB=BA=E6=96=B0=E6=89=B9=E6=AC=A1?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E4=BD=BF=E7=94=A8=E5=B7=B2=E6=9C=89=E6=89=B9?= =?UTF-8?q?=E6=AC=A1=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/wpm/models.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/apps/wpm/models.py b/apps/wpm/models.py index e1701f16..85e042b4 100644 --- a/apps/wpm/models.py +++ b/apps/wpm/models.py @@ -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):