123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- <?php
- namespace App\Console\Commands\ContentManage;
- use Illuminate\Console\Command;
- use Modules\ContentManage\Models\BookCategories;
- use Modules\ContentManage\Models\Cp\Cps;
- use Modules\ContentManage\Services\Books\BooksService;
- use Modules\ContentManage\Services\Books\ChapterService;
- class BookImport extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'ContentManage:bookimport {--name=: 书名}
- {--cp_id=:cp_id}
- {--category_id=:分类id}
- {--author=:作者}
- {--vip=:vip起始章节默认:8}
- {--filename=:文件名,放在storage/app/目录下}';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '导入书籍文本';
- /**
- * Execute the console command.
- */
- public function handle(): void
- {
- $create_info = $this->createBook();
- if($create_info['error']){
- return ;
- }
- $filname = $this->option('filename');
- if(!$filname){
- $this->error('filename empty');
- return ;
- }
- $result = $create_info['book'];
- $chapter_result = $this->createChapter( $result->id,$filname);
- $result->size = $chapter_result['size'];
- $result->chapter_count = $chapter_result['chapter_count'];
- $result->first_cid = $chapter_result['first_cid'];
- $result->last_cid = $chapter_result['last_cid'];
- $result->last_chapter = $chapter_result['last_chapter'];
- $result->save();
- }
- private function createBook(){
- $cp_id = $this->option('cp_id');
- $book_name = $this->option('name');
- $author = $this->option('author');
- $category_id = $this->option('category_id');
- $cp_name = $this->getCpName($cp_id);
- if(!$cp_name){
- $this->error('cp_id不存在');
- return ['error'=>1];
- }
- $category_info = $this->getCategoryInfo($category_id);
- if(!$category_info){
- $this->error('分类id不存在');
- return ['error'=>2];
- }
- $result = BooksService::createBook([
- 'cp_name'=>$cp_name,'cp_id'=>$cp_id,'name'=>$book_name,'author'=>$author ?? '','intro'=>'','cover'=>'','category_id'=>$category_id,
- 'category_name'=>$category_info->category_name,'status'=>1,'channel'=>$category_info->channel_id
- ]);
- if(!$result){
- $this->error('已经存在了');
- return ['error'=>3];
- }
- return ['error'=>0,'book'=>$result];
- }
- private function createChapter($bid,$filname){
- $vip_start = $this->option('vip');
- $path = storage_path('app/'.$filname);
- return ChapterService::createChapterFromFile($bid,1,$vip_start ?? 8,$path);
- }
- private function getCpName($cp_id){
- $cp_info = Cps::where('cp_id',$cp_id)->select('cp_nick')->first();
- if($cp_info){
- return $cp_info->cp_nick;
- }
- return '';
- }
- private function getCategoryInfo($category_id){
- return BookCategories::where('id',$category_id)->first();
- }
- }
|