<?php

namespace app\command;

use app\model\dynasty\Dynasty;
use app\model\dynasty\DynastyAuthor;
use Overtrue\Pinyin\Pinyin;
use QL\QueryList;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use function DI\get;

class PoemAuthor extends Command
{

    protected static string $defaultName = 'poem:author';
    protected static string $defaultDescription = '采集作者';

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        try {
            $nextPage = 1; $done = 0; $subPage = 1;
            while (is_numeric($nextPage)) {
                $output->writeln("开始".getDateFull());
                $resp = (new Dynasty)->where("type",1)->page($nextPage,10)->select();
                if ($resp->isEmpty()) {
                    $output->writeln("没有更多任务啦！===".getDateFull());
                    $nextPage = null;
                    return self::SUCCESS;
                }
                $author = (new DynastyAuthor);
                foreach ($resp as $key=>$val) {
                    $output->writeln("朝代：{$val['name']}==页码：{$subPage}===链接地址：https://www.gushiji.cc/shiren/{$val['pinyin']}.html====".getDateFull());
                    while (is_numeric($subPage)) {
                        if ($subPage == 1) {
                            $url = "https://www.gushiji.cc/shiren/{$val['pinyin']}.html";
                        } else {
                            $url = "https://www.gushiji.cc/shiren/{$val['pinyin']}_{$subPage}.html";
                        }
                        $ql = QueryList::getInstance()->get($url)->rules([
                            "name"      => ['.simple-people>li','texts'],
                            "link"      => ['.simple-people>li>a','attrs(href)'],
                            "avatar"    => ['.simple-people>li>a>img','attrs(src)'],
                            "page"      => ['#pagenum>li.info','text'],
                        ])->query()->getData(function ($item,$key){
                            foreach ($item['avatar'] as $k=>$v){
                                if (empty($v)){
                                    $v = "/skin/images/nopicture.jpg";
                                }
                                $item['avatar'][$k] = "https://www.gushiji.cc".$v;
                            }
                            foreach ($item['link'] as $k=>$v){
                                $link = explode("/",$v);
                                $item['link'][$k] = $link[2]??'';
                            }
                            $poem = [];
                            foreach ($item['name'] as $k=>$v){
                                preg_match('/([^()（）]+)[(（](\d+)[)）]/u', $v, $match);
                                $item['name'][$k] = $match[1]??'';
                                $poem[$k] = $match[2]??0;
                            }
                            preg_match_all('/\d+/', $item['page'], $matches);
                            $item['page'] = $matches[0][0] ?? 0;
                            $item['total'] = $matches[0][1] ?? 0;
                            $item['poem'] = $poem;
                            return $item;
                        });
                        $respData = $ql->all();
                        if ($subPage > $respData['page']) {
                            $output->writeln("朝代：{$val['name']}==没有作者啦==".getDateFull());
                            $subPage = 1;
                            break;
                        }
                        foreach ($respData['name'] as $k=>$v){
                            $authorData = $author->where(['name' => $v, 'dynasty_id' => $val['id']])->findOrEmpty();
                            if ($authorData->isEmpty()) {
                                $output->writeln("【新增】朝代：{$val['name']}==诗人：{$v}==拼音：".trim($respData['link'][$k])."==".getDateFull());
                                $authorData->insertGetId([
                                    "name"          => $v,
                                    "poem_total"    => $respData['poem'][$k],
                                    "avatar"        => $respData['avatar'][$k],
                                    "dynasty_id"    => $val['id'],
                                    "dynasty_name"  => $val['name'],
                                    "initial"       => strtoupper(substr($respData['link'][$k], 0, 1)),
                                    "pinyin"        => $respData['link'][$k]
                                ]);
                            } else {
                                $output->writeln("【更新】朝代：{$val['name']}==诗人：{$v}==拼音：".trim($respData['link'][$k])."==".getDateFull());
                                $authorData->save([
                                    "name"          => $v,
                                    "poem_total"    => $respData['poem'][$k],
                                    "avatar"        => $respData['avatar'][$k],
                                    "dynasty_id"    => $val['id'],
                                    "dynasty_name"  => $val['name'],
                                    "initial"       => strtoupper(substr($respData['link'][$k], 0, 1)),
                                    "pinyin"        => $respData['link'][$k]
                                ]);
                            }
                        }
                        $subPage = $subPage+1;
                        $output->writeln("进入下一页=={$nextPage}-{$subPage}==".getDateFull());
                        $output->writeln("===========================================================================");
                    }
                }
                $nextPage = $nextPage+1;
                $subPage = 1;
                $output->writeln("进入下一页=={$nextPage}==".getDateFull());
            }
        } catch (\Throwable $throwable) {

        }
        return self::SUCCESS;
    }

}