File.php 3.7 KB

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