fix: simplify balance billing and govern operation logs

This commit is contained in:
hectorzhao
2026-07-14 17:40:32 +08:00
parent f35691f185
commit 28dad93e3e
40 changed files with 740 additions and 395 deletions
@@ -0,0 +1,37 @@
CREATE INDEX "OperationLog_createdAt_idx"
ON "OperationLog"("createdAt");
CREATE INDEX "OperationLog_resource_createdAt_idx"
ON "OperationLog"("resource", "createdAt");
CREATE TABLE "OperationLogArchive" (
"originalId" TEXT NOT NULL,
"tenantId" TEXT,
"userId" TEXT,
"action" TEXT NOT NULL,
"resource" TEXT NOT NULL,
"resourceId" TEXT,
"ipAddress" TEXT,
"userAgent" TEXT,
"detail" JSONB,
"createdAt" TIMESTAMP(3) NOT NULL,
"archiveMonth" TEXT NOT NULL,
"archivedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "OperationLogArchive_pkey" PRIMARY KEY ("originalId")
);
CREATE INDEX "OperationLogArchive_tenantId_createdAt_idx"
ON "OperationLogArchive"("tenantId", "createdAt");
CREATE INDEX "OperationLogArchive_userId_createdAt_idx"
ON "OperationLogArchive"("userId", "createdAt");
CREATE INDEX "OperationLogArchive_createdAt_idx"
ON "OperationLogArchive"("createdAt");
CREATE INDEX "OperationLogArchive_resource_createdAt_idx"
ON "OperationLogArchive"("resource", "createdAt");
CREATE INDEX "OperationLogArchive_archiveMonth_idx"
ON "OperationLogArchive"("archiveMonth");
@@ -0,0 +1,14 @@
ALTER TABLE "RechargeOrder" DROP CONSTRAINT IF EXISTS "RechargeOrder_planId_fkey";
DROP TABLE IF EXISTS "BillingPlan";
ALTER TABLE "TenantAccount"
DROP COLUMN "smsUnits",
DROP COLUMN "creditCents";
ALTER TABLE "AccountTransaction"
DROP COLUMN "smsUnits";
ALTER TABLE "RechargeOrder"
DROP COLUMN "planId",
DROP COLUMN "smsUnits";
+24 -21
View File
@@ -154,6 +154,29 @@ model OperationLog {
@@index([tenantId, createdAt])
@@index([userId, createdAt])
@@index([createdAt])
@@index([resource, createdAt])
}
model OperationLogArchive {
originalId String @id
tenantId String?
userId String?
action String
resource String
resourceId String?
ipAddress String?
userAgent String?
detail Json?
createdAt DateTime
archiveMonth String
archivedAt DateTime @default(now())
@@index([tenantId, createdAt])
@@index([userId, createdAt])
@@index([createdAt])
@@index([resource, createdAt])
@@index([archiveMonth])
}
model FileObject {
@@ -240,26 +263,10 @@ model DrainageField {
channelReportFields ChannelReportField[]
}
model BillingPlan {
id String @id @default(cuid())
name String
priceCents Int
smsUnits Int
validDays Int
status String @default("active")
description String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
rechargeOrders RechargeOrder[]
}
model TenantAccount {
id String @id @default(cuid())
tenantId String
balanceCents Int @default(0)
smsUnits Int @default(0)
creditCents Int @default(0)
status String @default("active")
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@ -274,7 +281,6 @@ model AccountTransaction {
tenantId String
transactionType String
amountCents Int @default(0)
smsUnits Int @default(0)
balanceAfter Int @default(0)
relatedType String?
relatedId String?
@@ -301,10 +307,8 @@ model BillingRule {
model RechargeOrder {
id String @id @default(cuid())
tenantId String
planId String?
orderNo String @unique
amountCents Int
smsUnits Int @default(0)
status String @default("created")
payMethod String?
paidAt DateTime?
@@ -313,8 +317,7 @@ model RechargeOrder {
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
tenant Tenant @relation(fields: [tenantId], references: [id])
plan BillingPlan? @relation(fields: [planId], references: [id])
tenant Tenant @relation(fields: [tenantId], references: [id])
@@index([tenantId, createdAt])
}