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


def _dedupe_role_daily(apps, schema_editor, model_name, user_field):
    Model = apps.get_model('backend', model_name)
    for row in Model.objects.values('club_id', user_field, 'Date').annotate(max_id=Max('id')):
        Model.objects.filter(
            club_id=row['club_id'],
            **{user_field: row[user_field], 'Date': row['Date']},
        ).exclude(id=row['max_id']).delete()


def _dedupe_merchant(apps, schema_editor):
    _dedupe_role_daily(apps, schema_editor, 'MerchantDailyStats', 'MerchantID')


def _dedupe_player(apps, schema_editor):
    _dedupe_role_daily(apps, schema_editor, 'PlayerDailyStats', 'PlayerID')


def _dedupe_leader(apps, schema_editor):
    _dedupe_role_daily(apps, schema_editor, 'LeaderDailyStats', 'LeaderID')


def _dedupe_manager(apps, schema_editor):
    _dedupe_role_daily(apps, schema_editor, 'ManagerDailyStats', 'ManagerID')


def _dedupe_manager_renewal(apps, schema_editor):
    _dedupe_role_daily(apps, schema_editor, 'ManagerRenewalDailyStats', 'ManagerID')


class Migration(migrations.Migration):

    dependencies = [
        ('backend', '0003_withdrawaldailystats_club_id'),
    ]

    operations = [
        migrations.AddField(
            model_name='merchantdailystats',
            name='club_id',
            field=models.CharField(db_index=True, default='xq', max_length=16, verbose_name='俱乐部ID'),
        ),
        migrations.AddField(
            model_name='playerdailystats',
            name='club_id',
            field=models.CharField(db_index=True, default='xq', max_length=16, verbose_name='俱乐部ID'),
        ),
        migrations.AddField(
            model_name='leaderdailystats',
            name='club_id',
            field=models.CharField(db_index=True, default='xq', max_length=16, verbose_name='俱乐部ID'),
        ),
        migrations.AddField(
            model_name='managerdailystats',
            name='club_id',
            field=models.CharField(db_index=True, default='xq', max_length=16, verbose_name='俱乐部ID'),
        ),
        migrations.AddField(
            model_name='managerrenewaldailystats',
            name='club_id',
            field=models.CharField(db_index=True, default='xq', max_length=16, verbose_name='俱乐部ID'),
        ),
        migrations.RunPython(_dedupe_merchant, migrations.RunPython.noop),
        migrations.AlterUniqueTogether(
            name='merchantdailystats',
            unique_together={('club_id', 'MerchantID', 'Date')},
        ),
        migrations.RunPython(_dedupe_player, migrations.RunPython.noop),
        migrations.AlterUniqueTogether(
            name='playerdailystats',
            unique_together={('club_id', 'PlayerID', 'Date')},
        ),
        migrations.RunPython(_dedupe_leader, migrations.RunPython.noop),
        migrations.AlterUniqueTogether(
            name='leaderdailystats',
            unique_together={('club_id', 'LeaderID', 'Date')},
        ),
        migrations.RunPython(_dedupe_manager, migrations.RunPython.noop),
        migrations.AlterUniqueTogether(
            name='managerdailystats',
            unique_together={('club_id', 'ManagerID', 'Date')},
        ),
        migrations.RunPython(_dedupe_manager_renewal, migrations.RunPython.noop),
        migrations.AlterUniqueTogether(
            name='managerrenewaldailystats',
            unique_together={('club_id', 'ManagerID', 'Date')},
        ),
    ]
