File.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace App\Models;
  3. use App\Models\Enums\FileObjectType;
  4. use App\Models\Scopes\CompanyScope;
  5. use Illuminate\Database\Eloquent\Factories\HasFactory;
  6. use Illuminate\Database\Eloquent\Model;
  7. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  8. use Illuminate\Database\Eloquent\Relations\HasOne;
  9. use Illuminate\Database\Eloquent\Relations\HasOneThrough;
  10. use Illuminate\Database\Eloquent\SoftDeletes;
  11. use Illuminate\Support\Facades\Auth;
  12. use Illuminate\Support\Facades\Schema;
  13. class File extends Model
  14. {
  15. use HasFactory, SoftDeletes;
  16. public static function getColumns(): array
  17. {
  18. return Schema::getColumnListing((new self)->getTable());
  19. }
  20. protected $fillable = [
  21. "company_id",
  22. "pathname",
  23. "title",
  24. "folder_id",
  25. "extension",
  26. "size",
  27. "object_type",
  28. "object_id",
  29. "created_by",
  30. "version",
  31. "is_latest_version",
  32. "downloads",
  33. "source",
  34. "is_hide",
  35. "hide_object_version",
  36. "is_bim",
  37. "approval_status",
  38. "latest_approval_id",
  39. "source_file_id",
  40. 'naming_rule_id',
  41. 'naming_rules',
  42. 'doc_stage',
  43. 'doc_type',
  44. 'custom_fields'
  45. ];
  46. protected $casts = [
  47. 'naming_rules' => 'array',
  48. 'custom_fields' => 'array'
  49. ];
  50. protected static function booted(): void
  51. {
  52. if (Auth::check() && !Auth::user()->super_admin) {
  53. static::addGlobalScope(new CompanyScope);
  54. }
  55. }
  56. public function createdBy(): BelongsTo
  57. {
  58. return $this->belongsTo(User::class, "created_by");
  59. }
  60. public function updatedBy(): BelongsTo
  61. {
  62. return $this->belongsTo(User::class, "updated_by");
  63. }
  64. public function folder(): BelongsTo
  65. {
  66. return $this->belongsTo(Folder::class);
  67. }
  68. public function container(): BelongsTo
  69. {
  70. return $this->belongsTo(Container::class, 'object_id');
  71. }
  72. public function containerLibrary(): HasOneThrough
  73. {
  74. return $this->hasOneThrough(Library::class, Container::class, 'id', 'id', 'object_id', 'library_id');
  75. }
  76. public function library(): HasOneThrough
  77. {
  78. return $this->hasOneThrough(Library::class, Container::class, 'id', 'id', 'object_id', 'library_id');
  79. }
  80. public function bimFile(): HasOne
  81. {
  82. return $this->hasOne(BimFile::class);
  83. }
  84. public function scopeFolderId($query, int $folderId = 0)
  85. {
  86. if ($folderId > 0) {
  87. return $query->where('folder_id', $folderId);
  88. }
  89. }
  90. // 某个容器库的文件
  91. public function scopeContainerLibraryId($query, int $libraryId = 0)
  92. {
  93. return $query->whereHas('container', function ($query) use ($libraryId) {
  94. if ($libraryId > 0) {
  95. return $query->where('library_id', $libraryId);
  96. }
  97. });
  98. }
  99. // 某个容器的文件
  100. public function scopeContainerId($query, int $containerId = 0)
  101. {
  102. return $query->whereHas('container', function ($query) use ($containerId) {
  103. if ($containerId > 0) {
  104. return $query->where('id', $containerId);
  105. }
  106. });
  107. }
  108. // 某个资产的文件
  109. public function scopeAssetId($query, int $assetId = 0)
  110. {
  111. return $query->whereHas('containerLibrary', function ($query) use ($assetId) {
  112. if ($assetId > 0) {
  113. return $query->where('asset_id', $assetId);
  114. }
  115. });
  116. }
  117. // 某个项目的文件
  118. public function scopeProjectId($query, int $projectId = 0)
  119. {
  120. return $query->whereHas('containerLibrary', function ($query) use ($projectId) {
  121. if ($projectId > 0) {
  122. return $query->where('project_id', $projectId);
  123. }
  124. });
  125. }
  126. public function scopeKeyword($query, string $keyword = '')
  127. {
  128. if (!empty($keyword)) {
  129. return $query->where('title', 'like', '%' . $keyword . '%');
  130. }
  131. }
  132. public function namingRule(): BelongsTo
  133. {
  134. return $this->belongsTo(NamingRule::class);
  135. }
  136. }