SuperballUpdatePoolAndStats.php 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Facade\TableName;
  4. use App\Services\SuperballActivityService;
  5. use Carbon\Carbon;
  6. use Illuminate\Console\Command;
  7. use Illuminate\Support\Facades\DB;
  8. /**
  9. * Superball 活跃活动:每分钟刷新奖池与展示统计(pool_amount、total_balls、completed_count)
  10. *
  11. * - pool_amount: 取当日游戏总流水的 1/10(来源:RecordGameRoomDayInfo.TotalBet)
  12. * - total_balls & completed_count: 按时间段做一些随机增长,用于前端展示实时热度
  13. */
  14. class SuperballUpdatePoolAndStats extends Command
  15. {
  16. protected $signature = 'superball:update-pool-stats';
  17. protected $description = 'Superball: update pool_amount (1/10 of total game turnover) and grow total_balls/completed_count every minute';
  18. /** @var SuperballActivityService */
  19. protected $service;
  20. public function __construct(SuperballActivityService $service)
  21. {
  22. parent::__construct();
  23. $this->service = $service;
  24. }
  25. public function handle(): int
  26. {
  27. return false;
  28. $today = Carbon::today();
  29. $dateStr = $today->format('Y-m-d');
  30. $dateId = $today->format('Ymd');
  31. try {
  32. // 1. 计算当日总流水(TotalBet)并取 1/10 作为奖池
  33. $roomRow = DB::table('QPRecordDB.dbo.RecordGameRoomDayInfo')
  34. ->where('DateID', $dateId)
  35. ->where('SortID', -1)
  36. ->selectRaw('SUM(TotalBet) AS flowing_water_new')
  37. ->first();
  38. $flowing = $roomRow && isset($roomRow->flowing_water_new)
  39. ? (int)$roomRow->flowing_water_new
  40. : 0;
  41. // 奖池 = 总流水的 1/10(内部单位),向下取整
  42. $poolAmount = (int)floor($flowing / 4);
  43. // 2. 确保当日 superball_daily 记录存在
  44. DB::connection('write')->transaction(function () use ($dateStr, $poolAmount, $today) {
  45. // 确保记录存在(getOrCreateDaily 只在不存在时插入)
  46. $this->service->getOrCreateDaily($dateStr);
  47. // 3. 计算本次要增加的 completed_count 和 total_balls
  48. $hour = (int)$today->format('G'); // 0-23
  49. if ($hour < 1) {
  50. // 00:00 - 00:59
  51. $completedInc = mt_rand(5, 10);
  52. $multipliers = [1, 2, 3, 6];
  53. } else {
  54. // 01:00 以后
  55. $completedInc = mt_rand(10, 20);
  56. $multipliers = [3,6,10,30];
  57. }
  58. $multiplier = $multipliers[array_rand($multipliers)];
  59. $ballsInc = $completedInc * $multiplier;
  60. DB::connection('write')->table(TableName::agent() . 'superball_daily')
  61. ->where('pool_date', $dateStr)
  62. ->update([
  63. 'pool_amount' => $poolAmount,
  64. 'completed_count' => DB::raw("completed_count + {$completedInc}"),
  65. 'total_balls' => DB::raw("total_balls + {$ballsInc}"),
  66. 'updated_at' => now()->format('Y-m-d H:i:s'),
  67. ]);
  68. });
  69. \Log::info("Superball pool stats updated for {$dateStr}, pool_amount={$poolAmount}");
  70. return true;
  71. } catch (\Throwable $e) {
  72. \Log::error('Superball update pool stats failed: ' . $e->getMessage());
  73. \Log::error('Superball update pool stats', [
  74. 'date' => $dateStr,
  75. 'error' => $e->getMessage(),
  76. ]);
  77. return false;
  78. }
  79. }
  80. }