where('id', $recordId)->find(); if (!$record) { return; } $status = (int) ($record['status'] ?? 0); $now = time(); if ($status !== 4) { Db::name('game_record')->where('id', $recordId)->update([ 'platform_profit_amount' => '0.0000', 'winner_user_count' => 0, 'update_time' => $now, ]); return; } $resultRaw = $record['result_number'] ?? null; if ($resultRaw === null || $resultRaw === '') { return; } $resultNum = (int) $resultRaw; $bets = Db::name('bet_order')->where('period_id', $recordId)->select()->toArray(); $totalBet = '0.0000'; $totalPayout = '0.0000'; $winnerUserIds = []; foreach ($bets as $bet) { $st = (int) ($bet['status'] ?? 0); if ($st === 3) { continue; } $tb = (string) ($bet['total_amount'] ?? '0'); $totalBet = bcadd($totalBet, $tb, 4); if ($st === 2) { $payout = bcadd((string) ($bet['win_amount'] ?? '0'), (string) ($bet['jackpot_extra_amount'] ?? '0'), 4); } else { $payout = self::estimatePayoutForBet($bet, $resultNum); } $totalPayout = bcadd($totalPayout, $payout, 4); if (bccomp($payout, '0', 4) > 0) { $uid = (int) ($bet['user_id'] ?? 0); if ($uid > 0) { $winnerUserIds[$uid] = true; } } } $profit = bcsub($totalBet, $totalPayout, 4); Db::name('game_record')->where('id', $recordId)->update([ 'platform_profit_amount' => $profit, 'winner_user_count' => count($winnerUserIds), 'update_time' => $now, ]); } /** * 与 GameLiveService::estimateLossForNumber 中单注派彩一致:命中号码时 unit × (streak+1) × 33。 */ private static function estimatePayoutForBet(array $bet, int $resultNumber): string { $pickNumbers = $bet['pick_numbers'] ?? null; if (is_string($pickNumbers)) { $decoded = json_decode($pickNumbers, true); $pickNumbers = is_array($decoded) ? $decoded : []; } if (!is_array($pickNumbers)) { $pickNumbers = []; } if (!in_array($resultNumber, array_map('intval', $pickNumbers), true)) { return '0.0000'; } $unit = (string) ($bet['unit_amount'] ?? '0'); $streak = (int) ($bet['streak_at_bet'] ?? 0); $odds = (string) (($streak + 1) * self::BASE_ODDS); return bcmul($unit, $odds, 4); } }