| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- <?php
- namespace App\Module\Mex\Logic;
- use App\Module\Mex\Models\MexMatchLog;
- use App\Module\Mex\Enums\MatchType;
- /**
- * 农贸市场撮合日志逻辑
- *
- * 处理撮合日志相关的核心业务逻辑
- */
- class MexMatchLogLogic
- {
- /**
- * 记录撮合日志
- *
- * @param MatchType $matchType 撮合类型
- * @param int $itemId 商品ID
- * @param int $batchSize 批处理大小
- * @param array $result 撮合结果
- * @param int|null $executionTimeMs 执行时间(毫秒)
- * @param string|null $errorMessage 错误消息
- * @return MexMatchLog 撮合日志记录
- */
- public static function logMatch(
- MatchType $matchType,
- int $itemId,
- int $batchSize,
- array $result,
- ?int $executionTimeMs = null,
- ?string $errorMessage = null
- ): MexMatchLog {
- return MexMatchLog::create([
- 'match_type' => $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();
- }
- }
|