<?php
/**
 * Created by PhpStorm.
 * User: 中闽 < 1464674022@qq.com >
 * Date: 2020/2/4
 * Time: 12:47
 */

namespace app\common\command;

use app\common\model\Article;
use app\common\model\Catalog;
use app\common\service\CmsService;
use think\console\Command;
use think\console\Input;
use think\console\Output;

class ExportTagList extends Command
{
    /**
     * 批量生成标签页html
     * php think ExportTagList
     */
    protected function configure()
    {
        $this->setName('ExportTagList');
    }

    protected function execute(Input $input, Output $output)
    {
        $catalog = (new Catalog())->where('title', '标签页')->find();
        if(!$catalog){
            exit('没有配置标签页栏目');
        }

        $articles = (new Article())->where('tag', '<>', '')->select();
        $tags = [];
        foreach ($articles as $article) {
            /** @var Article $article */
            $tags = array_merge($tags, $article->getTags());
        }
        $tags = array_unique($tags);

        $service = new CmsService();
        $catalogPath = $catalog->getRealPath();
        foreach ($tags as $tag) {
            $catalogPath2 = str_replace(['index', ' '], [$tag, '_'], $catalogPath);
            $service->info("标签页:" . $catalogPath2);
            $catalog->tag = $tag;//通过动态变量传递数据
            $content = $service->perviewCatalog($catalog);
            $service->createFile($catalogPath2, $content);
        }
    }
}