feat: 增加物料导入功能
This commit is contained in:
parent
61801784fc
commit
679e6bac4f
|
@ -1,12 +1,17 @@
|
||||||
from apps.mtm.models import Goal, Mgroup
|
from apps.mtm.models import Goal, Mgroup
|
||||||
from django.core.cache import cache
|
from django.core.cache import cache
|
||||||
from django.db.models import Q
|
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):
|
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'
|
key = f'mgroup_{mgroupId}_goals'
|
||||||
if reload is False:
|
if reload is False:
|
||||||
mgroup_goals = cache.get(key, None)
|
mgroup_goals = cache.get(key, None)
|
||||||
|
@ -16,6 +21,40 @@ def get_mgroup_goals(mgroupId, year, reload=False):
|
||||||
for goal in goals:
|
for goal in goals:
|
||||||
mgroup_goals[f'{goal.goal_cate.code}_year'] = goal.goal_val
|
mgroup_goals[f'{goal.goal_cate.code}_year'] = goal.goal_val
|
||||||
for i in range(12):
|
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)
|
cache.set(key, mgroup_goals, 10)
|
||||||
return mgroup_goals
|
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, '导入成功')
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
from django.shortcuts import render
|
from django.shortcuts import render
|
||||||
|
from django.conf import settings
|
||||||
from rest_framework.decorators import action
|
from rest_framework.decorators import action
|
||||||
from rest_framework.mixins import ListModelMixin
|
from rest_framework.mixins import ListModelMixin
|
||||||
from rest_framework.response import Response
|
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,
|
from apps.mtm.serializers import (GoalSerializer, MaterialSerializer,
|
||||||
MgroupGoalYearSerializer, MgroupSerializer,
|
MgroupGoalYearSerializer, MgroupSerializer,
|
||||||
ShiftSerializer, TeamSerializer, ProcessSerializer, RouteSerializer, TeamMemberSerializer)
|
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.viewsets import CustomGenericViewSet, CustomModelViewSet
|
||||||
from apps.utils.mixins import BulkCreateModelMixin, BulkDestroyModelMixin
|
from apps.utils.mixins import BulkCreateModelMixin, BulkDestroyModelMixin
|
||||||
|
from rest_framework.serializers import Serializer
|
||||||
|
from django.db import transaction
|
||||||
|
|
||||||
|
|
||||||
# Create your views here.
|
# Create your views here.
|
||||||
|
@ -26,7 +29,18 @@ class MaterialViewSet(CustomModelViewSet):
|
||||||
filterset_class = MaterialFilter
|
filterset_class = MaterialFilter
|
||||||
search_fields = ['name', 'code', 'number', 'specification']
|
search_fields = ['name', 'code', 'number', 'specification']
|
||||||
select_related_fields = ['process']
|
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):
|
class ShiftViewSet(ListModelMixin, CustomGenericViewSet):
|
||||||
|
|
Loading…
Reference in New Issue