feat: handover_revert 并发优化

This commit is contained in:
caoqianming 2026-01-12 10:21:15 +08:00
parent 2ecaeadff7
commit f9eee5a523
2 changed files with 31 additions and 18 deletions

View File

@ -14,7 +14,7 @@ from django.db.models import Count
from django.db import transaction from django.db import transaction
from django.db.models import Max from django.db.models import Max
import re import re
from django.db.models import Q from django.db.models import Q, F
import django.utils.timezone as timezone import django.utils.timezone as timezone
from apps.utils.sql import query_all_dict from apps.utils.sql import query_all_dict
import logging import logging
@ -163,6 +163,30 @@ class WMaterial(CommonBDModel):
), ),
state__in=[WMaterial.WM_OK, WMaterial.WM_REPAIR] state__in=[WMaterial.WM_OK, WMaterial.WM_REPAIR]
) )
@classmethod
def increase(cls, wm_id: str, user:User, count, count_eweight=None):
updates = {}
if count:
updates['count'] = F('count') + count
if count_eweight:
updates['count_eweight'] = count_eweight
if not updates:
return 0
updates["update_by"] = user
updates['update_time'] = timezone.now()
return cls.objects.filter(id=wm_id).update(**updates)
@classmethod
def decrease(cls, wm_id: str, user:User, count):
if not count:
return 0
updated = cls.objects.filter(id=wm_id, count__gte= count).update(
count=F('count') - count, update_by=user, update_time=timezone.now())
if updated == 0:
batch = WMaterial.objects.get(id=wm_id).batch
raise ParseError(f'{batch}_库存不足无法完成扣减')
return updated
class Fmlog(CommonADModel): class Fmlog(CommonADModel):
"""TN: 父级生产日志 """TN: 父级生产日志

View File

@ -776,14 +776,7 @@ def handover_submit(handover:Handover, user: User, now: Union[datetime.datetime,
batch = wm_from.batch batch = wm_from.batch
batches.append(batch) batches.append(batch)
updated = ( WMaterial.decrease(wm_id=wm_from.id, user=user, count=xcount)
WMaterial.objects
.filter(id=wm_from.id, count__gte=xcount)
.update(count=F('count') - xcount)
)
if updated == 0:
raise ParseError(f'{wm_from.batch} 车间库存不足!')
if need_add: if need_add:
# 开始变动 # 开始变动
@ -907,9 +900,7 @@ def handover_submit(handover:Handover, user: User, now: Union[datetime.datetime,
else: else:
raise ParseError("不支持该交接类型") raise ParseError("不支持该交接类型")
WMaterial.objects.filter(id=wm_to.id).update(count=F('count') + xcount) WMaterial.increase(wm_id=wm_to.id, user=user,count=xcount, count_eweight=handover.count_eweight if handover.count_eweight else None)
if handover.count_eweight:
WMaterial.objects.filter(id=wm_to.id).update(count_eweight=handover.count_eweight)
handover_or_b.wm_to = wm_to handover_or_b.wm_to = wm_to
handover_or_b.save() handover_or_b.save()
if material.tracking == Material.MA_TRACKING_SINGLE: if material.tracking == Material.MA_TRACKING_SINGLE:
@ -937,6 +928,7 @@ def handover_submit(handover:Handover, user: User, now: Union[datetime.datetime,
ana_batch_thread(xbatchs=batches) ana_batch_thread(xbatchs=batches)
def handover_revert(handover:Handover, handler:User=None): def handover_revert(handover:Handover, handler:User=None):
handover = Handover.objects.select_for_update().get(id=handover.id)
if handover.submit_time is None: if handover.submit_time is None:
raise ParseError('该交接单未提交!') raise ParseError('该交接单未提交!')
ticket:Ticket = handover.ticket ticket:Ticket = handover.ticket
@ -968,16 +960,13 @@ def handover_revert(handover:Handover, handler:User=None):
# 此时是自己交给自己,不需要做任何操作 # 此时是自己交给自己,不需要做任何操作
pass pass
else: else:
wm.count = wm.count + item.count WMaterial.increase(wm_id=wm.id, user=handler, count=item.count)
wm.save() WMaterial.decrease(wm_id=wm_to.id, user=handler, count=item.count)
wm_to.count = wm_to.count - item.count
if wm_to.count < 0:
raise ParseError('库存不足无法撤回!')
wm_to.save()
if material.tracking == Material.MA_TRACKING_SINGLE: if material.tracking == Material.MA_TRACKING_SINGLE:
handoverbws = Handoverbw.objects.filter(handoverb=item) handoverbws = Handoverbw.objects.filter(handoverb=item)
if handoverbws.count() != item.count: if handoverbws.count() != item.count:
raise ParseError("交接与明细数量不一致,操作失败") raise ParseError("交接与明细数量不一致,操作失败")
wm = WMaterial.objects.get(id=wm.id)
for item in handoverbws: for item in handoverbws:
wpr:Wpr = item.wpr wpr:Wpr = item.wpr
Wpr.change_or_new(wpr=wpr, wm=wm, old_wm=wpr.wm, old_mb=wpr.mb, add_version=False) Wpr.change_or_new(wpr=wpr, wm=wm, old_wm=wpr.wm, old_mb=wpr.mb, add_version=False)