FileAssociationService.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. namespace App\Services\File;
  3. use App\Models\Enums\FileObjectType;
  4. use App\Models\Enums\FileSource;
  5. use App\Models\File;
  6. use Carbon\Carbon;
  7. use Illuminate\Support\Facades\Auth;
  8. class FileAssociationService
  9. {
  10. /**
  11. * @param array $fileIds
  12. * @param string $objectId
  13. * @param FileObjectType $fileObjectType
  14. * @return void
  15. */
  16. public function association(array $fileIds, string $objectId, FileObjectType $fileObjectType)
  17. {
  18. if (! $fileIds) {
  19. return;
  20. }
  21. $files = File::query()
  22. ->whereIn("id", $fileIds)
  23. ->where("created_by", Auth::id())
  24. ->whereNull("object_id")
  25. ->where("object_type", $fileObjectType->value)
  26. ->where("created_at", ">=", Carbon::now()->subMinutes(10))
  27. ->orderBy("created_at")
  28. ->get();
  29. foreach ($files as $file) {
  30. if ($file->source == FileSource::ATTACHMENT->value) {
  31. $version = File::query()
  32. ->where("created_at", ">=", Carbon::now()->subMinutes(10))
  33. ->where("created_by", Auth::id())
  34. ->where("object_id", $objectId)
  35. ->where("object_type", $fileObjectType->value)
  36. ->where("title", $file->title)
  37. ->count();
  38. $file->version = $version + 1;
  39. }
  40. $file->object_id = $objectId;
  41. $file->save();
  42. }
  43. }
  44. }