<?php

namespace App\Http\Controllers;

use App\Models\Article;
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 checkCategory()
    {

        // $lists = Category::where('created_at','2020-06-03 15:57:55')->get();
        // $i=1;
        // foreach ($lists as $info){
        //     $old = DedeArctype::select('id', 'reid as parent_id', 'typename as title', 'content')->find($info->oldid);
        //     $cate = Category::where('oldid',$old->parent_id)->first();
        //     $info->parent_id = $cate->id;
        //     $info->save();
        //     $i++;
        // }

        // dd($i);
        // dump(count($lists));
        // dd();
        $cateids = Category::where('oldid', '>', 0)->pluck('oldid');
        $oldids  = DedeArctype::where('ishidden', 0)->pluck('id');
        $diffids = array_diff($oldids->toArray(), $cateids->toArray());
        dump(count($cateids));
        dump(count($oldids));
        dump($diffids);
        dd();
        foreach ($diffids as $diffid) {
            $info = DedeArctype::where('id', $diffid)
                               ->where('ishidden', 0)
                               ->select('id', 'reid as parent_id', 'typename as title', 'content')
                               ->first();
            $data = $this->getData($info);
            Category::create($data);
        }
    }

    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,
                    'created_at'  => $cate->created_at,
                ];

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

        }
        dump(count($article));
    }

    public function checkArticle()
    {
        $lists = Article::where('category_id', 0)->get();

        foreach ($lists as $list) {
            $old  = DedeArchive::find($list->oldid);
            $cate = Category::where('oldid', $old->typeid)->first();
            if (!$cate || !$old) {
                dump($old);
                dump($cate);
                dd('出问题了');
            }
            $list->category_id = $cate->id;
            $list->save();
        }
        dd();
        $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() > 1) {
            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;
    }

}
