<?php

namespace App\Console\Commands\Temp;

use Log;
use Illuminate\Console\Command;
use DB;

class SensitiveChapter extends Command
{
    /**
     * 执行命令   php artisan temp:sensitive_chapter
     *
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'temp:sensitive_chapter';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '敏感章节查询';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {

        print_r("======敏感章节查询 【任务执行开始】=====".date("y-m-d H:i:s"."\n"));
        Log::info("======敏感章节查询 【任务执行开始】=====".date("y-m-d H:i:s"."\n"));

        $bad_words = DB::table('bad_book_info')->pluck('badInfo')->all();
dump($bad_words);
        $bid_stas = [];
        $start = 0;
        $n = time();

        for($i=1;$i<5000;$i++)
        {
            dump($i);
            $end = 1000 * $i;
            $chapters = DB::select("select * from chapters where id >={$start} and id <{$end}");
            dump("select * from chapters where id >={$start} and id <{$end}");
            $start = $end;

            dump('start：' . date("Y-m-d H:i:s"));
            if(!$chapters) break;//章节获取即退出

            foreach ($chapters as $chapter) {
                $one_chapter_has_words = [];
                foreach ($bad_words as $bad_word)
                {
                    $bad_word = str_replace('/','\\',$bad_word);
                    preg_match_all("/{$bad_word}/", $chapter->content, $matches);
                    $current_count = count($matches[0]);
                    if ($current_count) {
                        $one_chapter_has_words[] = $bad_word;
                    }
                }

                if($one_chapter_has_words)
                {
                    //图书敏感章节数+1
                    $bid_stas[$chapter->bid] = @$bid_stas[$chapter->bid] + 1;
                    //插入章节敏感信息
                    $_chapter_data = [
                        'bid'=>$chapter->bid,
                        'cid'=>$chapter->id,
                        'sequence'=>$chapter->sequence,
                        'name'=>$chapter->name,
                        'words'=>implode(',',$one_chapter_has_words),
                        'time'=>$n,
                        'created_at'=>date("Y-m-d H:i:s"),
                        'updated_at'=>date("Y-m-d H:i:s")
                    ];
                    DB::table('sensitive_chapters')->insert($_chapter_data);
                }
            }
            dump($bid_stas);
            dump('end：' . date("Y-m-d H:i:s"));
        }

        foreach($bid_stas as $bid=>$num)
        {
            $book_config = DB::table('book_configs')->select('book_name')->where('bid',$bid)->first();
            $_data = [
                'bid'=>$bid,
                'num'=>$num,
                'time'=>$n,
                'book_name'=>$book_config ? $book_config->book_name : '',
                'created_at'=>date("Y-m-d H:i:s"),
                'updated_at'=>date("Y-m-d H:i:s")
            ];
            DB::table('book_sensitive_stats')->insert($_data);
        }

        Log::info("======敏感章节查询 【任务执行结束】=====" . date("y-m-d H:i:s" . "\n"));
        print_r("======敏感章节查询 【任务执行结束】=====" . date("y-m-d H:i:s" . "\n"));
    }
}
