BookImport.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace App\Console\Commands\ContentManage;
  3. use Illuminate\Console\Command;
  4. use Modules\ContentManage\Models\BookCategories;
  5. use Modules\ContentManage\Models\Cp\Cps;
  6. use Modules\ContentManage\Services\Books\BooksService;
  7. use Modules\ContentManage\Services\Books\ChapterService;
  8. class BookImport extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'ContentManage:bookimport {--name=: 书名}
  16. {--cp_id=:cp_id}
  17. {--category_id=:分类id}
  18. {--author=:作者}
  19. {--vip=:vip起始章节默认:8}
  20. {--filename=:文件名,放在storage/app/目录下}';
  21. /**
  22. * The console command description.
  23. *
  24. * @var string
  25. */
  26. protected $description = '导入书籍文本';
  27. /**
  28. * Execute the console command.
  29. */
  30. public function handle(): void
  31. {
  32. $create_info = $this->createBook();
  33. if($create_info['error']){
  34. return ;
  35. }
  36. $filname = $this->option('filename');
  37. if(!$filname){
  38. $this->error('filename empty');
  39. return ;
  40. }
  41. $result = $create_info['book'];
  42. $chapter_result = $this->createChapter( $result->id,$filname);
  43. $result->size = $chapter_result['size'];
  44. $result->chapter_count = $chapter_result['chapter_count'];
  45. $result->first_cid = $chapter_result['first_cid'];
  46. $result->last_cid = $chapter_result['last_cid'];
  47. $result->last_chapter = $chapter_result['last_chapter'];
  48. $result->save();
  49. }
  50. private function createBook(){
  51. $cp_id = $this->option('cp_id');
  52. $book_name = $this->option('name');
  53. $author = $this->option('author');
  54. $category_id = $this->option('category_id');
  55. $cp_name = $this->getCpName($cp_id);
  56. if(!$cp_name){
  57. $this->error('cp_id不存在');
  58. return ['error'=>1];
  59. }
  60. $category_info = $this->getCategoryInfo($category_id);
  61. if(!$category_info){
  62. $this->error('分类id不存在');
  63. return ['error'=>2];
  64. }
  65. $result = BooksService::createBook([
  66. 'cp_name'=>$cp_name,'cp_id'=>$cp_id,'name'=>$book_name,'author'=>$author ?? '','intro'=>'','cover'=>'','category_id'=>$category_id,
  67. 'category_name'=>$category_info->category_name,'status'=>1,'channel'=>$category_info->channel_id
  68. ]);
  69. if(!$result){
  70. $this->error('已经存在了');
  71. return ['error'=>3];
  72. }
  73. return ['error'=>0,'book'=>$result];
  74. }
  75. private function createChapter($bid,$filname){
  76. $vip_start = $this->option('vip');
  77. $path = storage_path('app/'.$filname);
  78. return ChapterService::createChapterFromFile($bid,1,$vip_start ?? 8,$path);
  79. }
  80. private function getCpName($cp_id){
  81. $cp_info = Cps::where('cp_id',$cp_id)->select('cp_nick')->first();
  82. if($cp_info){
  83. return $cp_info->cp_nick;
  84. }
  85. return '';
  86. }
  87. private function getCategoryInfo($category_id){
  88. return BookCategories::where('id',$category_id)->first();
  89. }
  90. }