feat: inm和wpm关于事务处理的修改
This commit is contained in:
parent
9bf44c4211
commit
7a82844842
|
@ -39,7 +39,6 @@ def correct_mb_count_notok():
|
|||
count_notok = mi.count_n_zw + mi.count_n_tw + mi.count_n_qp + mi.count_n_wq + mi.count_n_dl + mi.count_n_pb + mi.count_n_dxt + mi.count_n_js + mi.count_n_qx + mi.count_n_zz + mi.count_n_ysq + mi.count_n_hs + mi.count_n_b + mi.count_n_qt
|
||||
# 先处理库存
|
||||
try:
|
||||
with transaction.atomic():
|
||||
MIOItem.objects.filter(id=mi.id).update(count_notok=count_notok)
|
||||
InmService.update_mb_after_test(mi)
|
||||
except ParseError as e:
|
||||
|
|
|
@ -27,6 +27,7 @@ from apps.qm.serializers import FtestProcessSerializer
|
|||
from apps.mtm.models import Material
|
||||
from drf_yasg.utils import swagger_auto_schema
|
||||
from drf_yasg import openapi
|
||||
from django.db import connection
|
||||
|
||||
|
||||
# Create your views here.
|
||||
|
@ -151,6 +152,15 @@ class MIOViewSet(CustomModelViewSet):
|
|||
'item_mio__a_mioitem__batch']
|
||||
data_filter = True
|
||||
|
||||
@classmethod
|
||||
def lock_and_check_can_update(cls, mio:MIO):
|
||||
if not connection.in_atomic_block:
|
||||
raise ParseError("请在事务中调用该方法")
|
||||
mio:MIO = MIO.objects.select_for_update().get(id=mio.id)
|
||||
if mio.submit_time is not None:
|
||||
raise ParseError("该记录已提交无法更改")
|
||||
return mio
|
||||
|
||||
def add_info_for_list(self, data):
|
||||
# 获取检验状态
|
||||
mio_dict = {}
|
||||
|
@ -195,28 +205,29 @@ class MIOViewSet(CustomModelViewSet):
|
|||
return super().perform_destroy(instance)
|
||||
|
||||
@action(methods=['post'], detail=True, perms_map={'post': 'mio.submit'}, serializer_class=serializers.Serializer)
|
||||
@transaction.atomic
|
||||
def submit(self, request, *args, **kwargs):
|
||||
"""提交
|
||||
|
||||
提交
|
||||
"""
|
||||
ins = self.get_object()
|
||||
ins:MIO = self.get_object()
|
||||
if ins.inout_date is None:
|
||||
raise ParseError('出入库日期未填写')
|
||||
if ins.state != MIO.MIO_CREATE:
|
||||
raise ParseError('记录状态异常')
|
||||
with transaction.atomic():
|
||||
now = timezone.now()
|
||||
updated_count = MIO.objects.filter(id=ins.id, submit_time__isnull=True).update(
|
||||
submit_time=now, update_time=now, state=MIO.MIO_SUBMITED, submit_user=request.user, update_by=request.user)
|
||||
if updated_count == 1:
|
||||
ins.submit_user = request.user
|
||||
ins.submit_time = now
|
||||
ins.update_by = request.user
|
||||
ins.state = MIO.MIO_SUBMITED
|
||||
ins.save()
|
||||
InmService.update_inm(ins)
|
||||
else:
|
||||
raise ParseError('记录正在处理中,请稍后再试')
|
||||
InmService.update_material_count(ins)
|
||||
return Response(MIOListSerializer(instance=ins).data)
|
||||
|
||||
@action(methods=['post'], detail=True, perms_map={'post': 'mio.submit'}, serializer_class=serializers.Serializer)
|
||||
@transaction.atomic
|
||||
def revert(self, request, *args, **kwargs):
|
||||
"""撤回
|
||||
|
||||
|
@ -228,13 +239,12 @@ class MIOViewSet(CustomModelViewSet):
|
|||
raise ParseError('记录状态异常')
|
||||
if ins.submit_user != user:
|
||||
raise ParseError('非提交人不可撤回')
|
||||
with transaction.atomic():
|
||||
updated_count = MIO.objects.filter(id=ins.id, submit_time__isnull=False).update(
|
||||
submit_time=None, update_time=timezone.now(), state=MIO.MIO_CREATE, submit_user=None, update_by=request.user)
|
||||
if updated_count == 1:
|
||||
ins.submit_user = None
|
||||
ins.update_by = user
|
||||
ins.state = MIO.MIO_CREATE
|
||||
ins.submit_time = None
|
||||
ins.save()
|
||||
InmService.update_inm(ins, is_reverse=True)
|
||||
else:
|
||||
raise ParseError('记录正在处理中,请稍后再试')
|
||||
InmService.update_material_count(ins)
|
||||
return Response()
|
||||
|
||||
|
@ -356,9 +366,12 @@ class MIOItemViewSet(CustomListModelMixin, BulkCreateModelMixin, BulkDestroyMode
|
|||
def list(self, request, *args, **kwargs):
|
||||
return super().list(request, *args, **kwargs)
|
||||
|
||||
def perform_create(self, serializer):
|
||||
serializer.validated_data["mio"] = MIOViewSet.lock_and_check_can_update(serializer.validated_data['mio'])
|
||||
return super().perform_create(serializer)
|
||||
|
||||
def perform_destroy(self, instance):
|
||||
if instance.mio.state != MIO.MIO_CREATE:
|
||||
raise ParseError('出入库记录非创建中不可删除')
|
||||
MIOViewSet.lock_and_check_can_update(instance.mio)
|
||||
if has_perm(self.request.user, ['mio.update']) is False and instance.mio.create_by != self.request.user:
|
||||
raise PermissionDenied('无权限删除')
|
||||
return super().perform_destroy(instance)
|
||||
|
@ -490,20 +503,20 @@ class MIOItemwViewSet(CustomModelViewSet):
|
|||
mioitem.count_notok = MIOItemw.objects.filter(mioitem=mioitem, ftest__is_ok=False).count()
|
||||
mioitem.save()
|
||||
|
||||
@transaction.atomic
|
||||
def perform_create(self, serializer):
|
||||
MIOViewSet.lock_and_check_can_update(serializer.validated_data['mioitem'].mio)
|
||||
ins:MIOItemw = serializer.save()
|
||||
mioitem: MIOItem = ins.mioitem
|
||||
self.cal_mioitem_count(mioitem)
|
||||
self.cal_mioitem_count(ins.mioitem)
|
||||
|
||||
@transaction.atomic
|
||||
def perform_update(self, serializer):
|
||||
mioitemw = serializer.save()
|
||||
self.cal_mioitem_count(mioitemw.mioitem)
|
||||
ins:MIOItemw = serializer.instance
|
||||
MIOViewSet.lock_and_check_can_update(ins.mioitem.mio)
|
||||
ins:MIOItemw = serializer.save()
|
||||
self.cal_mioitem_count(ins.mioitem)
|
||||
|
||||
@transaction.atomic
|
||||
def perform_destroy(self, instance: MIOItemw):
|
||||
mioitem = instance.mioitem
|
||||
MIOViewSet.lock_and_check_can_update(mioitem.mio)
|
||||
ftest = instance.ftest
|
||||
instance.delete()
|
||||
if ftest:
|
||||
|
|
|
@ -9,7 +9,6 @@ def correct_tuihuo_log():
|
|||
mlogs = Mlog.objects.filter(mgroup__name='管料退火', count_use=0)
|
||||
for mlog in mlogs:
|
||||
try:
|
||||
with transaction.atomic():
|
||||
count_use = mlog.count_ok + mlog.count_notok
|
||||
material_has = WMaterial.objects.get(
|
||||
batch=mlog.batch, material=mlog.material_in, belong_dept=mlog.mgroup.belong_dept)
|
||||
|
|
|
@ -52,7 +52,6 @@ class StLogSerializer(CustomModelSerializer):
|
|||
def create(self, validated_data):
|
||||
current_sflog_id = validated_data.pop('current_sflog')
|
||||
current_note = validated_data.pop('current_note', '')
|
||||
with transaction.atomic():
|
||||
sflog = get_sflog(
|
||||
validated_data['mgroup'], validated_data['start_time'])
|
||||
if current_sflog_id != sflog.id:
|
||||
|
@ -67,9 +66,6 @@ class StLogSerializer(CustomModelSerializer):
|
|||
def update(self, instance, validated_data):
|
||||
if instance.is_shutdown: # 停机不可编辑end_time
|
||||
validated_data.pop('end_time', None)
|
||||
# if instance.end_time:
|
||||
# raise ParseError('该异常已结束无需编辑')
|
||||
with transaction.atomic():
|
||||
validated_data.pop('mgroup', None)
|
||||
validated_data.pop('start_time', None)
|
||||
end_time = validated_data.pop('end_time', None)
|
||||
|
@ -374,7 +370,7 @@ class MlogSerializer(CustomModelSerializer):
|
|||
mlogbindefect = validated_data.pop('mlogindefect', None)
|
||||
if mtask and mtask.state != Mtask.MTASK_ASSGINED:
|
||||
raise ParseError('该任务非下达中不可选择')
|
||||
with transaction.atomic():
|
||||
|
||||
mlogb = validated_data.pop('mlogb', [])
|
||||
instance: Mlog = super().create(validated_data)
|
||||
## 返工没有加工前不良
|
||||
|
@ -478,7 +474,7 @@ class MlogSerializer(CustomModelSerializer):
|
|||
if instance.mtask:
|
||||
validated_data.pop('handle_date', None)
|
||||
# validated_data.pop('handle_user', None)
|
||||
with transaction.atomic():
|
||||
|
||||
mlogb = validated_data.pop('mlogb', [])
|
||||
instance: Mlog = super().update(instance, validated_data)
|
||||
## 返工没有加工前不良
|
||||
|
@ -785,16 +781,8 @@ class MlogbInSerializer(CustomModelSerializer):
|
|||
raise ParseError('该记录已存在')
|
||||
if mlog.submit_time is not None:
|
||||
raise ParseError('生产日志已提交不可编辑')
|
||||
with transaction.atomic():
|
||||
ins:Mlogb = super().create(validated_data)
|
||||
# if mlog.is_fix:
|
||||
# if mlog.material_in is None:
|
||||
# mlog.material_in = ins.material_in
|
||||
# mlog.material_out = ins.material_in
|
||||
# mlog.save(update_fields=["material_in", "material_out"])
|
||||
# elif mlog.material_in != ins.material_in:
|
||||
# raise ParseError('该记录必须使用同一物料')
|
||||
|
||||
ins:Mlogb = super().create(validated_data)
|
||||
if mlogbdefect is not None and ins.material_in.tracking == Material.MA_TRACKING_BATCH:
|
||||
mlogb_defect_objects = [
|
||||
MlogbDefect(**{**item, "mlogb": ins, "id": idWorker.get_id()})
|
||||
|
@ -1057,31 +1045,12 @@ class MlogbOutUpdateSerializer(CustomModelSerializer):
|
|||
read_only_fields = EXCLUDE_FIELDS_BASE + ['mlog', 'mtask', 'wm_in', 'material_in', 'material_out',
|
||||
'count_use', 'count_break', 'count_pn_jgqbl', 'mlogbdefect', "qct", "count_json"]
|
||||
|
||||
# def create(self, validated_data):
|
||||
# material_out:Material = validated_data["material_out"]
|
||||
# mlogbdefect = validated_data.pop("mlogbdefect", [])
|
||||
# with transaction.atomic():
|
||||
# ins = super().create(validated_data)
|
||||
# if mlogbdefect and material_out.tracking == Material.MA_TRACKING_BATCH:
|
||||
# count_notok = 0
|
||||
# mlogbdefect_new = [item for item in mlogbdefect if item["count"] > 0]
|
||||
# for item in mlogbdefect_new:
|
||||
# defect:Defect = item["defect"]
|
||||
# MlogbDefect.objects.create(mlogb=ins, **item)
|
||||
# if defect.cate == Defect.DEFECT_NOTOK:
|
||||
# count_notok +=1
|
||||
# ins.count_notok = count_notok
|
||||
# ins.count_ok = ins.count_real - ins.count_notok
|
||||
# ins.save()
|
||||
# else:
|
||||
# raise ParseError("mlogbdefect仅支持批次件")
|
||||
# return ins
|
||||
def update(self, instance, validated_data):
|
||||
mlog:Mlog = instance.mlog
|
||||
if mlog.submit_time is not None:
|
||||
raise ParseError('生产日志已提交不可编辑')
|
||||
mlogbdefect = validated_data.pop("mlogbdefect", None)
|
||||
with transaction.atomic():
|
||||
|
||||
ins:Mlogb = super().update(instance, validated_data)
|
||||
if ins.need_inout is False:
|
||||
if ins.mlogb_from:
|
||||
|
@ -1325,7 +1294,6 @@ class HandoverSerializer(CustomModelSerializer):
|
|||
|
||||
def create(self, validated_data):
|
||||
handoverb = validated_data.pop('handoverb', [])
|
||||
with transaction.atomic():
|
||||
ins = super().create(validated_data)
|
||||
mtype = validated_data["mtype"]
|
||||
for ind, item in enumerate(handoverb):
|
||||
|
@ -1356,7 +1324,7 @@ class HandoverSerializer(CustomModelSerializer):
|
|||
|
||||
def update(self, instance, validated_data):
|
||||
handoverb = validated_data.pop('handoverb', [])
|
||||
with transaction.atomic():
|
||||
|
||||
insx = super().update(instance, validated_data)
|
||||
Handoverb.objects.filter(handover=instance).delete()
|
||||
for ind, item in enumerate(handoverb):
|
||||
|
|
Loading…
Reference in New Issue