-- 京东Cookie管理模块数据库表结构
-- 更新时间：2025-10-18
-- 说明：从MySQL数据库(kami)同步的实际表结构

-- 1. Cookie账户表
DROP TABLE IF EXISTS `jd_cookie_account`;

CREATE TABLE `jd_cookie_account` (
    `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    `cookie_id` varchar(64) NOT NULL COMMENT 'Cookie唯一标识',
    `cookie_value` text NOT NULL COMMENT 'Cookie内容',
    `account_name` varchar(100) DEFAULT NULL COMMENT '账户名称',
    `status` tinyint NOT NULL DEFAULT 1 COMMENT '状态：1正常 2暂停 3失效',
    `failure_count` int DEFAULT 0 COMMENT '连续失败次数',
    `last_used_at` datetime DEFAULT NULL COMMENT '最后使用时间',
    `suspend_until` datetime DEFAULT NULL COMMENT '暂停解除时间',
    `remark` varchar(500) 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_cookie_id` (`cookie_id`),
    KEY `idx_status` (`status`),
    KEY `idx_last_used` (`last_used_at`),
    KEY `idx_suspend_until` (`suspend_until`),
    KEY `idx_deleted_at` (`deleted_at`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '京东Cookie账户表';

-- 2. 京东订单表
DROP TABLE IF EXISTS `jd_cookie_jd_order`;

CREATE TABLE `jd_cookie_jd_order` (
    `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    `jd_order_id` varchar(64) NOT NULL COMMENT '京东订单号',
    `real_jd_order_id` varchar(128) DEFAULT NULL COMMENT '京东客户端返回的真实订单ID',
    `pay_id` varchar(64) NOT NULL COMMENT '支付ID',
    `amount`           decimal(10, 2) NOT NULL COMMENT '订单金额',
    `category` varchar(50) NOT NULL COMMENT '商品品类',
    `cookie_id` varchar(64) NOT NULL COMMENT '使用的Cookie ID',
    `status` tinyint NOT NULL DEFAULT 1 COMMENT '状态：1待支付 2已支付 3已过期 4已取消',
    `wx_pay_url` text DEFAULT NULL COMMENT '微信支付链接',
    `wx_pay_expire_at` datetime DEFAULT NULL COMMENT '微信支付链接过期时间',
    `order_expire_at` datetime NOT NULL COMMENT '订单过期时间(默认24小时)',
    `order_id`         varchar(64)  DEFAULT NULL COMMENT '关联的用户订单号',
    `paid_at` datetime DEFAULT NULL COMMENT '支付完成时间',
    `card_no` varchar(100) DEFAULT NULL COMMENT '卡号',
    `card_password` varchar(100) DEFAULT NULL COMMENT '卡密',
    `card_extracted_at` datetime 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 `uk_jd_order_id` (`jd_order_id`),
    KEY `idx_status` (`status`),
    KEY `idx_cookie_id` (`cookie_id`),
    KEY `idx_order_expire` (`order_expire_at`),
    KEY                `idx_current_order` (`order_id`),
    KEY `idx_deleted_at` (`deleted_at`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '京东订单表';

-- 3. 订单表
DROP TABLE IF EXISTS `jd_cookie_order`;

CREATE TABLE `jd_cookie_order` (
    `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    `order_id` varchar(64) NOT NULL COMMENT '订单号',
    `user_order_id` varchar(64) DEFAULT NULL COMMENT '用户订单号',
    `amount`        decimal(10, 2) NOT NULL COMMENT '订单金额',
    `category` varchar(50) NOT NULL COMMENT '商品品类',
    `jd_order_id` varchar(64) DEFAULT NULL COMMENT '关联的京东订单号',
    `status` tinyint NOT NULL DEFAULT 1 COMMENT '状态：1待支付 2已支付 3已过期 4已取消',
    `last_request_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP 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_id` (`order_id`),
    KEY `idx_status` (`status`),
    KEY `idx_jd_order_id` (`jd_order_id`),
    KEY `idx_last_request` (`last_request_at`),
    KEY             `idx_user_order_id` (`user_order_id`),
    KEY `idx_deleted_at` (`deleted_at`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '订单表';

-- 4. Cookie变更历史表
DROP TABLE IF EXISTS `jd_cookie_change_history`;

CREATE TABLE `jd_cookie_change_history` (
    `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    `history_uuid` varchar(36) NOT NULL COMMENT '历史记录唯一标识',
    `cookie_id` varchar(64) NOT NULL COMMENT 'Cookie ID',
    `change_type` varchar(20) NOT NULL COMMENT '变更类型：create,suspend,resume,fail,use,refresh_fail,replaced',
    `status_before` tinyint DEFAULT NULL COMMENT '变更前状态',
    `status_after` tinyint DEFAULT NULL COMMENT '变更后状态',
    `order_id` varchar(64) DEFAULT NULL COMMENT '关联的订单号',
    `user_order_id` varchar(64) 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`),
    UNIQUE KEY `uk_history_uuid` (`history_uuid`),
    KEY `idx_cookie_id` (`cookie_id`),
    KEY `idx_change_type` (`change_type`),
    KEY      `idx_user_order_id` (`user_order_id`),
    KEY `idx_created_at` (`created_at`),
    KEY `idx_deleted_at` (`deleted_at`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = 'Cookie变更历史表';

-- 5. 京东订单变更历史表
DROP TABLE IF EXISTS `jd_cookie_jd_order_change_history`;

CREATE TABLE `jd_cookie_jd_order_change_history` (
    `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    `history_uuid` varchar(36) NOT NULL COMMENT '历史记录唯一标识',
    `jd_order_id` varchar(64) NOT NULL COMMENT '京东订单号',
    `change_type` varchar(20) NOT NULL COMMENT '变更类型：create,bind,unbind,pay,expire,invalid,replace',
    `order_id` varchar(64) DEFAULT NULL COMMENT '关联的订单号',
    `wx_pay_url` 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_history_uuid` (`history_uuid`),
    KEY `idx_jd_order_id` (`jd_order_id`),
    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 = '京东订单变更历史表';

-- 6. 订单变更历史表
DROP TABLE IF EXISTS `jd_cookie_order_change_history`;

CREATE TABLE `jd_cookie_order_change_history` (
    `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    `history_uuid` varchar(36) NOT NULL COMMENT '历史记录唯一标识',
    `order_id` varchar(64) NOT NULL COMMENT '订单号',
    `change_type` varchar(20) NOT NULL COMMENT '变更类型：create,bind,pay,expire,rebind',
    `jd_order_id` varchar(64) 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_history_uuid` (`history_uuid`),
    KEY `idx_order_id` (`order_id`),
    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 = '订单变更历史表';