<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/7
 * Time: 17:17
 */
namespace App\Services\Content;

use App\Repositories\LinkRepository;
use App\Repositories\LinkCategoryRepository;

class LinkService
{
    protected $linkRepository;
    protected $linkCategoryRepository;

    /**
     * LinkService constructor.
     * @param $linkRepository
     */
    public function __construct(LinkRepository $linkRepository, LinkCategoryRepository $linkCategoryRepository)
    {
        $this->linkRepository = $linkRepository;
        $this->linkCategoryRepository = $linkCategoryRepository;
    }

    public function getLinks($data)
    {
        $where = array();
        /*if (array_key_exists('subsite_id', $data)) {
            $where[] = array('subsite_id','=',$data['subsite_id']);
        }*/
        $where[] = array('is_display','=','1');
        $limit = $data['limit'];
        if ($data['type']=='1') {
            $where[] = array('link_logo','=','');
        } elseif ($data['type']=='2') {
            $where[] = array('link_logo','<>','');
        }
        if (array_key_exists('alias', $data)) {
            $alias = $data['alias'];
            //获取链接分类信息
            //$link_category = $this->linkCategoryRepository->getCategories(array('alias'=>$alias));
            $link_categories = $this->linkCategoryRepository->getCategories();
            $link_category = $link_categories[$alias][0];
            if ($link_category->toArray()) {
                $where[] = array('type_id','=',$link_category->id);
            }
        }
        $links = $this->linkRepository->getLinks($where, $limit);
        if ($data['type']=='2') {
            if ($links->toArray()) {
                foreach ($links as $k => $v) {
                    //判断是本地图片还是链接图片
                    if (preg_match('/^http/', $v->link_logo)) {
                        $links[$k]->image_type = 'link';
                    } else {
                        $links[$k]->image_type = 'image';
                    }
                }
            }
        }
        return $links;
    }


}