/**
 * Migration: add-member
 * Version: 25.1.0
 * Generated: 2025-12-07T06:23:51.845Z
 *
 * This migration was automatically generated by TypeORM
 * based on entity definition changes.
 */

import { MigrationInterface, QueryRunner } from "typeorm";

export class Migration1765088629599 implements MigrationInterface {
    name = "Migration1765088629599";

    public async up(queryRunner: QueryRunner): Promise<void> {
        // 如果类型已存在则跳过
        await queryRunner.query(
            `DO $$ BEGIN
                CREATE TYPE "public"."membership_plans_duration_config_enum" AS ENUM('1', '2', '3', '4', '5', '6');
            EXCEPTION
                WHEN duplicate_object THEN NULL;
            END $$;`,
        );

        await queryRunner.query(
            `CREATE TABLE IF NOT EXISTS "membership_levels" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "name" character varying(64) NOT NULL, "give_power" integer NOT NULL DEFAULT '0', "level" integer NOT NULL, "status" boolean NOT NULL DEFAULT true, "icon" character varying(255) NOT NULL DEFAULT '0', "description" character varying(255), "benefits" jsonb NOT NULL DEFAULT '[]', CONSTRAINT "PK_706a2a088cb5704950a0316e0a6" PRIMARY KEY ("id")); COMMENT ON COLUMN "membership_levels"."name" IS '会员等级名称'; COMMENT ON COLUMN "membership_levels"."give_power" IS '每月赠送的积分'; COMMENT ON COLUMN "membership_levels"."level" IS '会员等级级别'; COMMENT ON COLUMN "membership_levels"."status" IS '状态'; COMMENT ON COLUMN "membership_levels"."icon" IS '会员等级图标'; COMMENT ON COLUMN "membership_levels"."description" IS '会员等级描述'; COMMENT ON COLUMN "membership_levels"."benefits" IS '会员等权益'`,
        );
        await queryRunner.query(
            `CREATE INDEX IF NOT EXISTS "IDX_706a2a088cb5704950a0316e0a" ON "membership_levels" ("id") `,
        );
        await queryRunner.query(`COMMENT ON TABLE "membership_levels" IS '会员等级'`);
        await queryRunner.query(
            `CREATE TABLE IF NOT EXISTS "membership_order" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "order_no" character varying NOT NULL, "transaction_id" character varying(128), "user_id" uuid NOT NULL, "terminal" integer NOT NULL DEFAULT '1', "plan_id" character varying NOT NULL, "level_id" character varying NOT NULL, "plan_snap" jsonb NOT NULL, "level_snap" jsonb NOT NULL, "pay_type" integer NOT NULL, "duration" character varying(64) NOT NULL, "total_amount" numeric(10,2) NOT NULL, "order_amount" numeric(10,2) NOT NULL, "status" integer NOT NULL DEFAULT '0', "pay_state" integer NOT NULL DEFAULT '0', "pay_time" TIMESTAMP WITH TIME ZONE, "refund_status" integer NOT NULL DEFAULT '0', "refund_time" TIMESTAMP WITH TIME ZONE, CONSTRAINT "PK_83d50bf01e32d9e71a6856f6a9b" PRIMARY KEY ("id")); COMMENT ON COLUMN "membership_order"."order_no" IS '订单号'; COMMENT ON COLUMN "membership_order"."transaction_id" IS '第三方平台交易流水号'; COMMENT ON COLUMN "membership_order"."user_id" IS '用户ID'; COMMENT ON COLUMN "membership_order"."plan_id" IS '会员套餐ID'; COMMENT ON COLUMN "membership_order"."level_id" IS '会员等级ID'; COMMENT ON COLUMN "membership_order"."plan_snap" IS '会员套餐快照'; COMMENT ON COLUMN "membership_order"."level_snap" IS '会员等级快照'; COMMENT ON COLUMN "membership_order"."pay_type" IS '支付类型'; COMMENT ON COLUMN "membership_order"."duration" IS '会员时长(已格式化)'; COMMENT ON COLUMN "membership_order"."total_amount" IS '订单总价'; COMMENT ON COLUMN "membership_order"."order_amount" IS '实付金额'; COMMENT ON COLUMN "membership_order"."status" IS '订单主状态:0-待支付;1-订单完成;2-取消订单'; COMMENT ON COLUMN "membership_order"."pay_state" IS '支付状态：0-待支付;1-已支付'; COMMENT ON COLUMN "membership_order"."pay_time" IS '支付时间'; COMMENT ON COLUMN "membership_order"."refund_status" IS '退款状态:0-未退款;1-已退款'; COMMENT ON COLUMN "membership_order"."refund_time" IS '退款时间'`,
        );
        await queryRunner.query(
            `CREATE INDEX IF NOT EXISTS "IDX_83d50bf01e32d9e71a6856f6a9" ON "membership_order" ("id") `,
        );
        await queryRunner.query(
            `CREATE INDEX IF NOT EXISTS "IDX_ade8328e90ff05a0c627908ddc" ON "membership_order" ("order_no") `,
        );
        await queryRunner.query(
            `CREATE INDEX IF NOT EXISTS "IDX_1544d298304b92314d9ce7c080" ON "membership_order" ("user_id") `,
        );
        await queryRunner.query(
            `CREATE INDEX IF NOT EXISTS "IDX_78a66097c0fbec1b44f9ccfc5e" ON "membership_order" ("plan_id") `,
        );
        await queryRunner.query(
            `CREATE INDEX IF NOT EXISTS "IDX_c147874a31680a71702d6aed6e" ON "membership_order" ("level_id") `,
        );
        await queryRunner.query(`COMMENT ON TABLE "membership_order" IS '会员订单'`);
        await queryRunner.query(
            `CREATE INDEX IF NOT EXISTS "IDX_ec4e57f4138e339fb111948a16" ON "user_subscription" ("id") `,
        );
        await queryRunner.query(
            `CREATE INDEX IF NOT EXISTS "IDX_3c6b79d14e6539ddb486aab80f" ON "user_subscription" ("user_id") `,
        );
        await queryRunner.query(`COMMENT ON TABLE "user_subscription" IS '用户订阅'`);
        await queryRunner.query(
            `CREATE TABLE IF NOT EXISTS "membership_plans" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "name" character varying(64) NOT NULL, "label" character varying(64), "duration_config" "public"."membership_plans_duration_config_enum" NOT NULL DEFAULT '1', "duration" jsonb NOT NULL DEFAULT '{}', "billing" jsonb NOT NULL DEFAULT '[]', "status" boolean NOT NULL DEFAULT true, "sort" integer NOT NULL DEFAULT '0', CONSTRAINT "PK_85ca9d6f4262a6bbff2a540c640" PRIMARY KEY ("id")); COMMENT ON COLUMN "membership_plans"."name" IS '计划名称'; COMMENT ON COLUMN "membership_plans"."label" IS '计划标签'; COMMENT ON COLUMN "membership_plans"."duration_config" IS '订阅时长配置'; COMMENT ON COLUMN "membership_plans"."duration" IS '订阅时长'; COMMENT ON COLUMN "membership_plans"."billing" IS '会员计费'; COMMENT ON COLUMN "membership_plans"."status" IS '状态'; COMMENT ON COLUMN "membership_plans"."sort" IS '排序'`,
        );
        await queryRunner.query(
            `CREATE INDEX IF NOT EXISTS "IDX_85ca9d6f4262a6bbff2a540c64" ON "membership_plans" ("id") `,
        );
        await queryRunner.query(`COMMENT ON TABLE "membership_plans" IS '订阅计划'`);
        await queryRunner.query(
            `CREATE TABLE IF NOT EXISTS "extension_feature" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "feature_code" character varying(128) NOT NULL, "name" character varying(100) NOT NULL, "description" text, "extension_id" uuid NOT NULL, "status" boolean NOT NULL DEFAULT true, CONSTRAINT "UQ_9b04a3a1f4fa6d7d7c0b713d8be" UNIQUE ("feature_code"), CONSTRAINT "PK_e1085a3a1ff815a4d56c7387742" PRIMARY KEY ("id")); COMMENT ON COLUMN "extension_feature"."feature_code" IS '功能唯一标识符'; COMMENT ON COLUMN "extension_feature"."name" IS '功能名称'; COMMENT ON COLUMN "extension_feature"."description" IS '功能描述'; COMMENT ON COLUMN "extension_feature"."extension_id" IS '关联的插件ID'; COMMENT ON COLUMN "extension_feature"."status" IS '功能状态'`,
        );
        await queryRunner.query(
            `CREATE INDEX IF NOT EXISTS "IDX_e1085a3a1ff815a4d56c738774" ON "extension_feature" ("id") `,
        );
        await queryRunner.query(
            `CREATE INDEX IF NOT EXISTS "IDX_9b04a3a1f4fa6d7d7c0b713d8b" ON "extension_feature" ("feature_code") `,
        );
        await queryRunner.query(
            `CREATE INDEX IF NOT EXISTS "IDX_3a8510aa093db5c036e46fd9c4" ON "extension_feature" ("extension_id") `,
        );
        await queryRunner.query(`COMMENT ON TABLE "extension_feature" IS '插件功能配置'`);
        await queryRunner.query(
            `CREATE TABLE IF NOT EXISTS "model_membership_levels" ("model_id" uuid NOT NULL, "level_id" uuid NOT NULL, CONSTRAINT "PK_622157d412014aa5319f0ca5f64" PRIMARY KEY ("model_id", "level_id"))`,
        );
        await queryRunner.query(
            `CREATE INDEX IF NOT EXISTS "IDX_9c3405d21ea8adaab3c3ea38c9" ON "model_membership_levels" ("model_id") `,
        );
        await queryRunner.query(
            `CREATE INDEX IF NOT EXISTS "IDX_4ce1671490d99b882efd63e557" ON "model_membership_levels" ("level_id") `,
        );
        await queryRunner.query(
            `CREATE TABLE IF NOT EXISTS "extension_feature_membership_levels" ("feature_id" uuid NOT NULL, "level_id" uuid NOT NULL, CONSTRAINT "PK_a340d98a9eccf80cfddfbb173b6" PRIMARY KEY ("feature_id", "level_id"))`,
        );
        await queryRunner.query(
            `CREATE INDEX IF NOT EXISTS "IDX_55989bb6234bf58156003f11f5" ON "extension_feature_membership_levels" ("feature_id") `,
        );
        await queryRunner.query(
            `CREATE INDEX IF NOT EXISTS "IDX_996bcaf0d89288971d3fd18d58" ON "extension_feature_membership_levels" ("level_id") `,
        );
        await queryRunner.query(
            `ALTER TABLE "ai_models" ADD COLUMN IF NOT EXISTS "membership_level" jsonb NOT NULL DEFAULT '[]'`,
        );
        await queryRunner.query(`COMMENT ON COLUMN "ai_models"."membership_level" IS '会员等级'`);
        await queryRunner.query(
            `ALTER TABLE "extension" ADD COLUMN IF NOT EXISTS "alias" character varying(100)`,
        );
        await queryRunner.query(`COMMENT ON COLUMN "extension"."alias" IS '应用别名'`);
        await queryRunner.query(
            `ALTER TABLE "account_log" ADD COLUMN IF NOT EXISTS "expire_at" TIMESTAMP WITH TIME ZONE`,
        );
        await queryRunner.query(`COMMENT ON COLUMN "account_log"."expire_at" IS '积分过期时间'`);
        await queryRunner.query(
            `ALTER TABLE "account_log" ADD COLUMN IF NOT EXISTS "available_amount" integer NOT NULL DEFAULT '0'`,
        );
        await queryRunner.query(
            `COMMENT ON COLUMN "account_log"."available_amount" IS '当前记录剩余可用数量'`,
        );
        await queryRunner.query(
            `CREATE INDEX IF NOT EXISTS "IDX_a4080a4f503b11ad37c759f08d" ON "account_log" ("expire_at") `,
        );
        await queryRunner.query(
            `DO $$ BEGIN
                ALTER TABLE "membership_order" ADD CONSTRAINT "FK_1544d298304b92314d9ce7c0808" FOREIGN KEY ("user_id") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE NO ACTION;
            EXCEPTION
                WHEN duplicate_object THEN NULL;
            END $$;`,
        );
        await queryRunner.query(
            `DO $$ BEGIN
                ALTER TABLE "user_subscription" ADD CONSTRAINT "FK_353a0e40480ecd1b1edc9e892f9" FOREIGN KEY ("order_id") REFERENCES "membership_order"("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
            EXCEPTION
                WHEN duplicate_object THEN NULL;
            END $$;`,
        );
        await queryRunner.query(
            `DO $$ BEGIN
                ALTER TABLE "user_subscription" ADD CONSTRAINT "FK_3c6b79d14e6539ddb486aab80f5" FOREIGN KEY ("user_id") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE NO ACTION;
            EXCEPTION
                WHEN duplicate_object THEN NULL;
            END $$;`,
        );
        await queryRunner.query(
            `DO $$ BEGIN
                ALTER TABLE "user_subscription" ADD CONSTRAINT "FK_273c32785b2e09ccdd2649068fd" FOREIGN KEY ("level_id") REFERENCES "membership_levels"("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
            EXCEPTION
                WHEN duplicate_object THEN NULL;
            END $$;`,
        );
        await queryRunner.query(
            `DO $$ BEGIN
                ALTER TABLE "extension_feature" ADD CONSTRAINT "FK_3a8510aa093db5c036e46fd9c49" FOREIGN KEY ("extension_id") REFERENCES "extension"("id") ON DELETE CASCADE ON UPDATE NO ACTION;
            EXCEPTION
                WHEN duplicate_object THEN NULL;
            END $$;`,
        );
        await queryRunner.query(
            `DO $$ BEGIN
                ALTER TABLE "model_membership_levels" ADD CONSTRAINT "FK_9c3405d21ea8adaab3c3ea38c97" FOREIGN KEY ("model_id") REFERENCES "ai_models"("id") ON DELETE CASCADE ON UPDATE CASCADE;
            EXCEPTION
                WHEN duplicate_object THEN NULL;
            END $$;`,
        );
        await queryRunner.query(
            `DO $$ BEGIN
                ALTER TABLE "model_membership_levels" ADD CONSTRAINT "FK_4ce1671490d99b882efd63e5578" FOREIGN KEY ("level_id") REFERENCES "membership_levels"("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
            EXCEPTION
                WHEN duplicate_object THEN NULL;
            END $$;`,
        );
        await queryRunner.query(
            `DO $$ BEGIN
                ALTER TABLE "extension_feature_membership_levels" ADD CONSTRAINT "FK_55989bb6234bf58156003f11f5b" FOREIGN KEY ("feature_id") REFERENCES "extension_feature"("id") ON DELETE CASCADE ON UPDATE CASCADE;
            EXCEPTION
                WHEN duplicate_object THEN NULL;
            END $$;`,
        );
        await queryRunner.query(
            `DO $$ BEGIN
                ALTER TABLE "extension_feature_membership_levels" ADD CONSTRAINT "FK_996bcaf0d89288971d3fd18d585" FOREIGN KEY ("level_id") REFERENCES "membership_levels"("id") ON DELETE CASCADE ON UPDATE CASCADE;
            EXCEPTION
                WHEN duplicate_object THEN NULL;
            END $$;`,
        );
    }

