<?php

namespace app\command;

use app\model\dynasty\DynastyAuthor;
use QL\QueryList;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class PoemData extends Command
{

    protected static string $defaultName = 'poem:data';

    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 DynastyAuthor)->page($nextPage,10)->select();
                if ($resp->isEmpty()) {
                    $output->writeln("没有更多任务啦！===".getDateFull());
                    $nextPage = null;
                    return self::SUCCESS;
                }
                $poem = (new \app\model\poem\PoemData);
                foreach ($resp as $key=>$val) {
                    while (is_numeric($subPage)) {
                        if ($subPage == 1) {
                            $url = "https://www.gushiji.cc/shiren/{$val['pinyin']}/";
                        } else {
                            $url = "https://www.gushiji.cc/shiren/{$val['pinyin']}/p{$subPage}.html";
                        }
                        $output->writeln("诗人：{$val['name']}==页码：{$subPage}===链接地址：{$url}====".getDateFull());
                        $ql = QueryList::getInstance()->get($url)->rules([
                            "page"      => ['#pagenum>li.info','text'],
                            "name"      => ['.simple-shiciqu>li','texts'],
                            "link"      => ['.simple-shiciqu>li>a','attrs(href)'],
                        ])->query()->getData(function ($item,$key){
                            preg_match_all('/\d+/', $item['page'], $matches);
                            $item['page'] = $matches[0][0] ?? 0;
                            $item['total'] = $matches[0][1] ?? 0;
                            foreach ($item['link'] as $k=>$v){
                                preg_match('/\/([^\/]+)\.html$/', $v, $match);
                                $item['link'][$k] = $match[1]??'';
                            }
                            return $item;
                        });
                        $respData = $ql->all();
                        if ($subPage > $respData['page']) {
                            $output->writeln("作者：{$val['name']}==没有作品啦==".getDateFull());
                            $subPage = 1;
                            break;
                        }
                        foreach ($respData['name'] as $k=>$v) {
                            $authorData = $poem->where(['poem_id' => $respData['link'][$k]])->findOrEmpty();
                            if ($authorData->isEmpty()) {
                                $output->writeln("【新增】作者作品：{$val['name']}==标题：{$v}====".getDateFull());
                                $authorData->insertGetId([
                                    "title"         => $v,
                                    "poem_id"       => $respData['link'][$k],
                                    "author_id"     => $val['id'],
                                    "author_name"   => $val['name'],
                                    "dynasty_id"    => $val['dynasty_id'],
                                    "dynasty_name"  => $val['dynasty_name'],
                                ]);
                            } else {
                                $output->writeln("【更新】作者作品：{$val['name']}==标题：{$v}====".getDateFull());
                                $authorData->save([
                                    "title"         => $v,
                                    "poem_id"       => $respData['link'][$k],
                                    "author_id"     => $val['id'],
                                    "author_name"   => $val['name'],
                                    "dynasty_id"    => $val['dynasty_id'],
                                    "dynasty_name"  => $val['dynasty_name'],
                                ]);
                            }
                        }
                        $subPage = $subPage+1;
                        $output->writeln("进入下一页=={$nextPage}-{$subPage}==".getDateFull());
                        $output->writeln("===========================================================================");
                    }
                }
                $nextPage = $nextPage+1;
                $subPage = 1;
                $output->writeln("进入下一个作者=={$nextPage}==".getDateFull());
            }
        } catch (\Throwable $throwable) {
            echo $throwable->getMessage()."\n";
        }
        return self::SUCCESS;
    }

}