RecordPaidRewardDailyStatistics.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. ->select('StatType', 'TotalAmount')
  47. ->get()->map(function ($item) {
  48. return json_decode(json_encode($item), true);
  49. })->toArray();
  50. $stats[] = $this->sumAll(array_merge($dbStats, $stats), 'total_send');
  51. return $stats;
  52. }
  53. private function sumAll(array $stats, string $type): array
  54. {
  55. $totalAmount = 0;
  56. foreach ($stats as $item) {
  57. $totalAmount += (int)($item['TotalAmount'] ?? 0);
  58. }
  59. return [
  60. 'StatType' => $type,
  61. 'TotalAmount' => $totalAmount,
  62. ];
  63. }
  64. private function fromScoreReason(string $day, string $type, int $reason): array
  65. {
  66. $start = $day . ' 00:00:00';
  67. $end = date('Y-m-d H:i:s', strtotime($day . ' +1 day'));
  68. $row = DB::connection('sqlsrv')
  69. ->table(TableName::QPRecordDB() . 'RecordUserScoreChange as r')
  70. ->join(TableName::QPAccountsDB() . 'YN_VIPAccount as va', 'va.UserID', '=', 'r.UserID')
  71. ->where('va.Recharge', '>', 0)
  72. ->where('r.Reason', $reason)
  73. ->where('r.UpdateTime', '>=', $start)
  74. ->where('r.UpdateTime', '<', $end)
  75. ->selectRaw('
  76. ISNULL(CAST(SUM(r.ChangeScore) AS BIGINT), 0) as TotalAmount
  77. ')
  78. ->first();
  79. return [
  80. 'StatType' => $type,
  81. 'TotalAmount' => (int)($row->TotalAmount ?? 0),
  82. ];
  83. }
  84. private function fromSuperballPrize(string $day, string $type): array
  85. {
  86. $row = DB::connection('sqlsrv')
  87. ->table(TableName::agent() . 'superball_prize_log as p')
  88. ->join(TableName::QPAccountsDB() . 'YN_VIPAccount as va', 'va.UserID', '=', 'p.user_id')
  89. ->where('va.Recharge', '>', 0)
  90. ->whereDate('p.created_at', $day)
  91. ->selectRaw('
  92. ISNULL(CAST(SUM(p.total_amount) AS BIGINT), 0) as TotalAmount
  93. ')
  94. ->first();
  95. return [
  96. 'StatType' => $type,
  97. 'TotalAmount' => (int)($row->TotalAmount ?? 0),
  98. ];
  99. }
  100. private function fromChristmasWheel(string $day, string $type): array
  101. {
  102. $start = $day . ' 00:00:00';
  103. $end = date('Y-m-d H:i:s', strtotime($day . ' +1 day'));
  104. $row = DB::connection('sqlsrv')
  105. ->table(TableName::agent() . 'christmas_wheel_history as h')
  106. ->join(TableName::QPAccountsDB() . 'YN_VIPAccount as va', 'va.UserID', '=', 'h.UserID')
  107. ->where('va.Recharge', '>', 0)
  108. ->where('h.created_at', '>=', $start)
  109. ->where('h.created_at', '<', $end)
  110. ->selectRaw('
  111. ISNULL(CAST(SUM(h.reward * 100) AS BIGINT), 0) as TotalAmount
  112. ')
  113. ->first();
  114. return [
  115. 'StatType' => $type,
  116. 'TotalAmount' => (int)($row->TotalAmount ?? 0),
  117. ];
  118. }
  119. private function signInState(string $day)
  120. {
  121. $res = DB::table('QPRecordDB.dbo.RecordSignIn as h')
  122. ->lock('with(nolock)')
  123. ->whereDate('SignInDate', $day)
  124. ->leftJoin(DB::raw('QPAccountsDB.dbo.YN_VIPAccount as va with(nolock)'),
  125. 'va.UserID', '=', 'h.UserID')
  126. ->where('va.Recharge', '>', 0)
  127. ->selectRaw('sum(RewardScore) as TotalAmount')
  128. ->first();
  129. return [
  130. 'StatType' => 'sign_in',
  131. 'TotalAmount' => (int)($res->TotalAmount ?? 0),
  132. ];
  133. }
  134. private function bankruptHelp(string $day)
  135. {
  136. $res = DB::table('QPRecordDB.dbo.RecordUserScoreChange as rus')
  137. ->lock('with(nolock)')
  138. ->leftJoin(DB::raw('QPAccountsDB.dbo.YN_VIPAccount as va with(nolock)'),
  139. 'va.UserID', '=', 'rus.UserID')
  140. ->whereDate('UpdateTime', $day)
  141. ->where('va.Recharge', '>', 0)
  142. ->where('rus.Reason', 13)
  143. ->selectRaw('sum(ChangeScore) as TotalAmount')
  144. ->first();
  145. return [
  146. 'StatType' => 'bankrupt_help',
  147. 'TotalAmount' => (int)($res->TotalAmount ?? 0),
  148. ];
  149. }
  150. }