-- 微信订阅消息的授权记账表 -- -- 背景:微信没有「还剩几条订阅授权」的查询接口,而一次性订阅是发一条扣一条。 -- 这里按 (userId, templateId) 记账,让服务端和体检接口能说清「为什么收不到」。 -- 记账只是近似值,不参与是否发送的硬判定(可用 force 绕过)。 CREATE TABLE IF NOT EXISTS "WechatSubscribeGrant" ( "id" serial PRIMARY KEY, "userId" integer NOT NULL, "templateId" text NOT NULL, "kind" text, "grantedCount" integer DEFAULT 0 NOT NULL, "sentCount" integer DEFAULT 0 NOT NULL, "blockedCount" integer DEFAULT 0 NOT NULL, "lastGrantedAt" timestamp, "lastSentAt" timestamp, "lastBlockedAt" timestamp, "lastErrorCode" integer, "lastErrorMessage" text, "createdAt" timestamp DEFAULT now() NOT NULL, "updatedAt" timestamp DEFAULT now() NOT NULL ); CREATE UNIQUE INDEX IF NOT EXISTS "WechatSubscribeGrant_userId_templateId_unq" ON "WechatSubscribeGrant" ("userId", "templateId"); CREATE INDEX IF NOT EXISTS "WechatSubscribeGrant_userId_idx" ON "WechatSubscribeGrant" ("userId");