ReportBooks.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace App\Console\Commands\Report;
  3. use App\Modules\Book\Models\Book;
  4. use App\Modules\Book\Models\BookConfig;
  5. use GuzzleHttp\Client;
  6. use Illuminate\Console\Command;
  7. use Illuminate\Support\Facades\Redis;
  8. use Hashids;
  9. class ReportBooks extends Command
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'report:books';
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = '上报书籍';
  23. /**
  24. * Execute the console command.
  25. *
  26. * @return mixed
  27. */
  28. public function handle()
  29. {
  30. $client = new Client(['timeout' => 10, 'verify' => false]);
  31. // 获取书籍id
  32. $offset_time = 30;//min
  33. $is_full_push = true;
  34. $bookConfigs = BookConfig::getBooksByOffsetTime($offset_time,$is_full_push);
  35. $bids = collect($bookConfigs)->pluck('bid')->all();
  36. \Log::info('report:books:bids:'.json_encode($bids));
  37. \Log::info('report:books:bids_all_num:'.count($bids));
  38. // 书籍列表
  39. $books = Book::getBooksByIds($bids);
  40. if ($books) {
  41. $reportData = [
  42. 'platform' => 'wangduyun',
  43. 'list' => []
  44. ];
  45. $num = 1;
  46. foreach ($books as $book) {
  47. $bid = getProp($book, 'id');
  48. $book_config = BookConfig::getOneByBid($bid);
  49. $reportData['list'][] = [
  50. 'bid' => getProp($book, 'id'),
  51. 'name' => getProp($book_config, 'book_name'),
  52. 'category_id' => getProp($book, 'category_id'),
  53. 'category_name' => getProp($book, 'category_name'),
  54. 'shelf_time' => !empty(getProp($book_config, 'shelf_time'))?getProp($book_config, 'shelf_time'):'1970-01-01',
  55. 'shelf_status' => getProp($book_config, 'is_on_shelf'),
  56. 'hash_bid' => Hashids::encode($bid),
  57. 'created_at' => date("Y-m-d H:i:s", strtotime(getProp($book_config, 'created_at'))),
  58. 'updated_at' => date("Y-m-d H:i:s", strtotime(getProp($book_config, 'updated_at'))),
  59. ];
  60. // \Log::info('$reportData:'.$num);\Log::info($reportData);
  61. // 循环推,兼容全量
  62. if($num >=500){
  63. // 执行上报
  64. $client->post(env('REPORT_URI') . '/api/reportBooks', [
  65. 'headers' => [
  66. 'x-code' => 'Mvnx1Yr3O8i!TS5u'
  67. ],
  68. 'json' => $reportData
  69. ]);
  70. $num = 1;
  71. $reportData['list'] = [];
  72. }
  73. $num++;
  74. }
  75. // 最后一波500内的推送
  76. // 执行上报
  77. $client->post(env('REPORT_URI') . '/api/reportBooks', [
  78. 'headers' => [
  79. 'x-code' => 'Mvnx1Yr3O8i!TS5u'
  80. ],
  81. 'json' => $reportData
  82. ]);
  83. }
  84. }
  85. }