<?php

namespace App\Console\Commands\ContentManage;

use Illuminate\Console\Command;
use Modules\ContentManage\Models\Cp\CpCollection as CpCollectionModel;
use Modules\ContentManage\Models\Book;
use Illuminate\Support\Facades\Process;
use Modules\ContentManage\Services\Notice\NoticesService;
use Modules\Permissions\Models\Roles;
use Log;


class cpCollection extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'ContentManage:cpcollection';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    private $cp_name = '';
    private $spider_name = '';
    private $cp_id = 0;
    private $commad_status = -1;
    private $start_time;

    private $record = null;



    public function __construct(protected readonly CpCollectionModel $CpCollectionModel,protected readonly Book $book)
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     */
    public function handle(): void
    {
        $this->getTask();
        if(!$this->cp_id){
            return ;
        }
        $this->start_time = date('Y-m-d H:i:s');
        $this->runSpider();
        if($this->commad_status){
            $this->record->spider_status = 3;
        }else{
            $this->record->spider_status = 2;
        }
        $spider_result = $this->getResult();
        $this->record->spider_result = $spider_result->pluck('id')->implode(',');
        $this->record->save();
        $this->notice($spider_result);
    }

    /**
     * 获取同步任务,一次只获取一个
     * @return void
     */
    private function getTask(){
        $record = $this->CpCollectionModel->where('created_at','>',date('Y-m-d H:i:s',time()-86400))->where('spider_status',0)->first();
        if($record){
            $record->spider_status = 1;
            $record->save();
            $this->cp_name = $record->cp_name; 
            $this->cp_id = $record->cp_id;
            $this->spider_name = str_replace('zy_','',$this->cp_name);
            $this->record = $record;
        }
    }


    /**
     * 执行脚本,并获取状态
     * @return void
     */
    private function runSpider(){
        $command = ' bash /project/www/zhiyu_content_spider/content_spider/bash/command.sh '.$this->spider_name;
        Log::info('cpCollection',['command'=>$command]);
        $result = Process::forever()->run($command);
        if($result->successful()){
            $this->commad_status = 0;
        }else{
            $this->commad_status = 1;
        }
    }

    /**
     * 获取同步结果
     */
    private function getResult(){
       return  $this->book->where('cp_id',$this->cp_id)
        ->where('created_at','>=',$this->start_time)
        ->where('created_at','<=',date('Y-m-d H:i:s'))
        ->select('id','name')->get();
    }


    /**
     * 通知同步结果
     * @param [type] $collection_result
     * @return void
     */
    private function notice($collection_result){
        $role = Roles::where('identify','contentadmin')->select('id','role_name')->first();
        $notice_obj = [];
        if($role){
            $notice_obj[] = ['id'=>$role->id,'name'=>$role->role_name];
        }

        $admin_role = Roles::where('identify','admin')->select('id','role_name')->first();
        if($admin_role){
            $notice_obj[] = ['id'=>$admin_role->id,'name'=>$admin_role->role_name];
        }
        if(!$notice_obj){
            $notice_obj = [['id'=>6,'name'=>'内容管理员']];
        }

        $end_time = date('Y-m-d H:i:s');
        $count = $collection_result->count();
        $books = $collection_result->pluck('name')->implode(',');
        $content =  $this->start_time.' 同步'.$this->cp_name."任务执行完成\r\n此次执行任务在 {$end_time}执行完成,增量同步{$count}本书如下:\r\n".$books;

        NoticesService::addNotice([
            'title' => $this->cp_name.'书籍同步完成',
            'notice_type_id' => 2,
            'type' => 3,
            'notice_obj'=>$notice_obj,
            'is_popup' => 1,
            'content' => $content
        ]);
    }
}