| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- <?php
- namespace App\Services\Sharing;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\DB;
- /** Read-only qualification shared by commission projection and reporting. */
- class ShareMemberQualification
- {
- public function read(string $date, bool $withMembers = true): array
- {
- $value = Carbon::createFromFormat('!Y-m-d', $date, ShareDefaults::TIMEZONE);
- if (!$value || $value->toDateString() !== $date) throw new \InvalidArgumentException('日期格式必须为Y-m-d');
- $missing = [];
- $schema = DB::connection()->getSchemaBuilder();
- foreach (['share_policies', 'share_system_state', 'share_bet_facts', 'users', 'balance_logs'] as $table) {
- if (!$schema->hasTable($table)) $missing[] = 'missing_table:' . $table;
- }
- $policy = in_array('missing_table:share_policies', $missing, true) ? null
- : DB::table('share_policies')->where('effective_date', '<=', $date)->orderByDesc('effective_date')->first();
- if (!$policy) $missing[] = 'no_effective_policy';
- elseif (($policy->valid_member_min_deposit ?? null) === null || ($policy->valid_member_min_effective_bet ?? null) === null) $missing[] = 'incomplete_qualification_policy';
- if (!in_array('missing_table:share_system_state', $missing, true)
- && !DB::table('share_system_state')->where('key', 'initialized_at')->exists()) $missing[] = 'relations_not_initialized';
- $meta = ['id' => $policy ? (int) $policy->id : null, 'effective_date' => $policy->effective_date ?? null,
- 'min_deposit' => isset($policy->valid_member_min_deposit) ? bcadd((string) $policy->valid_member_min_deposit, '0', 10) : null,
- 'min_effective_bet' => isset($policy->valid_member_min_effective_bet) ? bcadd((string) $policy->valid_member_min_effective_bet, '0', 10) : null,
- 'date' => $date, 'available' => !$missing, 'unavailable_reasons' => $missing];
- $result = ['policy' => $policy, 'policy_meta' => $meta, 'currency' => (string) config('third_game.currency', 'CNY'),
- 'users' => collect(), 'deposits' => collect(), 'effective' => collect(), 'qualified' => []];
- if ($missing || !$withMembers) return $result;
- $end = $date . ' 23:59:59';
- $result['users'] = DB::table('users')->where('from', '<>', 2)
- ->where(function ($q) use ($end) { $q->where('created_at', '<=', $end)->orWhereNull('created_at'); })
- ->get(['id', 'member_id'])->keyBy('id');
- $result['deposits'] = DB::table('balance_logs')->where('type', 1)->where('amount', '>', 0)
- ->whereIn('change_type', ['充值', '三方充值'])->where('created_at', '<=', $end)
- ->select('member_id')->selectRaw('SUM(amount) AS total')->groupBy('member_id')->pluck('total', 'member_id');
- $result['effective'] = DB::table('share_bet_facts')->where('settled', 1)->where('voided', 0)
- ->where('currency', $result['currency'])->where('bet_date', '<=', $date)
- ->select('user_id')->selectRaw('SUM(effective_amount) AS total')->groupBy('user_id')->pluck('total', 'user_id');
- foreach ($result['users'] as $id => $user) {
- if (bccomp((string) ($result['deposits'][$user->member_id] ?? 0), $meta['min_deposit'], 10) >= 0
- && bccomp((string) ($result['effective'][$id] ?? 0), $meta['min_effective_bet'], 10) >= 0) $result['qualified'][(int) $id] = true;
- }
- return $result;
- }
- }
|