|
|
@@ -252,37 +252,29 @@ class AgentReportService
|
|
|
$total = ['bet_count' => 0, 'bet_amount' => '0.0000', 'valid_bet_amount' => '0.0000', 'win_loss' => '0.0000'];
|
|
|
$sources = [];
|
|
|
if (Schema::hasTable('bets')) {
|
|
|
- $sources[] = DB::table('bets')->whereIn('member_id', clone $memberIds)->where('status', 2)
|
|
|
- ->whereBetween('created_at', [$start, $end])
|
|
|
- ->selectRaw('COUNT(*) bet_count, COALESCE(SUM(amount),0) bet_amount, COALESCE(SUM(amount),0) valid_bet_amount, COALESCE(SUM(profit - amount),0) win_loss')->first();
|
|
|
+ $orders = DB::table('bets')->where('status', 2)->whereBetween('created_at', [$start, $end]);
|
|
|
+ $sources[] = $this->scopedBetTotals($orders, 'member_id', clone $memberIds,
|
|
|
+ 'COUNT(*) bet_count, SUM(amount) bet_amount, SUM(amount) valid_bet_amount, SUM(profit - amount) win_loss', $platform);
|
|
|
}
|
|
|
foreach (['sport_game_order', 'jisu_game_order'] as $table) {
|
|
|
if (!Schema::hasTable($table)) {
|
|
|
continue;
|
|
|
}
|
|
|
- $sources[] = DB::table($table)->whereIn('member_id', clone $memberIds)->whereIn('status', [1, 2])
|
|
|
- ->whereBetween('created_at', [$start, $end])
|
|
|
- ->selectRaw('COUNT(*) bet_count, COALESCE(SUM(amount),0) bet_amount, COALESCE(SUM(amount),0) valid_bet_amount, COALESCE(SUM(CASE WHEN status = 1 THEN -amount ELSE profit_and_loss END),0) win_loss')->first();
|
|
|
+ $orders = DB::table($table)->whereIn('status', [1, 2])->whereBetween('created_at', [$start, $end]);
|
|
|
+ $sources[] = $this->scopedBetTotals($orders, 'member_id', clone $memberIds,
|
|
|
+ 'COUNT(*) bet_count, SUM(amount) bet_amount, SUM(amount) valid_bet_amount, SUM(CASE WHEN status = 1 THEN -amount ELSE profit_and_loss END) win_loss', $platform);
|
|
|
}
|
|
|
if (Schema::hasTable('lhc_order')) {
|
|
|
$amountExpression = Schema::hasColumn('lhc_order', 'total_amount') ? 'COALESCE(total_amount, amount)' : 'amount';
|
|
|
$winLossExpression = '(COALESCE(win_amount, 0) - ' . $amountExpression . ')';
|
|
|
- $sources[] = DB::table('lhc_order')->whereIn('member_id', clone $memberIds)->whereIn('lottery_status', [1, 2])
|
|
|
- ->whereBetween('created_at', [$start->timestamp, $end->timestamp])
|
|
|
- ->selectRaw("COUNT(*) bet_count, COALESCE(SUM({$amountExpression}),0) bet_amount, COALESCE(SUM({$amountExpression}),0) valid_bet_amount, COALESCE(SUM({$winLossExpression}),0) win_loss")->first();
|
|
|
+ $orders = DB::table('lhc_order')->whereIn('lottery_status', [1, 2])->whereBetween('created_at', [$start->timestamp, $end->timestamp]);
|
|
|
+ $sources[] = $this->scopedBetTotals($orders, 'member_id', clone $memberIds,
|
|
|
+ "COUNT(*) bet_count, SUM({$amountExpression}) bet_amount, SUM({$amountExpression}) valid_bet_amount, SUM({$winLossExpression}) win_loss", $platform);
|
|
|
}
|
|
|
if (Schema::hasTable('third_game_orders')) {
|
|
|
$orders = DB::table('third_game_orders')->where('status', 1)->whereBetween('last_update_time', [$start, $end]);
|
|
|
- $userIds = (clone $members)->select('id');
|
|
|
- $totals = 'COUNT(*) bet_count, SUM(bet_amount) bet_amount, SUM(valid_amount) valid_bet_amount, SUM(settled_amount) win_loss';
|
|
|
- if ($platform) {
|
|
|
- // 先合并同一会员的注单,全平台查询只需按会员判断一次统计范围。
|
|
|
- $byUser = $orders->whereNotNull('user_id')->select('user_id')->selectRaw($totals)->groupBy('user_id');
|
|
|
- $sources[] = DB::query()->fromSub($byUser, 'third_game_user_totals')->whereIn('user_id', $userIds)
|
|
|
- ->selectRaw('SUM(bet_count) bet_count, SUM(bet_amount) bet_amount, SUM(valid_bet_amount) valid_bet_amount, SUM(win_loss) win_loss')->first();
|
|
|
- } else {
|
|
|
- $sources[] = $orders->whereIn('user_id', $userIds)->selectRaw($totals)->first();
|
|
|
- }
|
|
|
+ $sources[] = $this->scopedBetTotals($orders, 'user_id', (clone $members)->select('id'),
|
|
|
+ 'COUNT(*) bet_count, SUM(bet_amount) bet_amount, SUM(valid_amount) valid_bet_amount, SUM(settled_amount) win_loss', $platform);
|
|
|
}
|
|
|
foreach ($sources as $row) {
|
|
|
$total['bet_count'] += (int) ($row->bet_count ?? 0);
|
|
|
@@ -293,6 +285,22 @@ class AgentReportService
|
|
|
return $total;
|
|
|
}
|
|
|
|
|
|
+ private function scopedBetTotals($orders, string $identity, $eligible, string $totals, bool $platform): object
|
|
|
+ {
|
|
|
+ if (!$platform) {
|
|
|
+ return $orders->whereIn($identity, $eligible)->selectRaw($totals)->first();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 全平台先汇总同一会员的注单,避免每笔注单都查询会员表。
|
|
|
+ $byMember = $orders->whereNotNull($identity)->select($identity)->selectRaw($totals)->groupBy($identity);
|
|
|
+ if ($identity === 'member_id' && $orders->getConnection()->getDriverName() === 'mysql') {
|
|
|
+ // 旧注单表的字符排序规则可能不同,保留不同字节的会员号,仍按原规则匹配会员表。
|
|
|
+ $byMember->groupByRaw('CAST(member_id AS BINARY)');
|
|
|
+ }
|
|
|
+ return DB::query()->fromSub($byMember, 'game_member_totals')->whereIn($identity, $eligible)
|
|
|
+ ->selectRaw('SUM(bet_count) bet_count, SUM(bet_amount) bet_amount, SUM(valid_bet_amount) valid_bet_amount, SUM(win_loss) win_loss')->first();
|
|
|
+ }
|
|
|
+
|
|
|
private function emptySummary(): array
|
|
|
{
|
|
|
return [
|