-- 日志库迁移脚本（请在 psql 中按步骤执行）

-- 1. 在日志库创建表结构（takeout_logs_db）
\connect takeout_logs_db

CREATE TABLE IF NOT EXISTS tenant_audit_logs (
    "Id" bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    "TenantId" bigint NOT NULL,
    "Action" integer NOT NULL,
    "Title" character varying(128) NOT NULL,
    "Description" character varying(1024),
    "OperatorId" bigint,
    "OperatorName" character varying(64),
    "PreviousStatus" integer,
    "CurrentStatus" integer,
    "CreatedAt" timestamp with time zone NOT NULL,
    "UpdatedAt" timestamp with time zone,
    "DeletedAt" timestamp with time zone,
    "CreatedBy" bigint,
    "UpdatedBy" bigint,
    "DeletedBy" bigint
);
CREATE INDEX IF NOT EXISTS "IX_tenant_audit_logs_TenantId" ON tenant_audit_logs ("TenantId");

CREATE TABLE IF NOT EXISTS merchant_audit_logs (
    "Id" bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    "MerchantId" bigint NOT NULL,
    "Action" integer NOT NULL,
    "Title" character varying(128) NOT NULL,
    "Description" character varying(1024),
    "OperatorId" bigint,
    "OperatorName" character varying(64),
    "CreatedAt" timestamp with time zone NOT NULL,
    "UpdatedAt" timestamp with time zone,
    "DeletedAt" timestamp with time zone,
    "CreatedBy" bigint,
    "UpdatedBy" bigint,
    "DeletedBy" bigint,
    "TenantId" bigint NOT NULL
);
CREATE INDEX IF NOT EXISTS "IX_merchant_audit_logs_TenantId_MerchantId" ON merchant_audit_logs ("TenantId", "MerchantId");

CREATE TABLE IF NOT EXISTS operation_logs (
    "Id" bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    "OperationType" character varying(64) NOT NULL,
    "TargetType" character varying(64) NOT NULL,
    "TargetIds" text,
    "OperatorId" character varying(64),
    "OperatorName" character varying(128),
    "Parameters" text,
    "Result" text,
    "Success" boolean NOT NULL,
    "CreatedAt" timestamp with time zone NOT NULL,
    "UpdatedAt" timestamp with time zone,
    "DeletedAt" timestamp with time zone,
    "CreatedBy" bigint,
    "UpdatedBy" bigint,
    "DeletedBy" bigint
);
CREATE INDEX IF NOT EXISTS "IX_operation_logs_CreatedAt" ON operation_logs ("CreatedAt");
CREATE INDEX IF NOT EXISTS "IX_operation_logs_OperationType_CreatedAt" ON operation_logs ("OperationType", "CreatedAt");

CREATE TABLE IF NOT EXISTS member_growth_logs (
    "Id" bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    "MemberId" bigint NOT NULL,
    "ChangeValue" integer NOT NULL,
    "CurrentValue" integer NOT NULL,
    "Notes" character varying(256),
    "OccurredAt" timestamp with time zone NOT NULL,
    "CreatedAt" timestamp with time zone NOT NULL,
    "UpdatedAt" timestamp with time zone,
    "DeletedAt" timestamp with time zone,
    "CreatedBy" bigint,
    "UpdatedBy" bigint,
    "DeletedBy" bigint,
    "TenantId" bigint NOT NULL
);
CREATE INDEX IF NOT EXISTS "IX_member_growth_logs_TenantId_MemberId_OccurredAt" ON member_growth_logs ("TenantId", "MemberId", "OccurredAt");

-- 2. 迁移数据（建议使用 pg_dump/pg_restore 或应用侧批量拷贝）
--    示例：pg_dump -t tenant_audit_logs -t merchant_audit_logs -t operation_logs -t member_growth_logs takeout_app_db > logs_dump.sql
--          psql -d takeout_logs_db -f logs_dump.sql

-- 3. 在业务库删除旧日志表（takeout_app_db）
\connect takeout_app_db
DROP TABLE IF EXISTS tenant_audit_logs;
DROP TABLE IF EXISTS merchant_audit_logs;
DROP TABLE IF EXISTS operation_logs;
DROP TABLE IF EXISTS member_growth_logs;
