<?php

namespace App\Console\Commands\Book;

use App\Modules\Book\Models\Book;
use App\Modules\Book\Models\BookCategory;
use App\Modules\Book\Models\BookConfig;
use App\Modules\Book\Models\RententionBookList;
use App\Modules\Book\Services\RententionBookService;
use Illuminate\Console\Command;
use Log;

/**
 * 章节留存率
 */
class ChapterRetentionRate extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'calc_chapter_retention_rate {--is_init=} {--is_all=} {--bid=} {--begin_bid=} {--end_bid=}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '计算章节留存率';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 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"));

        $is_all = $this->option('is_all');
        $is_init = $this->option('is_init');
        if ($is_init) {
            $bids = RententionBookList::where('sex', 2)->pluck('bid')->all();
            foreach ($bids as $bid) {
                RententionBookService::saveRententionBook($bid);
            }
        } else if ($is_all) {
            $bids = $this->findAllRunBooks();
            foreach ($bids as $bid) {
                $this->runBook($bid);
            }
        } else {
            $bid = $this->option('bid');
            $begin_bid = $this->option('begin_bid');
            $end_bid = $this->option('end_bid');
            if ($begin_bid) {
                $bids = Book::where('id', '>=', $begin_bid)
                    ->where('id', '<', $end_bid)
                    ->orderBy('id', 'desc')
                    ->pluck('id')
                    ->all();
                foreach ($bids as $bid) {
                    $this->runBook($bid);
                }
            } else {
                $this->runBook($bid);
            }
        }

        Log::info("======计算章节留存率 【任务执行结束】=====" . date("y-m-d H:i:s" . "\n"));
        print_r("======计算章节留存率 【任务执行结束】=====" . date("y-m-d H:i:s" . "\n"));
    }

    public function findAllRunBooks()
    {
        $catetory_ids = BookCategory::where('pid', 2)->pluck('id')->all();
        $new_bids = Book::where('created_at', '>=', date('Y-m-d', strtotime('-3 months')))
            ->whereIn('category_id', $catetory_ids)
            ->pluck('id')->all();
        $bids = BookConfig::whereIn('bid', $new_bids)->whereIn('is_on_shelf', [1, 2])->pluck('bid')->all();
        $rentention_bids =  RententionBookList::join('book_configs', 'book_configs.bid', 'rentention_book_list.bid')
            ->where([
                'is_updated' => 1,
            ])->where('type', '!=', RententionBookService::BelowStandard)
            ->where('sex', 2)
            ->whereIn('is_on_shelf', [1, 2])
            ->pluck('rentention_book_list.bid')->all();
        return array_merge(
            $rentention_bids,
            $bids
        );
    }


    public function runBook(int $bid)
    {
        myLog('rentention_book')->info('bid: ' . $bid . ' begin: ' . date('Y-m-d H:i:s'));
        RententionBookService::runRentention($bid);
        RententionBookService::saveRententionBook($bid);
        myLog('rentention_book')->info('bid: ' . $bid . ' end: ' . date('Y-m-d H:i:s'));
    }
}