<?php

namespace app\common\model;

use cores\BaseModel;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\model\relation\HasOne;

class ServerCategory extends BaseModel
{
    // 定义表名
    protected $name = 'server_category';

    // 定义主键
    protected $pk = 'category_id';

    /**
     * 分类图片
     * @return HasOne
     */
    public function image(): HasOne
    {
        return $this->hasOne('UploadFile', 'file_id', 'image_id');
    }

    /**
     * @notes:分类详情
     * @param $where
     * @param array $with
     * @return static|array|null
     * @author: wanghousheng
     */
    public static function detail($where, array $with = [])
    {
        return static::get($where, $with);
    }

    /**
     * @notes:获取全部记录
     * @param array $where
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @author: wanghousheng
     */
    public function getList(array $where = []): array
    {
        $where = $this->setQueryDefaultValue($where);
        return $this->with(['image'])
            ->where($where)
            ->order(['sort', 'create_time'])
            ->select()
            ->toArray();
    }
}