$periods settlement_periods 行,须含 id、period_start、period_end、admin_site_id * @return array */ public function countsForPeriods(Collection $periods): array { if ($periods->isEmpty()) { return []; } $siteIds = $periods->pluck('admin_site_id')->map(static fn ($id): int => (int) $id)->unique()->all(); $siteCodes = DB::table('admin_sites') ->whereIn('id', $siteIds) ->pluck('code', 'id'); $out = []; foreach ($periods as $period) { $periodId = (int) $period->id; $siteCode = (string) ($siteCodes[(int) $period->admin_site_id] ?? ''); if ($siteCode === '') { $out[$periodId] = ['credit_ledger_count' => 0, 'share_ledger_count' => 0]; continue; } $start = Carbon::parse($period->period_start)->startOfDay(); $end = Carbon::parse($period->period_end)->endOfDay(); $creditCount = (int) DB::table('credit_ledger as cl') ->join('players as p', function ($join): void { $join->on('p.id', '=', 'cl.owner_id') ->where('cl.owner_type', '=', 'player'); }) ->where('p.site_code', $siteCode) ->where('p.funding_mode', PlayerFundingMode::CREDIT) ->whereBetween('cl.created_at', [$start, $end]) ->count(); $shareCount = (int) DB::table('share_ledger as sl') ->join('players as p', 'p.id', '=', 'sl.player_id') ->where('p.site_code', $siteCode) ->whereBetween('sl.settled_at', [$start, $end]) ->count(); $out[$periodId] = [ 'credit_ledger_count' => $creditCount, 'share_ledger_count' => $shareCount, ]; } return $out; } }