-- ******************************************************************
-- 表 kku_mex_orders 的创建SQL
-- 对应的Model: App\Module\Mex\Models\MexOrder
-- 警告: 此文件由系统自动生成，禁止修改！
-- ******************************************************************

CREATE TABLE `kku_mex_orders` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '订单ID，主键',
  `user_id` bigint(20) NOT NULL COMMENT '用户ID',
  `item_id` int(11) NOT NULL COMMENT '商品ID，关联物品表',
  `currency_type` int(11) NOT NULL DEFAULT '2' COMMENT '币种类型，关联FUND_CURRENCY_TYPE枚举，默认2为钻石',
  `order_type` enum('BUY','SELL') CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '订单类型：BUY买入，SELL卖出',
  `quantity` int(11) NOT NULL COMMENT '订单数量',
  `price` decimal(25,9) NOT NULL COMMENT '订单价格，支持5位小数',
  `total_amount` decimal(25,9) NOT NULL COMMENT '订单总金额',
  `status` enum('PENDING','COMPLETED','CANCELLED','FAILED') CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'PENDING' COMMENT '订单状态：PENDING等待，COMPLETED完成，CANCELLED取消，FAILED失败',
  `frozen_amount` decimal(25,9) DEFAULT '0.000000000' COMMENT '冻结金额（买单使用）',
  `completed_quantity` int(11) DEFAULT '0' COMMENT '已成交数量',
  `completed_amount` decimal(25,9) DEFAULT '0.000000000' COMMENT '已成交金额',
  `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `completed_at` timestamp NULL DEFAULT NULL COMMENT '完成时间',
  `failed_reason` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT '失败原因',
  `last_match_failure_reason` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT '最后无法成交原因，记录撮合过程中无法成交的具体原因',
  PRIMARY KEY (`id`) USING BTREE,
  KEY `idx_user_id` (`user_id`) USING BTREE,
  KEY `idx_item_id` (`item_id`) USING BTREE,
  KEY `idx_order_type` (`order_type`) USING BTREE,
  KEY `idx_status` (`status`) USING BTREE,
  KEY `idx_created_at` (`created_at`) USING BTREE,
  KEY `idx_status_price_created` (`status`,`price`,`created_at`) USING BTREE COMMENT '撮合排序复合索引',
  KEY `idx_currency_type` (`currency_type`) USING BTREE COMMENT '币种类型索引'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC COMMENT='农贸市场订单表';
