cpCollection.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace App\Console\Commands\ContentManage;
  3. use Illuminate\Console\Command;
  4. use Modules\ContentManage\Models\Cp\CpCollection as CpCollectionModel;
  5. use Modules\ContentManage\Models\Book;
  6. use Illuminate\Support\Facades\Process;
  7. use Modules\ContentManage\Services\Notice\NoticesService;
  8. use Modules\Permissions\Models\Roles;
  9. use Log;
  10. class cpCollection extends Command
  11. {
  12. /**
  13. * The name and signature of the console command.
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'ContentManage:cpcollection';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = 'Command description';
  24. private $cp_name = '';
  25. private $spider_name = '';
  26. private $cp_id = 0;
  27. private $commad_status = -1;
  28. private $start_time;
  29. private $record = null;
  30. public function __construct(protected readonly CpCollectionModel $CpCollectionModel,protected readonly Book $book)
  31. {
  32. parent::__construct();
  33. }
  34. /**
  35. * Execute the console command.
  36. */
  37. public function handle(): void
  38. {
  39. $this->getTask();
  40. if(!$this->cp_id){
  41. return ;
  42. }
  43. $this->start_time = date('Y-m-d H:i:s');
  44. $this->runSpider();
  45. if($this->commad_status){
  46. $this->record->spider_status = 3;
  47. }else{
  48. $this->record->spider_status = 2;
  49. }
  50. $spider_result = $this->getResult();
  51. $this->record->spider_result = $spider_result->pluck('id')->implode(',');
  52. $this->record->save();
  53. $this->notice($spider_result);
  54. }
  55. /**
  56. * 获取同步任务,一次只获取一个
  57. * @return void
  58. */
  59. private function getTask(){
  60. $record = $this->CpCollectionModel->where('created_at','>',date('Y-m-d H:i:s',time()-86400))->where('spider_status',0)->first();
  61. if($record){
  62. $record->spider_status = 1;
  63. $record->save();
  64. $this->cp_name = $record->cp_name;
  65. $this->cp_id = $record->cp_id;
  66. $this->spider_name = str_replace('zy_','',$this->cp_name);
  67. $this->record = $record;
  68. }
  69. }
  70. /**
  71. * 执行脚本,并获取状态
  72. * @return void
  73. */
  74. private function runSpider(){
  75. $command = ' bash /project/www/zhiyu_content_spider/content_spider/bash/command.sh '.$this->spider_name;
  76. Log::info('cpCollection',['command'=>$command]);
  77. $result = Process::forever()->run($command);
  78. if($result->successful()){
  79. $this->commad_status = 0;
  80. }else{
  81. $this->commad_status = 1;
  82. }
  83. }
  84. /**
  85. * 获取同步结果
  86. */
  87. private function getResult(){
  88. return $this->book->where('cp_id',$this->cp_id)
  89. ->where('created_at','>=',$this->start_time)
  90. ->where('created_at','<=',date('Y-m-d H:i:s'))
  91. ->select('id','name')->get();
  92. }
  93. /**
  94. * 通知同步结果
  95. * @param [type] $collection_result
  96. * @return void
  97. */
  98. private function notice($collection_result){
  99. $role = Roles::where('identify','contentadmin')->select('id','role_name')->first();
  100. $notice_obj = [];
  101. if($role){
  102. $notice_obj[] = ['id'=>$role->id,'name'=>$role->role_name];
  103. }
  104. $admin_role = Roles::where('identify','admin')->select('id','role_name')->first();
  105. if($admin_role){
  106. $notice_obj[] = ['id'=>$admin_role->id,'name'=>$admin_role->role_name];
  107. }
  108. if(!$notice_obj){
  109. $notice_obj = [['id'=>6,'name'=>'内容管理员']];
  110. }
  111. $end_time = date('Y-m-d H:i:s');
  112. $count = $collection_result->count();
  113. $books = $collection_result->pluck('name')->implode(',');
  114. $content = $this->start_time.' 同步'.$this->cp_name."任务执行完成\r\n此次执行任务在 {$end_time}执行完成,增量同步{$count}本书如下:\r\n".$books;
  115. NoticesService::addNotice([
  116. 'title' => $this->cp_name.'书籍同步完成',
  117. 'notice_type_id' => 2,
  118. 'type' => 3,
  119. 'notice_obj'=>$notice_obj,
  120. 'is_popup' => 1,
  121. 'content' => $content
  122. ]);
  123. }
  124. }