BookAdjust.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Modules\Book\Models\Chapter;
  5. use App\Modules\Book\Models\Book;
  6. class BookAdjust extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'book:adjust';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = '调整所有章节顺序prev_cid和next_cid';
  20. /**
  21. * Create a new command instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. }
  29. /**
  30. * Execute the console command.
  31. *
  32. * @return mixed
  33. */
  34. public function handle()
  35. {
  36. $this->adjustSequentAll();
  37. return 1;
  38. }
  39. /**
  40. * 调整所有顺序
  41. */
  42. public function adjustSequentAll(){
  43. set_time_limit(0);
  44. Book::where('last_cid','!=',0)->where('id','>=',807)->select('id')->get()->map(function ($item, $key){
  45. $this->adjustSequentOne($item->id);
  46. });
  47. }
  48. /**
  49. * 调整单本书的顺序
  50. * @param $bid
  51. */
  52. public function adjustSequentOne($bid){
  53. $chapter_list = Chapter::getChapterLists($bid);
  54. $prev = 0;
  55. foreach ($chapter_list as $chapter){
  56. if($prev){
  57. Chapter::where('id',$chapter->id)->update(['prev_cid'=>$prev]);
  58. Chapter::where('id',$prev)->update(['next_cid'=>$chapter->id]);
  59. }
  60. $prev = $chapter->id;
  61. }
  62. }
  63. }