File.php 3.4 KB

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