From 679e6bac4ff8db9574d3d130c53548c4c3db0c7f Mon Sep 17 00:00:00 2001 From: caoqianming Date: Fri, 19 Jan 2024 14:36:08 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E7=89=A9=E6=96=99?= =?UTF-8?q?=E5=AF=BC=E5=85=A5=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/mtm/services.py | 45 +++++++++++++++++++++++++++++++++++++++++--- apps/mtm/views.py | 18 ++++++++++++++++-- 2 files changed, 58 insertions(+), 5 deletions(-) diff --git a/apps/mtm/services.py b/apps/mtm/services.py index 9fab10bd..547d2fda 100644 --- a/apps/mtm/services.py +++ b/apps/mtm/services.py @@ -1,12 +1,17 @@ from apps.mtm.models import Goal, Mgroup from django.core.cache import cache from django.db.models import Q +from apps.mtm.models import Material +from rest_framework.exceptions import ParseError +from apps.utils.tools import ranstr + def get_mgroup_goals(mgroupId, year, reload=False): """ 获取工段某年的全部目标值, 以字典形式返回, 带缓存 """ - goals = Goal.objects.filter(Q(mgroup__id=mgroupId)|Q(mgroup__name=mgroupId), year=year) + goals = Goal.objects.filter( + Q(mgroup__id=mgroupId) | Q(mgroup__name=mgroupId), year=year) key = f'mgroup_{mgroupId}_goals' if reload is False: mgroup_goals = cache.get(key, None) @@ -16,6 +21,40 @@ def get_mgroup_goals(mgroupId, year, reload=False): for goal in goals: mgroup_goals[f'{goal.goal_cate.code}_year'] = goal.goal_val for i in range(12): - mgroup_goals[f'{goal.goal_cate.code}_{i+1}'] = getattr(goal, f'goal_val_{i+1}') + mgroup_goals[f'{goal.goal_cate.code}_{i+1}'] = getattr( + goal, f'goal_val_{i+1}') cache.set(key, mgroup_goals, 10) - return mgroup_goals \ No newline at end of file + return mgroup_goals + + +def daoru_material(path: str): + """ + 导入物料信息 + """ + type_dict = {'主要原料': 30, '半成品': 20, '成品': 10, + '辅助材料': 40, '加工工具': 50, '辅助工装': 60, '办公用品': 70} + from apps.utils.snowflake import idWorker + from openpyxl import load_workbook + wb = load_workbook(path) + sheet = wb.get_sheet_by_name('物料批次') + i = 3 + while sheet[f'a{i}'].value is not None: + type_str = sheet[f'a{i}'].value.replace(' ', '') + try: + type = type_dict[type_str] + except KeyError: + raise ParseError(f'{i}行物料类型错误') + name = sheet[f'b{i}'].value + specification = sheet[f'c{i}'].value + model = sheet[f'd{i}'].value + unit = sheet[f'e{i}'].value.replace(' ', '') + try: + Material.objects.update_or_create( + defaults={'type': type, 'name': name, 'specification': specification, + 'model': model, 'unit': unit, 'id': idWorker.get_id(), 'number': f'm{type}_{ranstr(6)}'}, + type=type, name=name, specification=specification, model=model, unit=unit + ) + except Exception as e: + raise ParseError(f'{i}行物料有误, 导入失败--{e}') + i = i + 1 + print(type, name, specification, model, unit, '导入成功') diff --git a/apps/mtm/views.py b/apps/mtm/views.py index 2de4cdf7..dc81766e 100644 --- a/apps/mtm/views.py +++ b/apps/mtm/views.py @@ -1,4 +1,5 @@ from django.shortcuts import render +from django.conf import settings from rest_framework.decorators import action from rest_framework.mixins import ListModelMixin from rest_framework.response import Response @@ -9,9 +10,11 @@ from apps.mtm.models import Goal, Material, Mgroup, Shift, Team, Process, Route, from apps.mtm.serializers import (GoalSerializer, MaterialSerializer, MgroupGoalYearSerializer, MgroupSerializer, ShiftSerializer, TeamSerializer, ProcessSerializer, RouteSerializer, TeamMemberSerializer) -from apps.mtm.services import get_mgroup_goals +from apps.mtm.services import get_mgroup_goals, daoru_material from apps.utils.viewsets import CustomGenericViewSet, CustomModelViewSet from apps.utils.mixins import BulkCreateModelMixin, BulkDestroyModelMixin +from rest_framework.serializers import Serializer +from django.db import transaction # Create your views here. @@ -26,7 +29,18 @@ class MaterialViewSet(CustomModelViewSet): filterset_class = MaterialFilter search_fields = ['name', 'code', 'number', 'specification'] select_related_fields = ['process'] - ordering = ['sort', 'id'] + ordering = ['sort', 'id', 'number', 'name'] + ordering_fields = ['sort', 'id', 'number', 'name', 'type', 'process'] + + @action(methods=['post'], detail=False, serializer_class=Serializer) + @transaction.atomic + def daoru(self, request, *args, **kwargs): + """导入物料 + + 导入物料 + """ + daoru_material(settings.BASE_DIR + request.data.get('path', '')) + return Response() class ShiftViewSet(ListModelMixin, CustomGenericViewSet):