FirstPayStatService.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace App\Services;
  3. use App\Models\AccountsInfo;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Facades\Log;
  6. use Illuminate\Support\Facades\Redis;
  7. class FirstPayStatService
  8. {
  9. public function stat($order_sn)
  10. {
  11. Log::info(
  12. '首次付费统计start',
  13. [
  14. 'order_sn' => $order_sn
  15. ]
  16. );
  17. $order = \App\Models\Order::query()->where([
  18. 'order_sn' => $order_sn
  19. ])->first();
  20. if(!$order){
  21. return false;
  22. }
  23. $has = \App\Models\Order::where([
  24. 'user_id' => $order->user_id,
  25. ['finished_at', '<', $order['created_at']]
  26. ])->count();
  27. if (!$has) {
  28. Log::info(
  29. '首次付费统计新增',
  30. [
  31. 'order_sn' => $order_sn
  32. ]
  33. );
  34. $res = DB::table('QPStatisticsDB.dbo.UserFirstPayPlayRoundPoints')->where('UserID', $order['user_id'])->first();
  35. if (!$res) {
  36. $account = AccountsInfo::query()->where('UserID', $order['user_id'])->first();
  37. $counts = DB::table('QPRecordDB.dbo.RecordUserGameDayCount')
  38. ->select('GameID', DB::raw('sum(Cnt) as Cnt'))
  39. ->where([
  40. 'UserID' => $order->user_id,
  41. ['DateID', '<=', date('Ymd')],
  42. ])
  43. ->groupBy('GameID')
  44. ->orderBy('Cnt', 'desc')->first();
  45. DB::table('QPStatisticsDB.dbo.UserFirstPayPlayRoundPoints')->insert([
  46. 'UserID' => $order->user_id,
  47. 'GameID' => $account->GameID,
  48. 'RegTime' => $account->RegisterDate,
  49. 'KindID' => $counts->GameID ?? 0,
  50. 'FirstPayTime' => $order->finished_at,
  51. 'PlayRounds' => $counts->Cnt ?? 0,
  52. 'UpdatedAt' => date('Y-m-d H:i:s'),
  53. ]);
  54. }
  55. $res = DB::table('QPStatisticsDB.dbo.DailyProfitStat')
  56. ->where('Date', date('Y-m-d'))->count();
  57. if ($res) {
  58. DB::table('QPStatisticsDB.dbo.DailyProfitStat')
  59. ->where('Date', date('Y-m-d'))
  60. ->increment('HadPaidUsers');
  61. } else {
  62. DB::table('QPStatisticsDB.dbo.DailyProfitStat')->insert([
  63. 'Date' => date('Y-m-d'),
  64. 'NewUsers' => 0,
  65. 'HadPaidUsers' => 1,
  66. 'PlayUsers' => 0,
  67. 'UpdatedAt' => date('Y-m-d H:i:s'),
  68. ]);
  69. }
  70. return true;
  71. }
  72. return false;
  73. }
  74. }