-- =====================================================
-- 商户结算单构成明细 + 应收流水占用字段（MVP）
-- 模块：business-finance / merchantsettlement、merchantreceivable
-- 版本：1.1
-- 创建时间：2026-06-29
-- 号段：明细主键 1926061480000000201 起；权限按钮 1926061480000000201
-- 说明：已有库执行本脚本；新环境 merchant_settlement.sql / merchant_receivable.sql 已含同结构时可跳过 ALTER
-- =====================================================

-- 应收流水：结算单占用标记
ALTER TABLE `biz_merchant_receivable`
    ADD COLUMN `settlement_bill_id` VARCHAR(64) DEFAULT NULL COMMENT '已纳入的结算单ID（非作废单占用）' AFTER `biz_time`,
    ADD COLUMN `settlement_bill_no` VARCHAR(64) DEFAULT NULL COMMENT '结算单号（冗余）' AFTER `settlement_bill_id`,
    ADD KEY `idx_settlement_bill_id` (`settlement_bill_id`);

CREATE TABLE IF NOT EXISTS `biz_merchant_settlement_bill_line` (
    `id` VARCHAR(64) NOT NULL COMMENT '主键ID',

    `settlement_bill_id` VARCHAR(64) NOT NULL COMMENT '结算单ID',
    `settlement_no` VARCHAR(64) NOT NULL COMMENT '结算单号（冗余）',
    `receivable_id` VARCHAR(64) NOT NULL COMMENT '应收流水ID',
    `receivable_no` VARCHAR(64) NOT NULL COMMENT '应收流水号（冗余）',

    `entry_type` VARCHAR(32) NOT NULL COMMENT '流水类型：PAY、REFUND',
    `biz_scene` VARCHAR(32) NOT NULL COMMENT '业务场景：MALL_ORDER 等',
    `biz_order_id` VARCHAR(64) DEFAULT NULL COMMENT '业务子单ID（对应 receivable.mall_order_id）',
    `biz_order_no` VARCHAR(64) DEFAULT NULL COMMENT '业务单号快照',
    `pay_batch_id` VARCHAR(64) DEFAULT NULL COMMENT '合并支付批次ID',
    `finance_pay_order_id` VARCHAR(64) DEFAULT NULL COMMENT '财务支付单ID',

    `base_amount` DECIMAL(12,2) NOT NULL DEFAULT 0.00 COMMENT '基数金额（元，快照）',
    `commission_amount` DECIMAL(12,2) NOT NULL DEFAULT 0.00 COMMENT '平台佣金（元，快照）',
    `merchant_amount` DECIMAL(12,2) NOT NULL DEFAULT 0.00 COMMENT '商户应得（元，快照）',
    `biz_time` DATETIME DEFAULT NULL COMMENT '业务发生时间（快照）',

    `create_by` VARCHAR(64) DEFAULT NULL COMMENT '创建者',
    `create_date` DATETIME DEFAULT NULL COMMENT '创建时间',
    `update_by` VARCHAR(64) DEFAULT NULL COMMENT '更新者',
    `update_date` DATETIME DEFAULT NULL COMMENT '更新时间',
    `del_flag` CHAR(1) DEFAULT '0' COMMENT '删除标记：0-正常 1-删除',
    `remarks` VARCHAR(255) DEFAULT NULL COMMENT '备注信息',
    `tenant_code` VARCHAR(64) DEFAULT NULL COMMENT '租户编码',
    `tenant_id` VARCHAR(64) DEFAULT NULL COMMENT '租户ID',

    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_receivable_bill` (`receivable_id`, `settlement_bill_id`, `del_flag`),
    KEY `idx_settlement_bill_id` (`settlement_bill_id`),
    KEY `idx_biz_order_id` (`biz_order_id`),
    KEY `idx_biz_time` (`biz_time`),
    KEY `idx_tenant_id` (`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='商户结算单构成明细（应收流水快照）';

-- 构成明细分页（与 info 同权，挂平台结算单菜单）
INSERT INTO sys_menu_button_info (
    id, create_date, create_by, update_date, update_by, del_flag, remarks,
    tenant_id, tenant_code, organize_id, button_code, button_name, enable, menu_id, sort_num, sys_data, source
) VALUES
('1926061480000000201', NOW(), '1', NOW(), '1', '0', 'merchant_settlement_bill_line', '1', '1', NULL,
 'manager:business:finance:merchantSettlementBill:lineList', '构成明细', '1', '1926061470000000101', 55, '0', '0'),
('1926061480000000202', NOW(), '1', NOW(), '1', '0', 'merchant_settlement_bill_line', '1', '1', NULL,
 'manager:business:finance:merchantSettlementBill:lineList', '构成明细', '1', '1926061470000000107', 35, '0', '0');
