
CREATE TABLE `shop_store_member_level` (
                                           `store_member_level_id` int unsigned NOT NULL COMMENT '主表id',
                                           `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 '累计消费',
                                           `first_purchase_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '首次消费时间（成为店铺会员的时间）',
                                           `store_points` int NOT NULL DEFAULT '0' 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 '等级名称',
                                           `user_level_rate` decimal(6,2) NOT NULL DEFAULT '100.00' COMMENT '折扣率百分比',
                                           `user_level_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
                                           `create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '新建时间',
                                           `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
                                           `store_id` int unsigned NOT NULL COMMENT '店铺编号',
                                           `store_member_id` int unsigned NOT NULL COMMENT '店铺会员id',
                                           PRIMARY KEY (`store_member_level_id`) USING BTREE,
                                           KEY `index_user_level_id` (`user_level_id`) USING BTREE,
                                           KEY `index_user_level_name` (`user_level_name`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=1002 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='会员等级表-店铺';
alter table shop_store_member_level add column `user_id` int unsigned NOT NULL COMMENT '用户编号';
CREATE TABLE `shop_store_member` (
                                     `store_member_id` int NOT NULL 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 '店铺名称',
                                     `create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '新建时间',
                                     `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
                                     `user_account` varchar(100) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL COMMENT '用户名',
                                     `user_nickname` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT ' 用户昵称',
                                     `send_number` int unsigned NOT NULL DEFAULT '0' COMMENT '剩余订阅次数',
                                     `bind_openid` varchar(100) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL DEFAULT '' COMMENT '访问Open编号',
                                     PRIMARY KEY (`store_member_id`),
                                     UNIQUE KEY `unique_user_store` (`user_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 AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='会员表-店铺';

CREATE TABLE shop_store_points_account (
                                           user_id VARCHAR(32) PRIMARY KEY,
                                           store_member_id int NOT NULL COMMENT '店铺会员id',
                                           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   COMMENT '最后更新时间',
                                           FOREIGN KEY (store_member_id) REFERENCES shop_store_member(store_member_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='会员表-积分账户表';

CREATE TABLE shop_store_points_transaction (
                                               transaction_id VARCHAR(32) PRIMARY KEY,
                                               user_id int NOT NULL COMMENT '用户编号',
                                               store_member_id VARCHAR(32) NOT NULL COMMENT '店铺会员id',
                                               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 DEFAULT CURRENT_TIMESTAMP COMMENT '流水时间',
                                               expiry_date datetime  NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '过期日期',
                                               source_id VARCHAR(32) NOT NULL DEFAULT '' COMMENT '来源ID(订单ID等)',
                                               source_desc VARCHAR(100) NOT NULL DEFAULT '' COMMENT '来源描述',
                                               remark VARCHAR(200) COMMENT '备注',
                                               INDEX idx_account (user_id),
                                               INDEX idx_expiry (expiry_date),
                                               INDEX idx_transaction_time (transaction_time)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='会员表-积分流水表';

CREATE TABLE shop_store_points_rule (
                                        rule_id VARCHAR(32) PRIMARY KEY,
                                        rule_name VARCHAR(50) NOT NULL COMMENT '规则名称',
                                        rule_type TINYINT NOT NULL COMMENT '1-获取规则 2-过期规则',
                                        points_value INT default 0 COMMENT '获取积分数值或比例',
                                        expiry_days INT default 0 COMMENT '过期天数(0表示永久有效)',
                                        status TINYINT DEFAULT 1 COMMENT '1-启用 0-禁用',
                                        start_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '开始时间',
                                        end_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '结束时间',
                                        `create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '新建时间',
                                        `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
                                        store_id int unsigned NOT NULL COMMENT '店铺编号',
                                        warning_day INT NOT NULL default 0 COMMENT '预警通知，0是表示没有预警',
                                        remark VARCHAR(200) DEFAULT '' COMMENT '备注'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='会员表-积分规则表';
alter table shop_store_points_rule add index `rule_name` (`rule_name`) USING BTREE;

