# Generated by Django 5.2.7 on 2026-01-31 15:07

import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models


def forwards_copy_created_by_from_employee(apps, schema_editor):
    PreSalesOrder = apps.get_model('business', 'PreSalesOrder')
    PrePurchaseOrder = apps.get_model('business', 'PrePurchaseOrder')

    for order in (
        PreSalesOrder.objects.select_related('created_by_employee')
        .filter(created_by__isnull=True)
        .exclude(created_by_employee__isnull=True)
    ):
        employee = getattr(order, 'created_by_employee', None)
        if employee and getattr(employee, 'sys_user_id', None):
            order.created_by_id = employee.sys_user_id
            order.save(update_fields=['created_by'])

    for order in (
        PrePurchaseOrder.objects.select_related('created_by_employee')
        .filter(created_by__isnull=True)
        .exclude(created_by_employee__isnull=True)
    ):
        employee = getattr(order, 'created_by_employee', None)
        if employee and getattr(employee, 'sys_user_id', None):
            order.created_by_id = employee.sys_user_id
            order.save(update_fields=['created_by'])


def backwards_noop(apps, schema_editor):
    # We intentionally do not attempt to restore legacy employee ids.
    return


class Migration(migrations.Migration):

    dependencies = [
        ('basic_info', '0024_frontend_page_and_visible_pages'),
        ('business', '0023_prepurchaseorder_prepurchaseorderitem'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.RenameField(
            model_name='prepurchaseorder',
            old_name='created_by',
            new_name='created_by_employee',
        ),
        migrations.AddField(
            model_name='prepurchaseorder',
            name='operator',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='operated_pre_purchase_orders', to='basic_info.employee', verbose_name='经办人'),
        ),
        migrations.AddField(
            model_name='prepurchaseorder',
            name='created_by',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='created_pre_purchase_orders', to=settings.AUTH_USER_MODEL, verbose_name='创建者用户'),
        ),
        migrations.RenameField(
            model_name='presalesorder',
            old_name='created_by',
            new_name='created_by_employee',
        ),
        migrations.AddField(
            model_name='presalesorder',
            name='operator',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='operated_pre_sales_orders', to='basic_info.employee', verbose_name='经办人'),
        ),
        migrations.AddField(
            model_name='presalesorder',
            name='created_by',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='created_pre_sales_orders', to=settings.AUTH_USER_MODEL, verbose_name='创建者用户'),
        ),
        migrations.RunPython(forwards_copy_created_by_from_employee, backwards_noop),
        migrations.RemoveField(
            model_name='prepurchaseorder',
            name='created_by_employee',
        ),
        migrations.RemoveField(
            model_name='presalesorder',
            name='created_by_employee',
        ),
    ]
