from .models import Material

def correct_material_deleted():
    """
    矫正物料是否被删除
    """
    from apps.inm.models import MaterialBatch
    ms = MaterialBatch.objects.all().values('material').distinct()
    for m in ms:
        material = Material.objects.get_queryset(all=True).get(id=m['material'])
        if material.is_deleted:
            material.is_deleted = False
            material.save(update_fields=['is_deleted'])

def correct_material_cate():
    """
    矫正物料分类/ 光子的棒管
    """
    for m in Material.objects.all():
        cate = ''
        if '混料' in m.name:
            cate = '混料'
        elif '棒管' in m.name:
            cate = '棒管'
        elif '棒' in m.name:
            cate = '棒'
        elif '管' in m.name:
            cate = '管'
        if cate:
            Material.objects.filter(id=m.id).update(cate=cate)