-- 用户表
CREATE TABLE IF NOT EXISTS `member_users` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '用户编号',
    `mobile` varchar(11) NOT NULL COMMENT '手机号',
    `password` varchar(255) NOT NULL COMMENT '密码',
    `nickname` varchar(50) DEFAULT NULL COMMENT '昵称',
    `avatar` varchar(255) DEFAULT NULL COMMENT '头像',
    `gender` varchar(255) NOT NULL DEFAULT 'UNKNOWN' COMMENT '性别',
    `register_area` varchar(6) DEFAULT NULL COMMENT '注册地(行政区划代码)',
    `state` varchar(255) NOT NULL DEFAULT 'ENABLE' COMMENT '状态',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_mobile` (`mobile`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '用户表';

-- 社交账号表
CREATE TABLE IF NOT EXISTS `member_social_accounts` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '社交账号编号',
    `user_id` bigint unsigned NOT NULL COMMENT '用户编号',
    `platform` varchar(255) NOT NULL COMMENT '社交平台',
    `social_id` varchar(100) NOT NULL COMMENT '社交账号',
    `nickname` varchar(50) DEFAULT NULL COMMENT '昵称',
    `avatar` varchar(255) DEFAULT NULL COMMENT '头像',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_platform_social_id` (`platform`, `social_id`),
    KEY `idx_user_id` (`user_id`),
    KEY `idx_platform` (`platform`),
    CONSTRAINT `fk_msoc_muser_user_id` FOREIGN KEY (`user_id`) REFERENCES `member_users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '社交账号表';

-- 用户地址表
CREATE TABLE IF NOT EXISTS `member_addresses` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '地址编号',
    `user_id` bigint unsigned NOT NULL COMMENT '用户编号',
    `phone` varchar(11) NOT NULL COMMENT '联系电话',
    `location` varchar(255) NOT NULL COMMENT '定位地址',
    `address` varchar(255) DEFAULT NULL COMMENT '详细地址',
    `province` varchar(50) NOT NULL COMMENT '省',
    `city` varchar(50) NOT NULL COMMENT '市',
    `district` varchar(50) NOT NULL COMMENT '区',
    `longitude` decimal(10, 7) NOT NULL COMMENT '经度',
    `latitude` decimal(10, 7) NOT NULL COMMENT '纬度',
    `area_code` varchar(6) NOT NULL COMMENT '行政区划代码',
    `is_default` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否默认地址',
    `state` varchar(255) NOT NULL DEFAULT 'ENABLE' COMMENT '状态',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    KEY `idx_user_id` (`user_id`),
    KEY `idx_is_default` (`is_default`),
    CONSTRAINT `fk_madr_muser_user_id` FOREIGN KEY (`user_id`) REFERENCES `member_users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '用户地址表';

-- 用户实名认证表
CREATE TABLE IF NOT EXISTS `member_real_auth_records` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '实名认证编号',
    `user_id` bigint unsigned NOT NULL COMMENT '用户编号',
    `real_name` varchar(50) NOT NULL COMMENT '真实姓名',
    `id_card` varchar(18) NOT NULL COMMENT '身份证号',
    `id_card_front_photo` varchar(255) NOT NULL COMMENT '身份证正面照片',
    `id_card_back_photo` varchar(255) NOT NULL COMMENT '身份证反面照片',
    `id_card_hand_photo` varchar(255) NOT NULL COMMENT '手持身份证照片',
    `auditor` varchar(50) DEFAULT NULL COMMENT '审核人',
    `audit_time` timestamp NULL DEFAULT NULL COMMENT '审核时间',
    `audit_remark` varchar(255) DEFAULT NULL COMMENT '审核回执',
    `state` varchar(255) NOT NULL DEFAULT 'PENDING' COMMENT '状态',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    KEY `idx_user_id` (`user_id`),
    CONSTRAINT `fk_mrar_muser_user_id` FOREIGN KEY (`user_id`) REFERENCES `member_users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '用户实名认证表';

-- 技师表
CREATE TABLE IF NOT EXISTS `coach_users` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '技师编号',
    `user_id` bigint unsigned NOT NULL COMMENT '用户编号',
    `info_record_id` bigint unsigned DEFAULT NULL COMMENT '技师信息记录编号',
    `real_auth_record_id` bigint unsigned DEFAULT NULL COMMENT '技师实名认证记录编号',
    `qual_record_id` bigint unsigned DEFAULT NULL COMMENT '技师资质认证记录编号',
    `shop_id` bigint unsigned DEFAULT NULL COMMENT '店铺编号',
    `level` varchar(255) DEFAULT NULL COMMENT '技师等级',
    `virtual_order` int DEFAULT '0' COMMENT '虚拟订单数',
    `score` decimal(3, 1) DEFAULT '5.0' COMMENT '评分',
    `work_status` varchar(255) NOT NULL DEFAULT 'REST' COMMENT '工作状态',
    `virtual_status` varchar(255) NOT NULL DEFAULT 'DISABLE' COMMENT '虚拟状态',
    `state` varchar(255) NOT NULL DEFAULT 'ENABLE' COMMENT '状态',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_user_id` (`user_id`),
    KEY `idx_shop_id` (`shop_id`),
    KEY `idx_work_status` (`work_status`),
    CONSTRAINT `fk_coach_user_id` FOREIGN KEY (`user_id`) REFERENCES `member_users` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '技师表';

-- 技师实名认证记录表
CREATE TABLE IF NOT EXISTS `coach_real_auth_records` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '实名认证编号',
    `coach_id` bigint unsigned NOT NULL COMMENT '技师编号',
    `real_name` varchar(50) NOT NULL COMMENT '真实姓名',
    `id_card` varchar(18) NOT NULL COMMENT '身份证号',
    `id_card_front_photo` varchar(255) NOT NULL COMMENT '身份证正面照片',
    `id_card_back_photo` varchar(255) NOT NULL COMMENT '身份证反面照片',
    `id_card_hand_photo` varchar(255) NOT NULL COMMENT '手持身份证照片',
    `auditor` varchar(50) DEFAULT NULL COMMENT '审核人',
    `audit_time` timestamp NULL DEFAULT NULL COMMENT '审核时间',
    `audit_remark` varchar(255) DEFAULT NULL COMMENT '审核回执',
    `state` varchar(255) NOT NULL DEFAULT 'pending' COMMENT '状态',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    KEY `idx_coach_id` (`coach_id`),
    CONSTRAINT `fk_carar_coach_coach_id` FOREIGN KEY (`coach_id`) REFERENCES `coach_users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '技师实名认证记录表';

-- 技师信息记录表
CREATE TABLE IF NOT EXISTS `coach_info_records` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '技师信息记录编号',
    `coach_id` bigint unsigned NOT NULL COMMENT '技师编号',
    `nickname` varchar(50) DEFAULT NULL COMMENT '昵称',
    `avatar` varchar(255) DEFAULT NULL COMMENT '头像',
    `gender` varchar(255) NOT NULL DEFAULT 'UNKNOWN' COMMENT '性别',
    `mobile` varchar(11) NOT NULL COMMENT '服务电话',
    `birthday` date DEFAULT NULL COMMENT '出生日期',
    `work_years` int DEFAULT NULL COMMENT '从业年份',
    `intention_city` varchar(50) DEFAULT NULL COMMENT '意向城市',
    `introduction` text COMMENT '个人简介',
    `auditor` varchar(50) DEFAULT NULL COMMENT '审核人',
    `audit_time` timestamp NULL DEFAULT NULL COMMENT '审核时间',
    `audit_remark` varchar(255) DEFAULT NULL COMMENT '审核回执',
    `state` varchar(255) NOT NULL DEFAULT 'PENDING' COMMENT '状态',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    KEY `idx_cir_coach_coach_id` (`coach_id`),
    CONSTRAINT `fk_info_records_coach_id` FOREIGN KEY (`coach_id`) REFERENCES `coach_users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '技师信息记录表';

