76 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			Python
		
	
	
	
			
		
		
	
	
			76 lines
		
	
	
		
			3.1 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, Process
 | ||
| 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('物料')
 | ||
|     process_l = Process.objects.all()
 | ||
|     process_d = {p.name: p for p in process_l}
 | ||
|     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]
 | ||
|             name = sheet[f'b{i}'].value.replace(' ', '')
 | ||
|             specification = sheet[f'c{i}'].value.replace(
 | ||
|                 '×', '*').replace(' ', '')
 | ||
|             model = sheet[f'd{i}'].value.replace(' ', '')
 | ||
|             unit = sheet[f'e{i}'].value.replace(' ', '')
 | ||
|             count_safe = sheet[f'g{i}'].value
 | ||
|         except Exception as e:
 | ||
|             raise ParseError(f'{i}行物料信息错误: {str(e)}')
 | ||
|         if type in [20, 30]:
 | ||
|             try:
 | ||
|                 process = process_d[sheet[f'f{i}'].value.replace(' ', '')]
 | ||
|             except Exception as e:
 | ||
|                 raise ParseError(f'{i}行物料信息错误: {str(e)}')
 | ||
|         try:
 | ||
|             filters = {'type': type, 'name': name, 'specification': specification,
 | ||
|                        'model': model, 'unit__iexact': unit}
 | ||
|             if type in [20, 30]:
 | ||
|                 filters['process'] = process
 | ||
|             default = {'type': type, 'name': name, 'specification': specification,
 | ||
|                        'model': model, 'unit': unit, 'number': f'm{type}_{ranstr(6)}', 'id': idWorker.get_id(), 'count_safe': count_safe}
 | ||
|             material, is_created = Material.objects.get_or_create(
 | ||
|                 **filters, defaults=default)
 | ||
|             if not is_created:
 | ||
|                 material.count_safe = count_safe
 | ||
|                 material.save()
 | ||
|         except Exception as e:
 | ||
|             raise ParseError(f'{i}行物料有误, 导入失败--{e}')
 | ||
|         i = i + 1
 | ||
|         print(type, name, specification, model, unit, '导入成功')
 |