diff --git a/hb_server/apps/mtm/migrations/0004_auto_20210830_1129.py b/hb_server/apps/mtm/migrations/0004_auto_20210830_1129.py new file mode 100644 index 0000000..ccb2d7c --- /dev/null +++ b/hb_server/apps/mtm/migrations/0004_auto_20210830_1129.py @@ -0,0 +1,68 @@ +# Generated by Django 3.2.6 on 2021-08-30 03:29 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('mtm', '0003_auto_20210827_1604'), + ] + + operations = [ + migrations.AddField( + model_name='inputmaterial', + name='process', + field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to='mtm.process', verbose_name='关联工序'), + preserve_default=False, + ), + migrations.AddField( + model_name='inputmaterial', + name='product', + field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, related_name='inputmaterial_product', to='mtm.material', verbose_name='关联产品'), + preserve_default=False, + ), + migrations.AddField( + model_name='inputmaterial', + name='sort', + field=models.IntegerField(default=1, verbose_name='排序号'), + ), + migrations.AddField( + model_name='outputmaterial', + name='process', + field=models.ForeignKey(default=2, on_delete=django.db.models.deletion.CASCADE, to='mtm.process', verbose_name='关联工序'), + preserve_default=False, + ), + migrations.AddField( + model_name='outputmaterial', + name='product', + field=models.ForeignKey(default=3, on_delete=django.db.models.deletion.CASCADE, related_name='outputmaterial_product', to='mtm.material', verbose_name='关联产品'), + preserve_default=False, + ), + migrations.AddField( + model_name='outputmaterial', + name='sort', + field=models.IntegerField(default=1, verbose_name='排序号'), + ), + migrations.AlterField( + model_name='inputmaterial', + name='material', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='inputmaterial', to='mtm.material', verbose_name='输入物料'), + ), + migrations.AlterField( + model_name='inputmaterial', + name='number', + field=models.FloatField(default=1, verbose_name='消耗量'), + ), + migrations.AlterField( + model_name='outputmaterial', + name='material', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='outputmaterial', to='mtm.material', verbose_name='输出物料'), + ), + migrations.AlterField( + model_name='outputmaterial', + name='number', + field=models.FloatField(default=1, verbose_name='产出量'), + ), + ] diff --git a/hb_server/apps/mtm/models.py b/hb_server/apps/mtm/models.py index 7012f3f..d31e7ee 100644 --- a/hb_server/apps/mtm/models.py +++ b/hb_server/apps/mtm/models.py @@ -110,9 +110,12 @@ class InputMaterial(CommonAModel): """ 输入物料 """ - material = models.ForeignKey(Material, verbose_name='输入物料', on_delete=models.CASCADE) - number = models.FloatField('消耗量', default=0) + material = models.ForeignKey(Material, verbose_name='输入物料', on_delete=models.CASCADE, related_name='inputmaterial') + number = models.FloatField('消耗量', default=1) unit = models.CharField('单位', max_length=20) + product = models.ForeignKey(Material, verbose_name='关联产品', on_delete=models.CASCADE, related_name='inputmaterial_product') + process = models.ForeignKey(Process, verbose_name='关联工序', on_delete=models.CASCADE) + sort = models.IntegerField('排序号', default=1) class Meta: verbose_name = '输入物料' @@ -123,9 +126,12 @@ class OutputMaterial(CommonAModel): """ 输出物料 """ - material = models.ForeignKey(Material, verbose_name='输出物料', on_delete=models.CASCADE) - number = models.FloatField('产出量', default=0) + material = models.ForeignKey(Material, verbose_name='输出物料', on_delete=models.CASCADE, related_name='outputmaterial') + number = models.FloatField('产出量', default=1) unit = models.CharField('单位', max_length=20) + product = models.ForeignKey(Material, verbose_name='关联产品', on_delete=models.CASCADE, related_name='outputmaterial_product') + process = models.ForeignKey(Process, verbose_name='关联工序', on_delete=models.CASCADE) + sort = models.IntegerField('排序号', default=1) class Meta: verbose_name = '输出物料' diff --git a/hb_server/apps/mtm/serializers.py b/hb_server/apps/mtm/serializers.py index a2b531a..8583a76 100644 --- a/hb_server/apps/mtm/serializers.py +++ b/hb_server/apps/mtm/serializers.py @@ -1,6 +1,6 @@ from rest_framework import serializers -from .models import Material, Process, ProductProcess, Step +from .models import InputMaterial, Material, OutputMaterial, Process, ProductProcess, Step from apps.system.serializers import FileSimpleSerializer @@ -52,4 +52,37 @@ class ProductProcessListSerializer(serializers.ModelSerializer): class ProductProcessUpdateSerializer(serializers.ModelSerializer): class Meta: model = ProductProcess - fields = ['sort'] \ No newline at end of file + fields = ['sort'] + +class InputMaterialListSerializer(serializers.ModelSerializer): + material_ = MaterialSimpleSerializer(source='material', read_only=True) + class Meta: + model = InputMaterial + fields = '__all__' + +class OutputMaterialListSerializer(serializers.ModelSerializer): + material_ = MaterialSimpleSerializer(source='material', read_only=True) + class Meta: + model = OutputMaterial + fields = '__all__' + + +class InputMaterialSerializer(serializers.ModelSerializer): + class Meta: + model = InputMaterial + fields = ['number', 'unit', 'sort', 'material', 'product', 'process'] + +class InputMaterialUpdateSerializer(serializers.ModelSerializer): + class Meta: + model = InputMaterial + fields = ['number', 'unit', 'sort'] + +class OutputMaterialSerializer(serializers.ModelSerializer): + class Meta: + model = OutputMaterial + fields = ['number', 'unit', 'sort', 'material', 'product', 'process'] + +class OutputMaterialUpdateSerializer(serializers.ModelSerializer): + class Meta: + model = OutputMaterial + fields = ['number', 'unit', 'sort'] \ No newline at end of file diff --git a/hb_server/apps/mtm/urls.py b/hb_server/apps/mtm/urls.py index 9d4d325..9c9b812 100644 --- a/hb_server/apps/mtm/urls.py +++ b/hb_server/apps/mtm/urls.py @@ -1,14 +1,16 @@ from django.db.models import base from rest_framework import urlpatterns -from apps.mtm.views import MaterialViewSet, ProcessViewSet, ProductProcessViewSet, StepViewSet +from apps.mtm.views import InputMaterialViewSet, MaterialViewSet, OutputMaterialViewSet, ProcessViewSet, StepViewSet from django.urls import path, include from rest_framework.routers import DefaultRouter router = DefaultRouter() router.register('material', MaterialViewSet, basename='material') router.register('process', ProcessViewSet, basename='process') -router.register('productprocess', ProductProcessViewSet, basename='productprocess') +# router.register('productprocess', ProductProcessViewSet, basename='productprocess') router.register('step', StepViewSet, basename='step') +router.register('inputmaterial', InputMaterialViewSet, basename='inputmaterial') +router.register('outputmaterial', OutputMaterialViewSet, basename='outputmaterial') urlpatterns = [ path('', include(router.urls)), ] diff --git a/hb_server/apps/mtm/views.py b/hb_server/apps/mtm/views.py index fc7c922..df92385 100644 --- a/hb_server/apps/mtm/views.py +++ b/hb_server/apps/mtm/views.py @@ -2,12 +2,13 @@ from django.shortcuts import render from rest_framework.viewsets import ModelViewSet, GenericViewSet from rest_framework.mixins import CreateModelMixin, ListModelMixin, UpdateModelMixin, RetrieveModelMixin, DestroyModelMixin -from apps.mtm.models import Material, Process, ProductProcess, Step -from apps.mtm.serializers import MaterialDetailSerializer, MaterialSerializer, MaterialSimpleSerializer, ProductProcessListSerializer, ProductProcessUpdateSerializer, ProcessSerializer, StepSerializer +from apps.mtm.models import InputMaterial, Material, OutputMaterial, Process, ProductProcess, Step +from apps.mtm.serializers import InputMaterialListSerializer, InputMaterialSerializer, InputMaterialUpdateSerializer, MaterialDetailSerializer, MaterialSerializer, MaterialSimpleSerializer, OutputMaterialListSerializer, OutputMaterialSerializer, OutputMaterialUpdateSerializer, ProductProcessListSerializer, ProductProcessUpdateSerializer, ProcessSerializer, StepSerializer from apps.system.mixins import CreateUpdateModelAMixin, OptimizationMixin from rest_framework.decorators import action from rest_framework.response import Response from utils.pagination import PageOrNot +from rest_framework.exceptions import APIException # Create your views here. @@ -61,8 +62,9 @@ class ProcessViewSet(PageOrNot, CreateUpdateModelAMixin, ModelViewSet): serializer = self.serializer_class(instance=Step.objects.filter(process=process, is_deleted=False), many=True) return Response(serializer.data) -class StepViewSet(CreateModelMixin, UpdateModelMixin, RetrieveModelMixin, DestroyModelMixin, GenericViewSet): +class StepViewSet(CreateUpdateModelAMixin, CreateModelMixin, UpdateModelMixin, RetrieveModelMixin, DestroyModelMixin, GenericViewSet): """ + 子工序-增删改查 """ perms_map = {'*':'process_update'} queryset = Step.objects.all() @@ -72,17 +74,63 @@ class StepViewSet(CreateModelMixin, UpdateModelMixin, RetrieveModelMixin, Destro ordering = ['sort'] -class ProductProcessViewSet(PageOrNot, CreateModelMixin, UpdateModelMixin, ListModelMixin, DestroyModelMixin, GenericViewSet): +# class ProductProcessViewSet(PageOrNot, CreateModelMixin, UpdateModelMixin, ListModelMixin, DestroyModelMixin, GenericViewSet): +# """ +# 产品生产工艺流程增删改查 +# """ +# perms_map={'*':'*'} +# queryset = ProductProcess.objects.select_related('process', 'product').all() +# filterset_fields = ['process', 'product'] +# serializer_class = ProductProcessListSerializer +# ordering = ['sort'] + +# def get_serializer_class(self): +# if self.action == 'update': +# return ProductProcessUpdateSerializer +# return super().get_serializer_class() + +class InputMaterialViewSet(CreateUpdateModelAMixin, ModelViewSet): """ - 产品生产工艺流程增删改查 + 输入物料-增删改查 """ - perms_map={'*':'*'} - queryset = ProductProcess.objects.select_related('process', 'product').all() - filterset_fields = ['process', 'product'] - serializer_class = ProductProcessListSerializer - ordering = ['sort'] + perms_map = {'*':'*'} + queryset = InputMaterial.objects.select_related('material').all() + serializer_class = InputMaterialSerializer + filterset_fields = ['process', 'material'] + ordering = ['sort', '-create_time'] def get_serializer_class(self): - if self.action == 'update': - return ProductProcessUpdateSerializer - return super().get_serializer_class() \ No newline at end of file + if self.action == 'list': + return InputMaterialListSerializer + elif self.action == 'update': + return InputMaterialUpdateSerializer + return InputMaterialSerializer + + def perform_create(self, serializer): + data = serializer.data + if InputMaterial.objects.filter(material=data['material'], product=data['product'], process=data['process'], is_deleted=False).exists(): + raise APIException('该物料已存在') + serializer.save(create_by =self.request.user) + +class OutputMaterialViewSet(CreateUpdateModelAMixin, ModelViewSet): + """ + 输出物料-增删改查 + """ + perms_map = {'*':'*'} + queryset = OutputMaterial.objects.select_related('material').all() + serializer_class = OutputMaterialSerializer + filterset_fields = ['process', 'material'] + ordering = ['sort', '-create_time'] + + def get_serializer_class(self): + if self.action == 'list': + return OutputMaterialListSerializer + elif self.action == 'update': + return OutputMaterialUpdateSerializer + return OutputMaterialSerializer + + def perform_create(self, serializer): + data = serializer.data + if OutputMaterial.objects.filter(material=data['material'], product=data['product'], process=data['process'], is_deleted=False).exists(): + raise APIException('该物料已存在') + serializer.save(create_by=self.request.user) \ No newline at end of file diff --git a/hb_server/utils/pagination.py b/hb_server/utils/pagination.py index 76f8c74..f33f120 100644 --- a/hb_server/utils/pagination.py +++ b/hb_server/utils/pagination.py @@ -5,12 +5,21 @@ class MyPagination(PageNumberPagination): page_size = 10 page_size_query_param = 'page_size' + def paginate_queryset(self, queryset, request, view): + if request.query_params.get('pageoff', None) or request.query_params.get('page', None)=='0': + if queryset.count()<500: + return None + elif queryset.count()>=500: + raise ParseError('单次请求数据量大,请分页获取') + return super().paginate_queryset(queryset, request, view=view) + class PageOrNot: def paginate_queryset(self, queryset): if (self.paginator is None): return None - elif self.request.query_params.get('pageoff', None) and queryset.count()<500: - return None - elif self.request.query_params.get('pageoff', None) and queryset.count()>=500: - raise ParseError('单次请求数据量大,请求中止') + elif self.request.query_params.get('pageoff', None) or self.request.query_params.get('page', None) == 0: + if queryset.count()<500: + return None + elif queryset.count()>=500: + raise ParseError('单次请求数据量大,请求中止') return self.paginator.paginate_queryset(queryset, self.request, view=self)