feat: add reconciliation and quality reporting

This commit is contained in:
hectorzhao
2026-07-15 14:22:50 +08:00
parent 16311546af
commit 8c3336600e
32 changed files with 1730 additions and 200 deletions
@@ -0,0 +1,59 @@
ALTER TABLE "SmsSubmitRecord"
ADD COLUMN "costUnitPrice" INTEGER NOT NULL DEFAULT 0,
ADD COLUMN "costAmountCents" INTEGER NOT NULL DEFAULT 0;
UPDATE "SmsSubmitRecord" submit
SET
"costUnitPrice" = channel."unitPrice",
"costAmountCents" = channel."unitPrice" * message."billingUnits"
FROM "SmsChannel" channel, "SmsMessageRecord" message
WHERE submit."channelId" = channel.id
AND submit."messageRecordId" = message.id;
CREATE TABLE "DailyReconciliationReport" (
"id" TEXT NOT NULL,
"reportDate" DATE NOT NULL,
"tenantId" TEXT NOT NULL,
"tenantName" TEXT NOT NULL,
"applicationId" TEXT NOT NULL,
"applicationName" TEXT NOT NULL,
"sentUnits" INTEGER NOT NULL DEFAULT 0,
"successUnits" INTEGER NOT NULL DEFAULT 0,
"generatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
CONSTRAINT "DailyReconciliationReport_pkey" PRIMARY KEY ("id")
);
CREATE TABLE "DailyProfitReport" (
"id" TEXT NOT NULL,
"reportDate" DATE NOT NULL,
"dimensionType" TEXT NOT NULL,
"dimensionId" TEXT NOT NULL,
"dimensionName" TEXT NOT NULL,
"tenantId" TEXT,
"tenantName" TEXT,
"applicationId" TEXT,
"channelId" TEXT,
"sentUnits" INTEGER NOT NULL DEFAULT 0,
"successUnits" INTEGER NOT NULL DEFAULT 0,
"revenueCents" INTEGER NOT NULL DEFAULT 0,
"costCents" INTEGER NOT NULL DEFAULT 0,
"profitCents" INTEGER NOT NULL DEFAULT 0,
"profitRateBps" INTEGER NOT NULL DEFAULT 0,
"generatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
CONSTRAINT "DailyProfitReport_pkey" PRIMARY KEY ("id")
);
CREATE UNIQUE INDEX "DailyReconciliationReport_reportDate_tenantId_applicationId_key"
ON "DailyReconciliationReport"("reportDate", "tenantId", "applicationId");
CREATE INDEX "DailyReconciliationReport_reportDate_idx" ON "DailyReconciliationReport"("reportDate");
CREATE INDEX "DailyReconciliationReport_tenantId_reportDate_idx" ON "DailyReconciliationReport"("tenantId", "reportDate");
CREATE INDEX "DailyReconciliationReport_applicationId_reportDate_idx" ON "DailyReconciliationReport"("applicationId", "reportDate");
CREATE UNIQUE INDEX "DailyProfitReport_reportDate_dimensionType_dimensionId_key"
ON "DailyProfitReport"("reportDate", "dimensionType", "dimensionId");
CREATE INDEX "DailyProfitReport_dimensionType_reportDate_idx" ON "DailyProfitReport"("dimensionType", "reportDate");
CREATE INDEX "DailyProfitReport_tenantId_reportDate_idx" ON "DailyProfitReport"("tenantId", "reportDate");
CREATE INDEX "DailyProfitReport_applicationId_reportDate_idx" ON "DailyProfitReport"("applicationId", "reportDate");
CREATE INDEX "DailyProfitReport_channelId_reportDate_idx" ON "DailyProfitReport"("channelId", "reportDate");
@@ -0,0 +1,23 @@
CREATE TABLE "CommonReportField" (
"id" TEXT NOT NULL,
"drainageFieldId" TEXT NOT NULL,
"reportType" TEXT NOT NULL,
"required" BOOLEAN NOT NULL DEFAULT false,
"sortOrder" INTEGER NOT NULL DEFAULT 100,
"status" TEXT NOT NULL DEFAULT 'active',
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
CONSTRAINT "CommonReportField_pkey" PRIMARY KEY ("id")
);
CREATE UNIQUE INDEX "CommonReportField_drainageFieldId_reportType_key"
ON "CommonReportField"("drainageFieldId", "reportType");
CREATE INDEX "CommonReportField_reportType_status_sortOrder_idx"
ON "CommonReportField"("reportType", "status", "sortOrder");
ALTER TABLE "CommonReportField"
ADD CONSTRAINT "CommonReportField_drainageFieldId_fkey"
FOREIGN KEY ("drainageFieldId") REFERENCES "DrainageField"("id")
ON DELETE RESTRICT ON UPDATE CASCADE;
@@ -0,0 +1,65 @@
ALTER TABLE "SmsMessageRecord" ADD COLUMN "drainageInfoId" TEXT;
ALTER TABLE "SmsMessageRecord"
ADD CONSTRAINT "SmsMessageRecord_drainageInfoId_fkey"
FOREIGN KEY ("drainageInfoId") REFERENCES "SmsDrainageInfo"("id") ON DELETE SET NULL ON UPDATE CASCADE;
CREATE INDEX "SmsMessageRecord_drainageInfoId_queuedAt_idx"
ON "SmsMessageRecord"("drainageInfoId", "queuedAt");
WITH drainage_matches AS (
SELECT
message.id AS "messageRecordId",
drainage.id AS "drainageInfoId",
ROW_NUMBER() OVER (
PARTITION BY message.id
ORDER BY LENGTH(drainage.url) DESC, drainage."updatedAt" DESC, drainage.id
) AS match_rank,
COUNT(*) OVER (
PARTITION BY message.id, LENGTH(drainage.url)
) AS same_length_matches
FROM "SmsMessageRecord" message
JOIN "SmsDrainageInfo" drainage
ON drainage."signatureId" = message."signatureId"
AND drainage."auditStatus" = 'approved'
AND LENGTH(BTRIM(drainage.url)) > 0
AND POSITION(BTRIM(drainage.url) IN message.content) > 0
WHERE message."drainageInfoId" IS NULL
)
UPDATE "SmsMessageRecord" message
SET "drainageInfoId" = matched."drainageInfoId"
FROM drainage_matches matched
WHERE message.id = matched."messageRecordId"
AND matched.match_rank = 1
AND matched.same_length_matches = 1;
CREATE TABLE "DailyQualityReport" (
"id" TEXT NOT NULL,
"reportDate" DATE NOT NULL,
"dimensionType" TEXT NOT NULL,
"dimensionId" TEXT NOT NULL,
"dimensionName" TEXT NOT NULL,
"tenantId" TEXT,
"tenantName" TEXT,
"applicationId" TEXT,
"channelId" TEXT,
"signatureId" TEXT,
"drainageInfoId" TEXT,
"sentUnits" INTEGER NOT NULL DEFAULT 0,
"successUnits" INTEGER NOT NULL DEFAULT 0,
"successRateBps" INTEGER NOT NULL DEFAULT 0,
"avgArrivalMs" INTEGER,
"generatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
CONSTRAINT "DailyQualityReport_pkey" PRIMARY KEY ("id")
);
CREATE UNIQUE INDEX "DailyQualityReport_reportDate_dimensionType_dimensionId_key"
ON "DailyQualityReport"("reportDate", "dimensionType", "dimensionId");
CREATE INDEX "DailyQualityReport_dimensionType_reportDate_sentUnits_idx"
ON "DailyQualityReport"("dimensionType", "reportDate", "sentUnits");
CREATE INDEX "DailyQualityReport_tenantId_reportDate_idx" ON "DailyQualityReport"("tenantId", "reportDate");
CREATE INDEX "DailyQualityReport_applicationId_reportDate_idx" ON "DailyQualityReport"("applicationId", "reportDate");
CREATE INDEX "DailyQualityReport_channelId_reportDate_idx" ON "DailyQualityReport"("channelId", "reportDate");
CREATE INDEX "DailyQualityReport_signatureId_reportDate_idx" ON "DailyQualityReport"("signatureId", "reportDate");
CREATE INDEX "DailyQualityReport_drainageInfoId_reportDate_idx" ON "DailyQualityReport"("drainageInfoId", "reportDate");
+95
View File
@@ -261,6 +261,7 @@ model DrainageField {
updatedAt DateTime @updatedAt
channelReportFields ChannelReportField[]
commonReportFields CommonReportField[]
}
model TenantAccount {
@@ -449,6 +450,7 @@ model SmsDrainageInfo {
signature SmsSignature @relation(fields: [signatureId], references: [id], onDelete: Cascade)
application SmsApplication? @relation(fields: [applicationId], references: [id])
reportTasks ChannelSignatureReportTask[]
messageRecords SmsMessageRecord[]
@@index([tenantId, auditStatus, updatedAt])
@@index([signatureId, auditStatus])
@@ -716,6 +718,22 @@ model ChannelReportField {
@@index([drainageFieldId, reportType])
}
model CommonReportField {
id String @id @default(cuid())
drainageFieldId String
reportType String
required Boolean @default(false)
sortOrder Int @default(100)
status String @default("active")
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
drainageField DrainageField @relation(fields: [drainageFieldId], references: [id], onDelete: Restrict)
@@unique([drainageFieldId, reportType])
@@index([reportType, status, sortOrder])
}
model SignatureReportMaterial {
id String @id @default(cuid())
signatureId String
@@ -974,6 +992,7 @@ model SmsMessageRecord {
applicationId String?
templateId String?
signatureId String?
drainageInfoId String?
reviewTaskId String?
messageId String @unique
phoneNumber String
@@ -1004,6 +1023,7 @@ model SmsMessageRecord {
application SmsApplication? @relation(fields: [applicationId], references: [id])
template SmsTemplate? @relation(fields: [templateId], references: [id])
signature SmsSignature? @relation(fields: [signatureId], references: [id])
drainageInfo SmsDrainageInfo? @relation(fields: [drainageInfoId], references: [id])
reviewTask SmsSendTask? @relation(fields: [reviewTaskId], references: [id])
channel SmsChannel? @relation(fields: [channelId], references: [id])
submitRecords SmsSubmitRecord[]
@@ -1018,6 +1038,7 @@ model SmsMessageRecord {
@@index([reviewTaskId, status])
@@index([phoneNumber])
@@index([gatewayMessageId])
@@index([drainageInfoId, queuedAt])
}
model CmppSubmitSession {
@@ -1048,6 +1069,8 @@ model SmsSubmitRecord {
sequenceId Int?
gatewayMessageId String?
submitStatus String @default("queued")
costUnitPrice Int @default(0)
costAmountCents Int @default(0)
errorCode String?
errorMessage String?
submittedAt DateTime?
@@ -1066,6 +1089,78 @@ model SmsSubmitRecord {
@@index([gatewayMessageId])
}
model DailyReconciliationReport {
id String @id @default(cuid())
reportDate DateTime @db.Date
tenantId String
tenantName String
applicationId String
applicationName String
sentUnits Int @default(0)
successUnits Int @default(0)
generatedAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@unique([reportDate, tenantId, applicationId])
@@index([reportDate])
@@index([tenantId, reportDate])
@@index([applicationId, reportDate])
}
model DailyProfitReport {
id String @id @default(cuid())
reportDate DateTime @db.Date
dimensionType String
dimensionId String
dimensionName String
tenantId String?
tenantName String?
applicationId String?
channelId String?
sentUnits Int @default(0)
successUnits Int @default(0)
revenueCents Int @default(0)
costCents Int @default(0)
profitCents Int @default(0)
profitRateBps Int @default(0)
generatedAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@unique([reportDate, dimensionType, dimensionId])
@@index([dimensionType, reportDate])
@@index([tenantId, reportDate])
@@index([applicationId, reportDate])
@@index([channelId, reportDate])
}
model DailyQualityReport {
id String @id @default(cuid())
reportDate DateTime @db.Date
dimensionType String
dimensionId String
dimensionName String
tenantId String?
tenantName String?
applicationId String?
channelId String?
signatureId String?
drainageInfoId String?
sentUnits Int @default(0)
successUnits Int @default(0)
successRateBps Int @default(0)
avgArrivalMs Int?
generatedAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@unique([reportDate, dimensionType, dimensionId])
@@index([dimensionType, reportDate, sentUnits])
@@index([tenantId, reportDate])
@@index([applicationId, reportDate])
@@index([channelId, reportDate])
@@index([signatureId, reportDate])
@@index([drainageInfoId, reportDate])
}
model SmsMessageSegmentAudit {
id String @id @default(cuid())
tenantId String?