<?php
namespace App\Services\Content;

use App\Repositories\ArticleRepository;
use App\Repositories\ArticlePropertyReoository;
use Illuminate\Support\Facades\Cache;

class ArticleService
{

    /**
     * @var ArticleRepository
     */
    protected $articleRepository;
    protected $articlePropertyReoository;

    /**
     * ArticleAuthService constructor.
     * @param $articleRepository
     */
    public function __construct(ArticleRepository $articleRepository, ArticlePropertyReoository $articlePropertyReoository)
    {
        $this->articleRepository = $articleRepository;
        $this->articlePropertyReoository = $articlePropertyReoository;
    }

    public function list($key = '', $id = '', $page = '')
    {
        $where = array();
        $map = array();
        $where[] = array('is_display','=','1');
        $map[] = array('is_display','=','1');

        if ($key) {
            $where[] = array('title','like','%'.$key.'%');
            $map[] = array('title','like','%'.$key.'%');
        }

        if ($id) {
            $where[] = array('type_id','=',$id);

            $list=$this->articleRepository->getArticles($where, $page);
        } else {
            $or_map = $map;
            $or_map[] = array('type_id','=','1');
//            $map[] = array('parent_id','=','1');
            $list=$this->articleRepository->getAllArticles($map, $or_map, $page);
        }
        if ($list->toArray()) {
            foreach ($list as $k => $v) {
                $list[$k]->origin_title = $v->title;
                $style= '';
                if ($v->tit_color) {
                    $style .='color:'.$v->tit_color.';';
                }
                if ($v->tit_b=='1') {
                    $style .='font-weight:bold;';
                }
                if ($style) {
                    $list[$k]->title = '<span style='.$style.'>'.$v->title.'</span>';
                }
            }
        }

        return $list;
    }
    //获取指定属性的新闻资讯
    public function getPropertyArticles($property_id, $limit = '5')
    {
        return $this->articleRepository->getPropertyArticles($property_id, $limit);
    }
    //获取指定分类的新闻资讯
    public function getArticlesByType($type_id, $limit = '5')
    {
        $articles = $this->articleRepository->getArticlesByType($type_id, $limit);
        foreach ($articles as $key => $val){
            $articles[$key]['url'] = route('news.show', ['id'=>$val->id]);
        }
        return $articles;
    }
    //获取前台显示的新闻信息
    public function getArticleInfo($id,$fairjob = 0)
    {
        $page_new = get_subsite_id() == 0 ? 'news.show' : 'jkq.news.show';
        $where = array(
            'id' => $id,
            'is_display'=>'1',
        );
        $article_info = $this->articleRepository->firstWhere($where);
        if (empty($article_info)){
            return $article_info;
        }
        if ($article_info) {
            $property = $this->articlePropertyReoository->getProperty(['id'=>$article_info->property_id]);
            $article_info->property = '';
            if ($property) {
                $article_info->property = $property->category_name;
            }
        }

        //获取相同分类下的所有新闻列表
        $lists = $this->list('', $article_info->type_id);
        $pre_info = array();
        $nex_info = array();
        $article_info->prev = 0;
        $article_info->next = 0;

        $info_key = -1;
        if ($lists->toArray()) {
            foreach ($lists as $k => $v) {
                if ($v->id==$id) {
                    $info_key = $k;
                }
            }
        }

        $prev = $info_key>-1 && !empty($lists[$info_key-1]) ? $lists[$info_key-1] : 0;
        if ($prev) {
            $article_info->prev = 1;
            $article_info->prev_title = $prev->origin_title;
            if($fairjob){
                $article_info->prev_url = route('jobfair.new.show', ['id'=>$prev->id]);
            }else{
                $article_info->prev_url = route($page_new, ['id'=>$prev->id]);
            }

        }

        $next = $info_key>-1 && !empty($lists[$info_key+1]) ? $lists[$info_key+1] : 0;
        if ($next) {
            $article_info->next = 1;
            $article_info->next_title = $next->origin_title;
            if($fairjob){
                $article_info->next_url = route('jobfair.new.show', ['id'=>$next->id]);
            }else{
                $article_info->next_url = route($page_new, ['id'=>$next->id]);
            }

        }

        return $article_info;
    }

    public function incrementData($where, $num, $filed)
    {
        return $this->articleRepository->incrementData($where, $num, $filed);
    }
    public function getArticleCache($params, $type = 'home')
    {
        $lists = Cache::get('article_index_list_'.$type.'_'.get_subsite_id());
        if ($lists === null) {
            //获取指定分类的资讯信息
            $where = array(
                array('is_display','=',1)
            );
            $whereIn = array();
            if (array_has($params, 'type_id')) {
                if (is_array($params['type_id'])) {
                    $whereIn['type_id'] = $params['type_id'];
                } else {
                    $where[] = array('type_id', '=',$params['type_id']);
                }
            }
            $order = array(
                'list_order' => 'desc',
                'created_at' => 'desc'
            );
            $rst = $this->articleRepository->filterArticles($where, $whereIn, $order);
            $lists = array();
            if ($rst->isNotEmpty()) {
                foreach ($rst as $k => $v) {
                    if (array_has($params, 'titlelen')) {
                        $dot = '...';
                        if (array_has($params, 'dot')) {
                            $dot = $params['dot'];
                        }
                    }
                    $rst[$k]->dot_title = cut_str($v->title, $params['titlelen'], 0, $dot);
                    $lists[$v->type_id][] = $rst[$k];
                }
            }
            if ($lists && array_has($params, 'limit')) {
                if (array_has($params, 'type_id')) {
                    if (is_array($params['type_id'])) {
                        foreach ($params['type_id'] as $key => $val) {
                            if (array_has($lists, $val)) {
                                $lists[$val] = array_slice($lists[$val], 0, $params['limit'], true);
                            } else {
                                $lists[$val] = array();
                            }
                        }
                    } else {
                        $lists = array_slice($lists, 0, $params['limit'], true);
                    }
                }
            }
            Cache::put('article_index_list_'.$type.'_'.get_subsite_id(), $lists, '1800');
        }
        return $lists;
    }


}