123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- <?php
- namespace App\Console\Sync;
- use App\Libs\BatchUpdateTrait;
- use App\Models\Book\Book;
- use App\Models\Book\BookConfig;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\DB;
- class SyncBooks extends Command
- {
- use BatchUpdateTrait;
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'sync:books {--zw_ids=}';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '从内容平台同步书籍';
- private $zwBooks = [];
- public function handle()
- {
- // 传参
- $idsStr = $this->option('zw_ids');
- $passZwIds = filterValidIds(explode(',', $idsStr));
- // 获取书籍列表
- $existedZwIds = $this->getAllBookZwIds($passZwIds);
- // 合并
- $zwIds = array_unique(array_merge($passZwIds, $existedZwIds));
- // 执行更新
- $this->runSync($zwIds);
- }
- /**
- * @param $zwIds
- * @return array
- */
- private function getAllBookZwIds($zwIds): array
- {
- // 查询当前所有书
- $query = Book::select('id', 'zw_id');
- if ($zwIds) {
- $query->whereIn('zw_id', $zwIds);
- } else {
- $query->where('zw_id', '>', 0);
- }
- $books = $query->get();
- if ($books->isEmpty()) {
- return [];
- }
- // 标记已存在书籍
- $result = [];
- foreach ($books as $book) {
- $zwId = (int)getProp($book, 'zw_id');
- if (!isset($this->zwBooks[$zwId])) {
- $this->zwBooks[$zwId] = (int)getProp($book, 'id');
- }
- $result[] = $zwId;
- }
- return $result;
- }
- /**
- * @param $zwIds
- * @return void
- */
- private function runSync($zwIds): void
- {
- // 判空
- if (empty($zwIds)) {
- return;
- }
- // 获取内容平台书籍
- $zwBooks = $this->getZwBooksByIds($zwIds);
- if (empty($zwBooks)) {
- return;
- }
- // 组装书籍数据
- $now = date('Y-m-d H:i:s');
- $zwIds = $upBooks = $inBooks = $initConfigs = [];
- foreach ($zwBooks as $zwBook) {
- $zwId = (int)getProp($zwBook, 'id');
- $cover = getProp($zwBook, 'cover') ?: getProp($zwBook, 'book_config_cover');
- $intro = getProp($zwBook, 'intro');
- $intro = str_replace(" ", "", $intro);
- $intro = trim($intro);
- $zwIds[] = $zwId;
- $bookItem = [
- 'zw_id' => $zwId,
- 'name' => getProp($zwBook, 'name'),
- 'author' => getProp($zwBook, 'author'),
- 'intro' => $intro,
- 'cover' => $cover,
- 'keyword' => getProp($zwBook, 'keyword'),
- 'gender' => getProp($zwBook, 'gender'), # 1男 2女
- 'category_id' => getProp($zwBook, 'category_id'),
- 'category_name' => getProp($zwBook, 'category_name'),
- 'ncategory_id' => getProp($zwBook, 'ncategory_id'),
- 'size' => getProp($zwBook, 'size'),
- 'status' => getProp($zwBook, 'status'),
- 'chapter_count' => getProp($zwBook, 'chapter_count'),
- 'updated_at' => $now,
- ];
- // 书籍配置表
- $initConfigs[$zwId] = [
- 'cover' => $cover,
- 'book_name' => getProp($zwBook, 'name'),
- 'source_domain' => getProp($zwBook, 'gender') == 1 ? 'fanqqe.com' : 'fanqrj.com',
- 'cp_source' => getProp($zwBook, 'source'),
- 'updated_at' => $now,
- ];
- // 区分更新还是写入
- $bid = (int)getProp($this->zwBooks, $zwId);
- if ($bid) {
- // 书籍数据
- $bookItem['id'] = $bid;
- $upBooks[] = $bookItem;
- } else {
- $bookItem['created_at'] = $now;
- $inBooks[] = $bookItem;
- }
- }
- Book::insert($inBooks);
- $this->batchUpdateDb('books', $upBooks);
- // 获取这批书籍
- $books = Book::select('id', 'zw_id')->whereIn('zw_id', $zwIds)->get();
- if ($books->isEmpty()) {
- return;
- }
- $bids = collect($books)->pluck('id')->all();
- $bookConfigs = BookConfig::select('id', 'bid')->whereIn('bid', $bids)->get();
- $configBids = collect($bookConfigs)->pluck('bid')->all();
- // 区分更新or新增
- $inConfigs = $upConfigs = [];
- foreach ($books as $book) {
- $bid = (int)getProp($book, 'id');
- $zwId = (int)getProp($book, 'zw_id');
- $bookConfig = getProp($initConfigs, $zwId, []);
- $bookConfig['bid'] = $bid;
- if ($bookConfig && in_array($bid, $configBids)) {
- // 书籍数据
- $upConfigs[] = $bookConfig;
- } else {
- $bookConfig['copyright_limit_data'] = '0000-00-00';
- $bookConfig['charge_type'] = 'CHAPTER';
- $bookConfig['shelf_time'] = $now;
- $bookConfig['created_at'] = $now;
- $inConfigs[] = $bookConfig;
- }
- }
- BookConfig::insert($inConfigs);
- $this->batchUpdateDb('book_configs', $upConfigs, 'bid');
- }
- /**
- * 获取内容平台书籍
- *
- * @param $ids
- * @return array
- */
- private function getZwBooksByIds($ids): array
- {
- if (empty($ids)) {
- return [];
- }
- $result = DB::connection('zw_content_mysql')
- ->table('books')
- ->leftjoin('book_configs', 'book_configs.bid', '=', 'books.id')->whereIn('books.id', $ids)
- ->select('books.id', 'books.name', 'books.out_bid', 'books.source', 'books.source_name', 'books.author', 'books.source_bid',
- 'books.intro', 'books.gender', 'books.cover', 'books.size', 'books.keyword', 'books.category_name', 'books.status', 'books.chapter_count',
- 'books.updated_at', 'books.primary_cover', 'books.category_id', 'books.last_chapter_update_time', 'books.series', 'books.labels',
- 'books.styles', 'books.man_type', 'books.woman_type', 'books.time_back', 'books.directional', 'books.is_app', 'books.end_time', 'books.post_time',
- 'books.ncategory_id', 'books.roles', 'books.authorization_status', 'books.is_vip', 'books.is_first', 'books.is_check', 'books.is_contract',
- 'books.author_id', 'book_configs.is_on_shelf', 'book_configs.cover as book_config_cover', 'books.group_id'
- )
- ->get();
- return $result ? $result->toArray() : [];
- }
- }
|