# Generated by Django 5.2.12 on 2026-05-31 08:00

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


class Migration(migrations.Migration):

    dependencies = [
        ("account", "0030_macrosizingconfig_market_temperature_fields"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.AddField(
            model_name="transactionmodel",
            name="broker_name",
            field=models.CharField(blank=True, default="", max_length=64, verbose_name="券商名称"),
        ),
        migrations.AddField(
            model_name="transactionmodel",
            name="broker_trade_key",
            field=models.CharField(
                blank=True,
                db_index=True,
                max_length=64,
                null=True,
                unique=True,
                verbose_name="券商成交去重键",
            ),
        ),
        migrations.AddField(
            model_name="transactionmodel",
            name="external_trade_id",
            field=models.CharField(
                blank=True, default="", max_length=128, verbose_name="外部成交编号"
            ),
        ),
        migrations.AddField(
            model_name="transactionmodel",
            name="raw_payload",
            field=models.JSONField(blank=True, default=dict, verbose_name="原始导入行"),
        ),
        migrations.CreateModel(
            name="BrokerTradeImportBatchModel",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                (
                    "broker_name",
                    models.CharField(default="manual", max_length=64, verbose_name="券商名称"),
                ),
                (
                    "source_filename",
                    models.CharField(blank=True, default="", max_length=255, verbose_name="文件名"),
                ),
                (
                    "file_hash",
                    models.CharField(db_index=True, max_length=64, verbose_name="文件哈希"),
                ),
                (
                    "status",
                    models.CharField(
                        choices=[
                            ("previewed", "已预览"),
                            ("completed", "已完成"),
                            ("completed_with_errors", "完成但有错误"),
                            ("failed", "失败"),
                        ],
                        db_index=True,
                        default="previewed",
                        max_length=32,
                        verbose_name="状态",
                    ),
                ),
                ("total_rows", models.IntegerField(default=0, verbose_name="总行数")),
                ("imported_rows", models.IntegerField(default=0, verbose_name="导入行数")),
                ("skipped_rows", models.IntegerField(default=0, verbose_name="跳过行数")),
                ("error_rows", models.IntegerField(default=0, verbose_name="错误行数")),
                ("errors", models.JSONField(blank=True, default=list, verbose_name="错误详情")),
                ("preview_rows", models.JSONField(blank=True, default=list, verbose_name="预览行")),
                ("created_at", models.DateTimeField(auto_now_add=True, verbose_name="创建时间")),
                ("updated_at", models.DateTimeField(auto_now=True, verbose_name="更新时间")),
                (
                    "portfolio",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="broker_trade_import_batches",
                        to="account.portfoliomodel",
                        verbose_name="投资组合",
                    ),
                ),
                (
                    "user",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="broker_trade_import_batches",
                        to=settings.AUTH_USER_MODEL,
                        verbose_name="用户",
                    ),
                ),
            ],
            options={
                "verbose_name": "券商交易导入批次",
                "verbose_name_plural": "券商交易导入批次",
                "db_table": "broker_trade_import_batch",
                "ordering": ["-created_at"],
            },
        ),
        migrations.AddField(
            model_name="transactionmodel",
            name="import_batch",
            field=models.ForeignKey(
                blank=True,
                null=True,
                on_delete=django.db.models.deletion.SET_NULL,
                related_name="transactions",
                to="account.brokertradeimportbatchmodel",
                verbose_name="导入批次",
            ),
        ),
        migrations.AddIndex(
            model_name="transactionmodel",
            index=models.Index(
                fields=["broker_name", "external_trade_id"], name="transaction_broker__28598c_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="brokertradeimportbatchmodel",
            index=models.Index(fields=["user", "-created_at"], name="idx_broker_import_user_time"),
        ),
        migrations.AddIndex(
            model_name="brokertradeimportbatchmodel",
            index=models.Index(
                fields=["portfolio", "-created_at"], name="idx_broker_import_pf_time"
            ),
        ),
        migrations.AddConstraint(
            model_name="brokertradeimportbatchmodel",
            constraint=models.UniqueConstraint(
                fields=("user", "portfolio", "file_hash"),
                name="uq_broker_import_user_portfolio_hash",
            ),
        ),
    ]