-- 技师实名认证记录表
CREATE TABLE IF NOT EXISTS `coach_real_records` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '技师实名认证记录编号',
    `coach_id` bigint unsigned NOT NULL COMMENT '技师编号',
    `real_name` varchar(50) NOT NULL COMMENT '姓名',
    `id_card` varchar(18) NOT NULL COMMENT '身份证号',
    `id_card_front_photo` varchar(255) NOT NULL COMMENT '身份证正面照片',
    `id_card_back_photo` varchar(255) NOT NULL COMMENT '身份证反面照片',
    `id_card_hand_photo` varchar(255) NOT NULL COMMENT '手持身份证照片',
    `auditor` varchar(50) DEFAULT NULL COMMENT '审核人',
    `audit_time` timestamp NULL DEFAULT NULL COMMENT '审核时间',
    `audit_remark` varchar(255) DEFAULT NULL COMMENT '审核回执',
    `state` varchar(255) NOT NULL DEFAULT 'PENDING' COMMENT '状态',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    KEY `idx_crar_coach_coach_id` (`coach_id`),
    CONSTRAINT `fk_real_auth_records_coach_id` FOREIGN KEY (`coach_id`) REFERENCES `coach_users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '技师实名认证记录表';

-- 技师资质认证记录表
CREATE TABLE IF NOT EXISTS `coach_qual_records` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '技师资质认证记录编号',
    `coach_id` bigint unsigned NOT NULL COMMENT '技师编号',
    `qual_type` varchar(255) NOT NULL COMMENT '资质类型',
    `qual_no` varchar(100) DEFAULT NULL COMMENT '资质证书编号',
    `qual_photo` varchar(255) NOT NULL COMMENT '资质证书照片',
    `valid_start` date DEFAULT NULL COMMENT '有效期开始日期',
    `valid_end` date DEFAULT NULL COMMENT '有效期结束日期',
    `auditor` varchar(50) DEFAULT NULL COMMENT '审核人',
    `audit_time` timestamp NULL DEFAULT NULL COMMENT '审核时间',
    `audit_remark` varchar(255) DEFAULT NULL COMMENT '审核回执',
    `state` varchar(255) NOT NULL DEFAULT 'PENDING' COMMENT '状态',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    KEY `idx_cqr_coach_coach_id` (`coach_id`),
    CONSTRAINT `fk_qual_records_coach_id` FOREIGN KEY (`coach_id`) REFERENCES `coach_users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '技师资质认证记录表';

-- 技师评分表
CREATE TABLE IF NOT EXISTS `coach_scores` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '评分编号',
    `coach_id` bigint unsigned NOT NULL COMMENT '技师编号',
    `service_duration` int DEFAULT '0' COMMENT '服务时长(分钟)',
    `order_count` int DEFAULT '0' COMMENT '订单数量',
    `good_rate` decimal(5, 2) DEFAULT '0.00' COMMENT '好评率',
    `efficiency` decimal(5, 2) DEFAULT '0.00' COMMENT '服务效率',
    `punctual_rate` decimal(5, 2) DEFAULT '0.00' COMMENT '准时率',
    `completion_rate` decimal(5, 2) DEFAULT '0.00' COMMENT '完成率',
    `rating_score` decimal(3, 1) DEFAULT '5.0' COMMENT '评价得分',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_csco_coach_coach_id` (`coach_id`),
    CONSTRAINT `fk_scores_coach_id` FOREIGN KEY (`coach_id`) REFERENCES `coach_users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '技师评分表';

