admin/sql/20260728_order_file.sql
zzh 03f11a276f Add order attachment files table and move system labels off Order.
Persist generated/printed/downstream labels in t_order_file, drop Order.LabelUrl generate-status fields, and expose file list/upload/delete APIs with customer read-only systemLabel access.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-28 16:32:55 +08:00

51 lines
1.8 KiB
SQL

-- Order files table + migrate LabelUrl into systemLabel, then drop legacy columns.
-- Run on MySQL. Backup first.
CREATE TABLE IF NOT EXISTS `t_order_file` (
`Id` INT NOT NULL AUTO_INCREMENT,
`OrderId` INT NOT NULL COMMENT 'Order Id',
`FileType` VARCHAR(32) NOT NULL COMMENT 'tradeProof/systemLabel/downstreamLabel/lastMileLabel/infraredImage',
`FileName` VARCHAR(255) NULL COMMENT 'Original file name',
`FileUrl` VARCHAR(512) NOT NULL COMMENT 'Relative or absolute URL',
`ContentType` VARCHAR(128) NULL,
`FileSize` BIGINT NULL,
`Sort` INT NOT NULL DEFAULT 0,
`Source` VARCHAR(32) NOT NULL DEFAULT 'upload' COMMENT 'upload/systemGenerate/downstream/migrate',
`Remark` VARCHAR(512) NULL,
`IsDeleted` TINYINT NOT NULL DEFAULT 0,
`ConcurrencyStamp` CHAR(36) NOT NULL DEFAULT (UUID()),
`CreateUserId` INT NOT NULL DEFAULT 0,
`CreateTime` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`UpdateUserId` INT NULL,
`UpdateTime` DATETIME NULL,
PRIMARY KEY (`Id`),
KEY `idx_ord_file_order_type` (`OrderId`, `FileType`, `IsDeleted`)
) COMMENT='Order attachment files';
-- Migrate existing system label URL (ignore if column already dropped)
INSERT INTO `t_order_file` (`OrderId`, `FileType`, `FileName`, `FileUrl`, `ContentType`, `Sort`, `Source`, `CreateUserId`, `CreateTime`)
SELECT
`Id`,
'systemLabel',
SUBSTRING_INDEX(`LabelUrl`, '/', -1),
`LabelUrl`,
'application/pdf',
0,
'migrate',
0,
NOW()
FROM `t_order`
WHERE `LabelUrl` IS NOT NULL
AND TRIM(`LabelUrl`) <> ''
AND NOT EXISTS (
SELECT 1 FROM `t_order_file` f
WHERE f.`OrderId` = `t_order`.`Id`
AND f.`FileType` = 'systemLabel'
AND f.`IsDeleted` = 0
);
ALTER TABLE `t_order`
DROP COLUMN `LabelUrl`,
DROP COLUMN `LabelGenerateStatus`,
DROP COLUMN `LabelGenerateError`;