File.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 library(): HasOneThrough
  60. {
  61. return $this->hasOneThrough(Library::class, Container::class, 'id', 'id', 'object_id', 'library_id');
  62. }
  63. public function bimFile(): HasOne
  64. {
  65. return $this->hasOne(BimFile::class);
  66. }
  67. public function scopeFolderId($query, int $folderId = 0)
  68. {
  69. if ($folderId > 0) {
  70. return $query->where('folder_id', $folderId);
  71. }
  72. }
  73. // 某个容器库的文件
  74. public function scopeContainerLibraryId($query, int $libraryId = 0)
  75. {
  76. return $query->whereHas('container', function ($query) use ($libraryId) {
  77. if ($libraryId > 0) {
  78. return $query->where('library_id', $libraryId);
  79. }
  80. });
  81. }
  82. // 某个容器的文件
  83. public function scopeContainerId($query, int $containerId = 0)
  84. {
  85. return $query->whereHas('container', function ($query) use ($containerId) {
  86. if ($containerId > 0) {
  87. return $query->where('id', $containerId);
  88. }
  89. });
  90. }
  91. // 某个资产的文件
  92. public function scopeAssetId($query, int $assetId = 0)
  93. {
  94. return $query->whereHas('containerLibrary', function ($query) use ($assetId) {
  95. if ($assetId > 0) {
  96. return $query->where('asset_id', $assetId);
  97. }
  98. });
  99. }
  100. // 某个项目的文件
  101. public function scopeProjectId($query, int $projectId = 0)
  102. {
  103. return $query->whereHas('containerLibrary', function ($query) use ($projectId) {
  104. if ($projectId > 0) {
  105. return $query->where('project_id', $projectId);
  106. }
  107. });
  108. }
  109. public function scopeKeyword($query, string $keyword = '')
  110. {
  111. if (!empty($keyword)) {
  112. return $query->where('title', 'like', '%' . $keyword . '%');
  113. }
  114. }
  115. public function namingRule(): BelongsTo
  116. {
  117. return $this->belongsTo(NamingRule::class);
  118. }
  119. }