fix: 加固批量出库导入流程
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
-- AlterTable
|
||||
ALTER TABLE `outbound_import_session`
|
||||
ADD COLUMN `request_id` VARCHAR(191) NULL,
|
||||
ADD COLUMN `preview_job_id` VARCHAR(191) NULL,
|
||||
ADD COLUMN `import_job_id` VARCHAR(191) NULL,
|
||||
ADD COLUMN `import_run_token` VARCHAR(191) NULL,
|
||||
MODIFY `status` ENUM('PARSING', 'READY', 'IMPORTING', 'IMPORTED', 'PARTIAL', 'FAILED', 'EXPIRED') NOT NULL DEFAULT 'PARSING';
|
||||
|
||||
-- Drop obsolete row-level blockchain fields. Blockchain state belongs to trace_code.
|
||||
ALTER TABLE `outbound_import_row`
|
||||
DROP COLUMN `tx_hash`,
|
||||
DROP COLUMN `onchain_at`,
|
||||
DROP COLUMN `block_height`;
|
||||
|
||||
-- Backfill job links for sessions created before these columns existed.
|
||||
UPDATE `outbound_import_session` AS `session`
|
||||
SET `preview_job_id` = (
|
||||
SELECT `job`.`id`
|
||||
FROM `job`
|
||||
WHERE `job`.`type` = 'IMPORT_OUTBOUND_PREVIEW'
|
||||
AND JSON_UNQUOTE(JSON_EXTRACT(`job`.`payload`, '$.sessionId')) = `session`.`id`
|
||||
ORDER BY `job`.`created_at` DESC
|
||||
LIMIT 1
|
||||
)
|
||||
WHERE `session`.`preview_job_id` IS NULL;
|
||||
|
||||
UPDATE `outbound_import_session` AS `session`
|
||||
SET `import_job_id` = (
|
||||
SELECT `job`.`id`
|
||||
FROM `job`
|
||||
WHERE `job`.`type` = 'IMPORT_OUTBOUND'
|
||||
AND JSON_UNQUOTE(JSON_EXTRACT(`job`.`payload`, '$.sessionId')) = `session`.`id`
|
||||
ORDER BY `job`.`created_at` DESC
|
||||
LIMIT 1
|
||||
)
|
||||
WHERE `session`.`import_job_id` IS NULL
|
||||
AND `session`.`status` IN ('IMPORTING', 'IMPORTED', 'PARTIAL', 'FAILED');
|
||||
|
||||
-- Normalize terminal counts produced by legacy workers that left session totals stale.
|
||||
UPDATE `outbound_import_session` AS `session`
|
||||
JOIN `job` AS `job` ON `job`.`id` = `session`.`import_job_id`
|
||||
JOIN (
|
||||
SELECT
|
||||
`session_id`,
|
||||
SUM(`status` = 'IMPORTED') AS `success_count`,
|
||||
SUM(`status` = 'FAILED') AS `fail_count`,
|
||||
SUM(`status` = 'VALID') AS `remaining_count`
|
||||
FROM `outbound_import_row`
|
||||
GROUP BY `session_id`
|
||||
) AS `row_count` ON `row_count`.`session_id` = `session`.`id`
|
||||
SET
|
||||
`session`.`valid_count` = `row_count`.`success_count`,
|
||||
`session`.`invalid_count` = `row_count`.`fail_count`,
|
||||
`session`.`status` = CASE
|
||||
WHEN `row_count`.`success_count` > 0 AND `row_count`.`fail_count` > 0 THEN 'PARTIAL'
|
||||
WHEN `row_count`.`success_count` > 0 THEN 'IMPORTED'
|
||||
ELSE 'FAILED'
|
||||
END
|
||||
WHERE `job`.`status` IN ('SUCCESS', 'PARTIAL', 'FAILED')
|
||||
AND `session`.`status` IN ('IMPORTING', 'IMPORTED', 'PARTIAL', 'FAILED')
|
||||
AND `row_count`.`remaining_count` = 0;
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX `outbound_import_session_request_id_key` ON `outbound_import_session`(`request_id`);
|
||||
CREATE UNIQUE INDEX `outbound_import_session_preview_job_id_key` ON `outbound_import_session`(`preview_job_id`);
|
||||
CREATE UNIQUE INDEX `outbound_import_session_import_job_id_key` ON `outbound_import_session`(`import_job_id`);
|
||||
@@ -118,6 +118,7 @@ enum OutboundImportSessionStatus {
|
||||
READY
|
||||
IMPORTING
|
||||
IMPORTED
|
||||
PARTIAL
|
||||
FAILED
|
||||
EXPIRED
|
||||
}
|
||||
@@ -436,19 +437,23 @@ model OperationLog {
|
||||
}
|
||||
|
||||
model OutboundImportSession {
|
||||
id String @id @default(cuid())
|
||||
status OutboundImportSessionStatus @default(PARSING)
|
||||
fileName String @map("file_name")
|
||||
fileKey String? @map("file_key") @db.VarChar(512)
|
||||
total Int @default(0)
|
||||
validCount Int @default(0) @map("valid_count")
|
||||
invalidCount Int @default(0) @map("invalid_count")
|
||||
error String? @db.Text
|
||||
committedAt DateTime? @map("committed_at")
|
||||
expiresAt DateTime @map("expires_at")
|
||||
createdBy Int? @map("created_by")
|
||||
createdAt DateTime @default(now()) @map("created_at")
|
||||
updatedAt DateTime @updatedAt @map("updated_at")
|
||||
id String @id @default(cuid())
|
||||
status OutboundImportSessionStatus @default(PARSING)
|
||||
fileName String @map("file_name")
|
||||
fileKey String? @map("file_key") @db.VarChar(512)
|
||||
requestId String? @unique @map("request_id")
|
||||
previewJobId String? @unique @map("preview_job_id")
|
||||
importJobId String? @unique @map("import_job_id")
|
||||
importRunToken String? @map("import_run_token")
|
||||
total Int @default(0)
|
||||
validCount Int @default(0) @map("valid_count")
|
||||
invalidCount Int @default(0) @map("invalid_count")
|
||||
error String? @db.Text
|
||||
committedAt DateTime? @map("committed_at")
|
||||
expiresAt DateTime @map("expires_at")
|
||||
createdBy Int? @map("created_by")
|
||||
createdAt DateTime @default(now()) @map("created_at")
|
||||
updatedAt DateTime @updatedAt @map("updated_at")
|
||||
|
||||
creator SysUser? @relation(fields: [createdBy], references: [id])
|
||||
rows OutboundImportRow[]
|
||||
@@ -469,9 +474,6 @@ model OutboundImportRow {
|
||||
outTime DateTime? @map("out_time")
|
||||
outImagePaths Json? @map("out_image_paths")
|
||||
outImageKeys Json? @map("out_image_keys")
|
||||
txHash String? @map("tx_hash")
|
||||
onchainAt DateTime? @map("onchain_at")
|
||||
blockHeight BigInt? @map("block_height")
|
||||
remark String?
|
||||
reason String? @db.Text
|
||||
preview Json?
|
||||
|
||||
Reference in New Issue
Block a user