# Generated by Django 5.2.8 on 2026-04-10 03:36

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


def populate_merchant_fields(apps, schema_editor):
    Mission = apps.get_model("mission", "Mission")
    MissionParticipant = apps.get_model("mission", "MissionParticipant")
    MissionReply = apps.get_model("mission", "MissionReply")

    for mission in Mission.objects.select_related("creator").filter(merchant__isnull=True):
        mission.merchant_id = mission.creator.merchant_id
        mission.save(update_fields=["merchant"])

    for participant in MissionParticipant.objects.select_related("mission").filter(merchant__isnull=True):
        participant.merchant_id = participant.mission.merchant_id
        participant.save(update_fields=["merchant"])

    for reply in MissionReply.objects.select_related("mission").filter(merchant__isnull=True):
        reply.merchant_id = reply.mission.merchant_id
        reply.save(update_fields=["merchant"])


class Migration(migrations.Migration):

    dependencies = [
        ("basic_info", "0025_customer_uniq_customer_merchant_name"),
        ("mission", "0001_initial"),
    ]

    operations = [
        migrations.RemoveIndex(
            model_name="mission",
            name="mission_mis_content_be6081_idx",
        ),
        migrations.RemoveIndex(
            model_name="mission",
            name="mission_mis_is_comp_299039_idx",
        ),
        migrations.AddField(
            model_name="mission",
            name="merchant",
            field=models.ForeignKey(
                null=True,
                on_delete=django.db.models.deletion.PROTECT,
                related_name="missions",
                to="basic_info.merchant",
                verbose_name="所属商户",
            ),
        ),
        migrations.AddField(
            model_name="missionparticipant",
            name="merchant",
            field=models.ForeignKey(
                null=True,
                on_delete=django.db.models.deletion.PROTECT,
                related_name="mission_participants",
                to="basic_info.merchant",
                verbose_name="所属商户",
            ),
        ),
        migrations.AddField(
            model_name="missionreply",
            name="merchant",
            field=models.ForeignKey(
                null=True,
                on_delete=django.db.models.deletion.PROTECT,
                related_name="mission_replies",
                to="basic_info.merchant",
                verbose_name="所属商户",
            ),
        ),
        migrations.AddField(
            model_name="missionreply",
            name="rejected_by",
            field=models.ForeignKey(
                blank=True,
                null=True,
                on_delete=django.db.models.deletion.PROTECT,
                related_name="rejected_mission_replies",
                to="basic_info.employee",
                verbose_name="撤销人",
            ),
        ),
        migrations.RunPython(populate_merchant_fields, migrations.RunPython.noop),
        migrations.AlterField(
            model_name="mission",
            name="merchant",
            field=models.ForeignKey(
                on_delete=django.db.models.deletion.PROTECT,
                related_name="missions",
                to="basic_info.merchant",
                verbose_name="所属商户",
            ),
        ),
        migrations.AlterField(
            model_name="missionparticipant",
            name="merchant",
            field=models.ForeignKey(
                on_delete=django.db.models.deletion.PROTECT,
                related_name="mission_participants",
                to="basic_info.merchant",
                verbose_name="所属商户",
            ),
        ),
        migrations.AlterField(
            model_name="missionreply",
            name="merchant",
            field=models.ForeignKey(
                on_delete=django.db.models.deletion.PROTECT,
                related_name="mission_replies",
                to="basic_info.merchant",
                verbose_name="所属商户",
            ),
        ),
        migrations.AddIndex(
            model_name="mission",
            index=models.Index(fields=["merchant", "content_type", "content_id"], name="mission_mis_merchan_af589f_idx"),
        ),
        migrations.AddIndex(
            model_name="mission",
            index=models.Index(fields=["merchant", "is_completed", "is_cancelled"], name="mission_mis_merchan_6d6d37_idx"),
        ),
    ]