    public async down(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(
            `ALTER TABLE "extension_feature_membership_levels" DROP CONSTRAINT "FK_996bcaf0d89288971d3fd18d585"`,
        );
        await queryRunner.query(
            `ALTER TABLE "extension_feature_membership_levels" DROP CONSTRAINT "FK_55989bb6234bf58156003f11f5b"`,
        );
        await queryRunner.query(
            `ALTER TABLE "model_membership_levels" DROP CONSTRAINT "FK_4ce1671490d99b882efd63e5578"`,
        );
        await queryRunner.query(
            `ALTER TABLE "model_membership_levels" DROP CONSTRAINT "FK_9c3405d21ea8adaab3c3ea38c97"`,
        );
        await queryRunner.query(
            `ALTER TABLE "extension_feature" DROP CONSTRAINT "FK_3a8510aa093db5c036e46fd9c49"`,
        );
        await queryRunner.query(
            `ALTER TABLE "user_subscription" DROP CONSTRAINT "FK_273c32785b2e09ccdd2649068fd"`,
        );
        await queryRunner.query(
            `ALTER TABLE "user_subscription" DROP CONSTRAINT "FK_3c6b79d14e6539ddb486aab80f5"`,
        );
        await queryRunner.query(
            `ALTER TABLE "user_subscription" DROP CONSTRAINT "FK_353a0e40480ecd1b1edc9e892f9"`,
        );
        await queryRunner.query(
            `ALTER TABLE "membership_order" DROP CONSTRAINT "FK_1544d298304b92314d9ce7c0808"`,
        );
        await queryRunner.query(`DROP INDEX "public"."IDX_a4080a4f503b11ad37c759f08d"`);
        await queryRunner.query(
            `COMMENT ON COLUMN "account_log"."available_amount" IS '当前记录剩余可用数量'`,
        );
        await queryRunner.query(`ALTER TABLE "account_log" DROP COLUMN "available_amount"`);
        await queryRunner.query(`COMMENT ON COLUMN "account_log"."expire_at" IS '积分过期时间'`);
        await queryRunner.query(`ALTER TABLE "account_log" DROP COLUMN "expire_at"`);
        await queryRunner.query(`COMMENT ON COLUMN "extension"."alias" IS '应用别名'`);
        await queryRunner.query(`ALTER TABLE "extension" DROP COLUMN "alias"`);
        await queryRunner.query(`COMMENT ON COLUMN "ai_models"."membership_level" IS '会员等级'`);
        await queryRunner.query(`ALTER TABLE "ai_models" DROP COLUMN "membership_level"`);
        await queryRunner.query(
            `ALTER TABLE "permissions" ADD "plugin_pack_name" character varying`,
        );
        await queryRunner.query(`DROP INDEX "public"."IDX_996bcaf0d89288971d3fd18d58"`);
        await queryRunner.query(`DROP INDEX "public"."IDX_55989bb6234bf58156003f11f5"`);
        await queryRunner.query(`DROP TABLE "extension_feature_membership_levels"`);
        await queryRunner.query(`DROP INDEX "public"."IDX_4ce1671490d99b882efd63e557"`);
        await queryRunner.query(`DROP INDEX "public"."IDX_9c3405d21ea8adaab3c3ea38c9"`);
        await queryRunner.query(`DROP TABLE "model_membership_levels"`);
        await queryRunner.query(`COMMENT ON TABLE "extension_feature" IS NULL`);
        await queryRunner.query(`DROP INDEX "public"."IDX_3a8510aa093db5c036e46fd9c4"`);
        await queryRunner.query(`DROP INDEX "public"."IDX_9b04a3a1f4fa6d7d7c0b713d8b"`);
        await queryRunner.query(`DROP INDEX "public"."IDX_e1085a3a1ff815a4d56c738774"`);
        await queryRunner.query(`DROP TABLE "extension_feature"`);
        await queryRunner.query(`COMMENT ON TABLE "membership_plans" IS NULL`);
        await queryRunner.query(`DROP INDEX "public"."IDX_85ca9d6f4262a6bbff2a540c64"`);
        await queryRunner.query(`DROP TABLE "membership_plans"`);
        await queryRunner.query(`DROP TYPE "public"."membership_plans_duration_config_enum"`);
        await queryRunner.query(`COMMENT ON TABLE "user_subscription" IS NULL`);
        await queryRunner.query(`DROP INDEX "public"."IDX_3c6b79d14e6539ddb486aab80f"`);
        await queryRunner.query(`DROP INDEX "public"."IDX_ec4e57f4138e339fb111948a16"`);
        await queryRunner.query(`DROP TABLE "user_subscription"`);
        await queryRunner.query(`COMMENT ON TABLE "membership_order" IS NULL`);
        await queryRunner.query(`DROP INDEX "public"."IDX_c147874a31680a71702d6aed6e"`);
        await queryRunner.query(`DROP INDEX "public"."IDX_78a66097c0fbec1b44f9ccfc5e"`);
        await queryRunner.query(`DROP INDEX "public"."IDX_1544d298304b92314d9ce7c080"`);
        await queryRunner.query(`DROP INDEX "public"."IDX_ade8328e90ff05a0c627908ddc"`);
        await queryRunner.query(`DROP INDEX "public"."IDX_83d50bf01e32d9e71a6856f6a9"`);
        await queryRunner.query(`DROP TABLE "membership_order"`);
        await queryRunner.query(`COMMENT ON TABLE "membership_levels" IS NULL`);
        await queryRunner.query(`DROP INDEX "public"."IDX_706a2a088cb5704950a0316e0a"`);
        await queryRunner.query(`DROP TABLE "membership_levels"`);
    }
}
