<?php


namespace App\Web\Models;



use App\Models\BaseModel;


class BlogTagModel extends BaseModel
{

    /**
     * 状态字段
     */
    const DELETED_AT = 'status';
    /**
     * @var string
     */
    protected $table = 'blog_tag';

    /**
     * 获取文章标签
     * */
    public function getBlogTagList($params,$fields=''){
        if (empty($fields)) {
            $fields = 'a.*,s.urla,s.seo_title,s.seo_keyword,s.seo_describe,s.trans_status';
        }
        list($pageSize, $page, $skip) = $this->getPaginatorParams($params);
        $where=[];
        if(isset($params['status'])){
            $where[]=['a.status','=',$params['status']];
        }else{
            $where[]=['a.status','<',2];
        }
        $query= $this->newInstance()->alias('a')->where($where);
        if (!empty($params['keyword'])) {
            $keyword = $params['keyword'];
            $query->where('a.tag_name', 'like', "%" . $keyword . "%");
        }
        $totalCount = $query->count();
        $list= $query->leftJoin('web_seo as s', 's.id', '=', 'a.seo_id')
            ->skip($skip)
            ->limit($pageSize)
            ->selectRaw($fields)
            ->orderBy('sort')
            ->get();
        if(!empty($list)){
            $list=$list->toArray();
        }else{
            $list=[];
        }
        $result = $this->buildPaginator($list, $skip, $page, $pageSize, $totalCount);
        return $result;
    }


    /**
     * 文章类型保存
     * @param array $data
     * */
    public function saveTagData($data){
        if (!empty($data['id'])) {
            $id = $data['id'];
            $this->newInstance()->where('id', $data['id'])->update($data);
        } else {
            $id = $this->newInstance()->insertGetId($data);
        }
        return $id;
    }

    /**
     * 检查 名称是否唯一
     * */
    public function checkTagNameUnique($name,$id=0){
        $where=[];
        if(!empty($id)){
            $where[]=['id', '<>', $id];
        }
        $where[]=['status', '<', 2];
        $where[]=['tag_name', '=', trim($name)];
        return $this->checkFieldUnique('tag_name',$where);
    }


    /**
     * 获取标签详情
     * @param array $params
     * */
    public function getBlogTagInfo($params=[]){
        if(empty($params)){
            return [];
        }
        $where=[];
        $where[]=['status','<',2];
        if(!empty($params['id'])){
            $where['id']=$params['id'];
        }
        $info= $this->newInstance()->where($where)->first();
        if(!empty($info)){
            return $info->toArray();
        }else{
            return [];
        }
    }

    /**
     * 获取标签基本列表
     * */
    public function getBaseTagList($params,$fields='a.*,b.urla'){
        $where=[];
        $where['a.status']=0;
        if(!empty($params['is_hot'])){
            $where['a.is_hot']=1;
        }
        $query= $this->newInstance()->alias('a')
            ->leftJoin('web_seo as b', 'a.seo_id', '=', 'b.id')
            ->where($where);
        $pageSize = $this->getPageSize($params);
        $list= $query->limit($pageSize)
            ->selectRaw($fields)
            ->orderBy('sort')
            ->get();
        if(!empty($list)){
            $list=$list->toArray();
        }else{
            $list=[];
        }
        return $list;
    }

    /**
     * 获取已发布并关联文章的标签
     * @param $plateId
     * @param $limit
     * @param $blogId
     * @param $fields
     * @return mixed
     */
    public function getLimitBlogTag($plateId, $limit, $blogId, $fields = '')
    {
        if (empty($fields)) {
            $fields = 'a.id AS tag_id,a.tag_name,br.blog_id,s.urla,count(br.blog_id) AS tag_count';
        }
        $where = [
            'bg.plate_id' => $plateId,
            'a.status' => 0,
            'bg.status' => 0
        ];
        if($blogId) {
            $where['br.blog_id'] = $blogId;
        }
        return $this->newInstance()->alias('a')
            ->leftJoin('web_seo as s', 's.id', '=', 'a.seo_id')
            ->leftJoin('blog_tag_relation as br', 'br.tag_id', '=', 'a.id')
            ->leftJoin('blog as bg', 'br.blog_id', '=', 'bg.id')
            ->where($where)
            ->limit($limit)
            ->selectRaw($fields)
            ->groupBy('a.id')
            ->get()
            ->toArray();
    }

    /**
     * @param $tagId
     * @return array
     */
    public function getBlogTagById($tagId)
    {
        if (empty($fields)) {
            $fields = 'a.id AS tag_id,bg.plate_id,bp.plate_name,br.blog_id,a.tag_name,s.urla,count(br.blog_id) AS tag_count';
        }
        $where = [
            'a.id' => $tagId,
            'a.status' => 0,
            'bg.status' => 0
        ];
        $data = $this->newInstance()->alias('a')
            ->leftJoin('web_seo as s', 's.id', '=', 'a.seo_id')
            ->leftJoin('blog_tag_relation as br', 'br.tag_id', '=', 'a.id')
            ->leftJoin('blog as bg', 'br.blog_id', '=', 'bg.id')
            ->leftJoin('blog_plate as bp', 'bp.id', '=', 'bg.plate_id')
            ->where($where)
            ->selectRaw($fields)
            ->groupBy('a.id')
            ->first();
        return $data ? $data->toArray() : [];
    }

}
