-- =====================================================
-- 会员积分规则体系
-- 模块：business-member
-- 版本：1.0
-- 创建时间：2026-06-23
-- 说明：积分场景规则、等级差异明细、发放幂等记录
-- 号段：规则 1926062310000000101~0105；等级明细 1926062310000000201~0213
-- =====================================================

-- ----
-- 1. 积分场景规则主表
-- ----
CREATE TABLE IF NOT EXISTS `app_member_points_rule` (
    `id` VARCHAR(64) NOT NULL COMMENT '主键ID',
    `scene_code` VARCHAR(32) NOT NULL COMMENT '场景编码：REGISTER/SIGN_IN/ORDER_CONSUME/BIRTHDAY/UPGRADE/INVITE/ACTIVITY/MANUAL',
    `rule_name` VARCHAR(128) NOT NULL COMMENT '规则名称',
    `calc_type` VARCHAR(32) NOT NULL COMMENT '计算方式：FIXED_BY_LEVEL/FIXED_PLUS_LEVEL_RATE/RATE_ON_AMOUNT/CUSTOM',
    `base_points` INT DEFAULT 0 COMMENT '基础固定分（FIXED_PLUS_LEVEL_RATE 等）',
    `amount_rate` DECIMAL(10,4) DEFAULT 0.0000 COMMENT '按金额计分：每元积分（RATE_ON_AMOUNT）',
    `apply_level_rate` TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否乘以会员等级 points_rate：0-否 1-是',
    `valid_days` INT DEFAULT 0 COMMENT '积分有效天数（0=永久）',
    `daily_limit` INT DEFAULT 0 COMMENT '每人每日上限次数（0=不限）',
    `total_limit` INT DEFAULT 0 COMMENT '每人累计上限次数（0=不限；注册建议=1）',
    `status` TINYINT NOT NULL DEFAULT 1 COMMENT '状态：0-禁用 1-启用',
    `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_scene_code` (`scene_code`, `tenant_id`),
    KEY `idx_status` (`status`),
    KEY `idx_tenant_id` (`tenant_id`),
    KEY `idx_create_date` (`create_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='会员积分场景规则表';

-- ----
-- 2. 积分规则等级明细（场景×等级）
-- ----
CREATE TABLE IF NOT EXISTS `app_member_points_rule_level` (
    `id` VARCHAR(64) NOT NULL COMMENT '主键ID',
    `rule_id` VARCHAR(64) NOT NULL COMMENT '规则ID',
    `level_id` VARCHAR(64) NOT NULL COMMENT '会员等级ID（app_member_level.id）',
    `level_code` VARCHAR(32) DEFAULT NULL COMMENT '等级编码（冗余）',
    `points_value` INT NOT NULL DEFAULT 0 COMMENT '该等级固定赠送积分',
    `extra_multiplier` DECIMAL(5,2) DEFAULT 1.00 COMMENT '额外倍率（在基础规则上乘算）',
    `status` TINYINT NOT NULL DEFAULT 1 COMMENT '状态：0-禁用 1-启用',
    `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_rule_level` (`rule_id`, `level_id`, `tenant_id`),
    KEY `idx_rule_id` (`rule_id`),
    KEY `idx_level_id` (`level_id`),
    KEY `idx_tenant_id` (`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='会员积分规则等级明细表';

-- ----
-- 3. 积分发放幂等记录
-- ----
CREATE TABLE IF NOT EXISTS `app_member_points_grant_record` (
    `id` VARCHAR(64) NOT NULL COMMENT '主键ID',
    `member_id` VARCHAR(64) NOT NULL COMMENT '会员ID',
    `scene_code` VARCHAR(32) NOT NULL COMMENT '场景编码',
    `biz_id` VARCHAR(64) NOT NULL COMMENT '业务幂等键',
    `biz_no` VARCHAR(64) DEFAULT NULL COMMENT '业务编号',
    `biz_desc` VARCHAR(500) DEFAULT NULL COMMENT '业务描述',
    `rule_id` VARCHAR(64) DEFAULT NULL COMMENT '命中规则ID',
    `rule_name` VARCHAR(128) DEFAULT NULL COMMENT '命中规则名称',
    `points` INT NOT NULL DEFAULT 0 COMMENT '实际发放积分',
    `points_log_id` VARCHAR(64) DEFAULT NULL COMMENT '积分流水ID',
    `revoked` TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否已冲正：0-否 1-是',
    `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_member_scene_biz` (`member_id`, `scene_code`, `biz_id`, `tenant_id`),
    KEY `idx_member_id` (`member_id`),
    KEY `idx_scene_code` (`scene_code`),
    KEY `idx_create_date` (`create_date`),
    KEY `idx_tenant_id` (`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='会员积分发放幂等记录表';

-- ----
-- 4. 默认规则种子（可重复执行：先按 remarks 清理）
-- ----
DELETE FROM `app_member_points_rule_level` WHERE `remarks` = 'm11_member_points_rule_seed';
DELETE FROM `app_member_points_rule` WHERE `remarks` = 'm11_member_points_rule_seed';

INSERT INTO `app_member_points_rule` (`id`, `scene_code`, `rule_name`, `calc_type`, `base_points`, `amount_rate`, `apply_level_rate`, `valid_days`, `daily_limit`, `total_limit`, `status`, `create_date`, `remarks`) VALUES
('1926062310000000101', 'REGISTER', '新会员注册赠送', 'FIXED_BY_LEVEL', 0, 0, 0, 365, 0, 1, 1, NOW(), 'm11_member_points_rule_seed'),
('1926062310000000102', 'SIGN_IN', '每日签到赠送', 'CUSTOM', 10, 0, 1, 365, 1, 0, 1, NOW(), 'm11_member_points_rule_seed'),
('1926062310000000103', 'ORDER_CONSUME', '订单消费赠送', 'RATE_ON_AMOUNT', 0, 1.0000, 1, 365, 0, 0, 1, NOW(), 'm11_member_points_rule_seed'),
('1926062310000000104', 'UPGRADE', '会员升级赠送', 'FIXED_BY_LEVEL', 0, 0, 0, 365, 0, 0, 1, NOW(), 'm11_member_points_rule_seed'),
('1926062310000000105', 'BIRTHDAY', '生日赠送', 'FIXED_BY_LEVEL', 0, 0, 0, 365, 0, 1, 1, NOW(), 'm11_member_points_rule_seed');

INSERT INTO `app_member_points_rule_level` (`id`, `rule_id`, `level_id`, `level_code`, `points_value`, `extra_multiplier`, `status`, `create_date`, `remarks`) VALUES
('1926062310000000201', '1926062310000000101', '1', 'NORMAL', 100, 1.00, 1, NOW(), 'm11_member_points_rule_seed'),
('1926062310000000202', '1926062310000000101', '2', 'SILVER', 150, 1.00, 1, NOW(), 'm11_member_points_rule_seed'),
('1926062310000000203', '1926062310000000101', '3', 'GOLD', 300, 1.00, 1, NOW(), 'm11_member_points_rule_seed'),
('1926062310000000204', '1926062310000000101', '4', 'PLATINUM', 500, 1.00, 1, NOW(), 'm11_member_points_rule_seed'),
('1926062310000000205', '1926062310000000101', '5', 'DIAMOND', 800, 1.00, 1, NOW(), 'm11_member_points_rule_seed'),
('1926062310000000206', '1926062310000000101', '6', 'VIP', 200, 1.00, 1, NOW(), 'm11_member_points_rule_seed'),
('1926062310000000207', '1926062310000000101', '7', 'SVIP', 300, 1.00, 1, NOW(), 'm11_member_points_rule_seed'),
('1926062310000000301', '1926062310000000104', '1', 'NORMAL', 0, 1.00, 1, NOW(), 'm11_member_points_rule_seed'),
('1926062310000000302', '1926062310000000104', '2', 'SILVER', 50, 1.00, 1, NOW(), 'm11_member_points_rule_seed'),
('1926062310000000303', '1926062310000000104', '3', 'GOLD', 100, 1.00, 1, NOW(), 'm11_member_points_rule_seed'),
('1926062310000000304', '1926062310000000104', '4', 'PLATINUM', 200, 1.00, 1, NOW(), 'm11_member_points_rule_seed'),
('1926062310000000305', '1926062310000000104', '5', 'DIAMOND', 500, 1.00, 1, NOW(), 'm11_member_points_rule_seed'),
('1926062310000000401', '1926062310000000105', '1', 'NORMAL', 0, 1.00, 1, NOW(), 'm11_member_points_rule_seed'),
('1926062310000000402', '1926062310000000105', '2', 'SILVER', 100, 1.00, 1, NOW(), 'm11_member_points_rule_seed'),
('1926062310000000403', '1926062310000000105', '3', 'GOLD', 200, 1.00, 1, NOW(), 'm11_member_points_rule_seed'),
('1926062310000000404', '1926062310000000105', '4', 'PLATINUM', 500, 1.00, 1, NOW(), 'm11_member_points_rule_seed'),
('1926062310000000405', '1926062310000000105', '5', 'DIAMOND', 1000, 1.00, 1, NOW(), 'm11_member_points_rule_seed');
