<?php


namespace App\Web\Models;


use App\Models\BaseModel;


class BlogPlateModel extends BaseModel
{


    /**
     * 状态字段
     */
    const DELETED_AT = 'status';
    const NEWS = 1;
    const SUCCESSCASES = 2;
    const Q_AND_A = 3;
    const PARTNERS = 4;
    const KNOWLEDGECENTRE = 5;
    const SOLUTION = 6;
const VIDEO = 7;
        const SPONSOR = 8; //视频
    const TOURISM = 9;
    const CATERING = 10;
    const ABOUTUS = 11;
const JOB = 13;
        const FUNGI_COMMENT = 13; //岗位招聘
    const Media = 15;
    /**
     * @var string
     */
    protected $table = 'blog_plate';

//    const HOTEL = 8;
//    const COMMENT = 13;
//    const TOURISM_PARTNERS = 15;

    /**
     * 获取文章板块
     * */
    public function getBlogPlateList($params, $fields = '')
    {
        list($pageSize, $page, $skip) = $this->getPaginatorParams($params);
        $where = [];
        if (isset($params['status'])) {
            $where[] = ['a.status', '=', $params['status']];
        } else {
            $where[] = ['a.status', '<', 2];
        }
        if (empty($fields)) {
            $fields = 'a.*,b.urla,b.seo_title,b.seo_keyword,b.seo_describe';
        }
        $query = $this->newInstance()->alias('a')
            ->leftJoin('web_seo as b', 'a.seo_id', '=', 'b.id')
            ->where($where);
        if (!empty($params['keyword'])) {
            $keyword = $params['keyword'];
            $query->where('a.plate_name', 'like', "%" . $keyword . "%");
        }
        $totalCount = $query->count();
        $list = $query->skip($skip)
            ->limit($pageSize)
            ->selectRaw($fields)
            ->orderBy('sort')
            ->get();
        foreach ($list as &$l) {
            $l['plate_fields'] = json_decode($l['plate_fields'], true);
        }
        if (!empty($list)) {
            $list = $list->toArray();
        } else {
            $list = [];
        }
        $result = $this->buildPaginator($list, $skip, $page, $pageSize, $totalCount);
        return $result;
    }


    /**
     * 文章类型保存
     * @param array $data
     * */
    public function savePlateData($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 checkPlateNameUnique($name, $id = 0)
    {
        $where = [];
        if (!empty($id)) {
            $where[] = ['id', '<>', $id];
        }
        $where[] = ['status', '<', 2];
        $where[] = ['plate_name', '=', trim($name)];
        return $this->checkFieldUnique('plate_name', $where);
    }


    /**
     * 获取板块详情
     * @param array $params
     * */
    public function getBlogPlateInfo($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 [];
        }
    }

}
