61 lines
2.3 KiB
Python
61 lines
2.3 KiB
Python
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)
|
|
key = f'mgroup_{mgroupId}_goals'
|
|
if reload is False:
|
|
mgroup_goals = cache.get(key, None)
|
|
if mgroup_goals is not None:
|
|
return mgroup_goals
|
|
mgroup_goals = {}
|
|
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}')
|
|
cache.set(key, mgroup_goals, 10)
|
|
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, '导入成功')
|