CheckStockModeNegative.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Notification\TelegramBot;
  4. use GuzzleHttp\Client;
  5. use Illuminate\Console\Command;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Support\Facades\Log;
  8. class CheckStockModeNegative extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'stock_mode:check_negative';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = 'Check stock-mode rooms (low/mid/high) and send Telegram alert when Stock is negative';
  22. /**
  23. * Execute the console command.
  24. *
  25. * @return int
  26. */
  27. public function handle()
  28. {
  29. $payload = null;
  30. $statusCode = null;
  31. $body = null;
  32. try {
  33. $client = new Client([
  34. 'timeout' => 10,
  35. 'connect_timeout' => 10,
  36. ]);
  37. $res = $client->request('GET', 'https://api1.usvip.org/api/stock_mode/check_negative');
  38. $statusCode = $res->getStatusCode();
  39. $body = (string) $res->getBody();
  40. $payload = json_decode($body, true);
  41. } catch (\Throwable $e) {
  42. $this->error('Failed to request stock_mode API: ' . $e->getMessage());
  43. Log::error('stock_mode/check_negative API request failed', [
  44. 'error' => $e->getMessage(),
  45. ]);
  46. }
  47. if ($statusCode && $statusCode >= 200 && $statusCode < 300 && is_array($payload)) {
  48. Log::info('stock_mode/check_negative API json result', [
  49. 'status' => $statusCode,
  50. 'payload' => $payload,
  51. ]);
  52. $apiNegativeRooms = [];
  53. if (isset($payload['data']) && is_array($payload['data'])) {
  54. foreach ($payload['data'] as $level => $room) {
  55. if (!is_array($room)) {
  56. continue;
  57. }
  58. $isNegative = $room['is_negative'] ?? false;
  59. if ($isNegative) {
  60. $apiNegativeRooms[] = [
  61. 'level' => $level,
  62. 'sort_id' => $room['sort_id'] ?? null,
  63. 'stock' => $room['stock'] ?? null,
  64. 'stock_raw' => $room['stock_raw'] ?? null,
  65. 'level_base' => $room['level_base'] ?? null,
  66. 'level_base_raw' => $room['level_base_raw'] ?? null,
  67. ];
  68. }
  69. }
  70. }
  71. if (!empty($apiNegativeRooms)) {
  72. $apiLines = [];
  73. $apiLines[] = '【库存模式报警】PigRoomStockStatic2库存为负';
  74. $apiLines[] = '时间: ' . date('Y-m-d H:i:s');
  75. foreach ($apiNegativeRooms as $info) {
  76. $apiLines[] = sprintf(
  77. '房间 Level=%s, SortID=%s, Stock=%s (原始=%s), LevelBase=%s (原始=%s)',
  78. $info['level'],
  79. $info['sort_id'] ?? '-',
  80. $info['stock'] ?? '-',
  81. $info['stock_raw'] ?? '-',
  82. $info['level_base'] ?? '-',
  83. $info['level_base_raw'] ?? '-'
  84. );
  85. }
  86. try {
  87. TelegramBot::getDefault()->sendMsg(implode("\n", $apiLines));
  88. } catch (\Throwable $e) {
  89. Log::error('Failed to send Telegram alert (API): ' . $e->getMessage());
  90. }
  91. }
  92. } elseif ($statusCode) {
  93. // HTTP 层面返回了非 2xx 或 JSON 解析失败;也把响应体打到日志里,方便排查
  94. Log::warning('stock_mode/check_negative API non-ok response', [
  95. 'status' => $statusCode,
  96. 'body' => $body,
  97. 'json_decode_error' => json_last_error_msg(),
  98. ]);
  99. }
  100. // 低/中/高三个房间:SortID 1,2,3
  101. $rooms = DB::connection('write')
  102. ->table('QPPlatformDB.dbo.RoomStockStatic2')
  103. ->where('GameID', 0)
  104. ->whereIn('SortID', [1, 2, 3])
  105. ->get();
  106. if ($rooms->isEmpty()) {
  107. return 0;
  108. }
  109. $negativeRooms = [];
  110. foreach ($rooms as $room) {
  111. if ($room->Stock < 0) {
  112. // 数据库存储的是 *100 后的值,这里除以 100 便于阅读
  113. $negativeRooms[] = [
  114. 'sort_id' => $room->SortID,
  115. 'stock_raw' => $room->Stock,
  116. 'stock' => round($room->Stock / 100, 2),
  117. 'level_base' => isset($room->LevelBase) ? round($room->LevelBase / 100, 2) : null,
  118. ];
  119. }
  120. }
  121. if (empty($negativeRooms)) {
  122. return 0;
  123. }
  124. $lines = [];
  125. $lines[] = '【库存模式报警】RoomStockStatic2 库存为负';
  126. $lines[] = '时间: ' . date('Y-m-d H:i:s');
  127. foreach ($negativeRooms as $info) {
  128. $lines[] = sprintf(
  129. '房间 SortID=%d, Stock=%s (原始=%d), LevelBase=%s',
  130. $info['sort_id'],
  131. $info['stock'],
  132. $info['stock_raw'],
  133. $info['level_base'] === null ? '-' : $info['level_base']
  134. );
  135. }
  136. try {
  137. TelegramBot::getDefault()->sendMsgWithEnv(implode("\n", $lines));
  138. } catch (\Throwable $e) {
  139. $this->error('Failed to send Telegram alert: ' . $e->getMessage());
  140. }
  141. return 0;
  142. }
  143. }