126 lines
7.1 KiB
SQL
126 lines
7.1 KiB
SQL
-- ============================================================
|
||
-- 渠道服务订单:多次操作流水模型迁移
|
||
-- Date: 2026-05-24
|
||
-- ============================================================
|
||
|
||
-- 1. 新增操作流水表
|
||
DROP TABLE IF EXISTS `biz_channel_service_order_txn`;
|
||
CREATE TABLE `biz_channel_service_order_txn` (
|
||
`id` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '主键ID',
|
||
`service_order_id` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '关联订单ID',
|
||
`txn_type` varchar(16) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT 'recharge/refund/purchase/unsubscribe',
|
||
`amount` decimal(18,2) DEFAULT NULL COMMENT '本次操作金额',
|
||
`txn_status` varchar(16) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'dsx' COMMENT 'dsx待生效 dqr待财务确认 yqr已确认 yzf已作废',
|
||
`products_json` text COLLATE utf8mb4_unicode_ci COMMENT '产品明细JSON(purchase/unsubscribe类型)',
|
||
`remark` varchar(512) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '备注',
|
||
`finance_submit_by` varchar(64) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '发起确认人ID',
|
||
`finance_submit_time` datetime DEFAULT NULL COMMENT '发起确认时间',
|
||
`finance_submit_remark` varchar(512) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '发起确认备注',
|
||
`finance_submit_attachments` text COLLATE utf8mb4_unicode_ci COMMENT '发起确认附件',
|
||
`finance_confirm_time` datetime DEFAULT NULL COMMENT '财务确认时间',
|
||
`finance_confirm_attachments` text COLLATE utf8mb4_unicode_ci COMMENT '财务确认附件',
|
||
`finance_remark` varchar(512) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '财务确认备注',
|
||
`balance_before` decimal(18,2) DEFAULT NULL COMMENT '操作前余额(yfk类型)',
|
||
`balance_after` decimal(18,2) DEFAULT NULL COMMENT '操作后余额(yfk类型)',
|
||
`tenant_no` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'zkxw' COMMENT '租户编号',
|
||
`order_num` int(11) DEFAULT NULL COMMENT '排序号',
|
||
`create_by` varchar(64) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '创建人id',
|
||
`create_dept_id` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '创建人部门ID',
|
||
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
|
||
`update_by` varchar(64) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '更新人id',
|
||
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
|
||
`del_flag` tinyint(4) NOT NULL DEFAULT '0' COMMENT '删除标记',
|
||
PRIMARY KEY (`id`),
|
||
KEY `idx_txn_order` (`service_order_id`, `del_flag`),
|
||
KEY `idx_txn_status` (`txn_status`, `tenant_no`, `del_flag`)
|
||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='渠道服务订单操作流水';
|
||
|
||
-- 2. 订单主表新增汇总字段
|
||
ALTER TABLE `biz_channel_service_order`
|
||
ADD COLUMN `total_recharge_amount` decimal(18,2) NOT NULL DEFAULT 0.00 COMMENT '累计充值总额' AFTER `channel_agreement_id`,
|
||
ADD COLUMN `total_refund_amount` decimal(18,2) NOT NULL DEFAULT 0.00 COMMENT '累计退款总额' AFTER `total_recharge_amount`,
|
||
ADD COLUMN `current_balance` decimal(18,2) NOT NULL DEFAULT 0.00 COMMENT '当前余额' AFTER `total_refund_amount`;
|
||
|
||
-- 3. 产品持仓表新增字段
|
||
ALTER TABLE `biz_channel_service_order_product`
|
||
ADD COLUMN `total_buy_qty` decimal(18,4) NOT NULL DEFAULT 0.0000 COMMENT '累计购买数量' AFTER `buy_amount`,
|
||
ADD COLUMN `total_unsubscribe_qty` decimal(18,4) NOT NULL DEFAULT 0.0000 COMMENT '累计退订数量' AFTER `total_buy_qty`,
|
||
ADD COLUMN `current_qty` decimal(18,4) NOT NULL DEFAULT 0.0000 COMMENT '当前持有数量' AFTER `total_unsubscribe_qty`,
|
||
ADD COLUMN `total_unsubscribe_amount` decimal(18,2) NOT NULL DEFAULT 0.00 COMMENT '累计退订金额' AFTER `current_qty`;
|
||
|
||
-- ============================================================
|
||
-- 以下为数据迁移(可选执行,用于保留历史数据)
|
||
-- ============================================================
|
||
|
||
-- 4. 迁移现有充值记录到流水表
|
||
INSERT INTO `biz_channel_service_order_txn` (
|
||
`id`, `service_order_id`, `txn_type`, `amount`, `txn_status`,
|
||
`remark`,
|
||
`finance_submit_by`, `finance_submit_time`, `finance_submit_remark`, `finance_submit_attachments`,
|
||
`finance_confirm_time`, `finance_confirm_attachments`, `finance_remark`,
|
||
`balance_before`, `balance_after`,
|
||
`tenant_no`, `create_by`, `create_dept_id`, `create_time`, `update_by`, `update_time`, `del_flag`
|
||
)
|
||
SELECT
|
||
REPLACE(UUID(),'-','') AS `id`,
|
||
o.`id` AS `service_order_id`,
|
||
'recharge' AS `txn_type`,
|
||
o.`prepay_amount` AS `amount`,
|
||
CASE
|
||
WHEN o.`finance_confirm_time` IS NOT NULL THEN 'yqr'
|
||
WHEN o.`finance_submit_time` IS NOT NULL THEN 'dqr'
|
||
ELSE 'dsx'
|
||
END AS `txn_status`,
|
||
o.`remark`,
|
||
o.`finance_submit_by`, o.`finance_submit_time`, o.`finance_submit_remark`, o.`finance_submit_attachments`,
|
||
o.`finance_confirm_time`, o.`finance_confirm_attachments`, o.`finance_remark`,
|
||
o.`balance_before`, o.`balance_after`,
|
||
o.`tenant_no`, o.`create_by`, o.`create_dept_id`, o.`create_time`, o.`update_by`, o.`update_time`, o.`del_flag`
|
||
FROM `biz_channel_service_order` o
|
||
WHERE o.`order_type` = 'yfk' AND (o.`finance_submit_time` IS NOT NULL OR o.`prepay_amount` IS NOT NULL);
|
||
|
||
-- 5. 迁移退款记录到流水表
|
||
INSERT INTO `biz_channel_service_order_txn` (
|
||
`id`, `service_order_id`, `txn_type`, `amount`, `txn_status`,
|
||
`remark`,
|
||
`finance_submit_by`, `finance_submit_time`, `finance_submit_remark`, `finance_submit_attachments`,
|
||
`finance_confirm_time`, `finance_confirm_attachments`, `finance_remark`,
|
||
`tenant_no`, `create_by`, `create_dept_id`, `create_time`, `update_by`, `update_time`, `del_flag`
|
||
)
|
||
SELECT
|
||
REPLACE(UUID(),'-','') AS `id`,
|
||
o.`id` AS `service_order_id`,
|
||
'refund' AS `txn_type`,
|
||
o.`refund_amount` AS `amount`,
|
||
CASE
|
||
WHEN o.`refund_finance_confirm_time` IS NOT NULL THEN 'yqr'
|
||
ELSE 'dqr'
|
||
END AS `txn_status`,
|
||
o.`refund_remark` AS `remark`,
|
||
o.`refund_apply_by` AS `finance_submit_by`,
|
||
o.`refund_apply_time` AS `finance_submit_time`,
|
||
o.`refund_remark` AS `finance_submit_remark`,
|
||
o.`refund_attachments` AS `finance_submit_attachments`,
|
||
o.`refund_finance_confirm_time` AS `finance_confirm_time`,
|
||
o.`refund_finance_confirm_attachments` AS `finance_confirm_attachments`,
|
||
o.`refund_finance_remark` AS `finance_remark`,
|
||
o.`tenant_no`, o.`refund_apply_by` AS `create_by`, o.`create_dept_id`,
|
||
COALESCE(o.`refund_apply_time`, o.`update_time`) AS `create_time`,
|
||
o.`update_by`, o.`update_time`, o.`del_flag`
|
||
FROM `biz_channel_service_order` o
|
||
WHERE o.`refund_amount` IS NOT NULL AND o.`refund_amount` > 0;
|
||
|
||
-- 6. 回填主表汇总字段(yfk类型)
|
||
UPDATE `biz_channel_service_order` o
|
||
SET
|
||
`total_recharge_amount` = COALESCE(o.`prepay_amount`, 0),
|
||
`total_refund_amount` = COALESCE(o.`refund_amount`, 0),
|
||
`current_balance` = COALESCE(o.`prepay_amount`, 0) - COALESCE(o.`refund_amount`, 0)
|
||
WHERE o.`order_type` = 'yfk' AND o.`prepay_amount` IS NOT NULL;
|
||
|
||
-- 7. 回填产品持仓表的累计字段
|
||
UPDATE `biz_channel_service_order_product` p
|
||
SET
|
||
`total_buy_qty` = COALESCE(p.`buy_qty`, 0),
|
||
`current_qty` = COALESCE(p.`buy_qty`, 0);
|