From 4cea056955884090094d113f385ffe7b2b4d89fa Mon Sep 17 00:00:00 2001 From: caoqianming Date: Thu, 26 Oct 2023 16:25:04 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20utask=20mtask=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E7=8A=B6=E6=80=81=E4=BB=A5=E5=8F=8A=E7=9B=B8=E5=85=B3=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/pm/migrations/0012_auto_20231026_1624.py | 23 ++++++++++++ apps/pm/models.py | 8 +++-- apps/pm/views.py | 36 +++++++++++++++++-- apps/wpm/serializers.py | 2 +- apps/wpm/services.py | 3 +- 5 files changed, 63 insertions(+), 9 deletions(-) create mode 100644 apps/pm/migrations/0012_auto_20231026_1624.py diff --git a/apps/pm/migrations/0012_auto_20231026_1624.py b/apps/pm/migrations/0012_auto_20231026_1624.py new file mode 100644 index 00000000..58e73600 --- /dev/null +++ b/apps/pm/migrations/0012_auto_20231026_1624.py @@ -0,0 +1,23 @@ +# Generated by Django 3.2.12 on 2023-10-26 08:24 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('pm', '0011_alter_utask_state'), + ] + + operations = [ + migrations.AlterField( + model_name='mtask', + name='state', + field=models.PositiveIntegerField(choices=[(10, '创建中'), (20, '已下达'), (34, '已停止'), (40, '已提交')], default=10, help_text="((10, '创建中'), (20, '已下达'), (34, '已停止'), (40, '已提交'))", verbose_name='状态'), + ), + migrations.AlterField( + model_name='utask', + name='state', + field=models.PositiveIntegerField(choices=[(10, '创建中'), (14, '已分解'), (20, '已下达'), (30, '生产中'), (34, '已停止'), (40, '已提交')], default=10, help_text="((10, '创建中'), (14, '已分解'), (20, '已下达'), (30, '生产中'), (34, '已停止'), (40, '已提交'))", verbose_name='状态'), + ), + ] diff --git a/apps/pm/models.py b/apps/pm/models.py index 275a011b..e8986287 100644 --- a/apps/pm/models.py +++ b/apps/pm/models.py @@ -13,12 +13,14 @@ class Utask(CommonBDModel): UTASK_DECOMPOSE = 14 UTASK_ASSGINED = 20 UTASK_WORKING = 30 + UTASK_STOP = 34 UTASK_DONE = 40 UTASK_STATES = ( (UTASK_CREATED, '创建中'), (UTASK_DECOMPOSE, '已分解'), (UTASK_ASSGINED, '已下达'), (UTASK_WORKING, '生产中'), + (UTASK_STOP, '已停止'), (UTASK_DONE, '已提交') ) state = models.PositiveIntegerField( @@ -44,12 +46,12 @@ class Mtask(CommonADModel): """ MTASK_CREATED = 10 MTASK_ASSGINED = 20 - MTASK_WORKING = 30 + MTASK_STOP = 34 MTASK_DONE = 40 MTASK_STATES = ( (MTASK_CREATED, '创建中'), (MTASK_ASSGINED, '已下达'), - (MTASK_WORKING, '生产中'), + (MTASK_STOP, '已停止'), (MTASK_DONE, '已提交') ) state = models.PositiveIntegerField( @@ -68,7 +70,7 @@ class Mtask(CommonADModel): start_date = models.DateField('计划开工日期') end_date = models.DateField('计划完工日期') utask = models.ForeignKey( - Utask, verbose_name='关联大任务', on_delete=models.CASCADE, null=True, blank=True, related_name='mtask_utask') + Utask, verbose_name='关联大任务', on_delete=models.CASCADE, related_name='mtask_utask', null=True, blank=True) @property def related(self): diff --git a/apps/pm/views.py b/apps/pm/views.py index 1bbe4135..1604e384 100644 --- a/apps/pm/views.py +++ b/apps/pm/views.py @@ -11,6 +11,7 @@ from .filters import MtaskFilter, UtaskFilter from .models import Mtask, Utask from .serializers import MtaskSerializer, SchedueSerializer, UtaskSerializer, MtaskDaySerializer from .services import PmService +from django.utils import timezone # Create your views here. @@ -27,6 +28,33 @@ class UtaskViewSet(CustomModelViewSet): select_related_fields = ['material'] ordering = ['-start_date'] + def perform_destroy(self, instance): + if instance.state >= Utask.UTASK_WORKING: + raise ParseError('该任务状态不可删除') + return super().perform_destroy(instance) + + @action(methods=['post'], detail=True, perms_map={'post': 'utask.stop'}, serializer_class=Serializer) + @transaction.atomic + def stop(self, request, *args, ): + """停止任务 + + 停止任务 + """ + obj = self.get_object() + user = request.user + now = timezone.now() + if obj.state == Utask.UTASK_WORKING: + obj.state = Utask.UTASK_STOP + obj.update_by = user + obj.update_time = now + obj.save() + # 停止所有小任务 + Mtask.objects.filter( + utask=obj, state=Mtask.MTASK_ASSGINED).update(state=Mtask.MTASK_STOP, update_by=user, update_time=now) + else: + raise ParseError('该状态下不可停止') + return Response() + @action(methods=['post'], detail=False, perms_map={'post': 'utask.schedue'}, serializer_class=SchedueSerializer) @transaction.atomic def schedue_utasks(self, request, *args, **kwargs): @@ -59,18 +87,20 @@ class UtaskViewSet(CustomModelViewSet): @action(methods=['post'], detail=False, perms_map={'post': 'utask.assgin'}, serializer_class=PkSerializer) @transaction.atomic - def assgin(self, request): + def assgin(self, request, *args, **kwargs): """下达任务 下达任务 """ ids = request.data.get('ids', []) + user = request.user + now = timezone.now() utasks = Utask.objects.filter( id__in=ids, state=Utask.UTASK_DECOMPOSE) # 已分解的任务 Mtask.objects.filter(utask__in=utasks, state=Mtask.MTASK_CREATED).update( - state=Mtask.MTASK_ASSGINED) + state=Mtask.MTASK_ASSGINED, update_by=user, update_time=now) utasks.update( - state=Utask.UTASK_ASSGINED) + state=Utask.UTASK_ASSGINED, update_by=user, update_time=now) # 此处要更新订单状态 from apps.sam.models import OrderItem, Order orderIds = OrderItem.objects.filter( diff --git a/apps/wpm/serializers.py b/apps/wpm/serializers.py index 5e907d9e..b06062d0 100644 --- a/apps/wpm/serializers.py +++ b/apps/wpm/serializers.py @@ -100,7 +100,7 @@ class MlogSerializer(CustomModelSerializer): if not WMaterial.objects.filter(batch=batch).exists(): raise ValidationError('批次号不存在') if Mlog.objects.filter(mtask=mtask, batch=batch, handle_date=handle_date, handle_user=handle_user).exists(): - raise ValidationError('存在相同的日志的记录') + raise ValidationError('存在相同的日志记录') return super().create(validated_data) def update(self, instance, validated_data): diff --git a/apps/wpm/services.py b/apps/wpm/services.py index 7bd2be7d..0d78ef98 100644 --- a/apps/wpm/services.py +++ b/apps/wpm/services.py @@ -165,7 +165,6 @@ def update_mtask(mtask: Mtask): utask.count_notok = res2['sum_count_notok'] if res2['sum_count_notok'] else 0 if utask.count_ok > 0 and utask.state == Utask.UTASK_ASSGINED: utask.state = Utask.UTASK_WORKING - utask.save() if Mtask.objects.filter(utask=utask).exclude(state=Mtask.MTASK_DONE).count() == 0: utask.state = Mtask.MTASK_DONE - utask.save() + utask.save()