CREATE TABLE `shop_store_member_level` (
                                           `user_level_id` int NOT NULL AUTO_INCREMENT COMMENT '等级编号',
                                           `user_level_name` varchar(50) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL DEFAULT '' COMMENT '等级名称',
                                           `user_level_spend` decimal(6,2) NOT NULL DEFAULT '0.00' COMMENT '累计消费',
                                           `user_level_rate` decimal(6,2) NOT NULL DEFAULT '0.00' COMMENT '折扣率百分比',
                                           `user_level_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
                                           `created_at` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '新建时间',
                                           `updated_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
                                           PRIMARY KEY (`user_level_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='会员等级表-店铺';

CREATE TABLE shop_store_member (
                                   store_member_id int PRIMARY KEY AUTO_INCREMENT COMMENT '店铺会员id',
                                   user_id  int unsigned NOT NULL COMMENT '用户编号',
                                   store_id int unsigned NOT NULL COMMENT '店铺编号',
                                   store_name varchar(50) NOT NULL COMMENT '店铺名称',
                                   first_purchase_time DATETIME NOT NULL COMMENT '首次消费时间（成为店铺会员的时间）',
                                   total_consumption DECIMAL(10,2) NOT NULL DEFAULT 0.00 COMMENT '在该店铺累计消费金额（默认0）',
                                   store_points int NOT NULL DEFAULT 0.00 COMMENT '积分',
                                   last_purchase_time DATETIME default null COMMENT '最近消费时间',
                                   member_level_id int unsigned NOT NULL DEFAULT 0 COMMENT '店铺会员等级id',
                                   member_level_name varchar(50) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL DEFAULT '' COMMENT '等级名称',
                                   `created_at` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '新建时间',
                                   `updated_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
                                   UNIQUE KEY unique_user_store (store_member_id, store_id),
                                   KEY `index_store_id` (`store_id`) USING BTREE,
                                   KEY `index_store_name` (`store_name`) USING BTREE,
                                   KEY `index_user_id` (`user_id`) USING BTREE
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='会员表-店铺';


-- 积分账户表
CREATE TABLE points_account (
                                user_id VARCHAR(32) PRIMARY KEY,
                                store_member_id VARCHAR(32) NOT NULL,
                                total_points INT DEFAULT 0 COMMENT '累计获得积分',
                                available_points INT DEFAULT 0 COMMENT '可用积分',
                                frozen_points INT DEFAULT 0 COMMENT '冻结积分',
                                expired_points INT DEFAULT 0 COMMENT '已过期积分',
                                last_update_time DATETIME NOT NULL,
                                FOREIGN KEY (member_id) REFERENCES members(member_id),
                                INDEX idx_member (member_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 积分流水表
CREATE TABLE points_transaction (
                                    transaction_id VARCHAR(32) PRIMARY KEY,
                                    user_id VARCHAR(32) NOT NULL,
                                    store_member_id VARCHAR(32) NOT NULL,
                                    points INT NOT NULL COMMENT '正数为获得，负数为消耗',
                                    balance_after INT NOT NULL COMMENT '交易后余额',
                                    transaction_type TINYINT NOT NULL COMMENT '1-获取 2-消费 3-过期 4-调整',
                                    transaction_time DATETIME NOT NULL,
                                    expiry_date DATE COMMENT '过期日期',
                                    source_id VARCHAR(32) COMMENT '来源ID(订单ID等)',
                                    source_desc VARCHAR(100) COMMENT '来源描述',
                                    remark VARCHAR(200),
                                    FOREIGN KEY (account_id) REFERENCES points_account(account_id),
                                    FOREIGN KEY (member_id) REFERENCES members(member_id),
                                    INDEX idx_member (member_id),
                                    INDEX idx_account (account_id),
                                    INDEX idx_expiry (expiry_date),
                                    INDEX idx_transaction_time (transaction_time)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 积分规则表
CREATE TABLE points_rule (
                             rule_id VARCHAR(32) PRIMARY KEY,
                             rule_name VARCHAR(50) NOT NULL,
                             rule_type TINYINT NOT NULL COMMENT '1-获取规则 2-过期规则',
                             points_value INT COMMENT '获取积分数值或比例',
                             expiry_days INT COMMENT '过期天数(0表示永久有效)',
                             status TINYINT DEFAULT 1 COMMENT '1-启用 0-禁用',
                             start_time DATETIME,
                             end_time DATETIME,
                             create_time DATETIME NOT NULL,
                             update_time DATETIME,
                             store_id int unsigned NOT NULL COMMENT '店铺编号',
                             warning_day INT NOT NULL default 0 COMMENT '预警通知，0是表示没有预警',
                             remark VARCHAR(200)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;