-- 技师定位地址表
CREATE TABLE IF NOT EXISTS `coach_locations` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '技师定位地址编号',
    `coach_id` bigint unsigned NOT NULL COMMENT '技师编号',
    `type` varchar(255) NOT NULL DEFAULT 'CURRENT' COMMENT '地址类型',
    `latitude` decimal(10, 7) NOT NULL COMMENT '纬度',
    `longitude` decimal(10, 7) NOT NULL COMMENT '经度',
    `province` varchar(50) NOT NULL COMMENT '省',
    `city` varchar(50) NOT NULL COMMENT '市',
    `district` varchar(50) NOT NULL COMMENT '区',
    `location` varchar(255) NOT NULL COMMENT '定位地址',
    `area_code` varchar(6) NOT NULL COMMENT '行政区划代码',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    KEY `idx_cloc_coach_coach_id` (`coach_id`),
    KEY `idx_type` (`type`),
    CONSTRAINT `fk_locations_coach_id` FOREIGN KEY (`coach_id`) REFERENCES `coach_users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '技师定位地址表';

-- 技师服务项目表
CREATE TABLE `coach_project` (
    `id` bigint UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键',
    `coach_id` bigint UNSIGNED NOT NULL COMMENT '技师ID',
    `project_id` bigint UNSIGNED NOT NULL COMMENT '项目ID',
    `discount_amount` decimal(10, 2) NOT NULL DEFAULT 0.00 COMMENT '优惠金额',
    `service_gender` varchar(20) NOT NULL DEFAULT 'all' COMMENT '服务性别(all:不限 male:男 female:女)',
    `service_distance` int NOT NULL DEFAULT 0 COMMENT '服务距离(米)',
    `traffic_fee_type` varchar(20) NOT NULL DEFAULT 'free' COMMENT '收取路费(free:免费 one_way:单程 round_trip:双程)',
    `traffic_fee` decimal(10, 2) NOT NULL DEFAULT 0.00 COMMENT '路费金额',
    `state` varchar(20) NOT NULL DEFAULT 'enable' COMMENT '状态(enable:启用 disable:禁用)',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_coach_project` (`coach_id`, `project_id`),
    KEY `idx_coach_id` (`coach_id`),
    KEY `idx_project_id` (`project_id`),
    KEY `idx_state` (`state`),
    CONSTRAINT `fk_cp_coach` FOREIGN KEY (`coach_id`) REFERENCES `coach_users` (`id`),
    CONSTRAINT `fk_cp_project` FOREIGN KEY (`project_id`) REFERENCES `project` (`id`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '技师服务项目表';

-- 订单表
CREATE TABLE IF NOT EXISTS `order` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '订单编号',
    `user_id` bigint unsigned NOT NULL COMMENT '用户编号',
    `coach_id` bigint unsigned NOT NULL COMMENT '技师编号',
    `project_id` bigint unsigned NOT NULL COMMENT '项目编号',
    `channel_id` bigint unsigned DEFAULT NULL COMMENT '渠道编号',
    `shop_id` bigint unsigned DEFAULT NULL COMMENT '店铺编号',
    `agent_id` bigint unsigned DEFAULT NULL COMMENT '代理编号',
    `customer_service_id` bigint unsigned DEFAULT NULL COMMENT '跟单客服编号',
    `type` ENUM('normal', 'add_time') NOT NULL COMMENT '订单类型',
    `source` ENUM('platform', 'shop') NOT NULL COMMENT '订单来源',
    `payment_type` ENUM('balance', 'wechat', 'alipay') NULL COMMENT '支付类型',
    `project_amount` DECIMAL(10, 2) NOT NULL COMMENT '项目金额',
    `traffic_amount` DECIMAL(10, 2) DEFAULT '0.00' COMMENT '路费金额',
    `total_amount` DECIMAL(10, 2) NOT NULL COMMENT '订单金额',
    `balance_amount` DECIMAL(10, 2) DEFAULT '0.00' COMMENT '余额抵扣金额',
    `pay_amount` DECIMAL(10, 2) NOT NULL DEFAULT '0.00' COMMENT '实付金额',
    `discount_amount` DECIMAL(10, 2) DEFAULT '0.00' COMMENT '优惠卷金额',
    `tip_amount` DECIMAL(10, 2) DEFAULT '0.00' COMMENT '打赏金额',
    `service_snapshot` json DEFAULT NULL COMMENT '项目快照',
    `service_time` timestamp NOT NULL COMMENT '服务时间',
    `distance` int DEFAULT '0' COMMENT '目的地距离(米)',
    `latitude` decimal(10, 7) NOT NULL COMMENT '目的地纬度',
    `longitude` decimal(10, 7) NOT NULL COMMENT '目的地经度',
    `location` varchar(255) NOT NULL COMMENT '目的地定位地址',
    `address` varchar(255) DEFAULT NULL COMMENT '目的地详细地址',
    `area_code` varchar(6) NOT NULL COMMENT '目的地行政区划代码',
    `remark` varchar(255) DEFAULT NULL COMMENT '订单备注',
    `state` ENUM(
        'wait_pay',
        'wait_receive',
        'service',
        'service_ing',
        'service_end',
        'leave',
        'complete',
        'cancel'
    ) NOT NULL COMMENT '订单状态',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    KEY `idx_user_id` (`user_id`),
    KEY `idx_coach_id` (`coach_id`),
    KEY `idx_service_id` (`project_id`),
    KEY `idx_shop_id` (`shop_id`),
    KEY `idx_agent_id` (`agent_id`),
    KEY `idx_service_time` (`service_time`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '订单表';

-- 订单异常记录表
CREATE TABLE IF NOT EXISTS `order_exc_records` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '订单异常记录编号',
    `order_id` bigint unsigned NOT NULL COMMENT '订单编号',
    `customer_service_id` bigint unsigned DEFAULT NULL COMMENT '跟单客服编号',
    `type` varchar(255) NOT NULL COMMENT '异常类型',
    `reason` varchar(255) NOT NULL COMMENT '异常原因',
    `remark` varchar(255) DEFAULT NULL COMMENT '备注',
    `state` varchar(255) NOT NULL DEFAULT 'ENABLE' COMMENT '状态',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    KEY `idx_oer_order_order_id` (`order_id`),
    KEY `idx_exc_type` (`type`),
    CONSTRAINT `fk_exc_records_order_id` FOREIGN KEY (`order_id`) REFERENCES `order_infos` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '订单异常记录表';

-- 订单记录表
CREATE TABLE IF NOT EXISTS `order_records` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '订单记录编号',
    `order_id` bigint unsigned NOT NULL COMMENT '订单编号',
    `object_id` bigint unsigned NOT NULL COMMENT '对象编号',
    `object_type` varchar(255) NOT NULL COMMENT '对象类型',
    `state` varchar(255) NOT NULL COMMENT '记录状态',
    `remark` varchar(255) DEFAULT NULL COMMENT '备注',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    KEY `idx_orec_order_order_id` (`order_id`),
    KEY `idx_object_id` (`object_id`),
    KEY `idx_object_type` (`object_type`),
    CONSTRAINT `fk_order_records_order_id` FOREIGN KEY (`order_id`) REFERENCES `order_infos` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '订单记录表';

-- 订单分销记录表
CREATE TABLE IF NOT EXISTS `order_dist_records` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '订单分销记录编号',
    `order_id` bigint unsigned NOT NULL COMMENT '订单编号',
    `object_id` bigint unsigned NOT NULL COMMENT '对象编号',
    `object_type` varchar(255) NOT NULL COMMENT '对象类型',
    `dist_type` varchar(255) NOT NULL COMMENT '分销类型',
    `amount` decimal(10, 2) NOT NULL DEFAULT '0.00' COMMENT '分销金额',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    KEY `idx_odr_order_order_id` (`order_id`),
    KEY `idx_object_id` (`object_id`),
    KEY `idx_object_type` (`object_type`),
    KEY `idx_dist_type` (`dist_type`),
    CONSTRAINT `fk_dist_records_order_id` FOREIGN KEY (`order_id`) REFERENCES `order_infos` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '订单分销记录表';

-- 抢单记录表
CREATE TABLE IF NOT EXISTS `order_grab_records` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '抢单池编号',
    `coach_id` bigint unsigned NOT NULL COMMENT '技师编号',
    `order_id` bigint unsigned NOT NULL COMMENT '订单编号',
    `state` varchar(255) NOT NULL DEFAULT 'JOINED' COMMENT '状态',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_ogr_coach_coach_id` (`coach_id`),
    KEY `idx_ogr_order_order_id` (`order_id`),
    CONSTRAINT `fk_grab_records_coach_id` FOREIGN KEY (`coach_id`) REFERENCES `coach_users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT `fk_grab_records_order_id` FOREIGN KEY (`order_id`) REFERENCES `order_infos` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '抢单记录表';

-- 评论表
CREATE TABLE IF NOT EXISTS `order_comments` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '评论编号',
    `order_id` bigint unsigned NOT NULL COMMENT '订单编号',
    `user_id` bigint unsigned NOT NULL COMMENT '用户编号',
    `coach_id` bigint unsigned NOT NULL COMMENT '技师编号',
    `content` text COMMENT '评论内容',
    `rating` tinyint unsigned NOT NULL DEFAULT '5' COMMENT '评论星级',
    `state` varchar(255) NOT NULL DEFAULT 'ENABLE' COMMENT '状态',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_ocmt_order_order_id` (`order_id`),
    KEY `idx_ocmt_muser_user_id` (`user_id`),
    KEY `idx_ocmt_coach_coach_id` (`coach_id`),
    CONSTRAINT `fk_comments_order_id` FOREIGN KEY (`order_id`) REFERENCES `order_infos` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT `fk_comments_user_id` FOREIGN KEY (`user_id`) REFERENCES `member_users` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE,
    CONSTRAINT `fk_comments_coach_id` FOREIGN KEY (`coach_id`) REFERENCES `coach_users` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '评论表';

-- 钱包表
CREATE TABLE IF NOT EXISTS `wallet` (
    `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '钱包编号',
    `owner_id` BIGINT UNSIGNED NOT NULL COMMENT '所属主体编号',
    `owner_type` varchar(255) NOT NULL DEFAULT '' COMMENT '钱包类型',
    `total_balance` DECIMAL(20, 2) NOT NULL DEFAULT 0.00 COMMENT '总余额',
    `available_balance` DECIMAL(20, 2) NOT NULL DEFAULT 0.00 COMMENT '可用余额',
    `frozen_amount` DECIMAL(20, 2) NOT NULL DEFAULT 0.00 COMMENT '冻结金额',
    `total_income` DECIMAL(20, 2) NOT NULL DEFAULT 0.00 COMMENT '累计收入',
    `total_expense` DECIMAL(20, 2) NOT NULL DEFAULT 0.00 COMMENT '累计支出',
    `state` varchar(255) NOT NULL DEFAULT 'ENABLE' COMMENT '状态：ENABLE-启用，DISABLE-禁用',
    `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_owner` (`owner_id`, `owner_type`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COMMENT = '钱包表';

-- 钱包交易记录表
CREATE TABLE IF NOT EXISTS `wallet_trans_records` (
    `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '交易记录编号',
    `wallet_id` BIGINT UNSIGNED NOT NULL COMMENT '钱包编号',
    `owner_type` varchar(255) NOT NULL COMMENT '业务类型(profit:分账 recharge:充值 withdraw:提现 refund:退款 payment:支付 gift:赠送 breach:违约 compensation:补偿)',
    `owner_id` BIGINT UNSIGNED DEFAULT NULL COMMENT '业务编号',
    `trans_type` varchar(32) NOT NULL COMMENT '交易类型(income:收入 expense:支出)',
    `storage_type` varchar(32) NOT NULL COMMENT '存储类型(balance:余额 recharge:充值)',
    `amount` decimal(10, 2) NOT NULL COMMENT '交易金额',
    `before_balance` decimal(10, 2) NOT NULL COMMENT '交易前余额',
    `after_balance` decimal(10, 2) NOT NULL COMMENT '交易后余额',
    `before_recharge_balance` decimal(10, 2) NOT NULL COMMENT '交易前充值余额',
    `after_recharge_balance` decimal(10, 2) NOT NULL COMMENT '交易后充值余额',
    `trans_time` datetime NOT NULL COMMENT '交易时间',
    `remark` varchar(255) DEFAULT NULL COMMENT '备注',
    `state` varchar(32) NOT NULL COMMENT '交易状态(processing:进行中 success:成功 fail:失败 cancel:取消)',
    `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    PRIMARY KEY (`id`),
    KEY `idx_wallet_id` (`wallet_id`),
    CONSTRAINT `fk_wallet_trans_records_wallet` FOREIGN KEY (`wallet_id`) REFERENCES `wallet` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COMMENT = '钱包交易记录表';

-- 提现记录表
CREATE TABLE IF NOT EXISTS `wallet_withdraw_records` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '提现编号',
    `wallet_id` bigint unsigned NOT NULL COMMENT '钱包编号',
    `trans_record_id` bigint unsigned DEFAULT NULL COMMENT '交易记录编号',
    `external_no` varchar(100) DEFAULT NULL COMMENT '外部交易单号',
    `withdraw_type` varchar(255) NOT NULL COMMENT '提现方式',
    `withdraw_account` varchar(100) NOT NULL COMMENT '提现账户',
    `withdraw_account_name` varchar(100) NOT NULL COMMENT '提现账户名',
    `amount` decimal(10, 2) NOT NULL COMMENT '提现金额',
    `fee` decimal(10, 2) NOT NULL DEFAULT '0.00' COMMENT '提现手续费',
    `withdraw_time` timestamp NOT NULL COMMENT '提现时间',
    `area_code` varchar(6) NOT NULL COMMENT '行政区划代码',
    `remark` varchar(255) DEFAULT NULL COMMENT '备注',
    `auditor` varchar(50) DEFAULT NULL COMMENT '审核人',
    `audit_time` timestamp NULL DEFAULT NULL COMMENT '审核时间',
    `audit_remark` varchar(255) DEFAULT NULL COMMENT '审核回执',
    `audit_state` varchar(255) NOT NULL DEFAULT 'PENDING' COMMENT '审核状态',
    `state` varchar(255) NOT NULL DEFAULT 'PROCESSING' COMMENT '状态',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    KEY `idx_wallet_id` (`wallet_id`),
    KEY `idx_trans_record_id` (`trans_record_id`),
    KEY `idx_external_no` (`external_no`),
    CONSTRAINT `fk_withdraws_wallet_id` FOREIGN KEY (`wallet_id`) REFERENCES `wallet` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE,
    CONSTRAINT `fk_withdraws_trans_record_id` FOREIGN KEY (`trans_record_id`) REFERENCES `wallet_trans_records` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '提现记录表';

-- 支付记录表
CREATE TABLE IF NOT EXISTS `wallet_payment_records` (
    `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '支付编号',
    `order_id` BIGINT UNSIGNED NOT NULL COMMENT '订单编号',
    `wallet_id` BIGINT UNSIGNED NOT NULL COMMENT '钱包编号',
    `trans_record_id` BIGINT UNSIGNED DEFAULT NULL COMMENT '交易记录编号',
    `payment_no` VARCHAR(32) NOT NULL COMMENT '交易单号',
    `payment_method` VARCHAR(32) NOT NULL COMMENT '支付方式',
    `total_amount` DECIMAL(12, 2) NOT NULL COMMENT '合计支付金额',
    `actual_amount` DECIMAL(12, 2) NOT NULL COMMENT '实际支付金额',
    `used_wallet_balance` DECIMAL(12, 2) NOT NULL COMMENT '使用钱包余额',
    `used_recharge_balance` DECIMAL(12, 2) NOT NULL COMMENT '使用充值余额',
    `payment_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '支付时间',
    `remark` VARCHAR(255) DEFAULT NULL COMMENT '备注',
    `state` VARCHAR(32) NOT NULL DEFAULT 'pending' COMMENT '状态',
    PRIMARY KEY (`id`),
    CONSTRAINT `fk_payment_order_id` FOREIGN KEY (`order_id`) REFERENCES `order_infos` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE,
    CONSTRAINT `fk_payment_wallet_id` FOREIGN KEY (`wallet_id`) REFERENCES `wallet` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE,
    CONSTRAINT `fk_payment_trans_record_id` FOREIGN KEY (`trans_record_id`) REFERENCES `wallet_trans_records` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COMMENT = '支付记录表';

-- 退款记录表
CREATE TABLE IF NOT EXISTS `wallet_refund_records` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '退款编号',
    `order_id` bigint unsigned NOT NULL COMMENT '订单编号',
    `wallet_id` bigint unsigned NOT NULL COMMENT '钱包编号',
    `trans_record_id` bigint unsigned DEFAULT NULL COMMENT '交易记录编号',
    `external_no` varchar(100) DEFAULT NULL COMMENT '外部交易单号',
    `refund_method` VARCHAR(32) NOT NULL COMMENT '退款方式',
    `total_refund_amount` decimal(12, 2) NOT NULL COMMENT '合计退款金额',
    `actual_refund_amount` decimal(12, 2) NOT NULL COMMENT '实际退款金额',
    `wallet_balance_refund_amount` decimal(12, 2) NOT NULL COMMENT '钱包余额退款金额',
    `recharge_balance_refund_amount` decimal(12, 2) NOT NULL COMMENT '充值余额退款金额',
    `refund_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '退款时间',
    `remark` varchar(255) DEFAULT NULL COMMENT '备注',
    `state` VARCHAR(32) NOT NULL DEFAULT 'pending' COMMENT '状态',
    PRIMARY KEY (`id`),
    KEY `idx_order_id` (`order_id`),
    KEY `idx_wallet_id` (`wallet_id`),
    CONSTRAINT `fk_refund_records_order_id` FOREIGN KEY (`order_id`) REFERENCES `order_infos` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT `fk_refund_records_wallet_id` FOREIGN KEY (`wallet_id`) REFERENCES `wallet` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COMMENT = '退款记录表';

-- 分账记录表
CREATE TABLE IF NOT EXISTS `wallet_split_records` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '分账记录编号',
    `order_id` bigint unsigned NOT NULL COMMENT '订单编号',
    `rule_id` bigint unsigned NOT NULL COMMENT '分账规则配置编号',
    `split_type` varchar(255) NOT NULL COMMENT '分账类型(服务佣金、平台分成、邀请奖励、团队邀请奖励、推荐奖励)',
    `amount` decimal(10, 2) NOT NULL COMMENT '参与分账金额',
    `split_ratio` decimal(5, 2) NOT NULL COMMENT '分账比例',
    `split_amount` decimal(10, 2) NOT NULL COMMENT '分账金额',
    `entry_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '入账时间',
    `remark` varchar(255) DEFAULT NULL COMMENT '入账备注',
    `state` VARCHAR(32) NOT NULL DEFAULT 'pending' COMMENT '状态',
    PRIMARY KEY (`id`),
    KEY `idx_order_id` (`order_id`),
    KEY `idx_rule_id` (`rule_id`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COMMENT = '分账记录表';

-- 营销活动表
CREATE TABLE IF NOT EXISTS `market_activities` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '营销活动编号',
    `object_id` bigint unsigned NOT NULL COMMENT '对象编号',
    `object_type` varchar(255) NOT NULL COMMENT '对象类型',
    `activity_name` varchar(255) NOT NULL COMMENT '活动名称',
    `activity_desc` text COMMENT '活动描述',
    `activity_type` varchar(255) NOT NULL COMMENT '活动类型',
    `start_time` timestamp NOT NULL COMMENT '开始时间',
    `end_time` timestamp NOT NULL COMMENT '结束时间',
    `budget_amount` decimal(10, 2) NOT NULL DEFAULT '0.00' COMMENT '预算金额',
    `join_count` int NOT NULL DEFAULT '0' COMMENT '参与数量',
    `state` varchar(255) NOT NULL DEFAULT 'ENABLE' COMMENT '状态',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    KEY `idx_object_id` (`object_id`),
    KEY `idx_object_type` (`object_type`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '营销活动表';

-- 优惠券表
CREATE TABLE IF NOT EXISTS `market_coupons` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '优惠券编号',
    `activity_id` bigint unsigned NOT NULL COMMENT '营销活动编号',
    `coupon_name` varchar(255) NOT NULL COMMENT '优惠券名称',
    `coupon_desc` text COMMENT '优惠券描述',
    `coupon_type` varchar(255) NOT NULL COMMENT '优惠券类型',
    `discount_type` varchar(255) NOT NULL COMMENT '优惠类型',
    `discount_value` decimal(10, 2) NOT NULL COMMENT '优惠值',
    `min_amount` decimal(10, 2) NOT NULL DEFAULT '0.00' COMMENT '最低消费金额',
    `valid_days` int DEFAULT NULL COMMENT '有效天数',
    `valid_start` timestamp NULL DEFAULT NULL COMMENT '有效期开始',
    `valid_end` timestamp NULL DEFAULT NULL COMMENT '有效期结束',
    `total_count` int NOT NULL DEFAULT '0' COMMENT '发放总量',
    `used_count` int NOT NULL DEFAULT '0' COMMENT '已使用数量',
    `per_limit` int NOT NULL DEFAULT '1' COMMENT '每人限领',
    `use_range` varchar(255) NOT NULL COMMENT '使用范围',
    `use_rule` json DEFAULT NULL COMMENT '使用规则',
    `sort` int NOT NULL DEFAULT '0' COMMENT '排序',
    `state` varchar(255) NOT NULL DEFAULT 'ENABLE' COMMENT '状态',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    KEY `idx_activity_id` (`activity_id`),
    CONSTRAINT `fk_coupons_activity_id` FOREIGN KEY (`activity_id`) REFERENCES `market_activities` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '优惠券表';

-- 优惠券领取记录表
CREATE TABLE IF NOT EXISTS `market_coupon_records` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '领取记录编号',
    `coupon_id` bigint unsigned NOT NULL COMMENT '优惠券编号',
    `user_id` bigint unsigned NOT NULL COMMENT '用户编号',
    `order_id` bigint unsigned DEFAULT NULL COMMENT '使用订单编号',
    `get_time` timestamp NOT NULL COMMENT '领取时间',
    `use_time` timestamp NULL DEFAULT NULL COMMENT '使用时间',
    `state` varchar(255) NOT NULL COMMENT '状态',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    KEY `idx_coupon_id` (`coupon_id`),
    KEY `idx_user_id` (`user_id`),
    KEY `idx_order_id` (`order_id`),
    CONSTRAINT `fk_coupon_records_coupon_id` FOREIGN KEY (`coupon_id`) REFERENCES `market_coupons` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT `fk_coupon_records_user_id` FOREIGN KEY (`user_id`) REFERENCES `member_users` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '优惠券领取记录表';

-- 分销团队表
CREATE TABLE IF NOT EXISTS `market_dist_teams` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '分销团队编号',
    `object_id` bigint unsigned NOT NULL COMMENT '对象编号',
    `object_type` varchar(255) NOT NULL COMMENT '对象类型',
    `user_id` bigint unsigned NOT NULL COMMENT '被邀用户编号',
    `level` varchar(255) NOT NULL DEFAULT 'LEVEL_1' COMMENT '层级',
    `state` varchar(255) NOT NULL DEFAULT 'ENABLE' COMMENT '状态',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_object_user` (
        `object_id`,
        `object_type`,
        `user_id`
    ),
    KEY `idx_user_id` (`user_id`),
    CONSTRAINT `fk_dist_teams_user_id` FOREIGN KEY (`user_id`) REFERENCES `member_users` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '分销团队表';

-- 分销规则表
CREATE TABLE IF NOT EXISTS `market_dist_rules` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '分销规则编号',
    `object_id` bigint unsigned NOT NULL COMMENT '对象编号',
    `object_type` varchar(255) NOT NULL COMMENT '对象类型',
    `rule_type` varchar(255) NOT NULL COMMENT '规则类型',
    `level` varchar(255) NOT NULL COMMENT '层级',
    `commission_rate` decimal(5, 2) NOT NULL DEFAULT '0.00' COMMENT '佣金比例',
    `state` varchar(255) NOT NULL DEFAULT 'ENABLE' COMMENT '状态',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    KEY `idx_object_id` (`object_id`),
    KEY `idx_status` (`state`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '分销规则表';

-- 分销记录表
CREATE TABLE IF NOT EXISTS `market_dist_records` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '分销记录编号',
    `team_id` bigint unsigned NOT NULL COMMENT '分销团队编号',
    `order_id` bigint unsigned NOT NULL COMMENT '订单编号',
    `commission_amount` decimal(10, 2) NOT NULL DEFAULT '0.00' COMMENT '佣金金额',
    `state` varchar(255) NOT NULL COMMENT '状态',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    KEY `idx_team_id` (`team_id`),
    KEY `idx_order_id` (`order_id`),
    CONSTRAINT `fk_dist_records_team_id` FOREIGN KEY (`team_id`) REFERENCES `market_dist_teams` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '分销记录表';

-- 项目分类表
CREATE TABLE IF NOT EXISTS `project_cate` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '项目分类编号',
    `name` varchar(255) NOT NULL COMMENT '项目分类名称',
    `cover` varchar(255) DEFAULT NULL COMMENT '分类封面',
    `sort` int DEFAULT NULL COMMENT '排序',
    `state` varchar(255) NOT NULL DEFAULT 'ENABLE' COMMENT '状态',
    PRIMARY KEY (`id`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COMMENT = '项目分类表';

-- 服务项目表
CREATE TABLE IF NOT EXISTS `project_service` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '服务项目编号',
    `cate_id` bigint unsigned NOT NULL COMMENT '项目分类编号',
    `cover` varchar(255) DEFAULT NULL COMMENT '项目封面',
    `title` varchar(255) NOT NULL COMMENT '项目标题',
    `subtitle` varchar(255) DEFAULT NULL COMMENT '项目副标题',
    `price` decimal(10, 2) NOT NULL COMMENT '项目金额',
    `original_price` decimal(10, 2) NOT NULL COMMENT '项目原价',
    `sales` int DEFAULT '0' COMMENT '虚拟销量',
    `duration` int NOT NULL COMMENT '服务时长',
    `project_desc` text COMMENT '项目介绍',
    `service_desc` text COMMENT '服务说明',
    `type` varchar(255) NOT NULL COMMENT '服务类型',
    `state` varchar(255) NOT NULL DEFAULT 'ENABLE' COMMENT '状态',
    PRIMARY KEY (`id`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COMMENT = '服务项目表';

-- 系统参数配置表
CREATE TABLE IF NOT EXISTS `sys_configs` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '参数编号',
    `param_name` varchar(255) NOT NULL COMMENT '参数名称',
    `param_key` varchar(255) NOT NULL COMMENT '参数键名',
    `param_value` varchar(255) NOT NULL COMMENT '参数键值',
    `param_type` varchar(255) NOT NULL COMMENT '参数类型',
    `is_system` varchar(255) NOT NULL DEFAULT 'NO' COMMENT '是否系统内置',
    `remark` varchar(255) DEFAULT NULL COMMENT '备注',
    `state` varchar(255) NOT NULL DEFAULT 'ENABLE' COMMENT '状态',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_param_key` (`param_key`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '系统参数配置表';

-- 业务规则配置表
CREATE TABLE IF NOT EXISTS `sys_business_rules` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '规则编号',
    `rule_code` varchar(255) NOT NULL COMMENT '规则代码',
    `rule_name` varchar(255) NOT NULL COMMENT '规则名称',
    `rule_type` varchar(255) NOT NULL COMMENT '规则类型',
    `rule_content` json NOT NULL COMMENT '规则内容',
    `state` varchar(255) NOT NULL DEFAULT 'ENABLE' COMMENT '状态',
    `remark` varchar(255) DEFAULT NULL COMMENT '备注',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_rule_code` (`rule_code`),
    KEY `idx_rule_type` (`rule_type`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '业务规则配置表';

-- 状态流转规则表
CREATE TABLE IF NOT EXISTS `sys_status_rules` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '状态流转规则编号',
    `business_type` varchar(255) NOT NULL COMMENT '业务类型',
    `current_status` varchar(255) NOT NULL COMMENT '当前状态',
    `target_status` varchar(255) NOT NULL COMMENT '目标状态',
    `is_allowed` varchar(255) NOT NULL DEFAULT 'NO' COMMENT '是否允许',
    `conditions` json DEFAULT NULL COMMENT '流转条件',
    `sort` int NOT NULL DEFAULT '0' COMMENT '排序',
    `remark` varchar(255) DEFAULT NULL COMMENT '备注',
    `state` varchar(255) NOT NULL DEFAULT 'ENABLE' COMMENT '状态',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    KEY `idx_business_type` (`business_type`),
    KEY `idx_current_status` (`current_status`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '状态流转规则表';

-- 状态变更记录表
CREATE TABLE IF NOT EXISTS `sys_status_logs` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '状态变更记录编号',
    `business_type` varchar(255) NOT NULL COMMENT '业务类型',
    `business_id` bigint unsigned NOT NULL COMMENT '业务编号',
    `from_status` varchar(255) NOT NULL COMMENT '原状态',
    `to_status` varchar(255) NOT NULL COMMENT '新状态',
    `operator_id` bigint unsigned DEFAULT NULL COMMENT '操作人编号',
    `operator_type` varchar(255) DEFAULT NULL COMMENT '操作人类型',
    `change_reason` varchar(255) DEFAULT NULL COMMENT '变更原因',
    `remark` varchar(255) DEFAULT NULL COMMENT '备注',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    KEY `idx_business_type_id` (
        `business_type`,
        `business_id`
    ),
    KEY `idx_operator_id` (`operator_id`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '状态变更记录表';

-- 定时任务表
CREATE TABLE IF NOT EXISTS `sys_scheduled_tasks` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '任务编号',
    `task_code` varchar(255) NOT NULL COMMENT '任务代码',
    `task_name` varchar(255) NOT NULL COMMENT '任务名称',
    `task_group` varchar(255) NOT NULL COMMENT '任务组',
    `task_class` varchar(255) NOT NULL COMMENT '执行类',
    `cron_expression` varchar(255) NOT NULL COMMENT 'CRON表达式',
    `task_params` json DEFAULT NULL COMMENT '执行参数',
    `concurrent` varchar(255) NOT NULL DEFAULT 'NO' COMMENT '是否并发执行',
    `error_strategy` varchar(255) NOT NULL DEFAULT 'CONTINUE' COMMENT '错误策略',
    `state` varchar(255) NOT NULL DEFAULT 'ENABLE' COMMENT '状态',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_task_code` (`task_code`),
    KEY `idx_task_group` (`task_group`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '定时任务表';

-- 任务执行日志表
CREATE TABLE IF NOT EXISTS `sys_task_logs` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '任务日志编号',
    `task_id` bigint unsigned NOT NULL COMMENT '任务编号',
    `execution_time` timestamp NOT NULL COMMENT '执行时间',
    `execution_result` varchar(255) NOT NULL COMMENT '执行结果',
    `error_message` text DEFAULT NULL COMMENT '错误信息',
    `execution_duration` int NOT NULL DEFAULT '0' COMMENT '执行时长(毫秒)',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    PRIMARY KEY (`id`),
    KEY `idx_task_id` (`task_id`),
    KEY `idx_execution_time` (`execution_time`),
    CONSTRAINT `fk_task_logs_task_id` FOREIGN KEY (`task_id`) REFERENCES `sys_scheduled_tasks` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '任务执行日志表';

-- 缓存配置表
CREATE TABLE IF NOT EXISTS `sys_cache_configs` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '缓存配置编号',
    `cache_key` varchar(255) NOT NULL COMMENT '缓存键前缀',
    `cache_type` varchar(255) NOT NULL COMMENT '缓存类型',
    `expire_time` int NOT NULL DEFAULT '3600' COMMENT '过期时间(秒)',
    `update_strategy` varchar(255) NOT NULL COMMENT '更新策略',
    `allow_null` varchar(255) NOT NULL DEFAULT 'NO' COMMENT '是否允许空值',
    `max_size` int DEFAULT NULL COMMENT '最大容量',
    `state` varchar(255) NOT NULL DEFAULT 'ENABLE' COMMENT '状态',
    `remark` varchar(255) DEFAULT NULL COMMENT '备注',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_cache_key` (`cache_key`),
    KEY `idx_cache_type` (`cache_type`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '缓存配置表';

-- 店铺表
CREATE TABLE IF NOT EXISTS `shop_infos` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '店铺编号',
    `user_id` bigint unsigned NOT NULL COMMENT '用户编号',
    `auth_record_id` bigint unsigned DEFAULT NULL COMMENT '店铺认证记录编号',
    `salesperson_id` bigint unsigned DEFAULT NULL COMMENT '业务员编号',
    `state` varchar(255) NOT NULL DEFAULT 'ENABLE' COMMENT '状态',
    PRIMARY KEY (`id`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COMMENT = '店铺表';

-- 店铺认证记录表
CREATE TABLE IF NOT EXISTS `shop_auth_records` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '店铺认证记录编号',
    `shop_id` bigint unsigned NOT NULL COMMENT '店铺编号',
    `shop_name` varchar(100) NOT NULL COMMENT '店铺名称',
    `shop_desc` text DEFAULT NULL COMMENT '店铺描述',
    `shop_address` varchar(255) NOT NULL COMMENT '店铺地址',
    `contact_phone` varchar(11) NOT NULL COMMENT '店铺联系电话',
    `contact_name` varchar(50) NOT NULL COMMENT '店铺联系人',
    `contact_id_card` varchar(18) NOT NULL COMMENT '店铺联系人身份证',
    `id_card_front_photo` varchar(255) NOT NULL COMMENT '店铺联系人身份证正面',
    `id_card_back_photo` varchar(255) NOT NULL COMMENT '店铺联系人身份证反面',
    `business_license` varchar(255) NOT NULL COMMENT '店铺营业执照',
    `license_front_photo` varchar(255) NOT NULL COMMENT '店铺营业执照正面',
    `license_back_photo` varchar(255) NOT NULL COMMENT '店铺营业执照反面',
    `storefront_photo` varchar(255) NOT NULL COMMENT '店铺门头照',
    `storefront_front_photo` varchar(255) NOT NULL COMMENT '店铺门头照正面',
    `storefront_back_photo` varchar(255) NOT NULL COMMENT '店铺门头照反面',
    `longitude` decimal(10, 7) NOT NULL COMMENT '店铺经营地址经度',
    `latitude` decimal(10, 7) NOT NULL COMMENT '店铺经营地址纬度',
    `operating_area` decimal(10, 2) NOT NULL COMMENT '店铺经营面积',
    `operating_years` int NOT NULL COMMENT '店铺经营年限',
    `technician_count` int NOT NULL COMMENT '店铺技师人数',
    `business_hours` json DEFAULT NULL COMMENT '店铺营业时间',
    `rating` decimal(3, 1) DEFAULT NULL COMMENT '评分',
    `auditor` varchar(50) DEFAULT NULL COMMENT '审核人',
    `audit_time` timestamp NULL DEFAULT NULL COMMENT '审核时间',
    `audit_remark` varchar(255) DEFAULT NULL COMMENT '审核回馈',
    `state` varchar(255) NOT NULL DEFAULT 'PENDING' COMMENT '审核状态',
    PRIMARY KEY (`id`),
    CONSTRAINT `fk_auth_record_shop` FOREIGN KEY (`shop_id`) REFERENCES `shop_infos` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COMMENT = '店铺认证记录表';

-- 店铺会员表
CREATE TABLE IF NOT EXISTS `shop_members` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '店铺会员编号',
    `shop_id` bigint unsigned NOT NULL COMMENT '店铺编号',
    `user_id` bigint unsigned NOT NULL COMMENT '用户编号',
    PRIMARY KEY (`id`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COMMENT = '店铺会员表';

-- 店铺服务项目表
CREATE TABLE IF NOT EXISTS `shop_services` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '店铺服务项目编号',
    `shop_id` bigint unsigned NOT NULL COMMENT '店铺编号',
    `service_id` bigint unsigned NOT NULL COMMENT '服务项目编号',
    `name` varchar(100) NOT NULL COMMENT '服务项目名称',
    `cover` varchar(255) DEFAULT NULL COMMENT '服务项目封面',
    `price` decimal(10, 2) NOT NULL COMMENT '服务项目价格',
    `duration` int NOT NULL COMMENT '服务时长(分钟)',
    `type` varchar(255) NOT NULL COMMENT '服务类型',
    `state` varchar(255) NOT NULL DEFAULT 'ENABLE' COMMENT '状态',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    KEY `idx_ss_shop_shop_id` (`shop_id`),
    KEY `idx_service_id` (`service_id`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '店铺服务项目表';

-- 店铺技师服务项目表
CREATE TABLE IF NOT EXISTS `shop_coach_services` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '店铺技师所属项目编号',
    `coach_id` bigint unsigned NOT NULL COMMENT '技师编号',
    `service_id` bigint unsigned NOT NULL COMMENT '店铺服务项目编号',
    `state` varchar(255) NOT NULL DEFAULT 'ENABLE' COMMENT '状态',
    PRIMARY KEY (`id`),
    CONSTRAINT `fk_shop_coach_services_coach_id` FOREIGN KEY (`coach_id`) REFERENCES `coach_users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT `fk_shop_coach_services_service_id` FOREIGN KEY (`service_id`) REFERENCES `shop_services` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COMMENT = '店铺技师开通项目表';

-- 店铺评价表
CREATE TABLE IF NOT EXISTS `shop_comments` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '店铺评价编号',
    `shop_id` bigint unsigned NOT NULL COMMENT '店铺编号',
    `user_id` bigint unsigned NOT NULL COMMENT '用户编号',
    `order_id` bigint unsigned NOT NULL COMMENT '订单编号',
    `content` text COMMENT '评价内容',
    `rating` decimal(2, 1) NOT NULL DEFAULT '5.0' COMMENT '评分',
    `comment_type` varchar(255) NOT NULL COMMENT '评价类型',
    `state` varchar(255) NOT NULL DEFAULT 'ENABLE' COMMENT '状态',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    KEY `idx_sc_shop_shop_id` (`shop_id`),
    KEY `idx_sc_muser_user_id` (`user_id`),
    KEY `idx_order_id` (`order_id`),
    CONSTRAINT `fk_shop_comments_shop_id` FOREIGN KEY (`shop_id`) REFERENCES `shop_infos` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT `fk_shop_comments_user_id` FOREIGN KEY (`user_id`) REFERENCES `member_users` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '店铺评价表';

-- 代理商表

CREATE TABLE IF NOT EXISTS `agent_infos` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '代理商编号',
    `user_id` bigint unsigned NOT NULL COMMENT '用户编号',
    `info_record_id` bigint unsigned DEFAULT NULL COMMENT '代理商信息记录编号',
    `real_auth_record_id` bigint unsigned DEFAULT NULL COMMENT '代理商实名认证记录编号',
    `qual_record_id` bigint unsigned DEFAULT NULL COMMENT '代理商资质认证记录编号',
    `state` varchar(255) NOT NULL DEFAULT 'ENABLE' COMMENT '状态',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_user_id` (`user_id`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '代理商表';

-- 代理商信息记录表
CREATE TABLE IF NOT EXISTS `agent_info_records` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '代理商信息记录编号',
    `agent_id` bigint unsigned NOT NULL COMMENT '代理商编号',
    `company_name` varchar(255) NOT NULL COMMENT '公司名称',
    `company_address` varchar(255) NOT NULL COMMENT '公司地址',
    `contact_name` varchar(50) NOT NULL COMMENT '联系人姓名',
    `contact_mobile` varchar(11) NOT NULL COMMENT '联系人手机号',
    `business_area` varchar(255) NOT NULL COMMENT '经营区域',
    `business_scope` varchar(255) NOT NULL COMMENT '经营范围',
    `auditor` varchar(50) DEFAULT NULL COMMENT '审核人',
    `audit_time` timestamp NULL DEFAULT NULL COMMENT '审核时间',
    `audit_remark` varchar(255) DEFAULT NULL COMMENT '审核回执',
    `state` varchar(255) NOT NULL DEFAULT 'PENDING' COMMENT '状态',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    KEY `idx_air_agent_agent_id` (`agent_id`),
    CONSTRAINT `fk_info_records_agent_id` FOREIGN KEY (`agent_id`) REFERENCES `agent_infos` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '代理商信息记录表';

-- 代理商实名认证记录表
CREATE TABLE IF NOT EXISTS `agent_real_auth_records` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '代理商实名认证记录编号',
    `agent_id` bigint unsigned NOT NULL COMMENT '代理商编号',
    `real_name` varchar(50) NOT NULL COMMENT '法人姓名',
    `id_card` varchar(18) NOT NULL COMMENT '法人身份证号',
    `id_card_front_photo` varchar(255) NOT NULL COMMENT '法人身份证正面照片',
    `id_card_back_photo` varchar(255) NOT NULL COMMENT '法人身份证反面照片',
    `business_license` varchar(255) NOT NULL COMMENT '营业执照照片',
    `auditor` varchar(50) DEFAULT NULL COMMENT '审核人',
    `audit_time` timestamp NULL DEFAULT NULL COMMENT '审核时间',
    `audit_remark` varchar(255) DEFAULT NULL COMMENT '审核回执',
    `state` varchar(255) NOT NULL DEFAULT 'PENDING' COMMENT '状态',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    KEY `idx_arar_agent_agent_id` (`agent_id`),
    CONSTRAINT `fk_real_auth_records_agent_id` FOREIGN KEY (`agent_id`) REFERENCES `agent_infos` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '代理商实名认证记录表';

-- 代理商资质认证记录表
CREATE TABLE IF NOT EXISTS `agent_qual_records` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '代理商资质认证记录编号',
    `agent_id` bigint unsigned NOT NULL COMMENT '代理商编号',
    `qual_type` varchar(255) NOT NULL COMMENT '资质类型',
    `qual_no` varchar(100) DEFAULT NULL COMMENT '资质证书编号',
    `qual_photo` varchar(255) NOT NULL COMMENT '资质证书照片',
    `valid_start` date DEFAULT NULL COMMENT '有效期开始日期',
    `valid_end` date DEFAULT NULL COMMENT '有效期结束日期',
    `auditor` varchar(50) DEFAULT NULL COMMENT '审核人',
    `audit_time` timestamp NULL DEFAULT NULL COMMENT '审核时间',
    `audit_remark` varchar(255) DEFAULT NULL COMMENT '审核回执',
    `state` varchar(255) NOT NULL DEFAULT 'PENDING' COMMENT '状态',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    KEY `idx_aqr_agent_agent_id` (`agent_id`),
    CONSTRAINT `fk_qual_records_agent_id` FOREIGN KEY (`agent_id`) REFERENCES `agent_infos` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '代理商资质认证记录表';

-- 代理商结算账户表
CREATE TABLE IF NOT EXISTS `agent_settlement_accounts` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '结算账户编号',
    `agent_id` bigint unsigned NOT NULL COMMENT '代理商编号',
    `account_type` varchar(255) NOT NULL COMMENT '账户类型',
    `acct_name` varchar(100) NOT NULL COMMENT '开户名',
    `acct_no` varchar(50) NOT NULL COMMENT '账号',
    `bank_name` varchar(100) DEFAULT NULL COMMENT '开户行',
    `bank_branch` varchar(100) DEFAULT NULL COMMENT '支行名称',
    `state` varchar(255) NOT NULL DEFAULT 'ENABLE' COMMENT '状态',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    KEY `idx_asa_agent_agent_id` (`agent_id`),
    CONSTRAINT `fk_settlement_accounts_agent_id` FOREIGN KEY (`agent_id`) REFERENCES `agent_infos` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '代理商结算账户表';

-- 代理商分成规则表
CREATE TABLE IF NOT EXISTS `agent_commission_rules` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '分成规则编号',
    `agent_id` bigint unsigned NOT NULL COMMENT '代理商编号',
    `rule_type` varchar(255) NOT NULL COMMENT '规则类型',
    `commission_rate` decimal(5, 2) NOT NULL DEFAULT '0.00' COMMENT '分成比例',
    `state` varchar(255) NOT NULL DEFAULT 'ENABLE' COMMENT '状态',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    KEY `idx_acr_agent_agent_id` (`agent_id`),
    CONSTRAINT `fk_commission_rules_agent_id` FOREIGN KEY (`agent_id`) REFERENCES `agent_infos` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '代理商分成规则表';

-- 代理商项目分类表
CREATE TABLE `agent_project_cate` (
    `id` bigint UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键',
    `agent_id` bigint UNSIGNED NOT NULL COMMENT '代理商ID',
    `cate_id` bigint UNSIGNED NOT NULL COMMENT '项目分类ID',
    `state` varchar(20) NOT NULL DEFAULT 'enable' COMMENT '状态(enable:启用 disable:禁用)',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    KEY `idx_agent_id` (`agent_id`),
    KEY `idx_category_id` (`cate_id`),
    KEY `idx_state` (`state`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '代理商项目分类表';

-- 代理商服务项目表
CREATE TABLE `agent_project` (
    `id` bigint UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键',
    `cate_id` bigint UNSIGNED NOT NULL COMMENT '代理商项目分类ID',
    `project_id` bigint UNSIGNED NOT NULL COMMENT '项目ID',
    `agent_id` bigint UNSIGNED NOT NULL COMMENT '代理商ID',
    `price` decimal(10, 2) NOT NULL DEFAULT 0.00 COMMENT '项目金额',
    `duration` int NOT NULL DEFAULT 0 COMMENT '服务时长(分钟)',
    `distance` int NOT NULL DEFAULT 0 COMMENT '接单距离(米)',
    `state` varchar(20) NOT NULL DEFAULT 'enable' COMMENT '状态(enable:启用 disable:禁用)',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_agent_project` (`cate_id`, `project_id`),
    KEY `idx_project_id` (`project_id`),
    KEY `idx_state` (`state`),
    CONSTRAINT `fk_apc_agent_project_cate` FOREIGN KEY (`cate_id`) REFERENCES `agent_project_cate` (`id`),
    CONSTRAINT `fk_apc_project` FOREIGN KEY (`project_id`) REFERENCES `project` (`id`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '代理商服务项目表';

-- 统计维度表
CREATE TABLE IF NOT EXISTS `stat_dimensions` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '统计维度编号',
    `dimension_code` varchar(255) NOT NULL COMMENT '维度代码',
    `dimension_name` varchar(255) NOT NULL COMMENT '维度名称',
    `dimension_type` varchar(255) NOT NULL COMMENT '维度类型',
    `state` varchar(255) NOT NULL DEFAULT 'ENABLE' COMMENT '状态',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_dimension_code` (`dimension_code`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '统计维度表';

-- 统计指标表
CREATE TABLE IF NOT EXISTS `stat_metrics` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '统计指标编号',
    `metric_code` varchar(255) NOT NULL COMMENT '指标代码',
    `metric_name` varchar(255) NOT NULL COMMENT '指标名称',
    `metric_type` varchar(255) NOT NULL COMMENT '指标类型',
    `metric_unit` varchar(255) DEFAULT NULL COMMENT '指标单位',
    `state` varchar(255) NOT NULL DEFAULT 'ENABLE' COMMENT '状态',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_metric_code` (`metric_code`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '统计指标表';

-- 统计任务表
CREATE TABLE IF NOT EXISTS `stat_tasks` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '统计任务编号',
    `task_code` varchar(255) NOT NULL COMMENT '任务代码',
    `task_name` varchar(255) NOT NULL COMMENT '任务名称',
    `task_type` varchar(255) NOT NULL COMMENT '任务类型',
    `task_cron` varchar(255) NOT NULL COMMENT '任务执行表达式',
    `task_params` json DEFAULT NULL COMMENT '任务参数',
    `state` varchar(255) NOT NULL DEFAULT 'ENABLE' COMMENT '状态',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_task_code` (`task_code`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '统计任务表';

-- 规则配置表
CREATE TABLE IF NOT EXISTS `rule_configs` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '规则编号',
    `rule_code` varchar(255) NOT NULL COMMENT '规则代码',
    `rule_name` varchar(255) NOT NULL COMMENT '规则名称',
    `rule_type` varchar(255) NOT NULL COMMENT '规则类型',
    `rule_desc` varchar(500) DEFAULT NULL COMMENT '规则描述',
    `rule_content` json NOT NULL COMMENT '规则内容',
    `priority` int NOT NULL DEFAULT '0' COMMENT '优先级',
    `state` varchar(255) NOT NULL DEFAULT 'ENABLE' COMMENT '状态',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_rule_code` (`rule_code`),
    KEY `idx_rule_type` (`rule_type`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '规则配置表';

-- 规则版本表
CREATE TABLE IF NOT EXISTS `rule_versions` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '版本编号',
    `rule_id` bigint unsigned NOT NULL COMMENT '规则编号',
    `version_no` varchar(50) NOT NULL COMMENT '版本号',
    `rule_content` json NOT NULL COMMENT '规则内容',
    `change_desc` varchar(500) DEFAULT NULL COMMENT '变更说明',
    `state` varchar(255) NOT NULL DEFAULT 'DRAFT' COMMENT '状态',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_rule_version` (`rule_id`, `version_no`),
    KEY `idx_rv_rc_rule_id` (`rule_id`),
    CONSTRAINT `fk_versions_rule_id` FOREIGN KEY (`rule_id`) REFERENCES `rule_configs` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '规则版本表';

-- 规则执行记录表
CREATE TABLE IF NOT EXISTS `rule_execution_records` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '执行记录编号',
    `rule_id` bigint unsigned NOT NULL COMMENT '规则编号',
    `version_id` bigint unsigned NOT NULL COMMENT '版本编号',
    `business_id` varchar(255) NOT NULL COMMENT '业务编号',
    `business_type` varchar(255) NOT NULL COMMENT '业务类型',
    `input_params` json DEFAULT NULL COMMENT '输入参数',
    `output_result` json DEFAULT NULL COMMENT '输出结果',
    `execution_time` timestamp NOT NULL COMMENT '执行时间',
    `exec_duration` int NOT NULL DEFAULT '0' COMMENT '执行时长(毫秒)',
    `exec_status` varchar(255) NOT NULL COMMENT '执行状态',
    `err_msg` text COMMENT '错误信息',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    KEY `idx_rer_rc_rule_id` (`rule_id`),
    KEY `idx_rer_rv_version_id` (`version_id`),
    KEY `idx_business_id` (`business_id`),
    KEY `idx_execution_time` (`execution_time`),
    KEY `idx_execution_status` (`exec_status`),
    CONSTRAINT `fk_execution_records_rule_id` FOREIGN KEY (`rule_id`) REFERENCES `rule_configs` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE,
    CONSTRAINT `fk_execution_records_version_id` FOREIGN KEY (`version_id`) REFERENCES `rule_versions` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '规则执行记录表';

-- 规则参数配置表
CREATE TABLE IF NOT EXISTS `rule_param_configs` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '参数配置编号',
    `rule_id` bigint unsigned NOT NULL COMMENT '规则编号',
    `param_code` varchar(255) NOT NULL COMMENT '参数代码',
    `param_name` varchar(255) NOT NULL COMMENT '参数名称',
    `param_type` varchar(255) NOT NULL COMMENT '参数类型',
    `param_value` varchar(500) DEFAULT NULL COMMENT '参数值',
    `param_desc` varchar(500) DEFAULT NULL COMMENT '参数描述',
    `is_required` varchar(255) NOT NULL DEFAULT 'NO' COMMENT '是否必填',
    `state` varchar(255) NOT NULL DEFAULT 'ENABLE' COMMENT '状态',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_rule_param` (`rule_id`, `param_code`),
    KEY `idx_rpc_rc_rule_id` (`rule_id`),
    CONSTRAINT `fk_param_configs_rule_id` FOREIGN KEY (`rule_id`) REFERENCES `rule_configs` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '规则参数配置表';

-- 规则依赖关系表
CREATE TABLE IF NOT EXISTS `rule_dependencies` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '依赖关系编号',
    `rule_id` bigint unsigned NOT NULL COMMENT '规则编号',
    `depend_rule_id` bigint unsigned NOT NULL COMMENT '依赖规则编号',
    `dependency_type` varchar(255) NOT NULL COMMENT '依赖类型',
    `state` varchar(255) NOT NULL DEFAULT 'ENABLE' COMMENT '状态',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_rule_dependency` (`rule_id`, `depend_rule_id`),
    KEY `idx_rd_rc_rule_id` (`rule_id`),
    KEY `idx_rd_rc_depend_rule_id` (`depend_rule_id`),
    CONSTRAINT `fk_dependencies_rule_id` FOREIGN KEY (`rule_id`) REFERENCES `rule_configs` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT `fk_dependencies_depend_rule_id` FOREIGN KEY (`depend_rule_id`) REFERENCES `rule_configs` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '规则依赖关系表';

-- 业务员表
CREATE TABLE IF NOT EXISTS `salesmen` (
    `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '业务员编号',
    `user_id` BIGINT UNSIGNED NOT NULL COMMENT '用户编号',
    `name` VARCHAR(255) NOT NULL COMMENT '姓名',
    `contact_phone` VARCHAR(11) NOT NULL COMMENT '联系电话',
    `contact_address` VARCHAR(255) NOT NULL COMMENT '联系地址',
    `id_card` VARCHAR(18) NOT NULL COMMENT '身份证',
    `id_card_front_photo` VARCHAR(255) NOT NULL COMMENT '身份证正面',
    `id_card_back_photo` VARCHAR(255) NOT NULL COMMENT '身份证反面',
    `id_card_hand_photo` VARCHAR(255) NOT NULL COMMENT '手持身份证',
    `activity_area` VARCHAR(255) NOT NULL COMMENT '活动区域',
    `invite_code` VARCHAR(50) NOT NULL COMMENT '邀请码',
    `audit_time` TIMESTAMP NULL DEFAULT NULL COMMENT '审核时间',
    `auditor` VARCHAR(50) DEFAULT NULL COMMENT '审核人',
    `audit_remark` VARCHAR(255) DEFAULT NULL COMMENT '审核回馈',
    `audit_status` ENUM('待审核', '审核通过', '审核拒绝') NOT NULL DEFAULT '待审核' COMMENT '审核状态',
    `state` ENUM('开启', '关闭') NOT NULL DEFAULT '开启' COMMENT '状态',
    `created_at` TIMESTAMP NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` TIMESTAMP NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` TIMESTAMP NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    CONSTRAINT `fk_salesmen_user_id` FOREIGN KEY (`user_id`) REFERENCES `member_users` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '业务员表';

-- 业务员变更记录表
CREATE TABLE IF NOT EXISTS `salesmen_change_records` (
    `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '业务员审核记录编号',
    `salesman_id` BIGINT UNSIGNED NOT NULL COMMENT '业务员编号',
    `change_before` TEXT NOT NULL COMMENT '变更前',
    `change_after` TEXT NOT NULL COMMENT '变更后',
    `changer` VARCHAR(255) NOT NULL COMMENT '变更人',
    `created_at` TIMESTAMP NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` TIMESTAMP NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` TIMESTAMP NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    CONSTRAINT `fk_change_records_salesman_id` FOREIGN KEY (`salesman_id`) REFERENCES `salesmen` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '业务员变更记录表';

-- 技师报警表
CREATE TABLE IF NOT EXISTS `coach_alerts` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '技师报警编号',
    `coach_id` bigint unsigned NOT NULL COMMENT '技师编号',
    `order_id` bigint unsigned NOT NULL COMMENT '订单编号',
    `alert_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '报警时间',
    `alert_type` varchar(255) NOT NULL COMMENT '报警类型',
    `alert_reason` varchar(255) NOT NULL COMMENT '报警原因',
    `alert_status` varchar(255) NOT NULL DEFAULT 'PENDING' COMMENT '报警状态',
    `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
    `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
    `deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    KEY `idx_coach_id` (`coach_id`),
    KEY `idx_order_id` (`order_id`),
    CONSTRAINT `fk_alerts_coach_id` FOREIGN KEY (`coach_id`) REFERENCES `coach_users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT `fk_alerts_order_id` FOREIGN KEY (`order_id`) REFERENCES `order_infos` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '技师报警表';

-- 技师变更记录表
CREATE TABLE IF NOT EXISTS `coach_change_records` (
    `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '技师变更记录编号',
    `order_id` BIGINT UNSIGNED NOT NULL COMMENT '订单编号',
    `coach_id_before` BIGINT UNSIGNED NOT NULL COMMENT '技师编号(变更前)',
    `coach_id_after` BIGINT UNSIGNED NOT NULL COMMENT '技师编号(变更后)',
    `change_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '变更时间',
    PRIMARY KEY (`id`),
    CONSTRAINT `fk_ccr_order_id` FOREIGN KEY (`order_id`) REFERENCES `order_infos` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT `fk_ccr_coach_id_before` FOREIGN KEY (`coach_id_before`) REFERENCES `coach_users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT `fk_ccr_coach_id_after` FOREIGN KEY (`coach_id_after`) REFERENCES `coach_users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '技师变更记录表';

-- 代理商项目配置表
CREATE TABLE agent_project_config (
    `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '服务范围编号',
    `agent_id` BIGINT UNSIGNED NULL COMMENT '代理商编号',
    `min_fee` DECIMAL(10, 2) NOT NULL DEFAULT 9 COMMENT '服务的起步价',
    `min_distance` INT NOT NULL DEFAULT 3 COMMENT '最小距离',
    `per_km_fee` DECIMAL(10, 2) NOT NULL DEFAULT 3 COMMENT '每公里的费用',
    `state` VARCHAR(50) NOT NULL COMMENT '状态',
    PRIMARY KEY (`id`),
    CONSTRAINT `fk_apc_agent_id` FOREIGN KEY (`agent_id`) REFERENCES `agent_infos` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '代理商项目配置表';
