diff --git a/apps/mtm/services.py b/apps/mtm/services.py index 547d2fda..1f32e264 100644 --- a/apps/mtm/services.py +++ b/apps/mtm/services.py @@ -1,7 +1,7 @@ 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 apps.mtm.models import Material, Process from rest_framework.exceptions import ParseError from apps.utils.tools import ranstr @@ -36,23 +36,37 @@ def daoru_material(path: str): from apps.utils.snowflake import idWorker from openpyxl import load_workbook wb = load_workbook(path) - sheet = wb.get_sheet_by_name('物料批次') + 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] - 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(' ', '') + 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(' ', '') + 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': unit} + if type in [20, 30]: + filters['process'] = process + default = filters + default['number'] = f'm{type}_{ranstr(6)}' + default['id'] = idWorker.get_id() 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 + defaults=default, + **filters ) except Exception as e: raise ParseError(f'{i}行物料有误, 导入失败--{e}') diff --git a/media/default/template/material.xlsx b/media/default/template/material.xlsx new file mode 100644 index 00000000..5d151b6e Binary files /dev/null and b/media/default/template/material.xlsx differ