-- 骆驼加油订单处理模块数据库表结构
-- 创建时间：2025-11-18
-- 说明：骆驼加油平台订单处理和账号管理

-- 1. 骆驼加油账号表
DROP TABLE IF EXISTS `camel_oil_account`;

CREATE TABLE `camel_oil_account` (
    `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    `account_name` varchar(128) DEFAULT NULL COMMENT '账号名称（备注）',
    `phone` varchar(20) DEFAULT NULL COMMENT '手机号（登录后记录，不可重复）',
    `token` text DEFAULT NULL COMMENT '登录Token',
    `status` tinyint NOT NULL DEFAULT 1 COMMENT '状态：1待登录 2在线 3暂停 4已失效 5登录失败',
    `token_expire_at` datetime DEFAULT NULL COMMENT 'Token过期时间',
    `last_login_at` datetime DEFAULT NULL COMMENT '最后登录时间',
    `last_used_at` datetime DEFAULT NULL COMMENT '最后使用时间',
    `daily_order_count` int NOT NULL DEFAULT 0 COMMENT '当日已下单数量',
    `daily_order_date` date DEFAULT NULL COMMENT '当日订单日期',
    `total_order_count` int NOT NULL DEFAULT 0 COMMENT '累计下单数量',
    `failure_reason` text DEFAULT NULL COMMENT '失败原因',
    `remark` text DEFAULT NULL COMMENT '备注信息',
    `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    `deleted_at` datetime DEFAULT NULL COMMENT '删除时间（软删除）',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_phone` (`phone`),
    KEY `idx_status` (`status`),
    KEY `idx_token_expire` (`token_expire_at`),
    KEY `idx_daily_order` (`daily_order_date`, `daily_order_count`),
    KEY `idx_last_used` (`last_used_at`),
    KEY `idx_deleted_at` (`deleted_at`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '骆驼加油账号表';

-- 2. 骆驼加油订单表
DROP TABLE IF EXISTS `camel_oil_order`;

CREATE TABLE `camel_oil_order` (
    `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    `order_no` varchar(64) NOT NULL COMMENT '系统订单号',
    `merchant_order_id` varchar(128) DEFAULT NULL COMMENT '商户订单号',
    `account_id` bigint NOT NULL COMMENT '使用的账号ID',
    `account_name` varchar(128) DEFAULT NULL COMMENT '账号名称',
    `platform_order_no` varchar(128) DEFAULT NULL COMMENT '骆驼平台订单号',
    `amount` decimal(10,2) NOT NULL COMMENT '订单金额',
    `alipay_url` text DEFAULT NULL COMMENT '支付宝支付链接',
    `card_password` varchar(256) DEFAULT NULL COMMENT '卡密',
    `card_number` varchar(256) DEFAULT NULL COMMENT '卡号',
    `status` tinyint NOT NULL DEFAULT 1 COMMENT '状态：1待支付 2已支付 3支付超时 4下单失败',
    `pay_status` tinyint NOT NULL DEFAULT 0 COMMENT '支付状态：0未支付 1已支付 2超时',
    `notify_status` tinyint NOT NULL DEFAULT 0 COMMENT '回调状态：0未回调 1已回调 2回调失败',
    `notify_count` int NOT NULL DEFAULT 0 COMMENT '回调次数',
    `last_check_at` datetime DEFAULT NULL COMMENT '最后检测支付时间',
    `paid_at` datetime DEFAULT NULL COMMENT '支付完成时间',
    `attach` text DEFAULT NULL COMMENT '附加信息',
    `failure_reason` text DEFAULT NULL COMMENT '失败原因',
    `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    `deleted_at` datetime DEFAULT NULL COMMENT '删除时间（软删除）',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_order_no` (`order_no`),
    KEY `idx_merchant_order_id` (`merchant_order_id`),
    KEY `idx_account_id` (`account_id`),
    CONSTRAINT `fk_camel_oil_order_account_id` FOREIGN KEY (`account_id`) REFERENCES `camel_oil_account` (`id`) ON DELETE RESTRICT,
    KEY `idx_platform_order_no` (`platform_order_no`),
    KEY `idx_status` (`status`),
    KEY `idx_pay_status` (`pay_status`),
    KEY `idx_notify_status` (`notify_status`),
    KEY `idx_created_at` (`created_at`),
    KEY `idx_account_status` (`account_id`, `status`),
    KEY `idx_card_password` (`card_password`),
    KEY `idx_card_number` (`card_number`),
    KEY `idx_deleted_at` (`deleted_at`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '骆驼加油订单表';

-- 3. 骆驼加油账号历史表
DROP TABLE IF EXISTS `camel_oil_account_history`;

CREATE TABLE `camel_oil_account_history` (
    `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    `account_id` bigint NOT NULL COMMENT '账号ID',
    `change_type` varchar(32) NOT NULL COMMENT '变更类型：create/login/offline/login_fail/pause/resume/invalidate/order_bind/order_complete/update/delete',
    `status_before` tinyint DEFAULT NULL COMMENT '变更前状态',
    `status_after` tinyint DEFAULT NULL COMMENT '变更后状态',
    `failure_count` int DEFAULT NULL COMMENT '失败次数',
    `remark` text DEFAULT NULL COMMENT '备注',
    `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    `deleted_at` datetime DEFAULT NULL COMMENT '删除时间（软删除）',
    PRIMARY KEY (`id`),
    KEY `idx_account_id` (`account_id`),
    CONSTRAINT `fk_camel_oil_account_history_account_id` FOREIGN KEY (`account_id`) REFERENCES `camel_oil_account` (`id`) ON DELETE CASCADE,
    KEY `idx_change_type` (`change_type`),
    KEY `idx_created_at` (`created_at`),
    KEY `idx_deleted_at` (`deleted_at`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '骆驼加油账号历史表';

-- 4. 骆驼加油订单历史表
DROP TABLE IF EXISTS `camel_oil_order_history`;

CREATE TABLE `camel_oil_order_history` (
    `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    `order_no` varchar(64) NOT NULL COMMENT '订单号',
    `change_type` varchar(32) NOT NULL COMMENT '变更类型：create/submit/get_pay_url/check_pay/paid/timeout/fail/callback_success/callback_fail',
    `account_id` bigint DEFAULT NULL COMMENT '关联账号ID',
    `account_name` varchar(128) DEFAULT NULL COMMENT '账号名称',
    `raw_data` text DEFAULT NULL COMMENT '原始响应数据',
    `remark` text DEFAULT NULL COMMENT '备注',
    `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    `deleted_at` datetime DEFAULT NULL COMMENT '删除时间（软删除）',
    PRIMARY KEY (`id`),
    KEY `idx_order_no` (`order_no`),
    KEY `idx_account_id` (`account_id`),
    CONSTRAINT `fk_camel_oil_order_history_account_id` FOREIGN KEY (`account_id`) REFERENCES `camel_oil_account` (`id`) ON DELETE SET NULL,
    KEY `idx_change_type` (`change_type`),
    KEY `idx_created_at` (`created_at`),
    KEY `idx_deleted_at` (`deleted_at`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '骆驼加油订单历史表';

-- 5. 骆驼加油预拉取订单表
DROP TABLE IF EXISTS `camel_oil_prefetch_order`;

CREATE TABLE `camel_oil_prefetch_order` (
    `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    `account_id` bigint NOT NULL COMMENT '拉取时使用的账号ID',
    `account_name` varchar(128) DEFAULT NULL COMMENT '账号名称',
    `amount` decimal(10,2) NOT NULL COMMENT '预拉取订单金额',
    `platform_order_no` varchar(128) DEFAULT NULL COMMENT '骆驼平台订单号',
    `alipay_url` text DEFAULT NULL COMMENT '支付宝支付链接',
    `status` tinyint NOT NULL DEFAULT 1 COMMENT '预拉取订单状态：1待匹配 2已匹配 3已过期 4已失效',
    `order_id` bigint DEFAULT NULL COMMENT '匹配后的订单ID（关联camel_oil_order表）',
    `matched_at` datetime DEFAULT NULL COMMENT '匹配时间',
    `expire_at` datetime DEFAULT NULL COMMENT '预拉取订单过期时间（通常为24小时后）',
    `failure_reason` text DEFAULT NULL COMMENT '失败原因',
    `remark` text DEFAULT NULL COMMENT '备注信息',
    `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    `deleted_at` datetime DEFAULT NULL COMMENT '删除时间（软删除）',
    PRIMARY KEY (`id`),
    KEY `idx_account_id` (`account_id`),
    CONSTRAINT `fk_camel_oil_prefetch_order_account_id` FOREIGN KEY (`account_id`) REFERENCES `camel_oil_account` (`id`) ON DELETE RESTRICT,
    KEY `idx_status` (`status`),
    KEY `idx_status_expire` (`status`, `expire_at`),
    KEY `idx_order_id` (`order_id`),
    CONSTRAINT `fk_camel_oil_prefetch_order_order_id` FOREIGN KEY (`order_id`) REFERENCES `camel_oil_order` (`id`) ON DELETE SET NULL,
    KEY `idx_platform_order_no` (`platform_order_no`),
    KEY `idx_created_at` (`created_at`),
    KEY `idx_deleted_at` (`deleted_at`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '骆驼加油预拉取订单表';

-- 6. 骆驼加油预拉取订单历史表
DROP TABLE IF EXISTS `camel_oil_prefetch_order_history`;

CREATE TABLE `camel_oil_prefetch_order_history` (
    `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    `prefetch_id` bigint NOT NULL COMMENT '预拉取订单ID',
    `change_type` varchar(32) NOT NULL COMMENT '变更类型：create/fetch/match/expire/invalidate',
    `account_id` bigint DEFAULT NULL COMMENT '关联账号ID',
    `account_name` varchar(128) DEFAULT NULL COMMENT '账号名称',
    `raw_data` text DEFAULT NULL COMMENT '原始响应数据',
    `remark` text DEFAULT NULL COMMENT '备注',
    `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    `deleted_at` datetime DEFAULT NULL COMMENT '删除时间（软删除）',
    PRIMARY KEY (`id`),
    KEY `idx_prefetch_id` (`prefetch_id`),
    CONSTRAINT `fk_camel_oil_prefetch_order_history_prefetch_id` FOREIGN KEY (`prefetch_id`) REFERENCES `camel_oil_prefetch_order` (`id`) ON DELETE CASCADE,
    KEY `idx_account_id` (`account_id`),
    CONSTRAINT `fk_camel_oil_prefetch_order_history_account_id` FOREIGN KEY (`account_id`) REFERENCES `camel_oil_account` (`id`) ON DELETE SET NULL,
    KEY `idx_change_type` (`change_type`),
    KEY `idx_created_at` (`created_at`),
    KEY `idx_deleted_at` (`deleted_at`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '骆驼加油预拉取订单历史表';

-- 7. 骆驼加油 Token 管理表
DROP TABLE IF EXISTS `camel_oil_token`;

CREATE TABLE `camel_oil_token` (
    `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    `token_name` varchar(128) NOT NULL COMMENT 'Token名称/标识',
    `token_value` text NOT NULL COMMENT 'Token具体值',
    `phone` varchar(20) DEFAULT NULL COMMENT '绑定的手机号',
    `status` tinyint NOT NULL DEFAULT 1 COMMENT '状态：1可用 2已禁用 3已过期',
    `bind_count` int NOT NULL DEFAULT 0 COMMENT '已绑定卡密数量',
    `total_bind_amount` decimal(15,2) NOT NULL DEFAULT 0 COMMENT '累计绑定金额',
    `last_bind_at` datetime DEFAULT NULL COMMENT '最后绑定时间',
    `last_used_at` datetime DEFAULT NULL COMMENT '最后使用时间',
    `remark` text DEFAULT NULL COMMENT '备注',
    `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    `deleted_at` datetime DEFAULT NULL COMMENT '删除时间（软删除）',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_token_value` (`token_value`(255)),
    KEY `idx_token_name` (`token_name`),
    KEY `idx_phone` (`phone`),
    KEY `idx_status` (`status`),
    KEY `idx_bind_count` (`bind_count`),
    KEY `idx_total_bind_amount` (`total_bind_amount`),
    KEY `idx_last_bind_at` (`last_bind_at`),
    KEY `idx_deleted_at` (`deleted_at`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '骆驼加油Token管理表';

-- 8. 骆驼加油卡密绑定记录表
DROP TABLE IF EXISTS `camel_oil_card_binding`;

CREATE TABLE `camel_oil_card_binding` (
    `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    `token_id` bigint NOT NULL COMMENT 'Token ID',
    `order_id` bigint NOT NULL COMMENT '订单ID',
    `card_number` varchar(256) DEFAULT NULL COMMENT '卡号',
    `card_password` varchar(256) DEFAULT NULL COMMENT '卡密',
    `amount` decimal(10,2) NOT NULL COMMENT '绑定金额',
    `remark` text DEFAULT NULL COMMENT '备注',
    `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    `deleted_at` datetime DEFAULT NULL COMMENT '删除时间（软删除）',
    PRIMARY KEY (`id`),
    KEY `idx_token_id` (`token_id`),
    CONSTRAINT `fk_camel_oil_card_binding_token_id` FOREIGN KEY (`token_id`) REFERENCES `camel_oil_token` (`id`) ON DELETE RESTRICT,
    KEY `idx_order_id` (`order_id`),
    CONSTRAINT `fk_camel_oil_card_binding_order_id` FOREIGN KEY (`order_id`) REFERENCES `camel_oil_order` (`id`) ON DELETE CASCADE,
    KEY `idx_amount` (`amount`),
    KEY `idx_created_at` (`created_at`),
    KEY `idx_deleted_at` (`deleted_at`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '骆驼加油卡密绑定记录表';
