File.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. ];
  38. protected $casts = [
  39. 'naming_rules' => 'array',
  40. ];
  41. protected static function booted(): void
  42. {
  43. if (Auth::check() && !Auth::user()->super_admin) {
  44. static::addGlobalScope(new CompanyScope);
  45. }
  46. }
  47. public function createdBy(): BelongsTo
  48. {
  49. return $this->belongsTo(User::class, "created_by");
  50. }
  51. public function folder(): BelongsTo
  52. {
  53. return $this->belongsTo(Folder::class);
  54. }
  55. public function container(): BelongsTo
  56. {
  57. return $this->belongsTo(Container::class, 'object_id');
  58. }
  59. public function containerLibrary(): HasOneThrough
  60. {
  61. return $this->hasOneThrough(Library::class, Container::class, 'id', 'id', 'object_id', 'library_id');
  62. }
  63. public function library(): HasOneThrough
  64. {
  65. return $this->hasOneThrough(Library::class, Container::class, 'id', 'id', 'object_id', 'library_id');
  66. }
  67. public function bimFile(): HasOne
  68. {
  69. return $this->hasOne(BimFile::class);
  70. }
  71. public function scopeFolderId($query, int $folderId = 0)
  72. {
  73. if ($folderId > 0) {
  74. return $query->where('folder_id', $folderId);
  75. }
  76. }
  77. // 某个容器库的文件
  78. public function scopeContainerLibraryId($query, int $libraryId = 0)
  79. {
  80. return $query->whereHas('container', function ($query) use ($libraryId) {
  81. if ($libraryId > 0) {
  82. return $query->where('library_id', $libraryId);
  83. }
  84. });
  85. }
  86. // 某个容器的文件
  87. public function scopeContainerId($query, int $containerId = 0)
  88. {
  89. return $query->whereHas('container', function ($query) use ($containerId) {
  90. if ($containerId > 0) {
  91. return $query->where('id', $containerId);
  92. }
  93. });
  94. }
  95. // 某个资产的文件
  96. public function scopeAssetId($query, int $assetId = 0)
  97. {
  98. return $query->whereHas('containerLibrary', function ($query) use ($assetId) {
  99. if ($assetId > 0) {
  100. return $query->where('asset_id', $assetId);
  101. }
  102. });
  103. }
  104. // 某个项目的文件
  105. public function scopeProjectId($query, int $projectId = 0)
  106. {
  107. return $query->whereHas('containerLibrary', function ($query) use ($projectId) {
  108. if ($projectId > 0) {
  109. return $query->where('project_id', $projectId);
  110. }
  111. });
  112. }
  113. public function scopeKeyword($query, string $keyword = '')
  114. {
  115. if (!empty($keyword)) {
  116. return $query->where('title', 'like', '%' . $keyword . '%');
  117. }
  118. }
  119. public function namingRule(): BelongsTo
  120. {
  121. return $this->belongsTo(NamingRule::class);
  122. }
  123. }