feat(db): payments 拆为 subscriptions 与 topups
订阅权益与充值入账分表;topups 对齐上游充值单(无 quota_granted), paid_at=complete_time;quota_derived 查询层派生。
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
CREATE TABLE "api_keys" (
|
||||
"id" bigint PRIMARY KEY GENERATED ALWAYS AS IDENTITY (sequence name "api_keys_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854775807 START WITH 1 CACHE 1),
|
||||
"key_hash" text NOT NULL,
|
||||
"key_ciphertext" "bytea" NOT NULL,
|
||||
"status" text DEFAULT 'active' NOT NULL,
|
||||
"member_id" bigint,
|
||||
"registered_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"log_synced_at" timestamp with time zone,
|
||||
"usage_synced_at" timestamp with time zone,
|
||||
"token_name" text DEFAULT '' NOT NULL,
|
||||
"total_granted" bigint DEFAULT 0 NOT NULL,
|
||||
"total_used" bigint DEFAULT 0 NOT NULL,
|
||||
"total_available" bigint DEFAULT 0 NOT NULL,
|
||||
"unlimited_quota" boolean DEFAULT false NOT NULL,
|
||||
"model_limits" jsonb,
|
||||
"model_limits_enabled" boolean DEFAULT false NOT NULL,
|
||||
"expires_at" timestamp with time zone,
|
||||
CONSTRAINT "api_keys_key_hash_unique" UNIQUE("key_hash")
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "audit_logs" (
|
||||
"id" bigint PRIMARY KEY GENERATED ALWAYS AS IDENTITY (sequence name "audit_logs_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854775807 START WITH 1 CACHE 1),
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"operator" text,
|
||||
"domain" text NOT NULL,
|
||||
"action" text NOT NULL,
|
||||
"summary" text DEFAULT '' NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "engine_status" (
|
||||
"id" integer PRIMARY KEY DEFAULT 1 NOT NULL,
|
||||
"running" boolean DEFAULT false NOT NULL,
|
||||
"sync_interval_ms" bigint DEFAULT 300000 NOT NULL,
|
||||
"last_round" jsonb,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "members" (
|
||||
"id" bigint PRIMARY KEY GENERATED ALWAYS AS IDENTITY (sequence name "members_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854775807 START WITH 1 CACHE 1),
|
||||
"name" text NOT NULL,
|
||||
"remark" text DEFAULT '' NOT NULL,
|
||||
"status" text DEFAULT 'active' NOT NULL,
|
||||
"token_hash" text NOT NULL,
|
||||
"token_issued_at" timestamp with time zone,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "members_name_unique" UNIQUE("name"),
|
||||
CONSTRAINT "members_token_hash_unique" UNIQUE("token_hash")
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "qpu_history" (
|
||||
"id" bigint PRIMARY KEY GENERATED ALWAYS AS IDENTITY (sequence name "qpu_history_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854775807 START WITH 1 CACHE 1),
|
||||
"qpu" numeric(18, 6) NOT NULL,
|
||||
"effective_from" timestamp with time zone DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "sessions" (
|
||||
"id" bigint PRIMARY KEY GENERATED ALWAYS AS IDENTITY (sequence name "sessions_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854775807 START WITH 1 CACHE 1),
|
||||
"user_id" bigint NOT NULL,
|
||||
"token_hash" text NOT NULL,
|
||||
"expires_at" timestamp with time zone NOT NULL,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "sessions_token_hash_unique" UNIQUE("token_hash")
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "subscriptions" (
|
||||
"id" bigint PRIMARY KEY GENERATED ALWAYS AS IDENTITY (sequence name "subscriptions_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854775807 START WITH 1 CACHE 1),
|
||||
"subscription_id" bigint NOT NULL,
|
||||
"period_start" timestamp with time zone NOT NULL,
|
||||
"period_end" timestamp with time zone,
|
||||
"quota_granted" bigint NOT NULL,
|
||||
"paid_amount" numeric(18, 2),
|
||||
"paid_at" timestamp with time zone,
|
||||
"remark" text DEFAULT '' NOT NULL,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "subscriptions_subscription_id_unique" UNIQUE("subscription_id")
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "topups" (
|
||||
"id" bigint PRIMARY KEY GENERATED ALWAYS AS IDENTITY (sequence name "topups_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854775807 START WITH 1 CACHE 1),
|
||||
"upstream_id" bigint,
|
||||
"amount" numeric(18, 6) NOT NULL,
|
||||
"money" numeric(18, 2) NOT NULL,
|
||||
"trade_no" text,
|
||||
"payment_method" text,
|
||||
"payment_provider" text,
|
||||
"status" text,
|
||||
"paid_at" timestamp with time zone NOT NULL,
|
||||
"remark" text DEFAULT '' NOT NULL,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "usage_logs" (
|
||||
"id" bigint PRIMARY KEY GENERATED ALWAYS AS IDENTITY (sequence name "usage_logs_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854775807 START WITH 1 CACHE 1),
|
||||
"api_key_id" bigint NOT NULL,
|
||||
"dedupe_key" text NOT NULL,
|
||||
"billing_source" text,
|
||||
"subscription_id" bigint,
|
||||
"user_id" bigint DEFAULT 0 NOT NULL,
|
||||
"created_at" timestamp with time zone NOT NULL,
|
||||
"type" smallint DEFAULT 2 NOT NULL,
|
||||
"content" text DEFAULT '' NOT NULL,
|
||||
"username" text DEFAULT '' NOT NULL,
|
||||
"token_name" text DEFAULT '' NOT NULL,
|
||||
"model_name" text DEFAULT '' NOT NULL,
|
||||
"quota" bigint DEFAULT 0 NOT NULL,
|
||||
"prompt_tokens" integer DEFAULT 0 NOT NULL,
|
||||
"completion_tokens" integer DEFAULT 0 NOT NULL,
|
||||
"use_time" integer DEFAULT 0 NOT NULL,
|
||||
"is_stream" boolean DEFAULT false NOT NULL,
|
||||
"channel" integer DEFAULT 0 NOT NULL,
|
||||
"channel_name" text DEFAULT '' NOT NULL,
|
||||
"token_id" bigint DEFAULT 0 NOT NULL,
|
||||
"group_name" text DEFAULT '' NOT NULL,
|
||||
"ip" text DEFAULT '' NOT NULL,
|
||||
"request_id" text DEFAULT '' NOT NULL,
|
||||
"upstream_request_id" text,
|
||||
"other" jsonb,
|
||||
CONSTRAINT "usage_logs_dedupe_key_unique" UNIQUE("dedupe_key")
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "users" (
|
||||
"id" bigint PRIMARY KEY GENERATED ALWAYS AS IDENTITY (sequence name "users_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854775807 START WITH 1 CACHE 1),
|
||||
"username" text NOT NULL,
|
||||
"password_hash" text NOT NULL,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "users_username_unique" UNIQUE("username")
|
||||
);
|
||||
--> statement-breakpoint
|
||||
ALTER TABLE "api_keys" ADD CONSTRAINT "api_keys_member_id_members_id_fk" FOREIGN KEY ("member_id") REFERENCES "public"."members"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "sessions" ADD CONSTRAINT "sessions_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "usage_logs" ADD CONSTRAINT "usage_logs_api_key_id_api_keys_id_fk" FOREIGN KEY ("api_key_id") REFERENCES "public"."api_keys"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
|
||||
CREATE INDEX "api_keys_member_id_idx" ON "api_keys" USING btree ("member_id");--> statement-breakpoint
|
||||
CREATE INDEX "audit_logs_created_at_idx" ON "audit_logs" USING btree ("created_at");--> statement-breakpoint
|
||||
CREATE INDEX "sessions_user_id_idx" ON "sessions" USING btree ("user_id");--> statement-breakpoint
|
||||
CREATE INDEX "subscriptions_period_idx" ON "subscriptions" USING btree ("period_start","period_end");--> statement-breakpoint
|
||||
CREATE INDEX "usage_logs_created_at_idx" ON "usage_logs" USING btree ("created_at");--> statement-breakpoint
|
||||
CREATE INDEX "usage_logs_api_key_created_idx" ON "usage_logs" USING btree ("api_key_id","created_at");--> statement-breakpoint
|
||||
CREATE INDEX "usage_logs_subscription_idx" ON "usage_logs" USING btree ("subscription_id");
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"version": "7",
|
||||
"dialect": "postgresql",
|
||||
"entries": [
|
||||
{
|
||||
"idx": 0,
|
||||
"version": "7",
|
||||
"when": 1791551016051,
|
||||
"tag": "0000_loose_goblin_queen",
|
||||
"breakpoints": true
|
||||
}
|
||||
]
|
||||
}
|
||||
+71
-23
@@ -4,17 +4,19 @@ import {
|
||||
auditLogs,
|
||||
engineStatus,
|
||||
members,
|
||||
payments,
|
||||
subscriptions,
|
||||
qpuHistory,
|
||||
sessions,
|
||||
topups,
|
||||
usageLogs,
|
||||
users,
|
||||
type ApiKeyRow,
|
||||
type AuditLogRow,
|
||||
type EngineStatusRow,
|
||||
type MemberRow,
|
||||
type PaymentRow,
|
||||
type SubscriptionRow,
|
||||
type SessionRow,
|
||||
type TopupRow,
|
||||
type UsageLogRow,
|
||||
type UserRow,
|
||||
} from "./schema.ts";
|
||||
@@ -384,41 +386,87 @@ export function createRepos(db: Db) {
|
||||
},
|
||||
},
|
||||
|
||||
payments: {
|
||||
async listAll(): Promise<PaymentRow[]> {
|
||||
return db.select().from(payments).orderBy(desc(payments.paid_at));
|
||||
subscriptions: {
|
||||
async listAll(): Promise<SubscriptionRow[]> {
|
||||
return db
|
||||
.select()
|
||||
.from(subscriptions)
|
||||
.orderBy(desc(subscriptions.period_start));
|
||||
},
|
||||
async findById(id: number): Promise<PaymentRow | null> {
|
||||
async findById(id: number): Promise<SubscriptionRow | null> {
|
||||
const rows = await db
|
||||
.select()
|
||||
.from(payments)
|
||||
.where(eq(payments.id, id))
|
||||
.from(subscriptions)
|
||||
.where(eq(subscriptions.id, id))
|
||||
.limit(1);
|
||||
return rows[0] ?? null;
|
||||
},
|
||||
async insert(values: {
|
||||
kind: string;
|
||||
paid_at: Date;
|
||||
paid_amount: number;
|
||||
remark: string;
|
||||
period_start: Date | null;
|
||||
period_end: Date | null;
|
||||
subscription_id: number | null;
|
||||
quota_granted: number | null;
|
||||
}): Promise<PaymentRow> {
|
||||
const [row] = await db.insert(payments).values(values).returning();
|
||||
async findBySubscriptionId(
|
||||
subscriptionId: number,
|
||||
): Promise<SubscriptionRow | null> {
|
||||
const rows = await db
|
||||
.select()
|
||||
.from(subscriptions)
|
||||
.where(eq(subscriptions.subscription_id, subscriptionId))
|
||||
.limit(1);
|
||||
return rows[0] ?? null;
|
||||
},
|
||||
async insert(
|
||||
values: typeof subscriptions.$inferInsert,
|
||||
): Promise<SubscriptionRow> {
|
||||
const [row] = await db.insert(subscriptions).values(values).returning();
|
||||
return row!;
|
||||
},
|
||||
async update(id: number, patch: Partial<typeof payments.$inferInsert>) {
|
||||
async update(
|
||||
id: number,
|
||||
patch: Partial<typeof subscriptions.$inferInsert>,
|
||||
) {
|
||||
const [row] = await db
|
||||
.update(payments)
|
||||
.update(subscriptions)
|
||||
.set(patch)
|
||||
.where(eq(payments.id, id))
|
||||
.where(eq(subscriptions.id, id))
|
||||
.returning();
|
||||
return row!;
|
||||
},
|
||||
async remove(id: number): Promise<void> {
|
||||
await db.delete(payments).where(eq(payments.id, id));
|
||||
await db.delete(subscriptions).where(eq(subscriptions.id, id));
|
||||
},
|
||||
},
|
||||
|
||||
topups: {
|
||||
async listAll(): Promise<TopupRow[]> {
|
||||
return db.select().from(topups).orderBy(desc(topups.paid_at));
|
||||
},
|
||||
async findById(id: number): Promise<TopupRow | null> {
|
||||
const rows = await db
|
||||
.select()
|
||||
.from(topups)
|
||||
.where(eq(topups.id, id))
|
||||
.limit(1);
|
||||
return rows[0] ?? null;
|
||||
},
|
||||
async findByUpstreamId(upstreamId: number): Promise<TopupRow | null> {
|
||||
const rows = await db
|
||||
.select()
|
||||
.from(topups)
|
||||
.where(eq(topups.upstream_id, upstreamId))
|
||||
.limit(1);
|
||||
return rows[0] ?? null;
|
||||
},
|
||||
async insert(values: typeof topups.$inferInsert): Promise<TopupRow> {
|
||||
const [row] = await db.insert(topups).values(values).returning();
|
||||
return row!;
|
||||
},
|
||||
async update(id: number, patch: Partial<typeof topups.$inferInsert>) {
|
||||
const [row] = await db
|
||||
.update(topups)
|
||||
.set(patch)
|
||||
.where(eq(topups.id, id))
|
||||
.returning();
|
||||
return row!;
|
||||
},
|
||||
async remove(id: number): Promise<void> {
|
||||
await db.delete(topups).where(eq(topups.id, id));
|
||||
},
|
||||
},
|
||||
|
||||
|
||||
@@ -117,20 +117,57 @@ export const usageLogs = pgTable(
|
||||
],
|
||||
);
|
||||
|
||||
export const payments = pgTable("payments", {
|
||||
/** 订阅权益登记(原 payments.kind=subscription) */
|
||||
export const subscriptions = pgTable(
|
||||
"subscriptions",
|
||||
{
|
||||
id: bigint("id", { mode: "number" }).primaryKey().generatedAlwaysAsIdentity(),
|
||||
subscription_id: bigint("subscription_id", { mode: "number" }).notNull().unique(),
|
||||
period_start: timestamp("period_start", {
|
||||
withTimezone: true,
|
||||
mode: "date",
|
||||
}).notNull(),
|
||||
period_end: timestamp("period_end", { withTimezone: true, mode: "date" }),
|
||||
quota_granted: bigint("quota_granted", { mode: "number" }).notNull(),
|
||||
paid_amount: numeric("paid_amount", {
|
||||
precision: 18,
|
||||
scale: 2,
|
||||
mode: "number",
|
||||
}),
|
||||
paid_at: timestamp("paid_at", { withTimezone: true, mode: "date" }),
|
||||
remark: text("remark").notNull().default(""),
|
||||
created_at: timestamp("created_at", {
|
||||
withTimezone: true,
|
||||
mode: "date",
|
||||
})
|
||||
.notNull()
|
||||
.defaultNow(),
|
||||
},
|
||||
(t) => [index("subscriptions_period_idx").on(t.period_start, t.period_end)],
|
||||
);
|
||||
|
||||
/** 充值入账事件(原 payments.kind=topup;无 quota_granted 列) */
|
||||
export const topups = pgTable("topups", {
|
||||
id: bigint("id", { mode: "number" }).primaryKey().generatedAlwaysAsIdentity(),
|
||||
kind: text("kind").notNull(),
|
||||
paid_at: timestamp("paid_at", { withTimezone: true, mode: "date" }).notNull(),
|
||||
paid_amount: numeric("paid_amount", {
|
||||
upstream_id: bigint("upstream_id", { mode: "number" }),
|
||||
amount: numeric("amount", { precision: 18, scale: 6, mode: "number" }).notNull(),
|
||||
money: numeric("money", {
|
||||
precision: 18,
|
||||
scale: 2,
|
||||
mode: "number",
|
||||
}).notNull(),
|
||||
trade_no: text("trade_no"),
|
||||
payment_method: text("payment_method"),
|
||||
payment_provider: text("payment_provider"),
|
||||
status: text("status"),
|
||||
paid_at: timestamp("paid_at", { withTimezone: true, mode: "date" }).notNull(),
|
||||
remark: text("remark").notNull().default(""),
|
||||
period_start: timestamp("period_start", { withTimezone: true, mode: "date" }),
|
||||
period_end: timestamp("period_end", { withTimezone: true, mode: "date" }),
|
||||
subscription_id: bigint("subscription_id", { mode: "number" }),
|
||||
quota_granted: bigint("quota_granted", { mode: "number" }),
|
||||
created_at: timestamp("created_at", {
|
||||
withTimezone: true,
|
||||
mode: "date",
|
||||
})
|
||||
.notNull()
|
||||
.defaultNow(),
|
||||
});
|
||||
|
||||
export const qpuHistory = pgTable("qpu_history", {
|
||||
@@ -207,7 +244,8 @@ export const engineStatus = pgTable("engine_status", {
|
||||
export type MemberRow = typeof members.$inferSelect;
|
||||
export type ApiKeyRow = typeof apiKeys.$inferSelect;
|
||||
export type UsageLogRow = typeof usageLogs.$inferSelect;
|
||||
export type PaymentRow = typeof payments.$inferSelect;
|
||||
export type SubscriptionRow = typeof subscriptions.$inferSelect;
|
||||
export type TopupRow = typeof topups.$inferSelect;
|
||||
export type QpuHistoryRow = typeof qpuHistory.$inferSelect;
|
||||
export type UserRow = typeof users.$inferSelect;
|
||||
export type SessionRow = typeof sessions.$inferSelect;
|
||||
|
||||
Reference in New Issue
Block a user