<?php


namespace App\Web\Models;



use App\Models\BaseModel;


class MeetingTagModel extends BaseModel
{

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

    /**
     * 获取产品标签
     * */
    public function getMeetingTagList($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 getMeetingTagInfo($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;
    }

}
