<?php

namespace App\Web\Services;


use App\Web\Models\BlogModel;
use App\Web\Models\BlogPlateModel;
use App\Web\Models\BlogTagModel;
use App\Web\Models\BlogTagRelationModel;
use App\Web\Models\BlogTypeModel;
use App\Web\Models\BlogTypeRelationModel;

class BlogRenderService
{
    protected $blogService;
    protected $blogModel;
    protected $blogTypeModel;
    protected $blogTagModel;
    protected $blogTypeRelationModel;
    protected $blogTagRelationModel;
    protected $blogPlateModel;

    public function __construct(BlogModel $blogModel,
                                BlogTypeModel $blogTypeModel,
                                BlogTypeRelationModel $blogTypeRelationModel,
                                BlogTagModel $blogTagModel,
                                BlogTagRelationModel $blogTagRelationModel,
                                BlogPlateModel $blogPlateModel,
                                BlogService $blogService)
    {
        $this->blogPlateModel = $blogPlateModel;
        $this->blogTagRelationModel = $blogTagRelationModel;
        $this->blogTypeRelationModel = $blogTypeRelationModel;
        $this->blogTagModel = $blogTagModel;
        $this->blogTypeModel = $blogTypeModel;
        $this->blogModel = $blogModel;
        $this->blogService = $blogService;
    }

    /**
     * 获取已发布的文章
     * @param $plateId
     * @param $keyword
     * @param $page
     * @param $pageSize
     * @param array $sortField
     * @param int $typeId
     * @param int $tagId
     * @param $fields
     * @return array
     */
    public function getPublishBlogList($plateId, $keyword, $page, $pageSize, array $sortField = [],  $typeId = 0,  $tagId = 0, $fields = null,$echoContent=false): array
    {
        $params = [
            'page' => $page,
            'page_size' => $pageSize,
            'type_id' => $typeId,
            'tag_id' => $tagId,
            'plate_id' => $plateId,
            'keyword' => $keyword
        ];
        if (!empty($sortField)) {
            foreach ($sortField as $sf) {
                $params['sort'][$sf] = 1;
            }
        }

        $fields ?? $fields = 'a.id,a.plate_id,a.pub_date,a.create_time,a.title,a.author,a.description,a.image_url,a.image_alt,a.is_top,a.is_recommend,a.download_url,a.total_view,b.urla,c.type_id,d.tag_id,a.expand_content,a.main_image';
        if ($echoContent) {
            $fields .= ',a.content';
        }
        $blogList = $this->blogModel->getPublishBlogListByPage($params, $fields);

        if(!empty($blogList['data'])) {
            $blogIds = array_column($blogList['data'], 'id');
            if ($blogIds) {
                $newestTypeAndTagData = $this->blogService->getTypeAndTagDataByBlogId($blogIds);
                foreach ($blogList['data'] as &$newestItem) {
                    if (!empty($newestTypeAndTagData[$newestItem['id']]['type_items'])) {
                        $newestItem['type_items'] = $newestTypeAndTagData[$newestItem['id']]['type_items'];
                    } else {
                        $newestItem['type_items'] = [];
                    }
                    if (!empty($newestTypeAndTagData[$newestItem['id']]['tag_items'])) {
                        $newestItem['tag_items'] = $newestTypeAndTagData[$newestItem['id']]['tag_items'];
                    } else {
                        $newestItem['tag_items'] = [];
                    }
                    $newestItem['expand_content'] = empty($newestItem['expand_content'])?[]:json_decode($newestItem['expand_content'],true);
                    $newestItem['expand_content'] = mapByKey($newestItem['expand_content'],'key');
                }
            }
        }
        return $blogList;
    }

    /**
     * @param $blogId
     * @return array
     */
    public function getPublishedBlogInfo($blogId): array
    {
        $data = $this->blogService->getBlogRenderData([
            'id' => $blogId,
            'status' => 0
        ]);
        if(!empty($data['type_items'])) {
            $typeIds = array_column($data['type_items'], 'type_id');
            $typeCountArr = $this->blogTypeRelationModel->getBlogTypeCount($typeIds);
            if(!empty($typeCountArr)) {
                foreach ($data['type_items'] as &$dyi) {
                    if(!empty($typeCountArr[$dyi['type_id']])) {
                        $dyi['type_count'] = $typeCountArr[$dyi['type_id']];
                    } else {
                        $dyi['type_count'] = 0;
                    }
                }
            }
        }
        if(!empty($data['tag_items'])) {
            $tagIds = array_column($data['tag_items'], 'tag_id');
            $tagCountArr = $this->blogTagRelationModel->getBlogTagCount($tagIds);
            if(!empty($tagCountArr)) {
                foreach ($data['tag_items'] as &$dgi) {
                    if(!empty($tagCountArr[$dgi['tag_id']])) {
                        $dgi['tag_count'] = $tagCountArr[$dgi['tag_id']];
                    } else {
                        $dgi['tag_count'] = 0;
                    }
                }
            }
        }
        return $data;
    }

    /**
     * 获取分类
     * @param $plateId
     * @param int $newsId
     * @return mixed
     */
    public function getPublishedTypeList($plateId, int $newsId = 0, $limit = 4)
    {
        return $this->blogTypeModel->getLimitBlogType($plateId, $limit, $newsId);
    }

    /**
     * 获取标签
     * @param $plateId
     * @param int $newsId
     * @return mixed
     */
    public function getPublishedTagList($plateId, int $newsId = 0)
    {
        return $this->blogTagModel->getLimitBlogTag($plateId, 69, $newsId);
    }

    /**
     * @param $plateId
     * @return array
     */
    public function getPlateInfo($plateId): array
    {
        return $this->blogPlateModel->getBlogPlateInfo(['id' => $plateId]);
    }

    public function getPreBlog($plateId, $newsId)
    {
       return $this->blogModel->getPrevBlog($plateId, $newsId);
    }
    public function getNextBlog($plateId, $newsId)
    {
        return $this->blogModel->getNextBlog($plateId, $newsId);
    }

    public function getBlogTypeInfo($typeId)
    {
       $blog_type =  $this->blogTypeModel->getBlogTypeById($typeId);
       if (!empty($blog_type['expand_content'])) {
           $blog_type['expand_content'] = json_decode($blog_type['expand_content'],true);
           $blog_type['expand_content'] = mapByKey($blog_type['expand_content'],'key');
       }
       return $blog_type;
    }


    public function getBlogTagInfo($tagId)
    {
        return $this->blogTagModel->getBlogTagById($tagId);
    }

}
