$matchType, 'item_id' => $itemId, 'batch_size' => $batchSize, 'matched_orders' => $result['matched_orders'] ?? 0, 'total_amount' => $result['total_amount'] ?? '0.00000', 'success' => $result['success'] ?? false, 'message' => $result['message'] ?? '', 'execution_time_ms' => $executionTimeMs, 'error_message' => $errorMessage, ]); } /** * 获取最后撮合时间信息 * * @return array 最后撮合时间信息 */ public static function getLastMatchTimes(): array { // 获取最后的卖出撮合时间 $lastSellMatch = MexMatchLog::where('match_type', MatchType::USER_SELL) ->orderBy('created_at', 'desc') ->first(); // 获取最后的买入撮合时间 $lastBuyMatch = MexMatchLog::where('match_type', MatchType::USER_BUY) ->orderBy('created_at', 'desc') ->first(); return [ 'last_sell_match_time' => $lastSellMatch ? $lastSellMatch->created_at : null, 'last_buy_match_time' => $lastBuyMatch ? $lastBuyMatch->created_at : null, ]; } /** * 获取撮合统计信息 * * @param int $days 统计天数 * @return array 统计信息 */ public static function getMatchStats(int $days = 7): array { $startDate = now()->subDays($days)->startOfDay(); // 总体统计 $totalStats = MexMatchLog::where('created_at', '>=', $startDate) ->selectRaw(' COUNT(*) as total_matches, SUM(matched_orders) as total_matched_orders, SUM(total_amount) as total_amount, AVG(execution_time_ms) as avg_execution_time, SUM(CASE WHEN success = 1 THEN 1 ELSE 0 END) as successful_matches ') ->first(); // 按类型统计 $typeStats = MexMatchLog::where('created_at', '>=', $startDate) ->groupBy('match_type') ->selectRaw(' match_type, COUNT(*) as count, SUM(matched_orders) as matched_orders, SUM(total_amount) as amount, SUM(CASE WHEN success = 1 THEN 1 ELSE 0 END) as successful_count ') ->get() ->keyBy('match_type'); return [ 'period' => $days, 'start_date' => $startDate, 'end_date' => now(), 'total' => [ 'matches' => $totalStats->total_matches ?? 0, 'matched_orders' => $totalStats->total_matched_orders ?? 0, 'total_amount' => $totalStats->total_amount ?? '0.00000', 'avg_execution_time' => $totalStats->avg_execution_time ? round($totalStats->avg_execution_time, 2) : 0, 'successful_matches' => $totalStats->successful_matches ?? 0, 'success_rate' => $totalStats->total_matches > 0 ? round(($totalStats->successful_matches / $totalStats->total_matches) * 100, 2) : 0, ], 'by_type' => [ 'USER_BUY' => [ 'matches' => $typeStats['USER_BUY']->count ?? 0, 'matched_orders' => $typeStats['USER_BUY']->matched_orders ?? 0, 'amount' => $typeStats['USER_BUY']->amount ?? '0.00000', 'successful_count' => $typeStats['USER_BUY']->successful_count ?? 0, ], 'USER_SELL' => [ 'matches' => $typeStats['USER_SELL']->count ?? 0, 'matched_orders' => $typeStats['USER_SELL']->matched_orders ?? 0, 'amount' => $typeStats['USER_SELL']->amount ?? '0.00000', 'successful_count' => $typeStats['USER_SELL']->successful_count ?? 0, ], ], ]; } /** * 获取商品撮合历史 * * @param int $itemId 商品ID * @param int $limit 限制数量 * @return array 撮合历史 */ public static function getItemMatchHistory(int $itemId, int $limit = 20): array { $logs = MexMatchLog::where('item_id', $itemId) ->orderBy('created_at', 'desc') ->limit($limit) ->get(); return $logs->toArray(); } }