diff --git a/backend/services/auth-service/prisma/migrations/0001_init/migration.sql b/backend/services/auth-service/prisma/migrations/0001_init/migration.sql index 0f9457eb..56d60f3e 100644 --- a/backend/services/auth-service/prisma/migrations/0001_init/migration.sql +++ b/backend/services/auth-service/prisma/migrations/0001_init/migration.sql @@ -1,7 +1,6 @@ -- ============================================================================ -- auth-service 初始化 migration --- 合并自: 20260111000000_init, 20260111083500_allow_nullable_phone_password, --- 20260112110000_add_nickname_to_synced_legacy_users +-- 合并自: 0001_init, 0002_add_transactional_idempotency -- ============================================================================ -- CreateEnum @@ -241,3 +240,26 @@ ALTER TABLE "sms_logs" ADD CONSTRAINT "sms_logs_user_id_fkey" FOREIGN KEY ("user -- AddForeignKey ALTER TABLE "login_logs" ADD CONSTRAINT "login_logs_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE; + +-- ============================================================================ +-- 事务性幂等消费支持 (从 0002_add_transactional_idempotency 合并) +-- 用于 1.0 -> 2.0 CDC 同步的 100% exactly-once 语义 +-- ============================================================================ + +-- CreateTable +CREATE TABLE "processed_cdc_events" ( + "id" BIGSERIAL NOT NULL, + "source_topic" TEXT NOT NULL, + "offset" BIGINT NOT NULL, + "table_name" TEXT NOT NULL, + "operation" TEXT NOT NULL, + "processed_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "processed_cdc_events_pkey" PRIMARY KEY ("id") +); + +-- CreateIndex (复合唯一索引保证幂等性) +CREATE UNIQUE INDEX "processed_cdc_events_source_topic_offset_key" ON "processed_cdc_events"("source_topic", "offset"); + +-- CreateIndex (时间索引用于清理旧数据) +CREATE INDEX "processed_cdc_events_processed_at_idx" ON "processed_cdc_events"("processed_at"); diff --git a/backend/services/auth-service/prisma/migrations/0002_add_transactional_idempotency/migration.sql b/backend/services/auth-service/prisma/migrations/0002_add_transactional_idempotency/migration.sql deleted file mode 100644 index 8004933a..00000000 --- a/backend/services/auth-service/prisma/migrations/0002_add_transactional_idempotency/migration.sql +++ /dev/null @@ -1,25 +0,0 @@ --- ============================================================================ --- 添加事务性幂等消费支持 --- 用于 1.0 -> 2.0 CDC 同步的 100% exactly-once 语义 --- ============================================================================ - --- 创建 processed_cdc_events 表(用于 CDC 事件幂等) --- 唯一键: (source_topic, offset) - Kafka topic 名称 + 消息偏移量 --- 用于保证每个 CDC 事件只处理一次(exactly-once 语义) -CREATE TABLE IF NOT EXISTS "processed_cdc_events" ( - "id" BIGSERIAL NOT NULL, - "source_topic" VARCHAR(200) NOT NULL, -- Kafka topic 名称(如 cdc.identity.public.user_accounts) - "offset" BIGINT NOT NULL, -- Kafka 消息偏移量(在 partition 内唯一) - "table_name" VARCHAR(100) NOT NULL, -- 源表名 - "operation" VARCHAR(10) NOT NULL, -- CDC 操作类型: c(create), u(update), d(delete), r(snapshot read) - "processed_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - - CONSTRAINT "processed_cdc_events_pkey" PRIMARY KEY ("id") -); - --- 复合唯一索引:(source_topic, offset) 保证幂等性 --- 注意:这不是数据库自增 ID,而是 Kafka 消息的唯一标识 -CREATE UNIQUE INDEX "processed_cdc_events_source_topic_offset_key" ON "processed_cdc_events"("source_topic", "offset"); - --- 时间索引用于清理旧数据 -CREATE INDEX "processed_cdc_events_processed_at_idx" ON "processed_cdc_events"("processed_at"); diff --git a/backend/services/contribution-service/prisma/migrations/0001_init/migration.sql b/backend/services/contribution-service/prisma/migrations/0001_init/migration.sql index 60dd28d7..e1c68e1a 100644 --- a/backend/services/contribution-service/prisma/migrations/0001_init/migration.sql +++ b/backend/services/contribution-service/prisma/migrations/0001_init/migration.sql @@ -1,7 +1,6 @@ -- ============================================================================ -- contribution-service 初始化 migration --- 合并自: 20260111000000_init, 20260111100000_add_referral_user_ids, --- 20260112020000_fix_status_varchar_length, 20260112200000_add_adoption_province_city +-- 合并自: 0001_init, 0002_add_transactional_idempotency, 20250120000001_add_region_to_system_accounts -- ============================================================================ -- ============================================ @@ -223,13 +222,15 @@ CREATE INDEX "unallocated_contributions_unalloc_type_idx" ON "unallocated_contri CREATE INDEX "unallocated_contributions_status_idx" ON "unallocated_contributions"("status"); -- ============================================ --- 6. 系统账户表 +-- 6. 系统账户表 (支持按省市细分) -- ============================================ CREATE TABLE "system_accounts" ( "id" BIGSERIAL NOT NULL, - "account_type" VARCHAR(20) NOT NULL, - "name" VARCHAR(100) NOT NULL, + "account_type" TEXT NOT NULL, + "base_type" TEXT NOT NULL, + "region_code" TEXT, + "name" TEXT NOT NULL, "contribution_balance" DECIMAL(30,10) NOT NULL DEFAULT 0, "contribution_never_expires" BOOLEAN NOT NULL DEFAULT false, "version" INTEGER NOT NULL DEFAULT 1, @@ -240,6 +241,8 @@ CREATE TABLE "system_accounts" ( ); CREATE UNIQUE INDEX "system_accounts_account_type_key" ON "system_accounts"("account_type"); +CREATE INDEX "system_accounts_base_type_idx" ON "system_accounts"("base_type"); +CREATE INDEX "system_accounts_region_code_idx" ON "system_accounts"("region_code"); CREATE TABLE "system_contribution_records" ( "id" BIGSERIAL NOT NULL, @@ -327,20 +330,36 @@ CREATE TABLE "cdc_sync_progress" ( CREATE UNIQUE INDEX "cdc_sync_progress_source_topic_key" ON "cdc_sync_progress"("source_topic"); +-- 2.0 服务间 Outbox 事件幂等表 CREATE TABLE "processed_events" ( "id" BIGSERIAL NOT NULL, "event_id" VARCHAR(100) NOT NULL, "event_type" VARCHAR(50) NOT NULL, - "source_service" VARCHAR(50), + "source_service" VARCHAR(100) NOT NULL, "processed_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT "processed_events_pkey" PRIMARY KEY ("id") ); -CREATE UNIQUE INDEX "processed_events_event_id_key" ON "processed_events"("event_id"); +CREATE UNIQUE INDEX "processed_events_source_service_event_id_key" ON "processed_events"("source_service", "event_id"); CREATE INDEX "processed_events_event_type_idx" ON "processed_events"("event_type"); CREATE INDEX "processed_events_processed_at_idx" ON "processed_events"("processed_at"); +-- 1.0 CDC 事件幂等表 +CREATE TABLE "processed_cdc_events" ( + "id" BIGSERIAL NOT NULL, + "source_topic" VARCHAR(200) NOT NULL, + "offset" BIGINT NOT NULL, + "table_name" VARCHAR(100) NOT NULL, + "operation" VARCHAR(10) NOT NULL, + "processed_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "processed_cdc_events_pkey" PRIMARY KEY ("id") +); + +CREATE UNIQUE INDEX "processed_cdc_events_source_topic_offset_key" ON "processed_cdc_events"("source_topic", "offset"); +CREATE INDEX "processed_cdc_events_processed_at_idx" ON "processed_cdc_events"("processed_at"); + -- ============================================ -- 9. 配置表 -- ============================================ diff --git a/backend/services/contribution-service/prisma/migrations/0002_add_transactional_idempotency/migration.sql b/backend/services/contribution-service/prisma/migrations/0002_add_transactional_idempotency/migration.sql deleted file mode 100644 index 529847de..00000000 --- a/backend/services/contribution-service/prisma/migrations/0002_add_transactional_idempotency/migration.sql +++ /dev/null @@ -1,45 +0,0 @@ --- ============================================================================ --- 添加事务性幂等消费支持 --- 用于 1.0 -> 2.0 CDC 同步的 100% exactly-once 语义 --- ============================================================================ - --- 1. 创建 processed_cdc_events 表(用于 CDC 事件幂等) --- 唯一键: (source_topic, offset) - Kafka topic 名称 + 消息偏移量 --- 用于保证每个 CDC 事件只处理一次(exactly-once 语义) -CREATE TABLE IF NOT EXISTS "processed_cdc_events" ( - "id" BIGSERIAL NOT NULL, - "source_topic" VARCHAR(200) NOT NULL, -- Kafka topic 名称(如 cdc.identity.public.user_accounts) - "offset" BIGINT NOT NULL, -- Kafka 消息偏移量(在 partition 内唯一) - "table_name" VARCHAR(100) NOT NULL, -- 源表名 - "operation" VARCHAR(10) NOT NULL, -- CDC 操作类型: c(create), u(update), d(delete), r(snapshot read) - "processed_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - - CONSTRAINT "processed_cdc_events_pkey" PRIMARY KEY ("id") -); - --- 复合唯一索引:(source_topic, offset) 保证幂等性 --- 注意:这不是数据库自增 ID,而是 Kafka 消息的唯一标识 -CREATE UNIQUE INDEX "processed_cdc_events_source_topic_offset_key" ON "processed_cdc_events"("source_topic", "offset"); - --- 时间索引用于清理旧数据 -CREATE INDEX "processed_cdc_events_processed_at_idx" ON "processed_cdc_events"("processed_at"); - --- 2. 修复 processed_events 表(用于 2.0 服务间 Outbox 事件幂等) --- 唯一键: (source_service, event_id) - 服务名 + outbox 表的 ID --- 不同服务的 outbox ID 可能相同,所以需要组合服务名作为复合唯一键 - --- 2.1 修改 source_service 列:扩展长度 50->100,且设为 NOT NULL --- 先为已有 NULL 值设置默认值 -UPDATE "processed_events" SET "source_service" = 'unknown' WHERE "source_service" IS NULL; - --- 修改列类型和约束 -ALTER TABLE "processed_events" - ALTER COLUMN "source_service" SET NOT NULL, - ALTER COLUMN "source_service" TYPE VARCHAR(100); - --- 2.2 删除旧的单字段唯一索引 -DROP INDEX IF EXISTS "processed_events_event_id_key"; - --- 2.3 创建新的复合唯一索引 --- 索引名使用蛇形命名以与列名保持一致 -CREATE UNIQUE INDEX IF NOT EXISTS "processed_events_source_service_event_id_key" ON "processed_events"("source_service", "event_id"); diff --git a/backend/services/contribution-service/prisma/migrations/20250120000001_add_region_to_system_accounts/migration.sql b/backend/services/contribution-service/prisma/migrations/20250120000001_add_region_to_system_accounts/migration.sql deleted file mode 100644 index 6411f23a..00000000 --- a/backend/services/contribution-service/prisma/migrations/20250120000001_add_region_to_system_accounts/migration.sql +++ /dev/null @@ -1,16 +0,0 @@ --- 系统账户按省市细分: 添加 base_type 和 region_code 字段 --- 将 accountType 从简单枚举改为组合键(如 PROVINCE_440000, CITY_440100) - --- 1. 添加新字段 (使用 TEXT 类型与 Prisma String 一致) -ALTER TABLE "system_accounts" ADD COLUMN "base_type" TEXT; -ALTER TABLE "system_accounts" ADD COLUMN "region_code" TEXT; - --- 2. 设置现有数据的 base_type(与 account_type 相同) -UPDATE "system_accounts" SET "base_type" = "account_type" WHERE "base_type" IS NULL; - --- 3. 将 base_type 设置为非空 -ALTER TABLE "system_accounts" ALTER COLUMN "base_type" SET NOT NULL; - --- 4. 创建索引(Prisma 默认命名格式: {table}_{field}_idx) -CREATE INDEX IF NOT EXISTS "system_accounts_base_type_idx" ON "system_accounts"("base_type"); -CREATE INDEX IF NOT EXISTS "system_accounts_region_code_idx" ON "system_accounts"("region_code"); diff --git a/backend/services/mining-admin-service/prisma/migrations/0001_init/migration.sql b/backend/services/mining-admin-service/prisma/migrations/0001_init/migration.sql index 51ee26f3..496c547c 100644 --- a/backend/services/mining-admin-service/prisma/migrations/0001_init/migration.sql +++ b/backend/services/mining-admin-service/prisma/migrations/0001_init/migration.sql @@ -1,8 +1,8 @@ -- ============================================================================ -- mining-admin-service 初始化 migration --- 合并自: 20260111000000_init, 20260112110000_add_referral_adoption_nickname, --- 20260112150000_add_unlocked_bonus_tiers, 20260112200000_add_contribution_records_network_progress, --- 20260113000000_use_prisma_relation_mode, 20260113100000_add_distribution_summary +-- 合并自: 0001_init, 0002_fix_processed_event_composite_key, +-- 20250120000001_add_region_to_synced_system_contributions, +-- 20250120000002_add_synced_system_contribution_records -- 注意: 使用 Prisma relationMode = "prisma",不在数据库层创建FK约束 -- ============================================================================ @@ -306,6 +306,8 @@ CREATE TABLE "synced_circulation_pools" ( CREATE TABLE "synced_system_contributions" ( "id" TEXT NOT NULL, "accountType" TEXT NOT NULL, + "base_type" TEXT NOT NULL DEFAULT '', + "region_code" TEXT, "name" TEXT NOT NULL, "contributionBalance" DECIMAL(30,8) NOT NULL DEFAULT 0, "contributionNeverExpires" BOOLEAN NOT NULL DEFAULT false, @@ -690,17 +692,18 @@ CREATE UNIQUE INDEX "synced_day_klines_klineDate_key" ON "synced_day_klines"("kl -- CreateIndex CREATE UNIQUE INDEX "synced_system_contributions_accountType_key" ON "synced_system_contributions"("accountType"); +-- CreateIndex (base_type 和 region_code 索引) +CREATE INDEX "synced_system_contributions_base_type_idx" ON "synced_system_contributions"("base_type"); +CREATE INDEX "synced_system_contributions_region_code_idx" ON "synced_system_contributions"("region_code"); + -- CreateIndex CREATE UNIQUE INDEX "cdc_sync_progress_sourceTopic_key" ON "cdc_sync_progress"("sourceTopic"); -- CreateIndex CREATE INDEX "cdc_sync_progress_sourceService_idx" ON "cdc_sync_progress"("sourceService"); --- CreateIndex -CREATE UNIQUE INDEX "processed_events_eventId_key" ON "processed_events"("eventId"); - --- CreateIndex -CREATE INDEX "processed_events_sourceService_idx" ON "processed_events"("sourceService"); +-- CreateIndex (使用复合唯一键替代单独的 eventId 唯一约束) +CREATE UNIQUE INDEX "processed_events_sourceService_eventId_key" ON "processed_events"("sourceService", "eventId"); -- CreateIndex CREATE INDEX "processed_events_processedAt_idx" ON "processed_events"("processedAt"); @@ -860,3 +863,34 @@ CREATE UNIQUE INDEX "synced_fee_configs_fee_type_key" ON "synced_fee_configs"("f -- AddForeignKey (保留 admin 相关的外键) ALTER TABLE "audit_logs" ADD CONSTRAINT "audit_logs_adminId_fkey" FOREIGN KEY ("adminId") REFERENCES "admin_users"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- ============================================================================ +-- 系统账户算力明细同步表 +-- 用于存储从 contribution-service 同步的系统账户算力来源明细 +-- ============================================================================ + +-- CreateTable +CREATE TABLE "synced_system_contribution_records" ( + "id" TEXT NOT NULL, + "original_record_id" BIGINT NOT NULL, + "system_account_type" TEXT NOT NULL, + "source_adoption_id" BIGINT NOT NULL, + "source_account_sequence" TEXT NOT NULL, + "distribution_rate" DECIMAL(10,6) NOT NULL, + "amount" DECIMAL(30,10) NOT NULL, + "effective_date" DATE NOT NULL, + "expire_date" DATE, + "is_expired" BOOLEAN NOT NULL DEFAULT false, + "created_at" TIMESTAMP(3) NOT NULL, + "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "synced_system_contribution_records_pkey" PRIMARY KEY ("id") +); + +-- CreateIndex +CREATE UNIQUE INDEX "synced_system_contribution_records_original_record_id_key" ON "synced_system_contribution_records"("original_record_id"); +CREATE INDEX "synced_system_contribution_records_system_account_type_idx" ON "synced_system_contribution_records"("system_account_type"); +CREATE INDEX "synced_system_contribution_records_source_adoption_id_idx" ON "synced_system_contribution_records"("source_adoption_id"); +CREATE INDEX "synced_system_contribution_records_source_account_sequence_idx" ON "synced_system_contribution_records"("source_account_sequence"); +CREATE INDEX "synced_system_contribution_records_created_at_idx" ON "synced_system_contribution_records"("created_at" DESC); diff --git a/backend/services/mining-admin-service/prisma/migrations/0002_fix_processed_event_composite_key/migration.sql b/backend/services/mining-admin-service/prisma/migrations/0002_fix_processed_event_composite_key/migration.sql deleted file mode 100644 index d67e3d1a..00000000 --- a/backend/services/mining-admin-service/prisma/migrations/0002_fix_processed_event_composite_key/migration.sql +++ /dev/null @@ -1,26 +0,0 @@ --- ============================================================================ --- 修复 processed_events 表的幂等键 --- 用于 2.0 服务间 Outbox 事件的 100% exactly-once 语义 --- ============================================================================ --- --- 问题: 原来使用 eventId 作为唯一键,但不同服务的 outbox ID 可能相同 --- 解决: 使用 (sourceService, eventId) 作为复合唯一键 --- --- 唯一键说明: --- - sourceService: 发送事件的服务名(如 "auth-service", "contribution-service") --- - eventId: 发送方 outbox 表的自增 ID(非 UUID,而是数据库自增主键) --- - 组合后在全局唯一,可用于精确追踪事件来源 --- ============================================================================ - --- 先清空已有数据(因为之前的数据可能有冲突) -TRUNCATE TABLE "processed_events"; - --- 删除旧的唯一索引(仅 eventId) -DROP INDEX IF EXISTS "processed_events_eventId_key"; - --- 删除旧的 sourceService 普通索引 -DROP INDEX IF EXISTS "processed_events_sourceService_idx"; - --- 创建新的复合唯一索引:(sourceService, eventId) --- 这个组合保证跨服务的唯一性 -CREATE UNIQUE INDEX "processed_events_sourceService_eventId_key" ON "processed_events"("sourceService", "eventId"); diff --git a/backend/services/mining-admin-service/prisma/migrations/20250120000001_add_region_to_synced_system_contributions/migration.sql b/backend/services/mining-admin-service/prisma/migrations/20250120000001_add_region_to_synced_system_contributions/migration.sql deleted file mode 100644 index c9cfecd7..00000000 --- a/backend/services/mining-admin-service/prisma/migrations/20250120000001_add_region_to_synced_system_contributions/migration.sql +++ /dev/null @@ -1,19 +0,0 @@ --- 同步的系统账户算力表按省市细分: 添加 base_type 和 region_code 字段 --- 支持组合键(如 PROVINCE_440000, CITY_440100) - --- 1. 添加新字段 (使用 TEXT 类型与 Prisma String 一致) -ALTER TABLE "synced_system_contributions" ADD COLUMN "base_type" TEXT NOT NULL DEFAULT ''; -ALTER TABLE "synced_system_contributions" ADD COLUMN "region_code" TEXT; - --- 2. 设置现有数据的 base_type(根据 account_type 提取) -UPDATE "synced_system_contributions" -SET "base_type" = CASE - WHEN "account_type" LIKE 'PROVINCE_%' THEN 'PROVINCE' - WHEN "account_type" LIKE 'CITY_%' THEN 'CITY' - ELSE "account_type" -END -WHERE "base_type" = ''; - --- 3. 创建索引(Prisma 默认命名格式: {table}_{field}_idx) -CREATE INDEX IF NOT EXISTS "synced_system_contributions_base_type_idx" ON "synced_system_contributions"("base_type"); -CREATE INDEX IF NOT EXISTS "synced_system_contributions_region_code_idx" ON "synced_system_contributions"("region_code"); diff --git a/backend/services/mining-admin-service/prisma/migrations/20250120000002_add_synced_system_contribution_records/migration.sql b/backend/services/mining-admin-service/prisma/migrations/20250120000002_add_synced_system_contribution_records/migration.sql deleted file mode 100644 index 464dd896..00000000 --- a/backend/services/mining-admin-service/prisma/migrations/20250120000002_add_synced_system_contribution_records/migration.sql +++ /dev/null @@ -1,30 +0,0 @@ --- 添加系统账户算力明细同步表 --- 用于存储从 contribution-service 同步的系统账户算力来源明细 - --- 1. 创建 synced_system_contribution_records 表 -CREATE TABLE IF NOT EXISTS "synced_system_contribution_records" ( - "id" TEXT NOT NULL, - "original_record_id" BIGINT NOT NULL, - "system_account_type" TEXT NOT NULL, - "source_adoption_id" BIGINT NOT NULL, - "source_account_sequence" TEXT NOT NULL, - "distribution_rate" DECIMAL(10,6) NOT NULL, - "amount" DECIMAL(30,10) NOT NULL, - "effective_date" DATE NOT NULL, - "expire_date" DATE, - "is_expired" BOOLEAN NOT NULL DEFAULT false, - "created_at" TIMESTAMP(3) NOT NULL, - "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - "updatedAt" TIMESTAMP(3) NOT NULL, - - CONSTRAINT "synced_system_contribution_records_pkey" PRIMARY KEY ("id") -); - --- 2. 创建唯一索引(原始记录ID唯一) -CREATE UNIQUE INDEX IF NOT EXISTS "synced_system_contribution_records_original_record_id_key" ON "synced_system_contribution_records"("original_record_id"); - --- 3. 创建查询索引 -CREATE INDEX IF NOT EXISTS "synced_system_contribution_records_system_account_type_idx" ON "synced_system_contribution_records"("system_account_type"); -CREATE INDEX IF NOT EXISTS "synced_system_contribution_records_source_adoption_id_idx" ON "synced_system_contribution_records"("source_adoption_id"); -CREATE INDEX IF NOT EXISTS "synced_system_contribution_records_source_account_sequence_idx" ON "synced_system_contribution_records"("source_account_sequence"); -CREATE INDEX IF NOT EXISTS "synced_system_contribution_records_created_at_idx" ON "synced_system_contribution_records"("created_at" DESC); diff --git a/backend/services/mining-service/prisma/migrations/0001_init/migration.sql b/backend/services/mining-service/prisma/migrations/0001_init/migration.sql index 5f5c6655..443e3c81 100644 --- a/backend/services/mining-service/prisma/migrations/0001_init/migration.sql +++ b/backend/services/mining-service/prisma/migrations/0001_init/migration.sql @@ -1,6 +1,7 @@ -- ============================================================================ -- mining-service 初始化 migration --- 合并自: 20260111000000_init (只有一个,无需合并) +-- 合并自: 0001_init, 0002_minute_to_second, 0003_add_system_accounts_and_pending_mining, +-- 20250120000001_add_region_to_system_mining_accounts -- ============================================================================ -- CreateEnum @@ -21,7 +22,11 @@ CREATE TABLE "mining_configs" ( "halvingPeriodYears" INTEGER NOT NULL DEFAULT 2, "currentEra" INTEGER NOT NULL DEFAULT 1, "eraStartDate" TIMESTAMP(3) NOT NULL, - "minuteDistribution" DECIMAL(30,18) NOT NULL, + "secondDistribution" DECIMAL(30,18) NOT NULL, + "network_total_contribution" DECIMAL(30, 8) NOT NULL DEFAULT 0, + "total_tree_count" INTEGER NOT NULL DEFAULT 0, + "contribution_per_tree" DECIMAL(20, 10) NOT NULL DEFAULT 22617, + "network_last_synced_at" TIMESTAMP(3), "isActive" BOOLEAN NOT NULL DEFAULT false, "activatedAt" TIMESTAMP(3), "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, @@ -38,7 +43,7 @@ CREATE TABLE "mining_eras" ( "endDate" TIMESTAMP(3), "initialDistribution" DECIMAL(30,8) NOT NULL, "totalDistributed" DECIMAL(30,8) NOT NULL DEFAULT 0, - "minuteDistribution" DECIMAL(30,18) NOT NULL, + "secondDistribution" DECIMAL(30,18) NOT NULL, "isActive" BOOLEAN NOT NULL DEFAULT true, "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, @@ -67,7 +72,7 @@ CREATE TABLE "mining_records" ( "miningMinute" TIMESTAMP(3) NOT NULL, "contributionRatio" DECIMAL(30,18) NOT NULL, "totalContribution" DECIMAL(30,8) NOT NULL, - "minuteDistribution" DECIMAL(30,18) NOT NULL, + "secondDistribution" DECIMAL(30,18) NOT NULL, "minedAmount" DECIMAL(30,18) NOT NULL, "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, @@ -94,6 +99,91 @@ CREATE TABLE "mining_transactions" ( CONSTRAINT "mining_transactions_pkey" PRIMARY KEY ("id") ); +-- CreateTable: 系统挖矿账户 (直接使用 TEXT 组合键) +CREATE TABLE "system_mining_accounts" ( + "id" TEXT NOT NULL, + "account_type" TEXT NOT NULL, + "base_type" TEXT NOT NULL, + "region_code" TEXT, + "name" TEXT NOT NULL, + "totalMined" DECIMAL(30, 8) NOT NULL DEFAULT 0, + "availableBalance" DECIMAL(30, 8) NOT NULL DEFAULT 0, + "totalContribution" DECIMAL(30, 8) NOT NULL DEFAULT 0, + "last_synced_at" TIMESTAMP(3), + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updated_at" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "system_mining_accounts_pkey" PRIMARY KEY ("id") +); + +-- CreateTable: 系统账户挖矿记录 +CREATE TABLE "system_mining_records" ( + "id" TEXT NOT NULL, + "account_type" TEXT NOT NULL, + "mining_minute" TIMESTAMP(3) NOT NULL, + "contribution_ratio" DECIMAL(30, 18) NOT NULL, + "total_contribution" DECIMAL(30, 8) NOT NULL, + "second_distribution" DECIMAL(30, 18) NOT NULL, + "mined_amount" DECIMAL(30, 18) NOT NULL, + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "system_mining_records_pkey" PRIMARY KEY ("id") +); + +-- CreateTable: 系统账户交易流水 +CREATE TABLE "system_mining_transactions" ( + "id" TEXT NOT NULL, + "account_type" TEXT NOT NULL, + "type" TEXT NOT NULL, + "amount" DECIMAL(30, 8) NOT NULL, + "balance_before" DECIMAL(30, 8) NOT NULL, + "balance_after" DECIMAL(30, 8) NOT NULL, + "reference_id" TEXT, + "reference_type" TEXT, + "memo" TEXT, + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "system_mining_transactions_pkey" PRIMARY KEY ("id") +); + +-- CreateTable: 待解锁算力挖矿 +CREATE TABLE "pending_contribution_mining" ( + "id" BIGSERIAL NOT NULL, + "source_adoption_id" BIGINT NOT NULL, + "source_account_sequence" VARCHAR(20) NOT NULL, + "would_be_account_sequence" VARCHAR(20), + "contribution_type" VARCHAR(30) NOT NULL, + "amount" DECIMAL(30, 10) NOT NULL, + "reason" VARCHAR(200), + "effective_date" DATE NOT NULL, + "expire_date" DATE NOT NULL, + "is_expired" BOOLEAN NOT NULL DEFAULT false, + "last_synced_at" TIMESTAMP(3), + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "pending_contribution_mining_pkey" PRIMARY KEY ("id") +); + +-- CreateTable: 待解锁算力挖矿记录 +CREATE TABLE "pending_mining_records" ( + "id" BIGSERIAL NOT NULL, + "pending_contribution_id" BIGINT NOT NULL, + "mining_minute" TIMESTAMP(3) NOT NULL, + "source_adoption_id" BIGINT NOT NULL, + "source_account_sequence" VARCHAR(20) NOT NULL, + "would_be_account_sequence" VARCHAR(20), + "contribution_type" VARCHAR(30) NOT NULL, + "contribution_amount" DECIMAL(30, 10) NOT NULL, + "network_total_contribution" DECIMAL(30, 10) NOT NULL, + "contribution_ratio" DECIMAL(30, 18) NOT NULL, + "second_distribution" DECIMAL(30, 18) NOT NULL, + "mined_amount" DECIMAL(30, 18) NOT NULL, + "allocated_to" VARCHAR(20) NOT NULL DEFAULT 'HEADQUARTERS', + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "pending_mining_records_pkey" PRIMARY KEY ("id") +); + -- CreateTable CREATE TABLE "mining_reward_allocations" ( "id" BIGSERIAL NOT NULL, @@ -316,6 +406,33 @@ CREATE INDEX "mining_transactions_counterparty_account_seq_idx" ON "mining_trans -- CreateIndex CREATE INDEX "mining_transactions_counterparty_user_id_idx" ON "mining_transactions"("counterparty_user_id"); +-- CreateIndex: system_mining_accounts +CREATE UNIQUE INDEX "system_mining_accounts_account_type_key" ON "system_mining_accounts"("account_type"); +CREATE INDEX "system_mining_accounts_totalContribution_idx" ON "system_mining_accounts"("totalContribution" DESC); +CREATE INDEX "system_mining_accounts_base_type_idx" ON "system_mining_accounts"("base_type"); +CREATE INDEX "system_mining_accounts_region_code_idx" ON "system_mining_accounts"("region_code"); + +-- CreateIndex: system_mining_records +CREATE UNIQUE INDEX "system_mining_records_account_type_mining_minute_key" ON "system_mining_records"("account_type", "mining_minute"); +CREATE INDEX "system_mining_records_mining_minute_idx" ON "system_mining_records"("mining_minute"); + +-- CreateIndex: system_mining_transactions +CREATE INDEX "system_mining_transactions_account_type_created_at_idx" ON "system_mining_transactions"("account_type", "created_at" DESC); + +-- CreateIndex: pending_contribution_mining +CREATE UNIQUE INDEX "pending_contribution_mining_source_adoption_id_would_be_acco_key" + ON "pending_contribution_mining"("source_adoption_id", "would_be_account_sequence", "contribution_type"); +CREATE INDEX "pending_contribution_mining_would_be_account_sequence_idx" ON "pending_contribution_mining"("would_be_account_sequence"); +CREATE INDEX "pending_contribution_mining_contribution_type_idx" ON "pending_contribution_mining"("contribution_type"); +CREATE INDEX "pending_contribution_mining_is_expired_idx" ON "pending_contribution_mining"("is_expired"); + +-- CreateIndex: pending_mining_records +CREATE UNIQUE INDEX "pending_mining_records_pending_contribution_id_mining_minute_key" + ON "pending_mining_records"("pending_contribution_id", "mining_minute"); +CREATE INDEX "pending_mining_records_mining_minute_idx" ON "pending_mining_records"("mining_minute"); +CREATE INDEX "pending_mining_records_source_account_sequence_idx" ON "pending_mining_records"("source_account_sequence"); +CREATE INDEX "pending_mining_records_would_be_account_sequence_idx" ON "pending_mining_records"("would_be_account_sequence"); + -- CreateIndex CREATE INDEX "mining_reward_allocations_mining_date_idx" ON "mining_reward_allocations"("mining_date"); @@ -415,8 +532,29 @@ ALTER TABLE "mining_records" ADD CONSTRAINT "mining_records_accountSequence_fkey -- AddForeignKey ALTER TABLE "mining_transactions" ADD CONSTRAINT "mining_transactions_accountSequence_fkey" FOREIGN KEY ("accountSequence") REFERENCES "mining_accounts"("accountSequence") ON DELETE RESTRICT ON UPDATE CASCADE; +-- AddForeignKey: system_mining_records +ALTER TABLE "system_mining_records" ADD CONSTRAINT "system_mining_records_account_type_fkey" + FOREIGN KEY ("account_type") REFERENCES "system_mining_accounts"("account_type") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey: system_mining_transactions +ALTER TABLE "system_mining_transactions" ADD CONSTRAINT "system_mining_transactions_account_type_fkey" + FOREIGN KEY ("account_type") REFERENCES "system_mining_accounts"("account_type") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey: pending_mining_records +ALTER TABLE "pending_mining_records" ADD CONSTRAINT "pending_mining_records_pending_contribution_id_fkey" + FOREIGN KEY ("pending_contribution_id") REFERENCES "pending_contribution_mining"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + -- AddForeignKey ALTER TABLE "burn_records" ADD CONSTRAINT "burn_records_blackHoleId_fkey" FOREIGN KEY ("blackHoleId") REFERENCES "black_holes"("id") ON DELETE RESTRICT ON UPDATE CASCADE; -- AddForeignKey ALTER TABLE "pool_transactions" ADD CONSTRAINT "pool_transactions_pool_account_id_fkey" FOREIGN KEY ("pool_account_id") REFERENCES "pool_accounts"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- 初始化系统账户 +INSERT INTO "system_mining_accounts" ("id", "account_type", "base_type", "name", "totalMined", "availableBalance", "totalContribution", "updated_at") +VALUES + (gen_random_uuid(), 'OPERATION', 'OPERATION', '运营账户', 0, 0, 0, NOW()), + (gen_random_uuid(), 'PROVINCE', 'PROVINCE', '省公司账户', 0, 0, 0, NOW()), + (gen_random_uuid(), 'CITY', 'CITY', '市公司账户', 0, 0, 0, NOW()), + (gen_random_uuid(), 'HEADQUARTERS', 'HEADQUARTERS', '总部账户', 0, 0, 0, NOW()) +ON CONFLICT ("account_type") DO NOTHING; diff --git a/backend/services/mining-service/prisma/migrations/0002_minute_to_second/migration.sql b/backend/services/mining-service/prisma/migrations/0002_minute_to_second/migration.sql deleted file mode 100644 index 72d95478..00000000 --- a/backend/services/mining-service/prisma/migrations/0002_minute_to_second/migration.sql +++ /dev/null @@ -1,13 +0,0 @@ --- ============================================================================ --- 将 minuteDistribution 改为 secondDistribution --- 支持每秒挖矿分配 --- ============================================================================ - --- 重命名 mining_configs 表的列 -ALTER TABLE "mining_configs" RENAME COLUMN "minuteDistribution" TO "secondDistribution"; - --- 重命名 mining_eras 表的列 -ALTER TABLE "mining_eras" RENAME COLUMN "minuteDistribution" TO "secondDistribution"; - --- 重命名 mining_records 表的列 -ALTER TABLE "mining_records" RENAME COLUMN "minuteDistribution" TO "secondDistribution"; diff --git a/backend/services/mining-service/prisma/migrations/0003_add_system_accounts_and_pending_mining/migration.sql b/backend/services/mining-service/prisma/migrations/0003_add_system_accounts_and_pending_mining/migration.sql deleted file mode 100644 index 6ebd1564..00000000 --- a/backend/services/mining-service/prisma/migrations/0003_add_system_accounts_and_pending_mining/migration.sql +++ /dev/null @@ -1,238 +0,0 @@ --- ============================================================================ --- 添加系统账户和待解锁算力挖矿功能 --- 1. MiningConfig 添加全网理论算力字段 --- 2. 系统账户(运营/省/市/总部)挖矿 --- 3. 待解锁算力挖矿记录 --- 4. 挖矿收益分配明细 --- ============================================================================ - --- ==================== MiningConfig 添加全网理论算力字段 ==================== - -ALTER TABLE "mining_configs" ADD COLUMN IF NOT EXISTS "network_total_contribution" DECIMAL(30, 8) NOT NULL DEFAULT 0; -ALTER TABLE "mining_configs" ADD COLUMN IF NOT EXISTS "total_tree_count" INTEGER NOT NULL DEFAULT 0; -ALTER TABLE "mining_configs" ADD COLUMN IF NOT EXISTS "contribution_per_tree" DECIMAL(20, 10) NOT NULL DEFAULT 22617; -ALTER TABLE "mining_configs" ADD COLUMN IF NOT EXISTS "network_last_synced_at" TIMESTAMP(3); - --- ==================== 系统账户类型枚举 ==================== - -DO $$ -BEGIN - IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'SystemAccountType') THEN - CREATE TYPE "SystemAccountType" AS ENUM ('OPERATION', 'PROVINCE', 'CITY', 'HEADQUARTERS'); - END IF; -END$$; - --- ==================== 系统挖矿账户 ==================== - -CREATE TABLE IF NOT EXISTS "system_mining_accounts" ( - "id" TEXT NOT NULL, - "account_type" "SystemAccountType" NOT NULL, - "name" VARCHAR(100) NOT NULL, - "totalMined" DECIMAL(30, 8) NOT NULL DEFAULT 0, - "availableBalance" DECIMAL(30, 8) NOT NULL DEFAULT 0, - "totalContribution" DECIMAL(30, 8) NOT NULL DEFAULT 0, - "last_synced_at" TIMESTAMP(3), - "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - "updated_at" TIMESTAMP(3) NOT NULL, - - CONSTRAINT "system_mining_accounts_pkey" PRIMARY KEY ("id") -); - -CREATE UNIQUE INDEX IF NOT EXISTS "system_mining_accounts_account_type_key" ON "system_mining_accounts"("account_type"); -CREATE INDEX IF NOT EXISTS "system_mining_accounts_totalContribution_idx" ON "system_mining_accounts"("totalContribution" DESC); - --- ==================== 系统账户挖矿记录 ==================== - -CREATE TABLE IF NOT EXISTS "system_mining_records" ( - "id" TEXT NOT NULL, - "account_type" "SystemAccountType" NOT NULL, - "mining_minute" TIMESTAMP(3) NOT NULL, - "contribution_ratio" DECIMAL(30, 18) NOT NULL, - "total_contribution" DECIMAL(30, 8) NOT NULL, - "second_distribution" DECIMAL(30, 18) NOT NULL, - "mined_amount" DECIMAL(30, 18) NOT NULL, - "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - - CONSTRAINT "system_mining_records_pkey" PRIMARY KEY ("id") -); - -CREATE UNIQUE INDEX IF NOT EXISTS "system_mining_records_account_type_mining_minute_key" ON "system_mining_records"("account_type", "mining_minute"); -CREATE INDEX IF NOT EXISTS "system_mining_records_mining_minute_idx" ON "system_mining_records"("mining_minute"); - -ALTER TABLE "system_mining_records" DROP CONSTRAINT IF EXISTS "system_mining_records_account_type_fkey"; -ALTER TABLE "system_mining_records" ADD CONSTRAINT "system_mining_records_account_type_fkey" - FOREIGN KEY ("account_type") REFERENCES "system_mining_accounts"("account_type") ON DELETE RESTRICT ON UPDATE CASCADE; - --- ==================== 系统账户交易流水 ==================== - -CREATE TABLE IF NOT EXISTS "system_mining_transactions" ( - "id" TEXT NOT NULL, - "account_type" "SystemAccountType" NOT NULL, - "type" TEXT NOT NULL, - "amount" DECIMAL(30, 8) NOT NULL, - "balance_before" DECIMAL(30, 8) NOT NULL, - "balance_after" DECIMAL(30, 8) NOT NULL, - "reference_id" TEXT, - "reference_type" TEXT, - "memo" TEXT, - "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - - CONSTRAINT "system_mining_transactions_pkey" PRIMARY KEY ("id") -); - -CREATE INDEX IF NOT EXISTS "system_mining_transactions_account_type_created_at_idx" ON "system_mining_transactions"("account_type", "created_at" DESC); - -ALTER TABLE "system_mining_transactions" DROP CONSTRAINT IF EXISTS "system_mining_transactions_account_type_fkey"; -ALTER TABLE "system_mining_transactions" ADD CONSTRAINT "system_mining_transactions_account_type_fkey" - FOREIGN KEY ("account_type") REFERENCES "system_mining_accounts"("account_type") ON DELETE RESTRICT ON UPDATE CASCADE; - --- ==================== 待解锁算力挖矿 ==================== - -CREATE TABLE IF NOT EXISTS "pending_contribution_mining" ( - "id" BIGSERIAL NOT NULL, - "source_adoption_id" BIGINT NOT NULL, - "source_account_sequence" VARCHAR(20) NOT NULL, - "would_be_account_sequence" VARCHAR(20), - "contribution_type" VARCHAR(30) NOT NULL, - "amount" DECIMAL(30, 10) NOT NULL, - "reason" VARCHAR(200), - "effective_date" DATE NOT NULL, - "expire_date" DATE NOT NULL, - "is_expired" BOOLEAN NOT NULL DEFAULT false, - "last_synced_at" TIMESTAMP(3), - "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - - CONSTRAINT "pending_contribution_mining_pkey" PRIMARY KEY ("id") -); - -CREATE UNIQUE INDEX IF NOT EXISTS "pending_contribution_mining_source_adoption_id_would_be_acco_key" - ON "pending_contribution_mining"("source_adoption_id", "would_be_account_sequence", "contribution_type"); -CREATE INDEX IF NOT EXISTS "pending_contribution_mining_would_be_account_sequence_idx" ON "pending_contribution_mining"("would_be_account_sequence"); -CREATE INDEX IF NOT EXISTS "pending_contribution_mining_contribution_type_idx" ON "pending_contribution_mining"("contribution_type"); -CREATE INDEX IF NOT EXISTS "pending_contribution_mining_is_expired_idx" ON "pending_contribution_mining"("is_expired"); - --- ==================== 待解锁算力挖矿记录 ==================== - -CREATE TABLE IF NOT EXISTS "pending_mining_records" ( - "id" BIGSERIAL NOT NULL, - "pending_contribution_id" BIGINT NOT NULL, - "mining_minute" TIMESTAMP(3) NOT NULL, - "source_adoption_id" BIGINT NOT NULL, - "source_account_sequence" VARCHAR(20) NOT NULL, - "would_be_account_sequence" VARCHAR(20), - "contribution_type" VARCHAR(30) NOT NULL, - "contribution_amount" DECIMAL(30, 10) NOT NULL, - "network_total_contribution" DECIMAL(30, 10) NOT NULL, - "contribution_ratio" DECIMAL(30, 18) NOT NULL, - "second_distribution" DECIMAL(30, 18) NOT NULL, - "mined_amount" DECIMAL(30, 18) NOT NULL, - "allocated_to" VARCHAR(20) NOT NULL DEFAULT 'HEADQUARTERS', - "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - - CONSTRAINT "pending_mining_records_pkey" PRIMARY KEY ("id") -); - -CREATE UNIQUE INDEX IF NOT EXISTS "pending_mining_records_pending_contribution_id_mining_minute_key" - ON "pending_mining_records"("pending_contribution_id", "mining_minute"); -CREATE INDEX IF NOT EXISTS "pending_mining_records_mining_minute_idx" ON "pending_mining_records"("mining_minute"); -CREATE INDEX IF NOT EXISTS "pending_mining_records_source_account_sequence_idx" ON "pending_mining_records"("source_account_sequence"); -CREATE INDEX IF NOT EXISTS "pending_mining_records_would_be_account_sequence_idx" ON "pending_mining_records"("would_be_account_sequence"); - -ALTER TABLE "pending_mining_records" DROP CONSTRAINT IF EXISTS "pending_mining_records_pending_contribution_id_fkey"; -ALTER TABLE "pending_mining_records" ADD CONSTRAINT "pending_mining_records_pending_contribution_id_fkey" - FOREIGN KEY ("pending_contribution_id") REFERENCES "pending_contribution_mining"("id") ON DELETE RESTRICT ON UPDATE CASCADE; - --- ==================== 挖矿收益分配明细 ==================== - -CREATE TABLE IF NOT EXISTS "mining_reward_allocations" ( - "id" BIGSERIAL NOT NULL, - "mining_date" DATE NOT NULL, - "contribution_record_id" BIGINT NOT NULL, - "source_adoption_id" BIGINT NOT NULL, - "source_account_sequence" VARCHAR(20) NOT NULL, - "owner_account_sequence" VARCHAR(20) NOT NULL, - "contribution_type" VARCHAR(30) NOT NULL, - "contribution_amount" DECIMAL(30, 10) NOT NULL, - "network_total_contribution" DECIMAL(30, 10) NOT NULL, - "contribution_ratio" DECIMAL(30, 18) NOT NULL, - "daily_mining_pool" DECIMAL(30, 10) NOT NULL, - "reward_amount" DECIMAL(30, 10) NOT NULL, - "allocation_status" VARCHAR(20) NOT NULL, - "is_unlocked" BOOLEAN NOT NULL, - "allocated_to_account_sequence" VARCHAR(20), - "allocated_to_system_account" VARCHAR(20), - "unlocked_reason" VARCHAR(200), - "owner_has_adopted" BOOLEAN NOT NULL, - "owner_direct_referral_count" INTEGER NOT NULL, - "owner_unlocked_level_depth" INTEGER NOT NULL, - "owner_unlocked_bonus_tiers" INTEGER NOT NULL, - "remark" VARCHAR(500), - "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - - CONSTRAINT "mining_reward_allocations_pkey" PRIMARY KEY ("id") -); - -CREATE INDEX IF NOT EXISTS "mining_reward_allocations_mining_date_idx" ON "mining_reward_allocations"("mining_date"); -CREATE INDEX IF NOT EXISTS "mining_reward_allocations_owner_account_sequence_mining_date_idx" ON "mining_reward_allocations"("owner_account_sequence", "mining_date"); -CREATE INDEX IF NOT EXISTS "mining_reward_allocations_source_account_sequence_idx" ON "mining_reward_allocations"("source_account_sequence"); -CREATE INDEX IF NOT EXISTS "mining_reward_allocations_source_adoption_id_idx" ON "mining_reward_allocations"("source_adoption_id"); -CREATE INDEX IF NOT EXISTS "mining_reward_allocations_allocation_status_idx" ON "mining_reward_allocations"("allocation_status"); -CREATE INDEX IF NOT EXISTS "mining_reward_allocations_contribution_record_id_idx" ON "mining_reward_allocations"("contribution_record_id"); - --- ==================== 每日挖矿收益汇总 ==================== - -CREATE TABLE IF NOT EXISTS "daily_mining_reward_summaries" ( - "id" BIGSERIAL NOT NULL, - "mining_date" DATE NOT NULL, - "account_sequence" VARCHAR(20) NOT NULL, - "unlocked_reward" DECIMAL(30, 10) NOT NULL DEFAULT 0, - "pending_reward_to_hq" DECIMAL(30, 10) NOT NULL DEFAULT 0, - "personal_reward" DECIMAL(30, 10) NOT NULL DEFAULT 0, - "level_reward" DECIMAL(30, 10) NOT NULL DEFAULT 0, - "bonus_reward" DECIMAL(30, 10) NOT NULL DEFAULT 0, - "pending_level_to_hq" DECIMAL(30, 10) NOT NULL DEFAULT 0, - "pending_bonus_to_hq" DECIMAL(30, 10) NOT NULL DEFAULT 0, - "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - - CONSTRAINT "daily_mining_reward_summaries_pkey" PRIMARY KEY ("id") -); - -CREATE UNIQUE INDEX IF NOT EXISTS "daily_mining_reward_summaries_mining_date_account_sequence_key" - ON "daily_mining_reward_summaries"("mining_date", "account_sequence"); -CREATE INDEX IF NOT EXISTS "daily_mining_reward_summaries_mining_date_idx" ON "daily_mining_reward_summaries"("mining_date"); -CREATE INDEX IF NOT EXISTS "daily_mining_reward_summaries_account_sequence_idx" ON "daily_mining_reward_summaries"("account_sequence"); - --- ==================== 总部待解锁收益明细 ==================== - -CREATE TABLE IF NOT EXISTS "headquarters_pending_rewards" ( - "id" BIGSERIAL NOT NULL, - "mining_date" DATE NOT NULL, - "would_be_account_sequence" VARCHAR(20) NOT NULL, - "source_adoption_id" BIGINT NOT NULL, - "source_account_sequence" VARCHAR(20) NOT NULL, - "contribution_record_id" BIGINT NOT NULL, - "contribution_type" VARCHAR(30) NOT NULL, - "contribution_amount" DECIMAL(30, 10) NOT NULL, - "reward_amount" DECIMAL(30, 10) NOT NULL, - "reason" VARCHAR(200) NOT NULL, - "owner_has_adopted" BOOLEAN NOT NULL, - "owner_direct_referral_count" INTEGER NOT NULL, - "required_condition" VARCHAR(100) NOT NULL, - "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - - CONSTRAINT "headquarters_pending_rewards_pkey" PRIMARY KEY ("id") -); - -CREATE INDEX IF NOT EXISTS "headquarters_pending_rewards_mining_date_idx" ON "headquarters_pending_rewards"("mining_date"); -CREATE INDEX IF NOT EXISTS "headquarters_pending_rewards_would_be_account_sequence_idx" ON "headquarters_pending_rewards"("would_be_account_sequence"); -CREATE INDEX IF NOT EXISTS "headquarters_pending_rewards_source_adoption_id_idx" ON "headquarters_pending_rewards"("source_adoption_id"); - --- ==================== 初始化系统账户 ==================== - -INSERT INTO "system_mining_accounts" ("id", "account_type", "name", "totalMined", "availableBalance", "totalContribution", "updated_at") -VALUES - (gen_random_uuid(), 'OPERATION', '运营账户', 0, 0, 0, NOW()), - (gen_random_uuid(), 'PROVINCE', '省公司账户', 0, 0, 0, NOW()), - (gen_random_uuid(), 'CITY', '市公司账户', 0, 0, 0, NOW()), - (gen_random_uuid(), 'HEADQUARTERS', '总部账户', 0, 0, 0, NOW()) -ON CONFLICT ("account_type") DO NOTHING; diff --git a/backend/services/mining-service/prisma/migrations/20250120000001_add_region_to_system_mining_accounts/migration.sql b/backend/services/mining-service/prisma/migrations/20250120000001_add_region_to_system_mining_accounts/migration.sql deleted file mode 100644 index d87dffa5..00000000 --- a/backend/services/mining-service/prisma/migrations/20250120000001_add_region_to_system_mining_accounts/migration.sql +++ /dev/null @@ -1,93 +0,0 @@ --- 系统挖矿账户按省市细分: 移除枚举类型,改用字符串组合键 --- 将 accountType 从枚举改为字符串(如 PROVINCE_440000, CITY_440100) - --- ============================================================ --- 步骤 1: 删除现有外键约束(如果存在) --- ============================================================ -ALTER TABLE "system_mining_records" DROP CONSTRAINT IF EXISTS "system_mining_records_account_type_fkey"; -ALTER TABLE "system_mining_transactions" DROP CONSTRAINT IF EXISTS "system_mining_transactions_account_type_fkey"; - --- ============================================================ --- 步骤 2: 修改 system_mining_accounts 主表 --- ============================================================ - --- 2.1 添加新列 (使用 TEXT 类型与 Prisma String 一致) -ALTER TABLE "system_mining_accounts" ADD COLUMN "account_type_new" TEXT; -ALTER TABLE "system_mining_accounts" ADD COLUMN "base_type" TEXT; -ALTER TABLE "system_mining_accounts" ADD COLUMN "region_code" TEXT; - --- 2.2 迁移数据:将枚举值转为字符串 -UPDATE "system_mining_accounts" -SET "account_type_new" = "account_type"::TEXT, - "base_type" = "account_type"::TEXT -WHERE "account_type_new" IS NULL; - --- 2.3 删除旧的唯一约束和列 -DROP INDEX IF EXISTS "system_mining_accounts_account_type_key"; -ALTER TABLE "system_mining_accounts" DROP COLUMN "account_type"; - --- 2.4 重命名新列 -ALTER TABLE "system_mining_accounts" RENAME COLUMN "account_type_new" TO "account_type"; - --- 2.5 添加非空约束和唯一约束 -ALTER TABLE "system_mining_accounts" ALTER COLUMN "account_type" SET NOT NULL; -ALTER TABLE "system_mining_accounts" ALTER COLUMN "base_type" SET NOT NULL; -CREATE UNIQUE INDEX "system_mining_accounts_account_type_key" ON "system_mining_accounts"("account_type"); - --- 2.6 创建索引(Prisma 默认命名格式: {table}_{field}_idx) -CREATE INDEX IF NOT EXISTS "system_mining_accounts_base_type_idx" ON "system_mining_accounts"("base_type"); -CREATE INDEX IF NOT EXISTS "system_mining_accounts_region_code_idx" ON "system_mining_accounts"("region_code"); - --- ============================================================ --- 步骤 3: 修改 system_mining_records 表 --- ============================================================ - --- 3.1 添加新列并迁移数据 (使用 TEXT) -ALTER TABLE "system_mining_records" ADD COLUMN "account_type_new" TEXT; -UPDATE "system_mining_records" SET "account_type_new" = "account_type"::TEXT; - --- 3.2 删除旧的唯一索引和列 -DROP INDEX IF EXISTS "system_mining_records_account_type_mining_minute_key"; -ALTER TABLE "system_mining_records" DROP COLUMN "account_type"; - --- 3.3 重命名新列并添加约束 -ALTER TABLE "system_mining_records" RENAME COLUMN "account_type_new" TO "account_type"; -ALTER TABLE "system_mining_records" ALTER COLUMN "account_type" SET NOT NULL; - --- 3.4 重建复合唯一索引 -CREATE UNIQUE INDEX "system_mining_records_account_type_mining_minute_key" ON "system_mining_records"("account_type", "mining_minute"); - --- 3.5 重建外键约束 -ALTER TABLE "system_mining_records" - ADD CONSTRAINT "system_mining_records_account_type_fkey" - FOREIGN KEY ("account_type") REFERENCES "system_mining_accounts"("account_type") - ON DELETE RESTRICT ON UPDATE CASCADE; - --- ============================================================ --- 步骤 4: 修改 system_mining_transactions 表 --- ============================================================ - --- 4.1 添加新列并迁移数据 (使用 TEXT) -ALTER TABLE "system_mining_transactions" ADD COLUMN "account_type_new" TEXT; -UPDATE "system_mining_transactions" SET "account_type_new" = "account_type"::TEXT; - --- 4.2 删除旧列 -ALTER TABLE "system_mining_transactions" DROP COLUMN "account_type"; - --- 4.3 重命名新列并添加约束 -ALTER TABLE "system_mining_transactions" RENAME COLUMN "account_type_new" TO "account_type"; -ALTER TABLE "system_mining_transactions" ALTER COLUMN "account_type" SET NOT NULL; - --- 4.4 重建索引(Prisma 默认命名格式: {table}_{field(s)}_idx) -CREATE INDEX IF NOT EXISTS "system_mining_transactions_account_type_createdAt_idx" ON "system_mining_transactions"("account_type", "created_at" DESC); - --- 4.5 重建外键约束 -ALTER TABLE "system_mining_transactions" - ADD CONSTRAINT "system_mining_transactions_account_type_fkey" - FOREIGN KEY ("account_type") REFERENCES "system_mining_accounts"("account_type") - ON DELETE RESTRICT ON UPDATE CASCADE; - --- ============================================================ --- 步骤 5: 清理旧的枚举类型(如果不再使用) --- ============================================================ --- DROP TYPE IF EXISTS "SystemAccountType"; diff --git a/backend/services/mining-wallet-service/prisma/migrations/0001_init/migration.sql b/backend/services/mining-wallet-service/prisma/migrations/0001_init/migration.sql index f46f559d..6a21d075 100644 --- a/backend/services/mining-wallet-service/prisma/migrations/0001_init/migration.sql +++ b/backend/services/mining-wallet-service/prisma/migrations/0001_init/migration.sql @@ -1,7 +1,6 @@ -- ============================================================================ -- mining-wallet-service 初始化 migration --- 合并自: 20260111000000_init, 20260112180000_add_contribution_balance, --- 20260112220000_remove_blockchain_tables +-- 合并自: 0001_init, 0002_split_share_pool -- 注意: 区块链相关表已移除 -- ============================================================================ @@ -9,7 +8,8 @@ CREATE TYPE "SystemAccountType" AS ENUM ('HEADQUARTERS', 'OPERATION', 'PROVINCE', 'CITY', 'FEE', 'HOT_WALLET', 'COLD_WALLET'); -- CreateEnum -CREATE TYPE "PoolAccountType" AS ENUM ('SHARE_POOL', 'BLACK_HOLE_POOL', 'CIRCULATION_POOL'); +-- 注意: SHARE_POOL 已拆分为 SHARE_POOL_A (100亿销毁) 和 SHARE_POOL_B (200万挖矿) +CREATE TYPE "PoolAccountType" AS ENUM ('SHARE_POOL', 'SHARE_POOL_A', 'SHARE_POOL_B', 'BLACK_HOLE_POOL', 'CIRCULATION_POOL'); -- CreateEnum CREATE TYPE "UserWalletType" AS ENUM ('CONTRIBUTION', 'TOKEN_STORAGE', 'GREEN_POINTS'); diff --git a/backend/services/mining-wallet-service/prisma/migrations/0002_split_share_pool/migration.sql b/backend/services/mining-wallet-service/prisma/migrations/0002_split_share_pool/migration.sql deleted file mode 100644 index 8f2ec87c..00000000 --- a/backend/services/mining-wallet-service/prisma/migrations/0002_split_share_pool/migration.sql +++ /dev/null @@ -1,14 +0,0 @@ --- ============================================================================ --- 拆分积分股池: SHARE_POOL -> SHARE_POOL_A + SHARE_POOL_B --- SHARE_POOL_A: 100亿 (10,000,000,000) - 用于销毁 --- SHARE_POOL_B: 200万 (2,000,000) - 用于挖矿分配 --- 总计: 100.02亿 (10,002,000,000) --- ============================================================================ - --- 1. 添加新的枚举值 -ALTER TYPE "PoolAccountType" ADD VALUE IF NOT EXISTS 'SHARE_POOL_A'; -ALTER TYPE "PoolAccountType" ADD VALUE IF NOT EXISTS 'SHARE_POOL_B'; - --- 2. 如果存在旧的 SHARE_POOL 记录,需要手动迁移数据后删除 --- 注意: PostgreSQL 不支持直接删除枚举值,如果需要删除旧值需要重建枚举 --- 这里只添加新值,旧的 SHARE_POOL 保留兼容 diff --git a/backend/services/trading-service/prisma/migrations/0001_init/migration.sql b/backend/services/trading-service/prisma/migrations/0001_init/migration.sql index 3df793ae..32326cf2 100644 --- a/backend/services/trading-service/prisma/migrations/0001_init/migration.sql +++ b/backend/services/trading-service/prisma/migrations/0001_init/migration.sql @@ -1,6 +1,9 @@ -- ============================================================================ -- trading-service 初始化 migration --- 合并自: 20260111000000_init (只有一个,无需合并) +-- 合并自: 0001_init, 0002_add_trading_burn_system, 0003_add_processed_events, +-- 0004_add_buy_enabled, 0005_add_market_maker_and_order_source, +-- 0006_add_market_maker_depth, 0007_add_trade_fee, 0008_add_c2c_orders, +-- 0009_add_original_quantity -- ============================================================================ -- CreateEnum @@ -35,6 +38,11 @@ CREATE TABLE "orders" ( "remainingQuantity" DECIMAL(30,8) NOT NULL, "averagePrice" DECIMAL(30,18) NOT NULL DEFAULT 0, "totalAmount" DECIMAL(30,8) NOT NULL DEFAULT 0, + "burn_quantity" DECIMAL(30,8) NOT NULL DEFAULT 0, + "burn_multiplier" DECIMAL(30,18) NOT NULL DEFAULT 0, + "effective_quantity" DECIMAL(30,8) NOT NULL DEFAULT 0, + "source" TEXT NOT NULL DEFAULT 'USER', + "source_label" TEXT, "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, "updatedAt" TIMESTAMP(3) NOT NULL, "cancelledAt" TIMESTAMP(3), @@ -53,7 +61,13 @@ CREATE TABLE "trades" ( "seller_sequence" TEXT NOT NULL, "price" DECIMAL(30,18) NOT NULL, "quantity" DECIMAL(30,8) NOT NULL, + "original_quantity" DECIMAL(30,8) NOT NULL DEFAULT 0, "amount" DECIMAL(30,8) NOT NULL, + "burn_quantity" DECIMAL(30,8) NOT NULL DEFAULT 0, + "effective_qty" DECIMAL(30,8) NOT NULL DEFAULT 0, + "fee" DECIMAL(30,8) NOT NULL DEFAULT 0, + "buyer_source" TEXT NOT NULL DEFAULT 'USER', + "seller_source" TEXT NOT NULL DEFAULT 'USER', "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT "trades_pkey" PRIMARY KEY ("id") @@ -225,6 +239,9 @@ CREATE INDEX "orders_type_status_price_idx" ON "orders"("type", "status", "price -- CreateIndex CREATE INDEX "orders_createdAt_idx" ON "orders"("createdAt" DESC); +-- CreateIndex +CREATE INDEX "orders_source_idx" ON "orders"("source"); + -- CreateIndex CREATE UNIQUE INDEX "trades_tradeNo_key" ON "trades"("tradeNo"); @@ -237,6 +254,12 @@ CREATE INDEX "trades_seller_sequence_idx" ON "trades"("seller_sequence"); -- CreateIndex CREATE INDEX "trades_created_at_idx" ON "trades"("created_at" DESC); +-- CreateIndex +CREATE INDEX "trades_buyer_source_idx" ON "trades"("buyer_source"); + +-- CreateIndex +CREATE INDEX "trades_seller_source_idx" ON "trades"("seller_source"); + -- CreateIndex CREATE INDEX "trading_transactions_accountSequence_createdAt_idx" ON "trading_transactions"("accountSequence", "createdAt" DESC); @@ -314,3 +337,333 @@ ALTER TABLE "trading_transactions" ADD CONSTRAINT "trading_transactions_accountS -- AddForeignKey ALTER TABLE "circulation_pool_transactions" ADD CONSTRAINT "circulation_pool_transactions_pool_id_fkey" FOREIGN KEY ("pool_id") REFERENCES "circulation_pools"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- ============================================================================ +-- 交易配置和销毁系统 (从 0002_add_trading_burn_system 合并) +-- ============================================================================ + +-- CreateTable +CREATE TABLE "trading_configs" ( + "id" TEXT NOT NULL, + "total_shares" DECIMAL(30,8) NOT NULL DEFAULT 10002000000, + "burn_target" DECIMAL(30,8) NOT NULL DEFAULT 10000000000, + "burn_period_minutes" INTEGER NOT NULL DEFAULT 2102400, + "minute_burn_rate" DECIMAL(30,18) NOT NULL DEFAULT 4756.468797564687, + "is_active" BOOLEAN NOT NULL DEFAULT false, + "buy_enabled" BOOLEAN NOT NULL DEFAULT false, + "depth_enabled" BOOLEAN NOT NULL DEFAULT false, + "activated_at" TIMESTAMP(3), + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updated_at" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "trading_configs_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "black_holes" ( + "id" TEXT NOT NULL, + "total_burned" DECIMAL(30,8) NOT NULL DEFAULT 0, + "target_burn" DECIMAL(30,8) NOT NULL, + "remaining_burn" DECIMAL(30,8) NOT NULL, + "last_burn_minute" TIMESTAMP(3), + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updated_at" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "black_holes_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "burn_records" ( + "id" TEXT NOT NULL, + "black_hole_id" TEXT NOT NULL, + "burn_minute" TIMESTAMP(3) NOT NULL, + "burn_amount" DECIMAL(30,18) NOT NULL, + "remaining_target" DECIMAL(30,8) NOT NULL, + "source_type" TEXT, + "source_account_seq" TEXT, + "source_order_no" TEXT, + "memo" TEXT, + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "burn_records_pkey" PRIMARY KEY ("id") +); + +-- CreateIndex +CREATE INDEX "burn_records_burn_minute_idx" ON "burn_records"("burn_minute"); +CREATE INDEX "burn_records_source_account_seq_idx" ON "burn_records"("source_account_seq"); +CREATE INDEX "burn_records_source_order_no_idx" ON "burn_records"("source_order_no"); +CREATE INDEX "burn_records_source_type_idx" ON "burn_records"("source_type"); + +-- AddForeignKey +ALTER TABLE "burn_records" ADD CONSTRAINT "burn_records_black_hole_id_fkey" FOREIGN KEY ("black_hole_id") REFERENCES "black_holes"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- CreateTable +CREATE TABLE "share_pools" ( + "id" TEXT NOT NULL, + "green_points" DECIMAL(30,8) NOT NULL DEFAULT 0, + "total_inflow" DECIMAL(30,8) NOT NULL DEFAULT 0, + "total_outflow" DECIMAL(30,8) NOT NULL DEFAULT 0, + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updated_at" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "share_pools_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "share_pool_transactions" ( + "id" TEXT NOT NULL, + "pool_id" TEXT NOT NULL, + "type" TEXT NOT NULL, + "amount" DECIMAL(30,8) NOT NULL, + "balance_before" DECIMAL(30,8) NOT NULL, + "balance_after" DECIMAL(30,8) NOT NULL, + "reference_id" TEXT, + "reference_type" TEXT, + "memo" TEXT, + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "share_pool_transactions_pkey" PRIMARY KEY ("id") +); + +-- CreateIndex +CREATE INDEX "share_pool_transactions_pool_id_created_at_idx" ON "share_pool_transactions"("pool_id", "created_at" DESC); + +-- AddForeignKey +ALTER TABLE "share_pool_transactions" ADD CONSTRAINT "share_pool_transactions_pool_id_fkey" FOREIGN KEY ("pool_id") REFERENCES "share_pools"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- CreateTable +CREATE TABLE "price_snapshots" ( + "id" TEXT NOT NULL, + "snapshot_time" TIMESTAMP(3) NOT NULL, + "price" DECIMAL(30,18) NOT NULL, + "green_points" DECIMAL(30,8) NOT NULL, + "black_hole_amount" DECIMAL(30,8) NOT NULL, + "circulation_pool" DECIMAL(30,8) NOT NULL, + "effective_denominator" DECIMAL(30,8) NOT NULL, + "minute_burn_rate" DECIMAL(30,18) NOT NULL, + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "price_snapshots_pkey" PRIMARY KEY ("id") +); + +-- CreateIndex +CREATE UNIQUE INDEX "price_snapshots_snapshot_time_key" ON "price_snapshots"("snapshot_time"); +CREATE INDEX "price_snapshots_snapshot_time_idx" ON "price_snapshots"("snapshot_time" DESC); + +-- ============================================================================ +-- 事件幂等处理 (从 0003_add_processed_events 合并) +-- ============================================================================ + +-- CreateTable +CREATE TABLE "processed_events" ( + "id" TEXT NOT NULL, + "event_id" TEXT NOT NULL, + "event_type" TEXT NOT NULL, + "source_service" TEXT NOT NULL, + "processed_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "processed_events_pkey" PRIMARY KEY ("id") +); + +-- CreateIndex +CREATE UNIQUE INDEX "processed_events_event_id_key" ON "processed_events"("event_id"); +CREATE INDEX "processed_events_event_id_idx" ON "processed_events"("event_id"); +CREATE INDEX "processed_events_processed_at_idx" ON "processed_events"("processed_at"); + +-- ============================================================================ +-- 做市商系统 (从 0005_add_market_maker_and_order_source 合并) +-- ============================================================================ + +-- CreateTable +CREATE TABLE "market_maker_configs" ( + "id" TEXT NOT NULL, + "name" TEXT NOT NULL, + "account_sequence" TEXT NOT NULL, + "cash_balance" DECIMAL(30,8) NOT NULL DEFAULT 0, + "share_balance" DECIMAL(30,8) NOT NULL DEFAULT 0, + "frozen_cash" DECIMAL(30,8) NOT NULL DEFAULT 0, + "frozen_shares" DECIMAL(30,8) NOT NULL DEFAULT 0, + "max_buy_ratio" DECIMAL(10,4) NOT NULL DEFAULT 0.05, + "min_interval_ms" INTEGER NOT NULL DEFAULT 1000, + "max_interval_ms" INTEGER NOT NULL DEFAULT 4000, + "price_strategy" TEXT NOT NULL DEFAULT 'TAKER', + "discount_rate" DECIMAL(10,4) NOT NULL DEFAULT 1.0, + "is_active" BOOLEAN NOT NULL DEFAULT false, + "last_run_at" TIMESTAMP(3), + "total_buy_count" INTEGER NOT NULL DEFAULT 0, + "total_buy_quantity" DECIMAL(30,8) NOT NULL DEFAULT 0, + "total_buy_amount" DECIMAL(30,8) NOT NULL DEFAULT 0, + "total_sell_count" INTEGER NOT NULL DEFAULT 0, + "total_sell_quantity" DECIMAL(30,8) NOT NULL DEFAULT 0, + "total_sell_amount" DECIMAL(30,8) NOT NULL DEFAULT 0, + "maker_enabled" BOOLEAN NOT NULL DEFAULT false, + "bid_enabled" BOOLEAN NOT NULL DEFAULT true, + "bid_levels" INTEGER NOT NULL DEFAULT 5, + "bid_spread" DECIMAL(10,4) NOT NULL DEFAULT 0.01, + "bid_level_spacing" DECIMAL(10,4) NOT NULL DEFAULT 0.005, + "bid_quantity_per_level" DECIMAL(30,8) NOT NULL DEFAULT 1000, + "ask_enabled" BOOLEAN NOT NULL DEFAULT true, + "ask_levels" INTEGER NOT NULL DEFAULT 5, + "ask_spread" DECIMAL(10,4) NOT NULL DEFAULT 0.01, + "ask_level_spacing" DECIMAL(10,4) NOT NULL DEFAULT 0.005, + "ask_quantity_per_level" DECIMAL(30,8) NOT NULL DEFAULT 1000, + "refresh_interval_ms" INTEGER NOT NULL DEFAULT 60000, + "last_refresh_at" TIMESTAMP(3), + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updated_at" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "market_maker_configs_pkey" PRIMARY KEY ("id") +); + +-- CreateIndex +CREATE UNIQUE INDEX "market_maker_configs_name_key" ON "market_maker_configs"("name"); +CREATE UNIQUE INDEX "market_maker_configs_account_sequence_key" ON "market_maker_configs"("account_sequence"); + +-- CreateTable +CREATE TABLE "market_maker_ledgers" ( + "id" TEXT NOT NULL, + "market_maker_id" TEXT NOT NULL, + "type" TEXT NOT NULL, + "asset_type" TEXT NOT NULL, + "amount" DECIMAL(30,8) NOT NULL, + "balance_before" DECIMAL(30,8) NOT NULL, + "balance_after" DECIMAL(30,8) NOT NULL, + "trade_id" TEXT, + "trade_no" TEXT, + "order_id" TEXT, + "order_no" TEXT, + "counterparty_seq" TEXT, + "counterparty_id" TEXT, + "price" DECIMAL(30,18), + "quantity" DECIMAL(30,8), + "memo" TEXT, + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "market_maker_ledgers_pkey" PRIMARY KEY ("id") +); + +-- CreateIndex +CREATE INDEX "market_maker_ledgers_market_maker_id_created_at_idx" ON "market_maker_ledgers"("market_maker_id", "created_at" DESC); +CREATE INDEX "market_maker_ledgers_type_idx" ON "market_maker_ledgers"("type"); +CREATE INDEX "market_maker_ledgers_trade_no_idx" ON "market_maker_ledgers"("trade_no"); +CREATE INDEX "market_maker_ledgers_order_no_idx" ON "market_maker_ledgers"("order_no"); +CREATE INDEX "market_maker_ledgers_counterparty_seq_idx" ON "market_maker_ledgers"("counterparty_seq"); +CREATE INDEX "market_maker_ledgers_created_at_idx" ON "market_maker_ledgers"("created_at" DESC); + +-- AddForeignKey +ALTER TABLE "market_maker_ledgers" ADD CONSTRAINT "market_maker_ledgers_market_maker_id_fkey" FOREIGN KEY ("market_maker_id") REFERENCES "market_maker_configs"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- CreateTable +CREATE TABLE "market_maker_daily_stats" ( + "id" TEXT NOT NULL, + "market_maker_id" TEXT NOT NULL, + "date" DATE NOT NULL, + "buy_count" INTEGER NOT NULL DEFAULT 0, + "buy_quantity" DECIMAL(30,8) NOT NULL DEFAULT 0, + "buy_amount" DECIMAL(30,8) NOT NULL DEFAULT 0, + "avg_buy_price" DECIMAL(30,18) NOT NULL DEFAULT 0, + "sell_count" INTEGER NOT NULL DEFAULT 0, + "sell_quantity" DECIMAL(30,8) NOT NULL DEFAULT 0, + "sell_amount" DECIMAL(30,8) NOT NULL DEFAULT 0, + "avg_sell_price" DECIMAL(30,18) NOT NULL DEFAULT 0, + "realized_pnl" DECIMAL(30,8) NOT NULL DEFAULT 0, + "cash_balance_end" DECIMAL(30,8) NOT NULL DEFAULT 0, + "share_balance_end" DECIMAL(30,8) NOT NULL DEFAULT 0, + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updated_at" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "market_maker_daily_stats_pkey" PRIMARY KEY ("id") +); + +-- CreateIndex +CREATE UNIQUE INDEX "market_maker_daily_stats_market_maker_id_date_key" ON "market_maker_daily_stats"("market_maker_id", "date"); +CREATE INDEX "market_maker_daily_stats_market_maker_id_date_idx" ON "market_maker_daily_stats"("market_maker_id", "date" DESC); + +-- CreateTable +CREATE TABLE "market_maker_orders" ( + "id" TEXT NOT NULL, + "market_maker_id" TEXT NOT NULL, + "order_id" TEXT NOT NULL, + "order_no" TEXT NOT NULL, + "side" TEXT NOT NULL, + "level" INTEGER NOT NULL, + "price" DECIMAL(30,18) NOT NULL, + "quantity" DECIMAL(30,8) NOT NULL, + "remaining_qty" DECIMAL(30,8) NOT NULL, + "status" TEXT NOT NULL DEFAULT 'ACTIVE', + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updated_at" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "market_maker_orders_pkey" PRIMARY KEY ("id") +); + +-- CreateIndex +CREATE UNIQUE INDEX "market_maker_orders_order_id_key" ON "market_maker_orders"("order_id"); +CREATE UNIQUE INDEX "market_maker_orders_order_no_key" ON "market_maker_orders"("order_no"); +CREATE INDEX "market_maker_orders_market_maker_id_side_status_idx" ON "market_maker_orders"("market_maker_id", "side", "status"); +CREATE INDEX "market_maker_orders_status_idx" ON "market_maker_orders"("status"); + +-- AddForeignKey +ALTER TABLE "market_maker_orders" ADD CONSTRAINT "market_maker_orders_market_maker_id_fkey" FOREIGN KEY ("market_maker_id") REFERENCES "market_maker_configs"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- ============================================================================ +-- C2C 交易系统 (从 0008_add_c2c_orders 合并) +-- ============================================================================ + +-- CreateEnum +CREATE TYPE "C2cOrderType" AS ENUM ('BUY', 'SELL'); + +-- CreateEnum +CREATE TYPE "C2cOrderStatus" AS ENUM ('PENDING', 'MATCHED', 'PAID', 'COMPLETED', 'CANCELLED', 'EXPIRED'); + +-- CreateEnum +CREATE TYPE "C2cPaymentMethod" AS ENUM ('ALIPAY', 'WECHAT', 'BANK'); + +-- CreateTable +CREATE TABLE "c2c_orders" ( + "id" TEXT NOT NULL, + "order_no" TEXT NOT NULL, + "type" "C2cOrderType" NOT NULL, + "status" "C2cOrderStatus" NOT NULL DEFAULT 'PENDING', + "maker_account_sequence" TEXT NOT NULL, + "maker_user_id" TEXT, + "maker_phone" TEXT, + "maker_nickname" TEXT, + "taker_account_sequence" TEXT, + "taker_user_id" TEXT, + "taker_phone" TEXT, + "taker_nickname" TEXT, + "price" DECIMAL(30,18) NOT NULL, + "quantity" DECIMAL(30,8) NOT NULL, + "total_amount" DECIMAL(30,8) NOT NULL, + "min_amount" DECIMAL(30,8) NOT NULL DEFAULT 0, + "max_amount" DECIMAL(30,8) NOT NULL DEFAULT 0, + "payment_method" "C2cPaymentMethod", + "payment_account" TEXT, + "payment_qr_code" TEXT, + "payment_real_name" TEXT, + "remark" TEXT, + "payment_timeout_minutes" INTEGER NOT NULL DEFAULT 15, + "confirm_timeout_minutes" INTEGER NOT NULL DEFAULT 60, + "payment_deadline" TIMESTAMP(3), + "confirm_deadline" TIMESTAMP(3), + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updated_at" TIMESTAMP(3) NOT NULL, + "matched_at" TIMESTAMP(3), + "paid_at" TIMESTAMP(3), + "completed_at" TIMESTAMP(3), + "cancelled_at" TIMESTAMP(3), + "expired_at" TIMESTAMP(3), + + CONSTRAINT "c2c_orders_pkey" PRIMARY KEY ("id") +); + +-- CreateIndex +CREATE UNIQUE INDEX "c2c_orders_order_no_key" ON "c2c_orders"("order_no"); +CREATE INDEX "c2c_orders_status_idx" ON "c2c_orders"("status"); +CREATE INDEX "c2c_orders_type_status_idx" ON "c2c_orders"("type", "status"); +CREATE INDEX "c2c_orders_maker_account_sequence_idx" ON "c2c_orders"("maker_account_sequence"); +CREATE INDEX "c2c_orders_taker_account_sequence_idx" ON "c2c_orders"("taker_account_sequence"); +CREATE INDEX "c2c_orders_created_at_idx" ON "c2c_orders"("created_at" DESC); +CREATE INDEX "c2c_orders_payment_deadline_idx" ON "c2c_orders"("payment_deadline"); +CREATE INDEX "c2c_orders_confirm_deadline_idx" ON "c2c_orders"("confirm_deadline"); diff --git a/backend/services/trading-service/prisma/migrations/0002_add_trading_burn_system/migration.sql b/backend/services/trading-service/prisma/migrations/0002_add_trading_burn_system/migration.sql deleted file mode 100644 index 6b6433db..00000000 --- a/backend/services/trading-service/prisma/migrations/0002_add_trading_burn_system/migration.sql +++ /dev/null @@ -1,139 +0,0 @@ --- ============================================================================ --- trading-service 添加交易销毁系统 --- 包含:交易配置、黑洞账户、积分股池、价格快照、订单销毁字段 --- ============================================================================ - --- ==================== 交易配置表 ==================== - --- CreateTable -CREATE TABLE "trading_configs" ( - "id" TEXT NOT NULL, - "total_shares" DECIMAL(30,8) NOT NULL DEFAULT 10002000000, - "burn_target" DECIMAL(30,8) NOT NULL DEFAULT 10000000000, - "burn_period_minutes" INTEGER NOT NULL DEFAULT 2102400, - "minute_burn_rate" DECIMAL(30,18) NOT NULL DEFAULT 4756.468797564687, - "is_active" BOOLEAN NOT NULL DEFAULT false, - "activated_at" TIMESTAMP(3), - "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - "updated_at" TIMESTAMP(3) NOT NULL, - - CONSTRAINT "trading_configs_pkey" PRIMARY KEY ("id") -); - --- ==================== 黑洞账户(销毁池)==================== - --- CreateTable -CREATE TABLE "black_holes" ( - "id" TEXT NOT NULL, - "total_burned" DECIMAL(30,8) NOT NULL DEFAULT 0, - "target_burn" DECIMAL(30,8) NOT NULL, - "remaining_burn" DECIMAL(30,8) NOT NULL, - "last_burn_minute" TIMESTAMP(3), - "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - "updated_at" TIMESTAMP(3) NOT NULL, - - CONSTRAINT "black_holes_pkey" PRIMARY KEY ("id") -); - --- CreateTable -CREATE TABLE "burn_records" ( - "id" TEXT NOT NULL, - "black_hole_id" TEXT NOT NULL, - "burn_minute" TIMESTAMP(3) NOT NULL, - "burn_amount" DECIMAL(30,18) NOT NULL, - "remaining_target" DECIMAL(30,8) NOT NULL, - "source_type" TEXT, - "source_account_seq" TEXT, - "source_order_no" TEXT, - "memo" TEXT, - "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - - CONSTRAINT "burn_records_pkey" PRIMARY KEY ("id") -); - --- CreateIndex -CREATE INDEX "burn_records_burn_minute_idx" ON "burn_records"("burn_minute"); - --- CreateIndex -CREATE INDEX "burn_records_source_account_seq_idx" ON "burn_records"("source_account_seq"); - --- CreateIndex -CREATE INDEX "burn_records_source_order_no_idx" ON "burn_records"("source_order_no"); - --- CreateIndex -CREATE INDEX "burn_records_source_type_idx" ON "burn_records"("source_type"); - --- AddForeignKey -ALTER TABLE "burn_records" ADD CONSTRAINT "burn_records_black_hole_id_fkey" FOREIGN KEY ("black_hole_id") REFERENCES "black_holes"("id") ON DELETE RESTRICT ON UPDATE CASCADE; - --- ==================== 积分股池(绿积分池)==================== - --- CreateTable -CREATE TABLE "share_pools" ( - "id" TEXT NOT NULL, - "green_points" DECIMAL(30,8) NOT NULL DEFAULT 0, - "total_inflow" DECIMAL(30,8) NOT NULL DEFAULT 0, - "total_outflow" DECIMAL(30,8) NOT NULL DEFAULT 0, - "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - "updated_at" TIMESTAMP(3) NOT NULL, - - CONSTRAINT "share_pools_pkey" PRIMARY KEY ("id") -); - --- CreateTable -CREATE TABLE "share_pool_transactions" ( - "id" TEXT NOT NULL, - "pool_id" TEXT NOT NULL, - "type" TEXT NOT NULL, - "amount" DECIMAL(30,8) NOT NULL, - "balance_before" DECIMAL(30,8) NOT NULL, - "balance_after" DECIMAL(30,8) NOT NULL, - "reference_id" TEXT, - "reference_type" TEXT, - "memo" TEXT, - "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - - CONSTRAINT "share_pool_transactions_pkey" PRIMARY KEY ("id") -); - --- CreateIndex -CREATE INDEX "share_pool_transactions_pool_id_created_at_idx" ON "share_pool_transactions"("pool_id", "created_at" DESC); - --- AddForeignKey -ALTER TABLE "share_pool_transactions" ADD CONSTRAINT "share_pool_transactions_pool_id_fkey" FOREIGN KEY ("pool_id") REFERENCES "share_pools"("id") ON DELETE RESTRICT ON UPDATE CASCADE; - --- ==================== 价格快照 ==================== - --- CreateTable -CREATE TABLE "price_snapshots" ( - "id" TEXT NOT NULL, - "snapshot_time" TIMESTAMP(3) NOT NULL, - "price" DECIMAL(30,18) NOT NULL, - "green_points" DECIMAL(30,8) NOT NULL, - "black_hole_amount" DECIMAL(30,8) NOT NULL, - "circulation_pool" DECIMAL(30,8) NOT NULL, - "effective_denominator" DECIMAL(30,8) NOT NULL, - "minute_burn_rate" DECIMAL(30,18) NOT NULL, - "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - - CONSTRAINT "price_snapshots_pkey" PRIMARY KEY ("id") -); - --- CreateIndex -CREATE UNIQUE INDEX "price_snapshots_snapshot_time_key" ON "price_snapshots"("snapshot_time"); - --- CreateIndex -CREATE INDEX "price_snapshots_snapshot_time_idx" ON "price_snapshots"("snapshot_time" DESC); - --- ==================== 订单表添加销毁相关字段 ==================== - --- AlterTable: 添加销毁相关字段到 orders 表 -ALTER TABLE "orders" ADD COLUMN "burn_quantity" DECIMAL(30,8) NOT NULL DEFAULT 0; -ALTER TABLE "orders" ADD COLUMN "burn_multiplier" DECIMAL(30,18) NOT NULL DEFAULT 0; -ALTER TABLE "orders" ADD COLUMN "effective_quantity" DECIMAL(30,8) NOT NULL DEFAULT 0; - --- ==================== 成交记录表添加销毁相关字段 ==================== - --- 添加销毁相关字段到 trades 表 -ALTER TABLE "trades" ADD COLUMN "burn_quantity" DECIMAL(30,8) NOT NULL DEFAULT 0; -ALTER TABLE "trades" ADD COLUMN "effective_qty" DECIMAL(30,8) NOT NULL DEFAULT 0; diff --git a/backend/services/trading-service/prisma/migrations/0003_add_processed_events/migration.sql b/backend/services/trading-service/prisma/migrations/0003_add_processed_events/migration.sql deleted file mode 100644 index 0a2589df..00000000 --- a/backend/services/trading-service/prisma/migrations/0003_add_processed_events/migration.sql +++ /dev/null @@ -1,23 +0,0 @@ --- ============================================================================ --- trading-service 添加已处理事件表(幂等性支持) --- ============================================================================ - --- CreateTable -CREATE TABLE "processed_events" ( - "id" TEXT NOT NULL, - "event_id" TEXT NOT NULL, - "event_type" TEXT NOT NULL, - "source_service" TEXT NOT NULL, - "processed_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - - CONSTRAINT "processed_events_pkey" PRIMARY KEY ("id") -); - --- CreateIndex -CREATE UNIQUE INDEX "processed_events_event_id_key" ON "processed_events"("event_id"); - --- CreateIndex -CREATE INDEX "processed_events_event_id_idx" ON "processed_events"("event_id"); - --- CreateIndex -CREATE INDEX "processed_events_processed_at_idx" ON "processed_events"("processed_at"); diff --git a/backend/services/trading-service/prisma/migrations/0004_add_buy_enabled/migration.sql b/backend/services/trading-service/prisma/migrations/0004_add_buy_enabled/migration.sql deleted file mode 100644 index 28d68068..00000000 --- a/backend/services/trading-service/prisma/migrations/0004_add_buy_enabled/migration.sql +++ /dev/null @@ -1,6 +0,0 @@ --- ============================================================================ --- trading-service 添加买入功能开关 --- ============================================================================ - --- AlterTable: 添加买入功能开关字段到 trading_configs 表 -ALTER TABLE "trading_configs" ADD COLUMN "buy_enabled" BOOLEAN NOT NULL DEFAULT false; diff --git a/backend/services/trading-service/prisma/migrations/0005_add_market_maker_and_order_source/migration.sql b/backend/services/trading-service/prisma/migrations/0005_add_market_maker_and_order_source/migration.sql deleted file mode 100644 index 73506eb2..00000000 --- a/backend/services/trading-service/prisma/migrations/0005_add_market_maker_and_order_source/migration.sql +++ /dev/null @@ -1,98 +0,0 @@ --- 订单来源字段 -ALTER TABLE "orders" ADD COLUMN "source" TEXT NOT NULL DEFAULT 'USER'; -ALTER TABLE "orders" ADD COLUMN "source_label" TEXT; -CREATE INDEX "orders_source_idx" ON "orders"("source"); - --- 成交记录来源字段 -ALTER TABLE "trades" ADD COLUMN "buyer_source" TEXT NOT NULL DEFAULT 'USER'; -ALTER TABLE "trades" ADD COLUMN "seller_source" TEXT NOT NULL DEFAULT 'USER'; -CREATE INDEX "trades_buyer_source_idx" ON "trades"("buyer_source"); -CREATE INDEX "trades_seller_source_idx" ON "trades"("seller_source"); - --- 做市商配置表 -CREATE TABLE "market_maker_configs" ( - "id" TEXT NOT NULL, - "name" TEXT NOT NULL, - "account_sequence" TEXT NOT NULL, - "cash_balance" DECIMAL(30,8) NOT NULL DEFAULT 0, - "share_balance" DECIMAL(30,8) NOT NULL DEFAULT 0, - "frozen_cash" DECIMAL(30,8) NOT NULL DEFAULT 0, - "frozen_shares" DECIMAL(30,8) NOT NULL DEFAULT 0, - "max_buy_ratio" DECIMAL(10,4) NOT NULL DEFAULT 0.05, - "min_interval_ms" INTEGER NOT NULL DEFAULT 1000, - "max_interval_ms" INTEGER NOT NULL DEFAULT 4000, - "price_strategy" TEXT NOT NULL DEFAULT 'TAKER', - "discount_rate" DECIMAL(10,4) NOT NULL DEFAULT 1.0, - "is_active" BOOLEAN NOT NULL DEFAULT false, - "last_run_at" TIMESTAMP(3), - "total_buy_count" INTEGER NOT NULL DEFAULT 0, - "total_buy_quantity" DECIMAL(30,8) NOT NULL DEFAULT 0, - "total_buy_amount" DECIMAL(30,8) NOT NULL DEFAULT 0, - "total_sell_count" INTEGER NOT NULL DEFAULT 0, - "total_sell_quantity" DECIMAL(30,8) NOT NULL DEFAULT 0, - "total_sell_amount" DECIMAL(30,8) NOT NULL DEFAULT 0, - "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - "updated_at" TIMESTAMP(3) NOT NULL, - - CONSTRAINT "market_maker_configs_pkey" PRIMARY KEY ("id") -); - -CREATE UNIQUE INDEX "market_maker_configs_name_key" ON "market_maker_configs"("name"); -CREATE UNIQUE INDEX "market_maker_configs_account_sequence_key" ON "market_maker_configs"("account_sequence"); - --- 做市商分类账表 -CREATE TABLE "market_maker_ledgers" ( - "id" TEXT NOT NULL, - "market_maker_id" TEXT NOT NULL, - "type" TEXT NOT NULL, - "asset_type" TEXT NOT NULL, - "amount" DECIMAL(30,8) NOT NULL, - "balance_before" DECIMAL(30,8) NOT NULL, - "balance_after" DECIMAL(30,8) NOT NULL, - "trade_id" TEXT, - "trade_no" TEXT, - "order_id" TEXT, - "order_no" TEXT, - "counterparty_seq" TEXT, - "counterparty_id" TEXT, - "price" DECIMAL(30,18), - "quantity" DECIMAL(30,8), - "memo" TEXT, - "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - - CONSTRAINT "market_maker_ledgers_pkey" PRIMARY KEY ("id") -); - -CREATE INDEX "market_maker_ledgers_market_maker_id_created_at_idx" ON "market_maker_ledgers"("market_maker_id", "created_at" DESC); -CREATE INDEX "market_maker_ledgers_type_idx" ON "market_maker_ledgers"("type"); -CREATE INDEX "market_maker_ledgers_trade_no_idx" ON "market_maker_ledgers"("trade_no"); -CREATE INDEX "market_maker_ledgers_order_no_idx" ON "market_maker_ledgers"("order_no"); -CREATE INDEX "market_maker_ledgers_counterparty_seq_idx" ON "market_maker_ledgers"("counterparty_seq"); -CREATE INDEX "market_maker_ledgers_created_at_idx" ON "market_maker_ledgers"("created_at" DESC); - -ALTER TABLE "market_maker_ledgers" ADD CONSTRAINT "market_maker_ledgers_market_maker_id_fkey" FOREIGN KEY ("market_maker_id") REFERENCES "market_maker_configs"("id") ON DELETE RESTRICT ON UPDATE CASCADE; - --- 做市商日统计表 -CREATE TABLE "market_maker_daily_stats" ( - "id" TEXT NOT NULL, - "market_maker_id" TEXT NOT NULL, - "date" DATE NOT NULL, - "buy_count" INTEGER NOT NULL DEFAULT 0, - "buy_quantity" DECIMAL(30,8) NOT NULL DEFAULT 0, - "buy_amount" DECIMAL(30,8) NOT NULL DEFAULT 0, - "avg_buy_price" DECIMAL(30,18) NOT NULL DEFAULT 0, - "sell_count" INTEGER NOT NULL DEFAULT 0, - "sell_quantity" DECIMAL(30,8) NOT NULL DEFAULT 0, - "sell_amount" DECIMAL(30,8) NOT NULL DEFAULT 0, - "avg_sell_price" DECIMAL(30,18) NOT NULL DEFAULT 0, - "realized_pnl" DECIMAL(30,8) NOT NULL DEFAULT 0, - "cash_balance_end" DECIMAL(30,8) NOT NULL DEFAULT 0, - "share_balance_end" DECIMAL(30,8) NOT NULL DEFAULT 0, - "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - "updated_at" TIMESTAMP(3) NOT NULL, - - CONSTRAINT "market_maker_daily_stats_pkey" PRIMARY KEY ("id") -); - -CREATE UNIQUE INDEX "market_maker_daily_stats_market_maker_id_date_key" ON "market_maker_daily_stats"("market_maker_id", "date"); -CREATE INDEX "market_maker_daily_stats_market_maker_id_date_idx" ON "market_maker_daily_stats"("market_maker_id", "date" DESC); diff --git a/backend/services/trading-service/prisma/migrations/0006_add_market_maker_depth/migration.sql b/backend/services/trading-service/prisma/migrations/0006_add_market_maker_depth/migration.sql deleted file mode 100644 index 2c9d841d..00000000 --- a/backend/services/trading-service/prisma/migrations/0006_add_market_maker_depth/migration.sql +++ /dev/null @@ -1,75 +0,0 @@ --- ============================================================================ --- trading-service 添加做市商双边深度功能 --- 包含:深度开关、做市商双边挂单配置、做市商挂单记录表 --- ============================================================================ - --- ==================== TradingConfig 添加深度开关 ==================== - --- 添加深度显示开关 -ALTER TABLE "trading_configs" ADD COLUMN IF NOT EXISTS "depth_enabled" BOOLEAN NOT NULL DEFAULT false; - --- ==================== MarketMakerConfig 添加双边挂单配置 ==================== - --- 挂单模式开关 -ALTER TABLE "market_maker_configs" ADD COLUMN IF NOT EXISTS "maker_enabled" BOOLEAN NOT NULL DEFAULT false; - --- 买单挂单配置 -ALTER TABLE "market_maker_configs" ADD COLUMN IF NOT EXISTS "bid_enabled" BOOLEAN NOT NULL DEFAULT true; -ALTER TABLE "market_maker_configs" ADD COLUMN IF NOT EXISTS "bid_levels" INTEGER NOT NULL DEFAULT 5; -ALTER TABLE "market_maker_configs" ADD COLUMN IF NOT EXISTS "bid_spread" DECIMAL(10, 4) NOT NULL DEFAULT 0.01; -ALTER TABLE "market_maker_configs" ADD COLUMN IF NOT EXISTS "bid_level_spacing" DECIMAL(10, 4) NOT NULL DEFAULT 0.005; -ALTER TABLE "market_maker_configs" ADD COLUMN IF NOT EXISTS "bid_quantity_per_level" DECIMAL(30, 8) NOT NULL DEFAULT 1000; - --- 卖单挂单配置 -ALTER TABLE "market_maker_configs" ADD COLUMN IF NOT EXISTS "ask_enabled" BOOLEAN NOT NULL DEFAULT true; -ALTER TABLE "market_maker_configs" ADD COLUMN IF NOT EXISTS "ask_levels" INTEGER NOT NULL DEFAULT 5; -ALTER TABLE "market_maker_configs" ADD COLUMN IF NOT EXISTS "ask_spread" DECIMAL(10, 4) NOT NULL DEFAULT 0.01; -ALTER TABLE "market_maker_configs" ADD COLUMN IF NOT EXISTS "ask_level_spacing" DECIMAL(10, 4) NOT NULL DEFAULT 0.005; -ALTER TABLE "market_maker_configs" ADD COLUMN IF NOT EXISTS "ask_quantity_per_level" DECIMAL(30, 8) NOT NULL DEFAULT 1000; - --- 挂单刷新配置 -ALTER TABLE "market_maker_configs" ADD COLUMN IF NOT EXISTS "refresh_interval_ms" INTEGER NOT NULL DEFAULT 60000; -ALTER TABLE "market_maker_configs" ADD COLUMN IF NOT EXISTS "last_refresh_at" TIMESTAMP(3); - --- ==================== 做市商挂单记录表 ==================== - --- CreateTable -CREATE TABLE IF NOT EXISTS "market_maker_orders" ( - "id" TEXT NOT NULL, - "market_maker_id" TEXT NOT NULL, - "order_id" TEXT NOT NULL, - "order_no" TEXT NOT NULL, - "side" TEXT NOT NULL, - "level" INTEGER NOT NULL, - "price" DECIMAL(30, 18) NOT NULL, - "quantity" DECIMAL(30, 8) NOT NULL, - "remaining_qty" DECIMAL(30, 8) NOT NULL, - "status" TEXT NOT NULL DEFAULT 'ACTIVE', - "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - "updated_at" TIMESTAMP(3) NOT NULL, - - CONSTRAINT "market_maker_orders_pkey" PRIMARY KEY ("id") -); - --- CreateIndex -CREATE UNIQUE INDEX IF NOT EXISTS "market_maker_orders_order_id_key" ON "market_maker_orders"("order_id"); - --- CreateIndex -CREATE UNIQUE INDEX IF NOT EXISTS "market_maker_orders_order_no_key" ON "market_maker_orders"("order_no"); - --- CreateIndex -CREATE INDEX IF NOT EXISTS "market_maker_orders_market_maker_id_side_status_idx" ON "market_maker_orders"("market_maker_id", "side", "status"); - --- CreateIndex -CREATE INDEX IF NOT EXISTS "market_maker_orders_status_idx" ON "market_maker_orders"("status"); - --- AddForeignKey (only if not exists) -DO $$ -BEGIN - IF NOT EXISTS ( - SELECT 1 FROM pg_constraint WHERE conname = 'market_maker_orders_market_maker_id_fkey' - ) THEN - ALTER TABLE "market_maker_orders" ADD CONSTRAINT "market_maker_orders_market_maker_id_fkey" - FOREIGN KEY ("market_maker_id") REFERENCES "market_maker_configs"("id") ON DELETE RESTRICT ON UPDATE CASCADE; - END IF; -END $$; diff --git a/backend/services/trading-service/prisma/migrations/0007_add_trade_fee/migration.sql b/backend/services/trading-service/prisma/migrations/0007_add_trade_fee/migration.sql deleted file mode 100644 index 9a5db853..00000000 --- a/backend/services/trading-service/prisma/migrations/0007_add_trade_fee/migration.sql +++ /dev/null @@ -1,10 +0,0 @@ --- ============================================================================ --- trading-service 添加交易手续费字段 --- 10%交易手续费进入积分股池(greenPoints) --- ============================================================================ - --- 添加手续费字段到 trades 表 -ALTER TABLE "trades" ADD COLUMN IF NOT EXISTS "fee" DECIMAL(30, 8) NOT NULL DEFAULT 0; - --- 添加注释 -COMMENT ON COLUMN "trades"."fee" IS '交易手续费(10%进入积分股池)'; diff --git a/backend/services/trading-service/prisma/migrations/0008_add_c2c_orders/migration.sql b/backend/services/trading-service/prisma/migrations/0008_add_c2c_orders/migration.sql deleted file mode 100644 index 195b9524..00000000 --- a/backend/services/trading-service/prisma/migrations/0008_add_c2c_orders/migration.sql +++ /dev/null @@ -1,71 +0,0 @@ --- CreateEnum -CREATE TYPE "C2cOrderType" AS ENUM ('BUY', 'SELL'); - --- CreateEnum -CREATE TYPE "C2cOrderStatus" AS ENUM ('PENDING', 'MATCHED', 'PAID', 'COMPLETED', 'CANCELLED', 'EXPIRED'); - --- CreateEnum -CREATE TYPE "C2cPaymentMethod" AS ENUM ('ALIPAY', 'WECHAT', 'BANK'); - --- CreateTable -CREATE TABLE "c2c_orders" ( - "id" TEXT NOT NULL, - "order_no" TEXT NOT NULL, - "type" "C2cOrderType" NOT NULL, - "status" "C2cOrderStatus" NOT NULL DEFAULT 'PENDING', - "maker_account_sequence" TEXT NOT NULL, - "maker_user_id" TEXT, - "maker_phone" TEXT, - "maker_nickname" TEXT, - "taker_account_sequence" TEXT, - "taker_user_id" TEXT, - "taker_phone" TEXT, - "taker_nickname" TEXT, - "price" DECIMAL(30,18) NOT NULL, - "quantity" DECIMAL(30,8) NOT NULL, - "total_amount" DECIMAL(30,8) NOT NULL, - "min_amount" DECIMAL(30,8) NOT NULL DEFAULT 0, - "max_amount" DECIMAL(30,8) NOT NULL DEFAULT 0, - "payment_method" "C2cPaymentMethod", - "payment_account" TEXT, - "payment_qr_code" TEXT, - "payment_real_name" TEXT, - "remark" TEXT, - "payment_timeout_minutes" INTEGER NOT NULL DEFAULT 15, - "confirm_timeout_minutes" INTEGER NOT NULL DEFAULT 60, - "payment_deadline" TIMESTAMP(3), - "confirm_deadline" TIMESTAMP(3), - "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - "updated_at" TIMESTAMP(3) NOT NULL, - "matched_at" TIMESTAMP(3), - "paid_at" TIMESTAMP(3), - "completed_at" TIMESTAMP(3), - "cancelled_at" TIMESTAMP(3), - "expired_at" TIMESTAMP(3), - - CONSTRAINT "c2c_orders_pkey" PRIMARY KEY ("id") -); - --- CreateIndex -CREATE UNIQUE INDEX "c2c_orders_order_no_key" ON "c2c_orders"("order_no"); - --- CreateIndex -CREATE INDEX "c2c_orders_status_idx" ON "c2c_orders"("status"); - --- CreateIndex -CREATE INDEX "c2c_orders_type_status_idx" ON "c2c_orders"("type", "status"); - --- CreateIndex -CREATE INDEX "c2c_orders_maker_account_sequence_idx" ON "c2c_orders"("maker_account_sequence"); - --- CreateIndex -CREATE INDEX "c2c_orders_taker_account_sequence_idx" ON "c2c_orders"("taker_account_sequence"); - --- CreateIndex -CREATE INDEX "c2c_orders_created_at_idx" ON "c2c_orders"("created_at" DESC); - --- CreateIndex -CREATE INDEX "c2c_orders_payment_deadline_idx" ON "c2c_orders"("payment_deadline"); - --- CreateIndex -CREATE INDEX "c2c_orders_confirm_deadline_idx" ON "c2c_orders"("confirm_deadline"); diff --git a/backend/services/trading-service/prisma/migrations/0009_add_original_quantity/migration.sql b/backend/services/trading-service/prisma/migrations/0009_add_original_quantity/migration.sql deleted file mode 100644 index 608867f4..00000000 --- a/backend/services/trading-service/prisma/migrations/0009_add_original_quantity/migration.sql +++ /dev/null @@ -1,2 +0,0 @@ --- Add original_quantity column to trades table -ALTER TABLE "trades" ADD COLUMN IF NOT EXISTS "original_quantity" DECIMAL(30, 8) NOT NULL DEFAULT 0;