DbQueue.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Http\helper\Helper;
  4. use Illuminate\Console\Command;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Facades\Log;
  7. use Illuminate\Support\Facades\Redis;
  8. class DbQueue extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'db_queue';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = '批量处理redis队列插入数据';
  22. /**
  23. * Create a new command instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. }
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return mixed
  35. */
  36. public function handle()
  37. {
  38. /*
  39. * redis队列数据格式要求:
  40. * db: 数据库前缀;table: 表名;type: 操作类型;data: 数据表内字段和数据,字段需要与表字段保持一致
  41. * 例: {"db": "QPRecordDB.dbo.", "table": "RecordNewThunderBonueLogs", "type": "insert", "data": {"UserID": 123, "KindID": 6001, "SortID": 1,"BuyBase": 600, "BuyScore": 60000, "ChangeScore": 0, "Control": 10, "Type": 2}}
  42. */
  43. $redis = Redis::connection('ServerGameRedis');
  44. return false;
  45. while(true){
  46. $queueData = $redis->command('brpop', ['dbQueue', 30]);
  47. if ($queueData && count($queueData) > 1) {
  48. $jsonArr = json_decode($queueData[1], true);
  49. $data = [];
  50. if ($jsonArr) {
  51. foreach ($jsonArr['data'] as $k => $v) {
  52. $data[$k] = $v;
  53. }
  54. Log::info('Redis队列任务:' . json_encode($data));
  55. if($jsonArr['table'] == 'UserWinRank'&&false){
  56. DB::connection('sqlsrv')->table($jsonArr['db'] . $jsonArr['table'])->where('UserID',$data['UserID'])->increment('Score',$data['Score']);
  57. if(@$data['CellScore']){
  58. //更新子游戏榜单
  59. $obj = [
  60. 'CellScore' => $data['CellScore'],
  61. 'Datatime' => date('Y-m-d H:i:s'),
  62. 'Icon' => "1,11,3",
  63. "Multiple" => intval($data['Score']/$data['CellScore']),
  64. "NickName" => $data['NickName'],
  65. "TotalScore" => $data['Score'],
  66. "UserID" => $data['UserID']
  67. ];
  68. $redis->zadd('subgameranking_'.$data['KindID'].'_'.$data['SortID'],intval($data['Score']/$data['CellScore']),json_encode($obj));
  69. }
  70. }else if($jsonArr['table'] == 'RecordNewThunderBonueLogs'){
  71. DB::connection('sqlsrv')->table($jsonArr['db'] . $jsonArr['table'])->insert($data);
  72. }else if($jsonArr['table'] == 'RecordJackpot'){
  73. $data['created'] = date('Y-m-d H:i:s');
  74. DB::connection('sqlsrv')->table($jsonArr['db'] . $jsonArr['table'])->insert($data);
  75. }
  76. }
  77. }else{
  78. sleep(5);
  79. }
  80. }
  81. }
  82. }