<?php

namespace App\Http\Controllers;

use App\Models\Article;
use App\Models\ArticleCategory;
use App\Models\Category;
use App\Models\DedeArchive;
use App\Models\DedeArctype;
use App\Traits\Tree;

class TestController extends Controller
{

    use Tree;

    public function index()
    {

    }

    public function set_article_category()
    {
        $articles = Article::whereHas('category')
                           ->chunk(200, function ($articles) {
                               foreach ($articles as $article) {
                                   ArticleCategory::create([
                                       'article_id'  => $article->id,
                                       'category_id' => $article->category_id,
                                   ]);
                               }
                           });

        return true;

    }

    public function setCateArticle()
    {
        $article = [];
        $lists   = Category::where('content', '<>', '')->where('type', 'article')->get();
        if ($lists->isEmpty()) {
            dd('没有数据');
        }
        foreach ($lists as $key => $cate) {
            if ($cate->content != '&nbsp;') {
                $data = [
                    'oldid'       => 0,
                    'title'       => $cate->title,
                    'category_id' => $cate->id,
                    'writer'      => 'admin',
                    'source'      => '未知',
                    'keywords'    => '',
                    'status'      => 1,
                    'description' => $cate->description,
                    'content'     => $cate->content,
                ];

                $info             = Article::create($data);
                $cate->article_id = $info->id;
                $cate->type       = Category::TYPE_LINK;
                $cate->save();
                $article[] = $info->id;
            }

        }
        dump(count($article));
    }

    public function checkArticle()
    {
        $articleids = Article::where('oldid', '>', 0)->pluck('oldid');
        $oldids     = DedeArchive::pluck('id');
        $diffids    = array_diff($oldids->toArray(), $articleids->toArray());
        dump(count($articleids));
        dump(count($oldids));
        dump($diffids);
        die();
        $map  = [
            'id' => ['in', $diffids],
        ];
        $list = DedeArchive::whereIn('id', $diffids)->get();
        foreach ($list as $key => $article) {
            $data = [
                'oldid'       => $article->id,
                'title'       => $article->title,
                'category_id' => $category->id ?? '0',
                'writer'      => $article->writer,
                'cover'       => $article->litpic,
                'source'      => $article->source,
                'keywords'    => $article->keywords,
                'description' => $article->description,
                'status'      => 1,
                'content'     => $article->info->body ?? '',
                'created_at'  => date('Y-m-d H:i:s', $article->pubdate),
            ];
            Article::create($data);
        }
    }

    //导入文章
    public function set_article()
    {
        $articles = Article::get();
        if ($articles->count() > 4) {
            dd('已经导入过数据');
        }
        $categorys = Category::get();
        $error     = $success = [];
        DedeArchive::whereNotNull('litpic')->chunk(200, function ($articles) use ($categorys) {

            foreach ($articles as $article) {

                $category = $categorys->where('oldid', $article->typeid)->first();
                $data     = [
                    'oldid'       => $article->id,
                    'title'       => $article->title,
                    'category_id' => $category->id ?? '0',
                    'writer'      => $article->writer,
                    'source'      => $article->source,
                    'cover'       => $article->litpic,
                    'keywords'    => $article->keywords,
                    'description' => $article->description,
                    'status'      => 1,
                    'content'     => $article->info->body ?? '',
                    'created_at'  => date('Y-m-d H:i:s', $article->pubdate),
                ];

                $res = Article::create($data);
                if (!$res) {
                    $error[] = $article->id;
                } else {
                    $success[] = $article->id;
                }
            }
        });

        dump($error);
        dump($success);
    }

    //导入分类
    public function set_category()
    {
        $categorys = Category::get();
        if ($categorys->count()) {
            dd('已经导入过数据');
        }
        $lists = DedeArctype::where('ishidden', 0)
                            ->select('id', 'reid as parent_id', 'typename as title', 'content')
                            ->get();
        $list  = Tree::list2tree($lists->toArray(), 'id', 'parent_id', 'children', 0);

        foreach ($list as $key => $value) {
            $info = Category::create($this->getData($value));
            if (isset($value['children']) && count($value['children']) > 0) {
                foreach ($value['children'] as $key => $children) {
                    $info->children()->create($this->getData($children));
                }
            }
        }
    }

    //格式化分类数据
    public function getData($category)
    {
        $data = [
            'oldid'     => $category['id'],
            'parent_id' => $category['parent_id'],
            'title'     => $category['title'],
            'content'   => $category['content'],
            'status'    => 1,
        ];

        return $data;
    }

}
