-- =============================================
-- Member 模块建表 — 从 PostgreSQL 测试 SQL 转换为 MySQL
-- 用于本地开发环境补全 member 表
-- =============================================

-- 1. 会员配置表
CREATE TABLE IF NOT EXISTS member_config (
    id bigint NOT NULL AUTO_INCREMENT,
    group_id bigint NULL DEFAULT NULL COMMENT '用户分组编号',
    level_id bigint NULL DEFAULT NULL COMMENT '等级编号',
    tenant_id bigint NOT NULL DEFAULT 0,
    creator varchar(64) DEFAULT '' COMMENT '创建者',
    create_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    updater varchar(64) DEFAULT '' COMMENT '更新者',
    update_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    deleted bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',
    PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='会员配置表';

-- 2. 会员标签表
CREATE TABLE IF NOT EXISTS member_tag (
    id bigint NOT NULL AUTO_INCREMENT,
    name varchar(50) NOT NULL COMMENT '标签名称',
    creator varchar(64) DEFAULT '' COMMENT '创建者',
    create_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    updater varchar(64) DEFAULT '' COMMENT '更新者',
    update_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    deleted bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',
    tenant_id bigint NOT NULL DEFAULT 0 COMMENT '租户编号',
    PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='会员标签';

-- 3. 用户分组表
CREATE TABLE IF NOT EXISTS member_group (
    id bigint NOT NULL AUTO_INCREMENT,
    name varchar(50) NOT NULL COMMENT '分组名称',
    remark varchar(255) DEFAULT '' COMMENT '备注',
    status tinyint NOT NULL DEFAULT 0 COMMENT '状态',
    creator varchar(64) DEFAULT '' COMMENT '创建者',
    create_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    updater varchar(64) DEFAULT '' COMMENT '更新者',
    update_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    deleted bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',
    tenant_id bigint NOT NULL DEFAULT 0 COMMENT '租户编号',
    PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='会员分组';

-- 4. 会员等级表（完整版，覆盖之前创建的简化版）
CREATE TABLE IF NOT EXISTS member_level (
    id bigint NOT NULL AUTO_INCREMENT,
    name varchar(50) NOT NULL COMMENT '等级名称',
    experience int NOT NULL DEFAULT 0 COMMENT '所需经验',
    level int NOT NULL DEFAULT 1 COMMENT '等级',
    discount_percent int NOT NULL DEFAULT 100 COMMENT '折扣百分比',
    icon varchar(255) DEFAULT '' COMMENT '图标',
    background_url varchar(255) DEFAULT '' COMMENT '背景图',
    status tinyint NOT NULL DEFAULT 0 COMMENT '状态',
    creator varchar(64) DEFAULT '' COMMENT '创建者',
    create_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    updater varchar(64) DEFAULT '' COMMENT '更新者',
    update_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    deleted bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',
    tenant_id bigint NOT NULL DEFAULT 0 COMMENT '租户编号',
    PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='会员等级';

-- 5. 会员等级记录表
CREATE TABLE IF NOT EXISTS member_level_record (
    id bigint NOT NULL AUTO_INCREMENT,
    user_id bigint NOT NULL COMMENT '用户编号',
    level_id bigint NOT NULL COMMENT '等级编号',
    level_name varchar(50) DEFAULT '' COMMENT '等级名称',
    experience int NOT NULL DEFAULT 0 COMMENT '当前经验',
    remark varchar(255) DEFAULT '' COMMENT '备注',
    creator varchar(64) DEFAULT '' COMMENT '创建者',
    create_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    updater varchar(64) DEFAULT '' COMMENT '更新者',
    update_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    deleted bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',
    tenant_id bigint NOT NULL DEFAULT 0 COMMENT '租户编号',
    PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='会员等级记录';

-- 6. 会员经验记录表
CREATE TABLE IF NOT EXISTS member_experience_record (
    id bigint NOT NULL AUTO_INCREMENT,
    user_id bigint NOT NULL COMMENT '用户编号',
    biz_type varchar(50) NOT NULL COMMENT '业务类型',
    biz_id varchar(100) NOT NULL COMMENT '业务编号',
    title varchar(100) NOT NULL COMMENT '标题',
    experience int NOT NULL COMMENT '经验',
    total_experience int NOT NULL DEFAULT 0 COMMENT '累计经验',
    description varchar(255) DEFAULT '' COMMENT '描述',
    creator varchar(64) DEFAULT '' COMMENT '创建者',
    create_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    updater varchar(64) DEFAULT '' COMMENT '更新者',
    update_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    deleted bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',
    tenant_id bigint NOT NULL DEFAULT 0 COMMENT '租户编号',
    PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='会员经验记录';

-- 7. 会员积分记录表
CREATE TABLE IF NOT EXISTS member_point_record (
    id bigint NOT NULL AUTO_INCREMENT,
    user_id bigint NOT NULL COMMENT '用户编号',
    biz_type varchar(50) NOT NULL COMMENT '业务类型',
    biz_id varchar(100) NOT NULL COMMENT '业务编号',
    title varchar(100) NOT NULL COMMENT '标题',
    point int NOT NULL COMMENT '积分',
    total_point int NOT NULL DEFAULT 0 COMMENT '累计积分',
    description varchar(255) DEFAULT '' COMMENT '描述',
    creator varchar(64) DEFAULT '' COMMENT '创建者',
    create_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    updater varchar(64) DEFAULT '' COMMENT '更新者',
    update_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    deleted bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',
    tenant_id bigint NOT NULL DEFAULT 0 COMMENT '租户编号',
    PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='会员积分记录';

-- 8. 会员签到配置表
CREATE TABLE IF NOT EXISTS member_sign_in_config (
    id bigint NOT NULL AUTO_INCREMENT,
    day int NOT NULL COMMENT '签到天数',
    point int NOT NULL DEFAULT 0 COMMENT '奖励积分',
    experience int NOT NULL DEFAULT 0 COMMENT '奖励经验',
    status tinyint NOT NULL DEFAULT 0 COMMENT '状态',
    creator varchar(64) DEFAULT '' COMMENT '创建者',
    create_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    updater varchar(64) DEFAULT '' COMMENT '更新者',
    update_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    deleted bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',
    tenant_id bigint NOT NULL DEFAULT 0 COMMENT '租户编号',
    PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='会员签到配置';

-- 9. 会员签到记录表
CREATE TABLE IF NOT EXISTS member_sign_in_record (
    id bigint NOT NULL AUTO_INCREMENT,
    user_id bigint NOT NULL COMMENT '用户编号',
    day int NOT NULL COMMENT '签到天数',
    point int NOT NULL DEFAULT 0 COMMENT '获得积分',
    experience int NOT NULL DEFAULT 0 COMMENT '获得经验',
    creator varchar(64) DEFAULT '' COMMENT '创建者',
    create_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    updater varchar(64) DEFAULT '' COMMENT '更新者',
    update_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    deleted bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',
    tenant_id bigint NOT NULL DEFAULT 0 COMMENT '租户编号',
    PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='会员签到记录';

-- 10. 会员地址表
CREATE TABLE IF NOT EXISTS member_address (
    id bigint NOT NULL AUTO_INCREMENT,
    user_id bigint NOT NULL COMMENT '用户编号',
    name varchar(20) NOT NULL COMMENT '收件人姓名',
    mobile varchar(20) NOT NULL COMMENT '手机号',
    area_id bigint NOT NULL COMMENT '地区编号',
    detail_address varchar(250) NOT NULL COMMENT '详细地址',
    default_status bit(1) NOT NULL DEFAULT b'0' COMMENT '是否默认',
    creator varchar(64) DEFAULT '' COMMENT '创建者',
    create_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    updater varchar(64) DEFAULT '' COMMENT '更新者',
    update_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    deleted bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',
    PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='会员地址';

-- =============================================
-- 种子数据
-- =============================================
INSERT IGNORE INTO member_level (id, name, level, experience, status, tenant_id) VALUES (1, '普通会员', 1, 0, 0, 1);
