Files
BMS/bms-back/sql/price/prc_order.sql
T
2026-10-16 17:46:50 +08:00

285 lines
13 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- ------------------------------------------------------------
-- REVIEW DRAFT: prc_order + rename ext -> prc_order_ext
-- Confirm schema before app code changes.
--
-- Target model:
-- prc_billing_task --transfer_no--> TAB_ORDER
-- -> upsert prc_order (UK transfer_no), delete task by transfer_no
-- -> prc_order 1:1 prc_order_ext (PK/FK = order_id)
-- -> prc_order_bill keeps bill_id; adds order_id + transfer_no
--
-- Naming:
-- order_id = BMS UUID PK (same rule as bill_id)
-- old prc_order_bill_ext renamed to prc_order_ext
-- columns moved to prc_order are DROPped from prc_order_ext
-- ------------------------------------------------------------
SET NAMES utf8mb4;
-- ============================================================
-- 1) New table: prc_order
-- ============================================================
-- TAB_ORDER mapping (core):
-- ORDER_ID -> order_no
-- ORG_ORDER_ID -> tracking_no
-- TRANSFER_ORDER_ID -> transfer_no
-- TRACK_ID -> track_id
-- CUSTOMER_CODE/NAME -> customer_code/name
-- PRODUCT_CODE/NAME -> product_code/name
-- PRODUCT_FLOOR_CODE -> product_floor_code
-- COUNTRY / COUNTRY_NAME -> country_code / country_name
-- CONSIGNEE_POSTCODE -> consignee_postcode
-- CONSIGNEE_NAME -> consignee_name
-- SHIPPER_NAME -> shipper_name
-- REGISTER_SITE_CODE -> warehouse_code
-- ORDER_PIECE/PACKAGE_NUM -> pieces
-- ACTUAL_WEIGHT -> actual_weight
-- VOLUME -> volume
-- VOLUME_WEIGHT -> volume_weight
-- LENGTH/WIDTH/HEIGHT -> pkg_length/width/height
-- DIMENSIONAL_WEIGHT_FACTOR -> dimensional_weight_factor
-- WEIGHT -> weight
-- BELONG_SALER* -> belong_saler / belong_saler_code
-- SETTLEMENT_MAN* -> settlement_man / settlement_man_code
-- SERVICE_MAN* -> service_man / service_man_code
-- TRACK_REMARK/... -> order_remark
-- ORDER_STATUS -> order_status
-- BL_PICKUP -> bl_pickup
-- PICKUP_DATE -> pickup_date
-- CREATE_DATE -> order_time
-- (task.bill_time) -> biz_date
-- ============================================================
CREATE TABLE IF NOT EXISTS `prc_order` (
`order_id` varchar(36) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '订单ID(BMS主键,规则同 bill_id:UUID)',
`order_no` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '客户订单号(TAB_ORDER.ORDER_ID)',
`tracking_no` varchar(64) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '运单号(TAB_ORDER.ORG_ORDER_ID)',
`transfer_no` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '转单号(TAB_ORDER.TRANSFER_ORDER_ID,唯一)',
`track_id` varchar(64) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '轨迹ID(TAB_ORDER.TRACK_ID)',
`belong_saler` varchar(64) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '所属销售员(TAB_ORDER.BELONG_SALER*)',
`belong_saler_code` varchar(64) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '销售员编码(TAB_ORDER.BELONG_SALER_CODE)',
`settlement_man` varchar(64) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '结算员(TAB_ORDER.SETTLEMENT_MAN*)',
`settlement_man_code` varchar(64) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '结算员编码(TAB_ORDER.SETTLEMENT_MAN_CODE)',
`service_man` varchar(64) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '客服员(TAB_ORDER.SERVICE_MAN*)',
`service_man_code` varchar(64) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '客服员编码',
`customer_code` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '客户代码(TAB_ORDER.CUSTOMER_CODE)',
`customer_name` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '客户名称(TAB_ORDER.CUSTOMER_NAME)',
`pieces` int(11) DEFAULT NULL COMMENT '件数(TAB_ORDER.ORDER_PIECE/PACKAGE_NUM)',
`actual_weight` decimal(10,3) DEFAULT NULL COMMENT '实际重量KG(TAB_ORDER.ACTUAL_WEIGHT)',
`weight` decimal(10,3) DEFAULT NULL COMMENT '实重/收货重KG(TAB_ORDER.WEIGHT)',
`dimensional_weight_factor` decimal(10,3) DEFAULT NULL COMMENT '计费重KG(TAB_ORDER.DIMENSIONAL_WEIGHT_FACTOR)',
`volume` decimal(12,4) DEFAULT NULL COMMENT '体积m3(TAB_ORDER.VOLUME)',
`volume_weight` decimal(10,3) DEFAULT NULL COMMENT '体积重KG(TAB_ORDER.VOLUME_WEIGHT)',
`pkg_length` decimal(10,2) DEFAULT NULL COMMENT '长CM(TAB_ORDER.LENGTH)',
`pkg_width` decimal(10,2) DEFAULT NULL COMMENT '宽CM(TAB_ORDER.WIDTH)',
`pkg_height` decimal(10,2) DEFAULT NULL COMMENT '高CM(TAB_ORDER.HEIGHT)',
`shipper_name` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '发件人(TAB_ORDER.SHIPPER_NAME)',
`consignee_name` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '收件人(TAB_ORDER.CONSIGNEE_NAME)',
`consignee_postcode` varchar(20) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '收件人邮编(TAB_ORDER.CONSIGNEE_POSTCODE)',
`warehouse_code` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '仓库(TAB_ORDER.REGISTER_SITE_CODE)',
`order_time` datetime DEFAULT NULL COMMENT '下单时间(TAB_ORDER.CREATE_DATE)',
`order_remark` varchar(500) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '订单备注(TAB_ORDER.TRACK_REMARK/等)',
`product_code` varchar(20) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '产品代码(TAB_ORDER.PRODUCT_CODE)',
`product_name` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '产品名称(TAB_ORDER.PRODUCT_NAME)',
`product_floor_code` text COLLATE utf8mb4_unicode_ci COMMENT '产品底级/渠道编码(TAB_ORDER.PRODUCT_FLOOR_CODE)',
`country_code` char(2) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '国家二字码(TAB_ORDER.COUNTRY)',
`country_name` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '国家名称(TAB_ORDER.COUNTRY_NAME)',
`order_status` tinyint(4) DEFAULT NULL COMMENT '订单状态(TAB_ORDER.ORDER_STATUS)',
`bl_pickup` tinyint(4) DEFAULT NULL COMMENT '是否收货(0未收货 1已收货,TAB_ORDER.BL_PICKUP)',
`pickup_date` datetime DEFAULT NULL COMMENT '提货/收件时间(TAB_ORDER.PICKUP_DATE)',
`biz_date` datetime DEFAULT NULL COMMENT '计费时间(任务bill_time,非上游字段)',
`success_status` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'WAITING' COMMENT '计费成功状态:WAITING待处理 SUCCESS成功 FAILED失败',
`fail_code` varchar(64) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '计费失败码(同 prc_billing_task.fail_code / BillTaskFailCode)',
`created_by` varchar(64) COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '创建人',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
`updated_by` varchar(64) COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '更新人',
`update_time` datetime DEFAULT NULL COMMENT '更新时间',
PRIMARY KEY (`order_id`),
UNIQUE KEY `uk_prc_order_transfer_no` (`transfer_no`),
KEY `idx_prc_order_order_no` (`order_no`),
KEY `idx_prc_order_tracking_no` (`tracking_no`),
KEY `idx_prc_order_track_id` (`track_id`),
KEY `idx_prc_order_customer` (`customer_code`,`biz_date`),
KEY `idx_prc_order_pickup_date` (`pickup_date`),
KEY `idx_prc_order_success_status` (`success_status`,`create_time`),
KEY `idx_prc_order_fail_code` (`fail_code`),
KEY `idx_prc_order_product` (`product_code`,`country_code`),
KEY `idx_prc_order_bl_pickup` (`bl_pickup`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='订单主表(上游TAB_ORDER核心字段+计费状态)';
-- ============================================================
-- 2) prc_order_bill: add order_id (keep bill_id; keep transfer_no)
-- ============================================================
SET @col := (
SELECT COLUMN_NAME FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'prc_order_bill'
AND COLUMN_NAME = 'order_id'
LIMIT 1
);
SET @sql := IF(
@col IS NULL,
'ALTER TABLE prc_order_bill ADD COLUMN order_id varchar(36) DEFAULT NULL COMMENT \'订单ID,对应 prc_order.order_id\' AFTER bill_id, ADD KEY idx_order_bill_order_id (order_id)',
'SELECT 1'
);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
-- Optional FK (enable after data backfill):
-- ALTER TABLE prc_order_bill
-- ADD CONSTRAINT fk_order_bill_order
-- FOREIGN KEY (order_id) REFERENCES prc_order(order_id);
-- ============================================================
-- 3) prc_order_bill_ext -> prc_order_ext, rebind to prc_order
-- WARNING: prefer empty/dev env; production needs data migration.
-- ============================================================
-- 3.1 Drop old FK to prc_order_bill (table may still be old name)
SET @fk := (
SELECT CONSTRAINT_NAME FROM information_schema.TABLE_CONSTRAINTS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'prc_order_bill_ext'
AND CONSTRAINT_TYPE = 'FOREIGN KEY'
AND CONSTRAINT_NAME = 'fk_order_bill_ext_bill'
LIMIT 1
);
SET @sql := IF(
@fk IS NOT NULL,
'ALTER TABLE prc_order_bill_ext DROP FOREIGN KEY fk_order_bill_ext_bill',
'SELECT 1'
);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
-- 3.2 Rename table bill_ext -> order_ext
SET @tbl := (
SELECT TABLE_NAME FROM information_schema.TABLES
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'prc_order_bill_ext'
LIMIT 1
);
SET @sql := IF(
@tbl IS NOT NULL,
'RENAME TABLE prc_order_bill_ext TO prc_order_ext',
'SELECT 1'
);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
-- 3.3 Rename PK column bill_id -> order_id
SET @col := (
SELECT COLUMN_NAME FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'prc_order_ext'
AND COLUMN_NAME = 'bill_id'
LIMIT 1
);
SET @sql := IF(
@col IS NOT NULL,
'ALTER TABLE prc_order_ext CHANGE COLUMN bill_id order_id varchar(36) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT \'订单ID,对应 prc_order.order_id\'',
'SELECT 1'
);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
-- 3.4 Drop columns moved onto prc_order
SET @col := (
SELECT COLUMN_NAME FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'prc_order_ext'
AND COLUMN_NAME = 'product_floor_code'
LIMIT 1
);
SET @sql := IF(
@col IS NOT NULL,
'ALTER TABLE prc_order_ext DROP COLUMN `product_floor_code`',
'SELECT 1'
);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
SET @col := (
SELECT COLUMN_NAME FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'prc_order_ext'
AND COLUMN_NAME = 'order_status'
LIMIT 1
);
SET @sql := IF(
@col IS NOT NULL,
'ALTER TABLE prc_order_ext DROP COLUMN `order_status`',
'SELECT 1'
);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
SET @col := (
SELECT COLUMN_NAME FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'prc_order_ext'
AND COLUMN_NAME = 'pickup_date'
LIMIT 1
);
SET @sql := IF(
@col IS NOT NULL,
'ALTER TABLE prc_order_ext DROP COLUMN `pickup_date`',
'SELECT 1'
);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
SET @col := (
SELECT COLUMN_NAME FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'prc_order_ext'
AND COLUMN_NAME = 'dimensional_weight_factor'
LIMIT 1
);
SET @sql := IF(
@col IS NOT NULL,
'ALTER TABLE prc_order_ext DROP COLUMN `dimensional_weight_factor`',
'SELECT 1'
);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
SET @col := (
SELECT COLUMN_NAME FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'prc_order_ext'
AND COLUMN_NAME = 'bl_pickup'
LIMIT 1
);
SET @sql := IF(
@col IS NOT NULL,
'ALTER TABLE prc_order_ext DROP COLUMN bl_pickup',
'SELECT 1'
);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
-- 3.5 MUST empty ext before FK (old order_id values are former bill_id)
-- Run these 3 statements even if the rest of the script already succeeded.
TRUNCATE TABLE prc_order_ext;
-- 3.6 Add FK to prc_order (skip if already exists)
SET @fk := (
SELECT CONSTRAINT_NAME FROM information_schema.TABLE_CONSTRAINTS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'prc_order_ext'
AND CONSTRAINT_TYPE = 'FOREIGN KEY'
AND CONSTRAINT_NAME = 'fk_order_ext_order'
LIMIT 1
);
SET @sql := IF(
@fk IS NULL,
'ALTER TABLE prc_order_ext ADD CONSTRAINT fk_order_ext_order FOREIGN KEY (order_id) REFERENCES prc_order (order_id) ON DELETE CASCADE',
'SELECT 1'
);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
-- 3.7 Refresh table comment
ALTER TABLE prc_order_ext COMMENT = '订单扩展表(上游TAB_ORDER非主表字段,挂 prc_order)';
-- ============================================================
-- Review checklist
-- ------------------------------------------------------------
-- prc_order: identity + L/W/H + weights + staff + billing status
-- prc_order_ext: leftover TAB_ORDER only (no dup of moved cols)
-- prc_order_bill: pricing result; +order_id; still has bill_id
-- Dropped from ext (now on prc_order):
-- product_floor_code, order_status, pickup_date, dimensional_weight_factor
-- ============================================================