CheckStockModeNegative.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Notification\TelegramBot;
  4. use Illuminate\Console\Command;
  5. use Illuminate\Support\Facades\DB;
  6. class CheckStockModeNegative extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'stock_mode:check_negative';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = 'Check stock-mode rooms (low/mid/high) and send Telegram alert when Stock is negative';
  20. /**
  21. * Execute the console command.
  22. *
  23. * @return int
  24. */
  25. public function handle()
  26. {
  27. // 低/中/高三个房间:SortID 1,2,3
  28. $rooms = DB::connection('write')
  29. ->table('QPPlatformDB.dbo.RoomStockStatic2')
  30. ->where('GameID', 0)
  31. ->whereIn('SortID', [1, 2, 3])
  32. ->get();
  33. if ($rooms->isEmpty()) {
  34. return 0;
  35. }
  36. $negativeRooms = [];
  37. foreach ($rooms as $room) {
  38. if ($room->Stock < 0) {
  39. // 数据库存储的是 *100 后的值,这里除以 100 便于阅读
  40. $negativeRooms[] = [
  41. 'sort_id' => $room->SortID,
  42. 'stock_raw' => $room->Stock,
  43. 'stock' => round($room->Stock / 100, 2),
  44. 'level_base' => isset($room->LevelBase) ? round($room->LevelBase / 100, 2) : null,
  45. ];
  46. }
  47. }
  48. if (empty($negativeRooms)) {
  49. return 0;
  50. }
  51. $lines = [];
  52. $lines[] = '【库存模式报警】RoomStockStatic2 库存为负';
  53. $lines[] = '时间: ' . date('Y-m-d H:i:s');
  54. foreach ($negativeRooms as $info) {
  55. $lines[] = sprintf(
  56. '房间 SortID=%d, Stock=%s (原始=%d), LevelBase=%s',
  57. $info['sort_id'],
  58. $info['stock'],
  59. $info['stock_raw'],
  60. $info['level_base'] === null ? '-' : $info['level_base']
  61. );
  62. }
  63. try {
  64. TelegramBot::getDefault()->sendMsgWithEnv(implode("\n", $lines));
  65. } catch (\Throwable $e) {
  66. $this->error('Failed to send Telegram alert: ' . $e->getMessage());
  67. }
  68. return 0;
  69. }
  70. }