from .models import Equipment
from apps.system.models import Dept
from apps.mtm.models import Mgroup

def daoru_equipment(path:str):
        from apps.utils.snowflake import idWorker
        from openpyxl import load_workbook
        wb = load_workbook(path)
        sheet = wb.get_sheet_by_name('em_equipment')
        i = 2
        while sheet[f'a{i}'].value:
            name = sheet[f'a{i}'].value
            number = sheet[f'b{i}'].value
            model = sheet[f'c{i}'].value
            parameter = sheet[f'd{i}'].value
            place = sheet[f'e{i}'].value
            power_kw = sheet[f'f{i}'].value
            belong_dept_name = sheet[f'g{i}'].value
            mgroup_name = sheet[f'h{i}'].value
            Equipment.objects.get_or_create(
                  number=number,
                  defaults={
                        'id': idWorker.get_id(),
                        'name': name,
                        'model': model,
                        'parameter': parameter,
                        'place': place,
                        'power_kw': power_kw,
                        'belong_dept': Dept.objects.get(name=belong_dept_name) if belong_dept_name else None,
                        'mgroup': Mgroup.objects.get(name=mgroup_name) if mgroup_name else None
                  }
            )
            i = i + 1
