-- 创建用户海报缓存表
-- 用于存储用户生成的推广海报，避免重复生成

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[user_poster_cache]') AND type in (N'U'))
BEGIN
    CREATE TABLE [dbo].[user_poster_cache] (
        [id] INT IDENTITY(1,1) NOT NULL,
        [user_id] INT NOT NULL,
        [app_id] NVARCHAR(100) NULL,
        [template_hash] NVARCHAR(64) NOT NULL,
        [cos_url] NVARCHAR(500) NOT NULL,
        [file_size] BIGINT NOT NULL DEFAULT 0,
        [mime_type] NVARCHAR(50) NOT NULL DEFAULT 'image/png',
        [status] INT NOT NULL DEFAULT 1,
        [expires_at] DATETIME2 NOT NULL,
        [platform] NVARCHAR(50) NOT NULL DEFAULT 'MP-WEIXIN',
        [created_at] DATETIME2 NOT NULL DEFAULT GETDATE(),
        [updated_at] DATETIME2 NOT NULL DEFAULT GETDATE(),
        CONSTRAINT [pk_user_poster_cache] PRIMARY KEY CLUSTERED ([id] ASC)
    );

    -- 创建索引
    CREATE INDEX [ix_user_poster_cache_user_id] ON [dbo].[user_poster_cache] ([user_id]);
    CREATE INDEX [ix_user_poster_cache_template_hash] ON [dbo].[user_poster_cache] ([template_hash]);
    CREATE INDEX [ix_user_poster_cache_platform] ON [dbo].[user_poster_cache] ([platform]);
    CREATE INDEX [ix_user_poster_cache_status] ON [dbo].[user_poster_cache] ([status]);
    CREATE INDEX [ix_user_poster_cache_expires_at] ON [dbo].[user_poster_cache] ([expires_at]);

    PRINT '表 user_poster_cache 创建成功';
END
ELSE
BEGIN
    PRINT '表 user_poster_cache 已存在';
END
GO
