<?php


namespace App\Web\Models;



use App\Models\BaseModel;


class BlogTypeModel extends BaseModel
{
    /**
     * 状态字段
     */
    const DELETED_AT = 'status';
    /**
     * @var string
     */
    protected $table = 'blog_type';

    /**
     * 查询条件获取文章列表
     * */
    public function getBlogTypeList($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];
        }
        if (!empty($params['plate_id'])) {
            $where[] = ['a.plate_id', '=', $params['plate_id']];
        }
        $query = $this->newInstance()->alias('a')->where($where);
        if (!empty($params['keyword'])) {
            $keyword = $params['keyword'];
            $query->where('a.type_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 saveTypeData($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 checkTypeNameUnique($name,$id=0){
        $where=[];
        if(!empty($id)){
            $where[]=['id', '<>', $id];
        }
        $where[]=['status', '<', 2];
        $where[]=['type_name', '=', trim($name)];
        return $this->checkFieldUnique('type_name',$where);
    }

    /**
     * 获取类型详情
     * @param array $params
     * */
    public function getBlogTypeInfo($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 getBaseTypeList($params,$fields='a.*'){
        $where=[];
        $where['a.status']=0;
        $query= $this->newInstance()->alias('a')->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 getLimitBlogType($plateId, $limit, $blogId = 0, $fields = '')
    {
        //
        if (empty($fields)) {
            $fields = 'a.id AS type_id,a.expand_content, br.blog_id,a.type_name,s.urla,s.seo_describe,count(br.blog_id) AS type_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_type_relation as br', 'br.type_id', '=', 'a.id')
            ->leftJoin('blog as bg', 'br.blog_id', '=', 'bg.id')
            ->where($where)
            ->limit($limit)
            ->selectRaw($fields)
            ->groupBy('a.id')
            ->orderBy('a.sort')
            ->get()
            ->toArray();
    }

    /**
     * @param $typeId
     * @return array
     */
    public function getBlogTypeById($typeId)
    {
        if (empty($fields)) {
            $fields = 'a.id AS type_id,bg.plate_id,bp.plate_name,br.blog_id,a.type_name,s.urla,count(br.blog_id) AS type_count,a.expand_content';
        }
        $where = [
            'a.id' => $typeId,
            'a.status' => 0,
            'bg.status' => 0
        ];
        $data = $this->newInstance()->alias('a')
            ->leftJoin('web_seo as s', 's.id', '=', 'a.seo_id')
            ->leftJoin('blog_type_relation as br', 'br.type_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() : [];
    }

}
