123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- <?php
- namespace App\Models;
- use App\Models\Enums\FileObjectType;
- use App\Models\Scopes\CompanyScope;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use Illuminate\Database\Eloquent\Relations\HasOne;
- use Illuminate\Database\Eloquent\Relations\HasOneThrough;
- use Illuminate\Database\Eloquent\SoftDeletes;
- use Illuminate\Support\Facades\Auth;
- class File extends Model
- {
- use HasFactory, SoftDeletes;
- protected $fillable = [
- "company_id",
- "pathname",
- "title",
- "folder_id",
- "extension",
- "size",
- "object_type",
- "object_id",
- "created_by",
- "version",
- "is_latest_version",
- "downloads",
- "source",
- "is_hide",
- "hide_object_version",
- "is_bim",
- "approval_status",
- "latest_approval_id",
- "source_file_id",
- 'naming_rule_id',
- 'naming_rules',
- 'doc_stage',
- 'doc_type',
- ];
- protected $casts = [
- 'naming_rules' => 'array',
- ];
- protected static function booted(): void
- {
- if (Auth::check() && !Auth::user()->super_admin) {
- static::addGlobalScope(new CompanyScope);
- }
- }
- public function createdBy(): BelongsTo
- {
- return $this->belongsTo(User::class, "created_by");
- }
- public function folder(): BelongsTo
- {
- return $this->belongsTo(Folder::class);
- }
- public function container(): BelongsTo
- {
- return $this->belongsTo(Container::class, 'object_id');
- }
- public function containerLibrary(): HasOneThrough
- {
- return $this->hasOneThrough(Library::class, Container::class, 'id', 'id', 'object_id', 'library_id');
- }
- public function library(): HasOneThrough
- {
- return $this->hasOneThrough(Library::class, Container::class, 'id', 'id', 'object_id', 'library_id');
- }
- public function bimFile(): HasOne
- {
- return $this->hasOne(BimFile::class);
- }
- public function scopeFolderId($query, int $folderId = 0)
- {
- if ($folderId > 0) {
- return $query->where('folder_id', $folderId);
- }
- }
- // 某个容器库的文件
- public function scopeContainerLibraryId($query, int $libraryId = 0)
- {
- return $query->whereHas('container', function ($query) use ($libraryId) {
- if ($libraryId > 0) {
- return $query->where('library_id', $libraryId);
- }
- });
- }
- // 某个容器的文件
- public function scopeContainerId($query, int $containerId = 0)
- {
- return $query->whereHas('container', function ($query) use ($containerId) {
- if ($containerId > 0) {
- return $query->where('id', $containerId);
- }
- });
- }
- // 某个资产的文件
- public function scopeAssetId($query, int $assetId = 0)
- {
- return $query->whereHas('containerLibrary', function ($query) use ($assetId) {
- if ($assetId > 0) {
- return $query->where('asset_id', $assetId);
- }
- });
- }
- // 某个项目的文件
- public function scopeProjectId($query, int $projectId = 0)
- {
- return $query->whereHas('containerLibrary', function ($query) use ($projectId) {
- if ($projectId > 0) {
- return $query->where('project_id', $projectId);
- }
- });
- }
- public function scopeKeyword($query, string $keyword = '')
- {
- if (!empty($keyword)) {
- return $query->where('title', 'like', '%' . $keyword . '%');
- }
- }
- public function namingRule(): BelongsTo
- {
- return $this->belongsTo(NamingRule::class);
- }
- }
|