ExtensionNewController.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\helper\Helper;
  5. use App\Models\AgentDepositCommission;
  6. use App\Models\AgentLevel;
  7. use App\Models\AgentTask;
  8. use App\Models\AgentUserInfo;
  9. use App\Models\AgentUserRecord;
  10. use App\Models\AgentUserReward;
  11. use App\Models\AgentWithdrawal;
  12. use App\Models\AccountsInfo;
  13. use Illuminate\Http\Request;
  14. use Illuminate\Support\Facades\DB;
  15. use Illuminate\Support\Facades\Log;
  16. class ExtensionNewController extends Controller
  17. {
  18. /**
  19. * 推广奖金审核列表
  20. */
  21. public function verify(Request $request)
  22. {
  23. $nickName = $request->NickName ?: '';
  24. $gameId = $request->GameID ?: '';
  25. $mobile = $request->mobile ?: '';
  26. $spreaderId = $request->SpreaderID ?: '';
  27. $startTime = $request->start_time ?: '';
  28. $endTime = $request->end_time ?: '';
  29. $type = $request->Type ?: '';
  30. $sort = $request->Sort ?: '';
  31. $where = [];
  32. if (!empty($nickName)) {
  33. $where[] = ['ai.NickName', 'like', $nickName . '%'];
  34. }
  35. if (!empty($gameId)) {
  36. $where[] = ['ai.GameID', $gameId];
  37. }
  38. if (!empty($spreaderId)) {
  39. $spreaderUserId = AccountsInfo::where('GameID', $spreaderId)->value('UserID');
  40. if ($spreaderUserId) {
  41. $where[] = ['aur.SpreaderID', $spreaderUserId];
  42. }
  43. }
  44. if (!empty($startTime)) {
  45. $where[] = ['aur.created_at', '>=', $startTime];
  46. }
  47. if (!empty($endTime)) {
  48. $where[] = ['aur.created_at', '<=', $endTime];
  49. }
  50. // 获取待审核的佣金记录
  51. $query = DB::table('agent_user_records as aur')
  52. ->join('accounts_info as ai', 'aur.UserID', '=', 'ai.UserID')
  53. ->join('agent_user_info as aui', 'aur.SpreaderID', '=', 'aui.UserID')
  54. ->where($where)
  55. ->select([
  56. 'aur.*',
  57. 'ai.NickName',
  58. 'ai.GameID',
  59. 'ai.RegisterDate',
  60. 'aui.level as spreader_level'
  61. ]);
  62. if (!empty($sort)) {
  63. $query->orderBy('aur.created_at', $sort);
  64. } else {
  65. $query->orderBy('aur.created_at', 'desc');
  66. }
  67. $list = $query->paginate(10);
  68. // 获取系统配置
  69. $config = [
  70. 'min_commission' => DB::table('system_config')->where('key', 'min_commission')->value('value') ?? 0,
  71. 'max_commission' => DB::table('system_config')->where('key', 'max_commission')->value('value') ?? 0,
  72. 'auto_verify' => DB::connection('write')->table('QPAccountsDB.dbo.SystemStatusInfo')
  73. ->where('StatusName', 'AutoVerify')
  74. ->value('StatusValue') ?? 0
  75. ];
  76. return view('admin.extension_new.verify', compact('list', 'config'));
  77. }
  78. /**
  79. * 更新审核状态
  80. */
  81. public function verify_update(Request $request, $id)
  82. {
  83. $status = $request->input('status');
  84. $remarks = $request->input('remarks', '');
  85. try {
  86. DB::beginTransaction();
  87. $record = AgentUserRecord::findOrFail($id);
  88. $record->status = $status;
  89. $record->remarks = $remarks;
  90. $record->verified_at = now();
  91. $record->save();
  92. // 如果审核通过,发放佣金
  93. if ($status == 1) {
  94. $this->processCommission($record);
  95. }
  96. DB::commit();
  97. return response()->json(['code' => 0, 'msg' => '更新成功']);
  98. } catch (\Exception $e) {
  99. DB::rollBack();
  100. Log::error('审核更新失败: ' . $e->getMessage());
  101. return response()->json(['code' => 1, 'msg' => '更新失败']);
  102. }
  103. }
  104. /**
  105. * 代理等级配置
  106. */
  107. public function register_config()
  108. {
  109. $list = AgentLevel::orderBy('level')->paginate(10);
  110. return view('admin.extension_new.register_config', compact('list'));
  111. }
  112. /**
  113. * 添加代理等级
  114. */
  115. public function register_config_add(Request $request)
  116. {
  117. if ($request->isMethod('post')) {
  118. $data = $request->validate([
  119. 'level_name' => 'required|string|max:50',
  120. 'level' => 'required|integer|min:1',
  121. 'required_referrals' => 'required|integer|min:0',
  122. 'commission_rate' => 'required|numeric|min:0|max:100'
  123. ]);
  124. AgentLevel::create($data);
  125. return response()->json(['code' => 0, 'msg' => '添加成功']);
  126. }
  127. return view('admin.extension_new.register_config_add');
  128. }
  129. /**
  130. * 更新代理等级
  131. */
  132. public function register_config_update(Request $request, $id)
  133. {
  134. $level = AgentLevel::findOrFail($id);
  135. if ($request->isMethod('post')) {
  136. $data = $request->validate([
  137. 'level_name' => 'required|string|max:50',
  138. 'required_referrals' => 'required|integer|min:0',
  139. 'commission_rate' => 'required|numeric|min:0|max:100'
  140. ]);
  141. $level->update($data);
  142. return response()->json(['code' => 0, 'msg' => '更新成功']);
  143. }
  144. return view('admin.extension_new.register_config_update', compact('level'));
  145. }
  146. /**
  147. * 设置佣金上限
  148. */
  149. public function upperLimit(Request $request)
  150. {
  151. if ($request->isMethod('post')) {
  152. $value = $request->input('value');
  153. DB::table('system_config')->updateOrInsert(
  154. ['key' => 'max_commission'],
  155. ['value' => $value]
  156. );
  157. return response()->json(['code' => 0, 'msg' => '设置成功']);
  158. }
  159. $value = DB::table('system_config')->where('key', 'max_commission')->value('value');
  160. return view('admin.extension_new.upper_limit', compact('value'));
  161. }
  162. /**
  163. * 设置最小审核金额
  164. */
  165. public function verifyScore(Request $request)
  166. {
  167. if ($request->isMethod('post')) {
  168. $value = $request->input('value');
  169. DB::table('system_config')->updateOrInsert(
  170. ['key' => 'min_commission'],
  171. ['value' => $value]
  172. );
  173. return response()->json(['code' => 0, 'msg' => '设置成功']);
  174. }
  175. $value = DB::table('system_config')->where('key', 'min_commission')->value('value');
  176. return view('admin.extension_new.verify_score', compact('value'));
  177. }
  178. /**
  179. * 查看用户来源
  180. */
  181. public function userSource(Request $request)
  182. {
  183. $userId = $request->input('user_id');
  184. $type = $request->input('type', 'all');
  185. $query = AgentUserRecord::where('SpreaderID', $userId);
  186. if ($type != 'all') {
  187. $query->where('type', $type);
  188. }
  189. $list = $query->with(['user:id,NickName,GameID', 'spreader:id,NickName,GameID'])
  190. ->orderBy('created_at', 'desc')
  191. ->paginate(10);
  192. return view('admin.extension_new.user_source', compact('list'));
  193. }
  194. /**
  195. * 已审核记录
  196. */
  197. public function verifyFinal(Request $request)
  198. {
  199. $nickName = $request->NickName ?: '';
  200. $gameId = $request->GameID ?: '';
  201. $spreaderId = $request->SpreaderID ?: '';
  202. $startTime = $request->start_time ?: '';
  203. $endTime = $request->end_time ?: '';
  204. $type = $request->Type ?: '';
  205. $sort = $request->Sort ?: '';
  206. $where = [];
  207. if (!empty($nickName)) {
  208. $where[] = ['ai.NickName', 'like', $nickName . '%'];
  209. }
  210. if (!empty($gameId)) {
  211. $where[] = ['ai.GameID', $gameId];
  212. }
  213. if (!empty($spreaderId)) {
  214. $spreaderUserId = AccountsInfo::where('GameID', $spreaderId)->value('UserID');
  215. if ($spreaderUserId) {
  216. $where[] = ['aur.SpreaderID', $spreaderUserId];
  217. }
  218. }
  219. if (!empty($startTime)) {
  220. $where[] = ['aur.verified_at', '>=', $startTime];
  221. }
  222. if (!empty($endTime)) {
  223. $where[] = ['aur.verified_at', '<=', $endTime];
  224. }
  225. $query = DB::table('agent_user_records as aur')
  226. ->join('accounts_info as ai', 'aur.UserID', '=', 'ai.UserID')
  227. ->join('agent_user_info as aui', 'aur.SpreaderID', '=', 'aui.UserID')
  228. ->where($where)
  229. ->where('aur.status', 1)
  230. ->select([
  231. 'aur.*',
  232. 'ai.NickName',
  233. 'ai.GameID',
  234. 'ai.RegisterDate',
  235. 'aui.level as spreader_level'
  236. ]);
  237. if (!empty($sort)) {
  238. $query->orderBy('aur.verified_at', $sort);
  239. } else {
  240. $query->orderBy('aur.verified_at', 'desc');
  241. }
  242. $list = $query->paginate(10);
  243. return view('admin.extension_new.verify_final', compact('list'));
  244. }
  245. /**
  246. * 更新审核备注
  247. */
  248. public function verifyRemarks(Request $request, $id)
  249. {
  250. $remarks = $request->input('remarks');
  251. try {
  252. $record = AgentUserRecord::findOrFail($id);
  253. $record->remarks = $remarks;
  254. $record->save();
  255. return response()->json(['code' => 0, 'msg' => '更新成功']);
  256. } catch (\Exception $e) {
  257. Log::error('更新备注失败: ' . $e->getMessage());
  258. return response()->json(['code' => 1, 'msg' => '更新失败']);
  259. }
  260. }
  261. /**
  262. * 更新已审核记录备注
  263. */
  264. public function verifyFinalRemarks(Request $request, $id)
  265. {
  266. return $this->verifyRemarks($request, $id);
  267. }
  268. /**
  269. * 任务配置列表
  270. */
  271. public function gameCountConfig()
  272. {
  273. $list = AgentTask::orderBy('required_referrals')->paginate(10);
  274. return view('admin.extension_new.gamecount_config', compact('list'));
  275. }
  276. /**
  277. * 添加任务配置
  278. */
  279. public function gameCountConfig_add(Request $request)
  280. {
  281. if ($request->isMethod('post')) {
  282. $data = $request->validate([
  283. 'task_title' => 'required|string|max:100',
  284. 'required_referrals' => 'required|integer|min:1',
  285. 'reward_amount' => 'required|numeric|min:0',
  286. 'status' => 'required|boolean'
  287. ]);
  288. AgentTask::create($data);
  289. return response()->json(['code' => 0, 'msg' => '添加成功']);
  290. }
  291. return view('admin.extension_new.gamecount_config_add');
  292. }
  293. /**
  294. * 更新任务配置
  295. */
  296. public function gameCountConfig_update(Request $request, $id)
  297. {
  298. $task = AgentTask::findOrFail($id);
  299. if ($request->isMethod('post')) {
  300. $data = $request->validate([
  301. 'task_title' => 'required|string|max:100',
  302. 'required_referrals' => 'required|integer|min:1',
  303. 'reward_amount' => 'required|numeric|min:0',
  304. 'status' => 'required|boolean'
  305. ]);
  306. $task->update($data);
  307. return response()->json(['code' => 0, 'msg' => '更新成功']);
  308. }
  309. return view('admin.extension_new.gamecount_config_update', compact('task'));
  310. }
  311. /**
  312. * 设置任务奖励上限
  313. */
  314. public function gameCountLimit(Request $request)
  315. {
  316. if ($request->isMethod('post')) {
  317. $value = $request->input('value');
  318. DB::table('system_config')->updateOrInsert(
  319. ['key' => 'max_task_reward'],
  320. ['value' => $value]
  321. );
  322. return response()->json(['code' => 0, 'msg' => '设置成功']);
  323. }
  324. $value = DB::table('system_config')->where('key', 'max_task_reward')->value('value');
  325. return view('admin.extension_new.gamecount_limit', compact('value'));
  326. }
  327. /**
  328. * 自动审核开关
  329. */
  330. public function autoVerify(Request $request)
  331. {
  332. $StatusValue = $request->input('StatusValue', 0);
  333. $result = DB::table('system_config')
  334. ->updateOrInsert(
  335. ['key' => 'auto_verify'],
  336. ['key' => 'auto_verify', 'value' => $StatusValue, 'updated_at' => now()]
  337. );
  338. if ($result) {
  339. return response()->json(['code' => 0, 'msg' => '操作成功']);
  340. } else {
  341. return response()->json(['code' => -1, 'msg' => '操作失败']);
  342. }
  343. }
  344. /**
  345. * 充值返佣比例设置
  346. */
  347. public function recharge_rate(Request $request)
  348. {
  349. // if ($request->isMethod('post')) {
  350. // $data = $request->validate([
  351. // 'level_1_rate' => 'required|numeric|min:0|max:100',
  352. // 'level_2_rate' => 'required|numeric|min:0|max:100'
  353. // ]);
  354. //
  355. // foreach ($data as $key => $value) {
  356. // DB::table('system_config')->updateOrInsert(
  357. // ['key' => $key],
  358. // ['value' => $value]
  359. // );
  360. // }
  361. //
  362. // return response()->json(['code' => 0, 'msg' => '设置成功']);
  363. // }
  364. //
  365. // $rates = [
  366. // 'level_1_rate' => DB::table('system_config')->where('key', 'level_1_rate')->value('value'),
  367. // 'level_2_rate' => DB::table('system_config')->where('key', 'level_2_rate')->value('value')
  368. // ];
  369. //
  370. // return view('admin.extension_new.recharge_rate', compact('rates'));
  371. }
  372. /**
  373. * 处理佣金发放
  374. */
  375. private function processCommission($record)
  376. {
  377. try {
  378. DB::beginTransaction();
  379. // 获取推广者信息
  380. $spreader = AgentUserInfo::where('UserID', $record->SpreaderID)->first();
  381. if (!$spreader) {
  382. throw new \Exception('推广者信息不存在');
  383. }
  384. // 获取推广者等级信息
  385. $level = AgentLevel::where('level', $spreader->level)->first();
  386. if (!$level) {
  387. throw new \Exception('推广者等级信息不存在');
  388. }
  389. // 计算佣金
  390. $commission = $record->amount * ($level->commission_rate / 100);
  391. // 创建佣金记录
  392. $commissionRecord = new AgentDepositCommission();
  393. $commissionRecord->SpreaderID = $record->SpreaderID;
  394. $commissionRecord->UserID = $record->UserID;
  395. $commissionRecord->order_sn = $record->order_sn;
  396. $commissionRecord->deposit_amount = $record->amount;
  397. $commissionRecord->commission_rate = $level->commission_rate;
  398. $commissionRecord->commission_amount = $commission;
  399. $commissionRecord->status = 1;
  400. $commissionRecord->save();
  401. // 更新推广者佣金统计
  402. $spreader->total_commission += $commission;
  403. $spreader->today_commission += $commission;
  404. $spreader->now_rewards += $commission;
  405. $spreader->save();
  406. DB::commit();
  407. } catch (\Exception $e) {
  408. DB::rollBack();
  409. Log::error('处理佣金失败: ' . $e->getMessage());
  410. throw $e;
  411. }
  412. }
  413. /**
  414. * 每日绑定查询
  415. */
  416. public function dailyBinding(Request $request)
  417. {
  418. $start_time = str_replace('T', ' ', $request->start_time) ?: date('Y-m-d 00:00:00');
  419. $end_time = str_replace('T', ' ', $request->end_time) ?: date('Y-m-d 23:59:59');
  420. $spreaderID = $request->SpreaderID ?: '';
  421. $GameID = $request->GameID ?: '';
  422. $date = $request->date ?: '';
  423. switch ($date) {
  424. case 1:
  425. $start_time = date('Y-m-d 00:00:00');
  426. break;
  427. case 2:
  428. $start_time = date('Y-m-d 00:00:00', strtotime('-1 day'));
  429. $end_time = date('Y-m-d 23:59:59', strtotime('-1 day'));
  430. break;
  431. case 3:
  432. //当前日期
  433. $sdefaultDate = date("Y-m-d 00:00:00");
  434. // 1表示每周星期一为开始日期 0表示每周日为开始日期
  435. $first = 1;
  436. //获取当前周的第几天 周日是 0 周一到周六是 1 - 6
  437. $w = date('w', strtotime($sdefaultDate));
  438. $start_time = date('Y-m-d 00:00:00', strtotime("$sdefaultDate -" . ($w ? $w - $first : 6) . ' days'));
  439. break;
  440. case 4:
  441. $start_time = date("Y-m-01 00:00:00");
  442. break;
  443. }
  444. // 构建基础查询 - 使用agent_user_records表
  445. $query = DB::connection('mysql')
  446. ->table('webgame.agent_user_records as aur')
  447. ->select([
  448. 'aur.UserID',
  449. 'aur.created_at as RegisterDate',
  450. 'aur.SpreaderID as SpreaderUserID'
  451. ]);
  452. // 添加条件过滤
  453. if (!empty($start_time)) {
  454. $query->where('aur.created_at', '>=', $start_time);
  455. }
  456. if (!empty($end_time)) {
  457. $query->where('aur.created_at', '<=', $end_time);
  458. }
  459. if (!empty($GameID)) {
  460. $query->where('aur.UserID', AccountsInfo::where('GameID', $GameID)
  461. ->value('UserID'));
  462. }
  463. // 如果指定了推广者ID,需要进行额外筛选
  464. if (!empty($spreaderID)) {
  465. $spreaderUserId = AccountsInfo::where('GameID', $spreaderID)
  466. ->value('UserID');
  467. if ($spreaderUserId) {
  468. $query->where('aur.SpreaderID', $spreaderUserId);
  469. }
  470. }
  471. // 获取总记录数用于分页
  472. $total = $query->count();
  473. // 分页参数
  474. $perPage = 10;
  475. $currentPage = $request->input('page', 1);
  476. // 获取当前页数据
  477. $paginatedRecords = $query->orderBy('aur.created_at', 'desc')
  478. ->forPage($currentPage, $perPage)
  479. ->get();
  480. $userIds = $paginatedRecords->pluck('UserID')->toArray();
  481. $spids = $paginatedRecords->pluck('SpreaderUserID')->toArray();
  482. $userIds=array_merge($userIds,$spids);
  483. $otherinfos=AccountsInfo::whereIn('UserID',$userIds)->select('UserID','GameID','LastLogonDate','RegisterDate')->get();
  484. // 整合数据
  485. $paginatedRecords = $paginatedRecords->map(function($record) use ( $otherinfos) {
  486. foreach($otherinfos as $otherinfo){
  487. if($otherinfo->UserID==$record->UserID){
  488. $record->RegisterDate=$otherinfo->RegisterDate;
  489. $record->LastLogonDate=$otherinfo->LastLogonDate;
  490. $record->GameID=$otherinfo->GameID;
  491. }
  492. if($otherinfo->UserID==$record->SpreaderUserID){
  493. $record->SpreaderID=$otherinfo->GameID;
  494. }
  495. }
  496. return $record;
  497. });
  498. // 创建分页实例
  499. $list = new \Illuminate\Pagination\LengthAwarePaginator(
  500. $paginatedRecords,
  501. $total,
  502. $perPage,
  503. $currentPage,
  504. ['path' => $request->url(), 'query' => $request->query()]
  505. );
  506. $start_time = Helper::timeChange($start_time);
  507. $end_time = Helper::timeChange($end_time);
  508. $dates = [1 => '今日', 2 => '昨日', 3 => '本周', 4 => '本月'];
  509. $data = compact('list', 'start_time', 'end_time', 'spreaderID', 'GameID', 'date', 'dates');
  510. return view('admin.extension_new.daily_binding', $data);
  511. }
  512. /**
  513. * 推广员奖励报表
  514. */
  515. public function reward(Request $request)
  516. {
  517. $UserID = (int)trim($request->UserID) ?: 0;
  518. $SpreaderID = (int)trim($request->SpreaderID) ?: 0;
  519. $start_time = trim($request->start_time) ?: '';
  520. $end_time = trim($request->end_time) ?: '';
  521. $Type = trim($request->Type) ?: '';
  522. $start_time = str_replace('T', ' ', $start_time);
  523. $end_time = str_replace('T', ' ', $end_time);
  524. // 构建查询 - 使用agent_deposit_commissions表
  525. $query = DB::connection('mysql')
  526. ->table('webgame.agent_deposit_commissions as adc')
  527. ->select([
  528. 'adc.id',
  529. 'adc.SpreaderID',
  530. 'adc.UserID',
  531. 'adc.order_sn',
  532. 'adc.deposit_amount',
  533. 'adc.commission_rate',
  534. 'adc.commission_amount',
  535. // 'adc.type',
  536. 'adc.status',
  537. 'adc.created_at',
  538. ]);
  539. // 添加查询条件
  540. if (!empty($Type)) {
  541. // $query->where('adc.type', $Type);
  542. }
  543. if (!empty($start_time)) {
  544. $query->where('adc.created_at', '>=', $start_time);
  545. }
  546. if (!empty($end_time)) {
  547. $query->where('adc.created_at', '<=', $end_time);
  548. }
  549. // 只查看已发放的佣金
  550. $query->where('adc.status', 1);
  551. if ($UserID > 0) {
  552. $query->where('adc.UserID', AccountsInfo::where('GameID',$UserID)->value('UserID'));
  553. }
  554. if ($SpreaderID > 0) {
  555. $query->where('adc.SpreaderID', AccountsInfo::where('GameID',$SpreaderID)->value('UserID'));
  556. }
  557. // 获取总记录数用于分页
  558. $total = $query->count();
  559. // 分页参数设置
  560. $perPage = 10;
  561. $currentPage = $request->input('page', 1);
  562. // 获取当前页数据
  563. $records = $query->orderBy('adc.created_at', 'desc')
  564. ->forPage($currentPage, $perPage)
  565. ->get();
  566. $userIds=$records->pluck('UserID')->toArray();
  567. $spreaderIds=$records->pluck('SpreaderID')->toArray();
  568. $userIds=array_merge($userIds,$spreaderIds);
  569. $otherinfos=AccountsInfo::whereIn('UserID',$userIds)->select('UserID','GameID','LastLogonDate','RegisterDate')->get();
  570. // 处理数据格式
  571. $processedRecords = $records->map(function($record) use ($otherinfos) {
  572. // 设置类型描述
  573. // $record->TypeDesc = $this->getTypeDesc($record->type);
  574. foreach ($otherinfos as $otherinfo){
  575. if($otherinfo->UserID==$record->UserID){
  576. $record->RegisterDate=$otherinfo->RegisterDate;
  577. $record->LastLogonDate=$otherinfo->LastLogonDate;
  578. $record->GameID=$otherinfo->GameID;
  579. }
  580. if($otherinfo->UserID==$record->SpreaderID){
  581. $record->SpreaderGameID=$otherinfo->GameID;
  582. }
  583. }
  584. return $record;
  585. });
  586. // 创建分页实例
  587. $list = new \Illuminate\Pagination\LengthAwarePaginator(
  588. $processedRecords,
  589. $total,
  590. $perPage,
  591. $currentPage,
  592. ['path' => $request->url(), 'query' => $request->query()]
  593. );
  594. // 获取自动审核状态
  595. $AutoVerify = DB::connection('write')->table('QPAccountsDB.dbo.SystemStatusInfo')
  596. ->where('StatusName', 'AutoVerify')
  597. ->value('StatusValue') ?? 0;
  598. $result = compact('list', 'UserID', 'SpreaderID', 'start_time', 'end_time', 'Type', 'AutoVerify');
  599. return view('admin.extension_new.reward_list', $result);
  600. }
  601. /**
  602. * 获取奖励类型描述
  603. */
  604. private function getTypeDesc($type)
  605. {
  606. $types = [
  607. 1 => '注册',
  608. 2 => '对局',
  609. 3 => '充值',
  610. ];
  611. return $types[$type] ?? '未知';
  612. }
  613. /**
  614. * 绑定列表查询
  615. */
  616. public function bind_list(Request $request)
  617. {
  618. $start_time = $request->start_time ?: '';
  619. $end_time = $request->end_time ?: '';
  620. $SpreaderID = $request->SpreaderID ?: '';
  621. $UserID = $request->UserID ?: '';
  622. $sort = $request->sort ?: '';
  623. // 跳转页面条件
  624. $source = $request->source ?: 1;
  625. $NickName = $request->NickName ?: '';
  626. $GameID = $request->GameID ?: '';
  627. $mobile = $request->mobile ?: '';
  628. $Type = $request->Type ?: '';
  629. $Sort = $request->Sort ?: '';
  630. $FinalScoreSort = $request->FinalScoreSort ?: '';
  631. // 基础查询 - 使用新的agent_user_records表
  632. $query = DB::connection('mysql')
  633. ->table('webgame.agent_user_records')
  634. ->select([
  635. 'UserID',
  636. 'SpreaderID',
  637. 'created_at',
  638. ]);
  639. // 构建查询条件
  640. if (!empty($start_time)) {
  641. $query->where('created_at', '>=', $start_time);
  642. }
  643. if (!empty($end_time)) {
  644. $query->where('created_at', '<=', $end_time);
  645. }
  646. if (!empty($UserID)) {
  647. $query->where('UserID', AccountsInfo::where('GameID', $UserID)
  648. ->value('UserID'));
  649. }
  650. if (!empty($SpreaderID)) {
  651. $spreaderUserId=AccountsInfo::where('GameID', $SpreaderID)
  652. ->value('UserID');
  653. if ($spreaderUserId) {
  654. $query->where('SpreaderID', $spreaderUserId);
  655. }
  656. }
  657. // 执行查询并进行分页
  658. $perPage = 10;
  659. $currentPage = $request->input('page', 1);
  660. // 获取总记录数用于分页
  661. $total = $query->count();
  662. // 应用排序
  663. $query->orderBy('created_at', 'desc');
  664. // 获取当前页数据
  665. $paginatedRecords = $query->forPage($currentPage, $perPage)->get();
  666. // 只获取当前页面需要的用户ID
  667. $userIds = $paginatedRecords->pluck('UserID')->toArray();
  668. $spreaderIds = $paginatedRecords->pluck('SpreaderID')->toArray();
  669. // 从代理系统中获取当前页面用户的信息
  670. $agentUsers = DB::connection('mysql')->table('webgame.agent_user_info')
  671. ->whereIn('UserID', array_merge($userIds, $spreaderIds))
  672. ->get()
  673. ->keyBy('UserID');
  674. // 针对当前页面的用户ID查询直属下级数量 (一级下级)
  675. $directCounts = DB::connection('mysql')
  676. ->table('webgame.agent_user_records')
  677. ->whereIn('SpreaderID', $userIds)
  678. ->select('SpreaderID', DB::raw('COUNT(*) as count'))
  679. ->groupBy('SpreaderID')
  680. ->get()
  681. ->keyBy('SpreaderID');
  682. // 获取二级下级数量
  683. $indirectCounts = collect();
  684. // 先获取直属下级的ID
  685. $directDownlines = DB::connection('mysql')
  686. ->table('webgame.agent_user_records')
  687. ->whereIn('SpreaderID', $userIds)
  688. ->select('SpreaderID', 'UserID')
  689. ->get();
  690. // 按SpreaderID分组直属下级
  691. $directDownlinesMap = [];
  692. foreach ($directDownlines as $record) {
  693. if (!isset($directDownlinesMap[$record->SpreaderID])) {
  694. $directDownlinesMap[$record->SpreaderID] = [];
  695. }
  696. $directDownlinesMap[$record->SpreaderID][] = $record->UserID;
  697. }
  698. $userIds=array_merge($userIds,$spreaderIds);
  699. $otherinfos=AccountsInfo::whereIn('UserID',$userIds)->select('UserID','GameID','LastLogonDate','RegisterDate')->get();
  700. // 对于每个用户,查询其直属下级的下级数量
  701. // foreach ($userIds as $userId) {
  702. // $childCount = 0;
  703. // if (isset($directDownlinesMap[$userId])) {
  704. // $directUserIds = $directDownlinesMap[$userId];
  705. // if (!empty($directUserIds)) {
  706. // $childCount = DB::connection('mysql')
  707. // ->table('webgame.agent_user_records')
  708. // ->whereIn('SpreaderID', $directUserIds)
  709. // ->count();
  710. // }
  711. // }
  712. // $indirectCounts->put($userId, $childCount);
  713. // }
  714. // 整合数据
  715. $processedRecords = $paginatedRecords->map(function($record) use ($agentUsers, $directCounts, $otherinfos) {
  716. // 添加佣金信息
  717. $record->Rewards = isset($agentUsers[$record->UserID]) ?
  718. $agentUsers[$record->UserID]->total_commission : 0;
  719. // 获取下级人数
  720. $record->downCount1 = isset($directCounts[$record->UserID]) ?
  721. $directCounts[$record->UserID]->count : 0;
  722. $record->downCount2 = 0;//$indirectCounts->get($record->UserID, 0);
  723. foreach($otherinfos as $otherinfo){
  724. if($otherinfo->UserID==$record->UserID){
  725. $record->RegisterDate=$otherinfo->RegisterDate;
  726. $record->LastLogonDate=$otherinfo->LastLogonDate;
  727. $record->GameID=$otherinfo->GameID;
  728. }
  729. if($otherinfo->UserID==$record->SpreaderID){
  730. $record->SpreaderGameID=$otherinfo->GameID;
  731. }
  732. }
  733. return $record;
  734. });
  735. // 如果有特殊排序要求,在这里应用
  736. if (!empty($sort)) {
  737. if ($sort == 'downCount1 + downCount2') {
  738. $processedRecords = $processedRecords->sortByDesc(function($record) {
  739. return $record->downCount1 + $record->downCount2;
  740. });
  741. }
  742. }
  743. // 创建分页实例
  744. $list = new \Illuminate\Pagination\LengthAwarePaginator(
  745. $processedRecords,
  746. $total,
  747. $perPage,
  748. $currentPage,
  749. ['path' => $request->url(), 'query' => $request->query()]
  750. );
  751. $result = [
  752. 'list' => $list,
  753. 'UserID' => $UserID,
  754. 'SpreaderID' => $SpreaderID,
  755. 'start_time' => $start_time,
  756. 'end_time' => $end_time,
  757. 'source' => $source,
  758. 'NickName' => $NickName,
  759. 'GameID' => $GameID,
  760. 'mobile' => $mobile,
  761. 'Type' => $Type,
  762. 'Sort' => $Sort,
  763. 'FinalScoreSort' => $FinalScoreSort,
  764. 'request' => $request
  765. ];
  766. return view('admin.extension_new.bind_list', $result);
  767. }
  768. }