RecordPaidRewardDailyStatistics.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Facade\TableName;
  4. use App\Models\RecordScoreInfo;
  5. use Carbon\Carbon;
  6. use Illuminate\Console\Command;
  7. use Illuminate\Support\Facades\DB;
  8. class RecordPaidRewardDailyStatistics extends Command
  9. {
  10. protected $signature = 'RecordPaidRewardDailyStatistics {day?}';
  11. protected $description = '统计付费用户各奖励类型每日送出金额';
  12. public function handle()
  13. {
  14. $day = $this->argument('day') ?: Carbon::yesterday()->format('Y-m-d');
  15. $dateId = date('Ymd', strtotime($day));
  16. $stats = $this->buildStats($day);
  17. foreach ($stats as $item) {
  18. DB::table(TableName::QPRecordDB() . 'RecordPaidRewardDailyStatistics')->updateOrInsert(
  19. [
  20. 'DateID' => $dateId,
  21. 'StatType' => $item['StatType'],
  22. ],
  23. [
  24. 'TotalAmount' => $item['TotalAmount'],
  25. 'CreatedAt' => now()->format('Y-m-d H:i:s'),
  26. 'UpdatedAt' => now()->format('Y-m-d H:i:s'),
  27. ]
  28. );
  29. }
  30. $this->info("RecordPaidRewardDailyStatistics done: {$day}");
  31. return true;
  32. }
  33. private function buildStats(string $day): array
  34. {
  35. $stats = [];
  36. $signIn = $this->signInState($day);
  37. $stats[] = $signIn;
  38. $bankruptHelp = $this->bankruptHelp($day);
  39. $stats[] = $bankruptHelp;
  40. $stats[] = $this->fromSuperballPrize($day, 'superball');
  41. $stats[] = $this->fromChristmasWheel($day, 'christmas_gift_wheel');
  42. $stats[] = $this->fromScoreReason($day, 'bound_phone', 21);
  43. $dbStats = DB::table(TableName::QPRecordDB() . 'RecordPaidRewardDailyStatistics')
  44. ->lock('with(nolock)')
  45. ->where('DateID', date('Ymd', strtotime($day)))
  46. ->whereIn('StatType', ['normal_recharge_chips', 'first_recharge_gift_chips', 'bankrupt_gift_chips',
  47. 'daily_gift_chips', 'vip_inactive_gift_chips', 'free_bonus_gift_chips', 'christmas_gift_chips', 'unknow_chips'])
  48. ->select('StatType', 'TotalAmount')
  49. ->get()->map(function ($item) {
  50. return json_decode(json_encode($item), true);
  51. })->toArray();
  52. $stats[] = $this->sumAll($dbStats, 'chips_all');
  53. return $stats;
  54. }
  55. private function sumAll(array $stats, string $type): array
  56. {
  57. $totalAmount = 0;
  58. foreach ($stats as $item) {
  59. $totalAmount += (int)($item['TotalAmount'] ?? 0);
  60. }
  61. return [
  62. 'StatType' => $type,
  63. 'TotalAmount' => $totalAmount,
  64. ];
  65. }
  66. private function fromScoreReason(string $day, string $type, int $reason): array
  67. {
  68. $start = $day . ' 00:00:00';
  69. $end = date('Y-m-d H:i:s', strtotime($day . ' +1 day'));
  70. $row = DB::connection('sqlsrv')
  71. ->table(TableName::QPRecordDB() . 'RecordUserScoreChange as r')
  72. ->join(TableName::QPAccountsDB() . 'YN_VIPAccount as va', 'va.UserID', '=', 'r.UserID')
  73. ->where('va.Recharge', '>', 0)
  74. ->where('r.Reason', $reason)
  75. ->where('r.UpdateTime', '>=', $start)
  76. ->where('r.UpdateTime', '<', $end)
  77. ->selectRaw('
  78. ISNULL(CAST(SUM(r.ChangeScore) AS BIGINT), 0) as TotalAmount
  79. ')
  80. ->first();
  81. return [
  82. 'StatType' => $type,
  83. 'TotalAmount' => (int)($row->TotalAmount ?? 0),
  84. ];
  85. }
  86. private function fromSuperballPrize(string $day, string $type): array
  87. {
  88. $row = DB::connection('sqlsrv')
  89. ->table(TableName::agent() . 'superball_prize_log as p')
  90. ->join(TableName::QPAccountsDB() . 'YN_VIPAccount as va', 'va.UserID', '=', 'p.user_id')
  91. ->where('va.Recharge', '>', 0)
  92. ->whereDate('p.created_at', $day)
  93. ->selectRaw('
  94. ISNULL(CAST(SUM(p.total_amount) AS BIGINT), 0) as TotalAmount
  95. ')
  96. ->first();
  97. return [
  98. 'StatType' => $type,
  99. 'TotalAmount' => (int)($row->TotalAmount ?? 0),
  100. ];
  101. }
  102. private function fromChristmasWheel(string $day, string $type): array
  103. {
  104. $start = $day . ' 00:00:00';
  105. $end = date('Y-m-d H:i:s', strtotime($day . ' +1 day'));
  106. $row = DB::connection('sqlsrv')
  107. ->table(TableName::agent() . 'christmas_wheel_history as h')
  108. ->join(TableName::QPAccountsDB() . 'YN_VIPAccount as va', 'va.UserID', '=', 'h.UserID')
  109. ->where('va.Recharge', '>', 0)
  110. ->where('h.created_at', '>=', $start)
  111. ->where('h.created_at', '<', $end)
  112. ->selectRaw('
  113. ISNULL(CAST(SUM(h.reward * 100) AS BIGINT), 0) as TotalAmount
  114. ')
  115. ->first();
  116. return [
  117. 'StatType' => $type,
  118. 'TotalAmount' => (int)($row->TotalAmount ?? 0),
  119. ];
  120. }
  121. private function signInState(string $day)
  122. {
  123. $res = DB::table('QPRecordDB.dbo.RecordSignIn as h')
  124. ->lock('with(nolock)')
  125. ->whereDate('SignInDate', $day)
  126. ->leftJoin(DB::raw('QPAccountsDB.dbo.YN_VIPAccount as va with(nolock)'),
  127. 'va.UserID', '=', 'h.UserID')
  128. ->where('va.Recharge', '>', 0)
  129. ->selectRaw('sum(RewardScore) as TotalAmount')
  130. ->first();
  131. return [
  132. 'StatType' => 'sign_in',
  133. 'TotalAmount' => (int)($res->TotalAmount ?? 0),
  134. ];
  135. }
  136. private function bankruptHelp(string $day)
  137. {
  138. $res = DB::table('QPRecordDB.dbo.RecordUserScoreChange as rus')
  139. ->lock('with(nolock)')
  140. ->leftJoin(DB::raw('QPAccountsDB.dbo.YN_VIPAccount as va with(nolock)'),
  141. 'va.UserID', '=', 'rus.UserID')
  142. ->whereDate('UpdateTime', $day)
  143. ->where('va.Recharge', '>', 0)
  144. ->where('rus.Reason', 13)
  145. ->selectRaw('sum(ChangeScore) as TotalAmount')
  146. ->first();
  147. return [
  148. 'StatType' => 'bankrupt_help',
  149. 'TotalAmount' => (int)($res->TotalAmount ?? 0),
  150. ];
  151. }
  152. }