File.php 3.5 KB

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