// +---------------------------------------------------------------------- declare (strict_types = 1); namespace addon\docs\model; use think\Model; /** * 文档版本模型 * * @author ywxapp */ class DocsVersion extends Model { protected $name = 'docs_version'; protected $autoWriteTimestamp = true; protected $createTime = 'create_at'; protected $updateTime = 'update_at'; protected $type = [ 'id' => 'integer', 'project_id' => 'integer', 'is_default' => 'integer', 'sort' => 'integer', 'status' => 'integer', ]; /** * 关联所属项目 */ public function project() { return $this->belongsTo(DocsProject::class, 'project_id', 'id'); } /** * 获取项目下的启用版本列表 * * @param int $projectId 项目ID * @return \think\Collection */ public static function listByProject(int $projectId) { return static::where('project_id', $projectId) ->where('status', 1) ->order('sort', 'asc') ->order('id', 'asc') ->select(); } /** * 解析项目的目标版本 * * 优先按名称精确匹配,其次取默认版本,最后退回第一个版本。 * * @param int $projectId 项目ID * @param string $name 版本标识,为空表示取默认 * @return static|null */ public static function resolve(int $projectId, string $name = '') { if ($name !== '') { $version = static::where('project_id', $projectId) ->where('name', $name) ->where('status', 1) ->find(); if ($version) { return $version; } } $default = static::where('project_id', $projectId) ->where('status', 1) ->where('is_default', 1) ->find(); if ($default) { return $default; } return static::where('project_id', $projectId) ->where('status', 1) ->order('sort', 'asc') ->order('id', 'asc') ->find(); } }