123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- <?php
- namespace App\Console\Commands\Report;
- use App\Modules\Book\Models\Book;
- use App\Modules\Book\Models\BookConfig;
- use GuzzleHttp\Client;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\Redis;
- use Hashids;
- class ReportBooks extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'report:books';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '上报书籍';
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- $client = new Client(['timeout' => 10, 'verify' => false]);
- // 获取书籍id
- $offset_time = 30;//min
- $is_full_push = true;
- $bookConfigs = BookConfig::getBooksByOffsetTime($offset_time,$is_full_push);
- $bids = collect($bookConfigs)->pluck('bid')->all();
-
- \Log::info('report:books:bids:'.json_encode($bids));
- \Log::info('report:books:bids_all_num:'.count($bids));
- // 书籍列表
- $books = Book::getBooksByIds($bids);
- if ($books) {
- $reportData = [
- 'platform' => 'wangduyun',
- 'list' => []
- ];
- $num = 1;
- foreach ($books as $book) {
- $bid = getProp($book, 'id');
- $book_config = BookConfig::getOneByBid($bid);
-
- $reportData['list'][] = [
- 'bid' => getProp($book, 'id'),
- 'name' => getProp($book_config, 'book_name'),
- 'category_id' => getProp($book, 'category_id'),
- 'category_name' => getProp($book, 'category_name'),
- 'shelf_time' => !empty(getProp($book_config, 'shelf_time'))?getProp($book_config, 'shelf_time'):'1970-01-01',
- 'shelf_status' => getProp($book_config, 'is_on_shelf'),
- 'hash_bid' => Hashids::encode($bid),
- 'created_at' => date("Y-m-d H:i:s", strtotime(getProp($book_config, 'created_at'))),
- 'updated_at' => date("Y-m-d H:i:s", strtotime(getProp($book_config, 'updated_at'))),
- ];
-
- // \Log::info('$reportData:'.$num);\Log::info($reportData);
- // 循环推,兼容全量
- if($num >=500){
- // 执行上报
- $client->post(env('REPORT_URI') . '/api/reportBooks', [
- 'headers' => [
- 'x-code' => 'Mvnx1Yr3O8i!TS5u'
- ],
- 'json' => $reportData
- ]);
-
- $num = 1;
- $reportData['list'] = [];
- }
-
- $num++;
-
- }
-
- // 最后一波500内的推送
- // 执行上报
- $client->post(env('REPORT_URI') . '/api/reportBooks', [
- 'headers' => [
- 'x-code' => 'Mvnx1Yr3O8i!TS5u'
- ],
- 'json' => $reportData
- ]);
-
- }
- }
- }
|