# Generated by Django 5.2.10 on 2026-02-26 02:36

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


class Migration(migrations.Migration):

    dependencies = [
        (
            "simulated_trading",
            "0009_rename_simulated_d_account_a973a7_idx_simulated_d_account_15d21a_idx_and_more",
        ),
        ("strategy", "0004_positionmanagementrulemodel"),
    ]

    operations = [
        migrations.CreateModel(
            name="RebalanceProposalModel",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                (
                    "source",
                    models.CharField(
                        choices=[
                            ("daily_inspection", "日更巡检"),
                            ("signal", "投资信号"),
                            ("manual", "手动创建"),
                            ("regime_change", "宏观象限变化"),
                            ("policy_change", "政策档位变化"),
                        ],
                        db_index=True,
                        default="daily_inspection",
                        max_length=50,
                        verbose_name="建议来源",
                    ),
                ),
                (
                    "source_description",
                    models.TextField(
                        blank=True, help_text="建议生成的原因说明", verbose_name="来源描述"
                    ),
                ),
                (
                    "status",
                    models.CharField(
                        choices=[
                            ("pending", "待审核"),
                            ("approved", "已批准"),
                            ("rejected", "已拒绝"),
                            ("executing", "执行中"),
                            ("completed", "已完成"),
                            ("failed", "执行失败"),
                            ("cancelled", "已取消"),
                        ],
                        db_index=True,
                        default="pending",
                        max_length=20,
                        verbose_name="执行状态",
                    ),
                ),
                (
                    "priority",
                    models.CharField(
                        choices=[
                            ("low", "低"),
                            ("normal", "普通"),
                            ("high", "高"),
                            ("urgent", "紧急"),
                        ],
                        db_index=True,
                        default="normal",
                        max_length=20,
                        verbose_name="优先级",
                    ),
                ),
                (
                    "proposals",
                    models.JSONField(
                        default=list,
                        help_text='再平衡建议列表:\n        [\n            {\n                "asset_code": "512880.SH",\n                "asset_name": "证券ETF",\n                "action": "buy" | "sell" | "hold",\n                "current_quantity": 1000,\n                "current_weight": 0.25,\n                "target_weight": 0.30,\n                "suggested_quantity": 200,\n                "reason": "仓位偏离目标",\n                "estimated_amount": 5000.00\n            }\n        ]\n        ',
                        verbose_name="再平衡建议",
                    ),
                ),
                (
                    "summary",
                    models.JSONField(
                        default=dict,
                        help_text='汇总信息:\n        {\n            "total_value": 100000.00,\n            "current_cash": 5000.00,\n            "rebalance_assets": ["512880.SH", "515050.SH"],\n            "buy_count": 2,\n            "sell_count": 1,\n            "estimated_trade_amount": 15000.00\n        }\n        ',
                        verbose_name="汇总信息",
                    ),
                ),
                (
                    "proposed_by",
                    models.CharField(
                        default="system",
                        help_text="谁创建了这个建议",
                        max_length=100,
                        verbose_name="建议人",
                    ),
                ),
                (
                    "proposed_at",
                    models.DateTimeField(auto_now_add=True, db_index=True, verbose_name="建议时间"),
                ),
                (
                    "reviewed_by",
                    models.CharField(blank=True, max_length=100, verbose_name="审核人"),
                ),
                (
                    "reviewed_at",
                    models.DateTimeField(blank=True, null=True, verbose_name="审核时间"),
                ),
                ("review_comment", models.TextField(blank=True, verbose_name="审核意见")),
                (
                    "executed_by",
                    models.CharField(blank=True, max_length=100, verbose_name="执行人"),
                ),
                (
                    "executed_at",
                    models.DateTimeField(blank=True, null=True, verbose_name="执行时间"),
                ),
                (
                    "execution_result",
                    models.JSONField(
                        blank=True,
                        help_text="实际执行的交易结果",
                        null=True,
                        verbose_name="执行结果",
                    ),
                ),
                (
                    "metadata",
                    models.JSONField(
                        blank=True,
                        default=dict,
                        help_text="额外的上下文信息",
                        verbose_name="元数据",
                    ),
                ),
                ("created_at", models.DateTimeField(auto_now_add=True, verbose_name="创建时间")),
                ("updated_at", models.DateTimeField(auto_now=True, verbose_name="更新时间")),
                (
                    "account",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="rebalance_proposals",
                        to="simulated_trading.simulatedaccountmodel",
                        verbose_name="所属账户",
                    ),
                ),
                (
                    "inspection_report",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="rebalance_proposals",
                        to="simulated_trading.dailyinspectionreportmodel",
                        verbose_name="关联巡检报告",
                    ),
                ),
                (
                    "strategy",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="rebalance_proposals",
                        to="strategy.strategymodel",
                        verbose_name="关联策略",
                    ),
                ),
            ],
            options={
                "verbose_name": "再平衡建议草案",
                "verbose_name_plural": "再平衡建议草案",
                "db_table": "simulated_rebalance_proposal",
                "ordering": ["-proposed_at"],
            },
        ),
        migrations.CreateModel(
            name="NotificationHistoryModel",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                (
                    "notification_type",
                    models.CharField(
                        db_index=True,
                        help_text="如: daily_inspection, rebalance_proposal, position_invalidated",
                        max_length=50,
                        verbose_name="通知类型",
                    ),
                ),
                (
                    "channel",
                    models.CharField(
                        choices=[
                            ("email", "邮件"),
                            ("in_app", "站内"),
                            ("alert", "告警"),
                            ("sms", "短信"),
                            ("webhook", "Webhook"),
                        ],
                        db_index=True,
                        max_length=20,
                        verbose_name="通知通道",
                    ),
                ),
                (
                    "recipient_user_id",
                    models.IntegerField(
                        blank=True, db_index=True, null=True, verbose_name="接收者用户ID"
                    ),
                ),
                (
                    "recipient_email",
                    models.EmailField(
                        blank=True, max_length=254, null=True, verbose_name="接收者邮箱"
                    ),
                ),
                ("subject", models.CharField(max_length=500, verbose_name="通知主题")),
                ("body", models.TextField(verbose_name="通知内容")),
                (
                    "status",
                    models.CharField(
                        choices=[
                            ("pending", "待发送"),
                            ("sending", "发送中"),
                            ("sent", "已发送"),
                            ("failed", "发送失败"),
                            ("retrying", "重试中"),
                        ],
                        db_index=True,
                        default="pending",
                        max_length=20,
                        verbose_name="发送状态",
                    ),
                ),
                ("error_message", models.TextField(blank=True, verbose_name="错误信息")),
                ("retry_count", models.IntegerField(default=0, verbose_name="重试次数")),
                (
                    "created_at",
                    models.DateTimeField(auto_now_add=True, db_index=True, verbose_name="创建时间"),
                ),
                ("sent_at", models.DateTimeField(blank=True, null=True, verbose_name="发送时间")),
                ("metadata", models.JSONField(blank=True, default=dict, verbose_name="元数据")),
                (
                    "account",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="notifications",
                        to="simulated_trading.simulatedaccountmodel",
                        verbose_name="关联账户",
                    ),
                ),
                (
                    "rebalance_proposal",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="notifications",
                        to="simulated_trading.rebalanceproposalmodel",
                        verbose_name="关联再平衡建议",
                    ),
                ),
            ],
            options={
                "verbose_name": "通知历史记录",
                "verbose_name_plural": "通知历史记录",
                "db_table": "simulated_notification_history",
                "ordering": ["-created_at"],
            },
        ),
        migrations.CreateModel(
            name="DailyNetValueModel",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("record_date", models.DateField(db_index=True, verbose_name="记录日期")),
                (
                    "net_value",
                    models.DecimalField(
                        decimal_places=4,
                        help_text="账户总资产（元）",
                        max_digits=15,
                        verbose_name="净值",
                    ),
                ),
                (
                    "cash",
                    models.DecimalField(
                        decimal_places=2,
                        help_text="可用现金（元）",
                        max_digits=15,
                        verbose_name="现金",
                    ),
                ),
                (
                    "market_value",
                    models.DecimalField(
                        decimal_places=2,
                        help_text="持仓市值（元）",
                        max_digits=15,
                        verbose_name="持仓市值",
                    ),
                ),
                (
                    "daily_return",
                    models.FloatField(
                        default=0.0, help_text="相对于前一日的收益率", verbose_name="日收益率(%)"
                    ),
                ),
                (
                    "cumulative_return",
                    models.FloatField(
                        default=0.0,
                        help_text="相对于初始资金的收益率",
                        verbose_name="累计收益率(%)",
                    ),
                ),
                (
                    "drawdown",
                    models.FloatField(
                        default=0.0, help_text="相对于历史最高点的回撤", verbose_name="回撤(%)"
                    ),
                ),
                ("total_trades", models.IntegerField(default=0, verbose_name="当日交易次数")),
                ("positions_count", models.IntegerField(default=0, verbose_name="持仓数量")),
                ("created_at", models.DateTimeField(auto_now_add=True, verbose_name="创建时间")),
                ("updated_at", models.DateTimeField(auto_now=True, verbose_name="更新时间")),
                (
                    "account",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="daily_net_values",
                        to="simulated_trading.simulatedaccountmodel",
                        verbose_name="所属账户",
                    ),
                ),
            ],
            options={
                "verbose_name": "每日净值记录",
                "verbose_name_plural": "每日净值记录",
                "db_table": "simulated_daily_net_value",
                "ordering": ["record_date"],
                "indexes": [
                    models.Index(
                        fields=["account", "-record_date"], name="simulated_d_account_fcb0a4_idx"
                    ),
                    models.Index(fields=["-record_date"], name="simulated_d_record__6cf433_idx"),
                ],
                "unique_together": {("account", "record_date")},
            },
        ),
        migrations.AddIndex(
            model_name="rebalanceproposalmodel",
            index=models.Index(
                fields=["account", "-proposed_at"], name="simulated_r_account_d2c53d_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="rebalanceproposalmodel",
            index=models.Index(
                fields=["status", "-proposed_at"], name="simulated_r_status_283502_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="rebalanceproposalmodel",
            index=models.Index(
                fields=["source", "-proposed_at"], name="simulated_r_source_752012_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="rebalanceproposalmodel",
            index=models.Index(
                fields=["priority", "-proposed_at"], name="simulated_r_priorit_595199_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="notificationhistorymodel",
            index=models.Index(
                fields=["account", "-created_at"], name="simulated_n_account_eca7d3_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="notificationhistorymodel",
            index=models.Index(
                fields=["status", "-created_at"], name="simulated_n_status_e55fb5_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="notificationhistorymodel",
            index=models.Index(
                fields=["channel", "-created_at"], name="simulated_n_channel_95a05e_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="notificationhistorymodel",
            index=models.Index(
                fields=["notification_type", "-created_at"], name="simulated_n_notific_9f57e6_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="notificationhistorymodel",
            index=models.Index(
                fields=["recipient_user_id", "-created_at"], name="simulated_n_recipie_4a7bed_idx"
            ),
        ),
    ]
