fix: batchlog创建时避免回环

This commit is contained in:
caoqianming 2025-05-26 15:26:22 +08:00
parent e47464805e
commit a4f2b6eb55
2 changed files with 15 additions and 7 deletions

View File

@ -621,18 +621,20 @@ class BatchSt(BaseModel):
unique_together = [("batch", "version")]
@classmethod
def g_create(cls, batch:str, mio=None, handover=None, mlog=None, material_start=None, reuse_node=False):
def g_create(cls, batch:str, mio=None, handover=None, mlog=None, material_start=None, reuse_node=False, exclude_batchst_ids=[]):
"""
创建新的批次
"""
if mio is None and handover is None and mlog is None:
try:
node = cls.objects.get(batch=batch)
node = cls.objects.exclude(id__in=exclude_batchst_ids).get(batch=batch)
except cls.DoesNotExist:
return cls.objects.create(batch=batch), True
except cls.MultipleObjectsReturned:
# 兼容性处理
node = cls.objects.filter(batch=batch).order_by('-version').first()
node = cls.objects.filter(batch=batch).exclude(id__in=exclude_batchst_ids).order_by('-version').first()
if node is None:
raise ParseError(f"{node.batch}-该批次号本次操作不可引用")
return node, False
else:
version = 1

View File

@ -175,14 +175,17 @@ def mlog_submit(mlog: Mlog, user: User, now: Union[datetime.datetime, None]):
can_matoutIds = process.get_canout_mat_ids()
# 建立关系链
m_outs = Mlogb.objects.filter(mlog=mlog, material_out__isnull=False)
exclude_batchst_ids = []
for item in m_outs:
if item.mlogb_from and item.batch != item.mlogb_from.batch:
source, _ = BatchSt.g_create(batch=item.mlogb_from.batch)
source, _ = BatchSt.g_create(batch=item.mlogb_from.batch, exclude_batchst_ids=exclude_batchst_ids)
target, _ = BatchSt.g_create(batch=item.batch, mlog=mlog, material_start=item.material_out)
exclude_batchst_ids.append(target.id)
BatchLog.g_create(source= source, target=target, mlog=mlog)
if item.mlogbw_from and item.batch != item.mlogbw_from.mlogb.batch:
source, _ = BatchSt.g_create(batch=item.mlogbw_from.mlogb.batch)
source, _ = BatchSt.g_create(batch=item.mlogbw_from.mlogb.batch, exclude_batchst_ids=exclude_batchst_ids)
target, _ = BatchSt.g_create(batch=item.batch, mlog=mlog, material_start=item.material_out)
exclude_batchst_ids.append(target.id)
BatchLog.g_create(source=source, target=target, mlog=mlog)
if material_in or is_fix: # 需要进行车间库存管理
@ -746,6 +749,7 @@ def handover_submit(handover:Handover, user: User, now: Union[datetime.datetime,
raise ParseError("合并批次时请提供新批次号")
new_target = None
exclude_batchst_ids = []
mids = []
for item in handoverb_list:
wmId, xcount, handover_or_b = item
@ -756,17 +760,19 @@ def handover_submit(handover:Handover, user: User, now: Union[datetime.datetime,
# 合并为新批
if mtype == Handover.H_MERGE:
source, _ = BatchSt.g_create(batch=wm_from.batch)
source, _ = BatchSt.g_create(batch=wm_from.batch, exclude_batchst_ids=exclude_batchst_ids)
batch = new_batch
if new_target is None:
new_target, _ = BatchSt.g_create(batch=batch, handover=handover, material_start=material)
exclude_batchst_ids.append(new_target.id)
BatchLog.g_create(source=source, target=new_target, handover=handover, relation_type="merge")
elif mtype == Handover.H_DIV:
if handover.wm is None:
raise ParseError('拆批请选择车间库存')
source, _ = BatchSt.g_create(batch=handover.wm.batch)
source, _ = BatchSt.g_create(batch=handover.wm.batch, exclude_batchst_ids=exclude_batchst_ids)
batch = handover_or_b.batch
target, _ = BatchSt.g_create(batch=batch, handover=handover, material_start=material)
exclude_batchst_ids.append(target.id)
BatchLog.g_create(source=source, target=target, handover=handover, relation_type="split")
else:
batch = wm_from.batch