FileRepository.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace App\Module\File\Repositories;
  3. use App\Module\File\Models\FileFile;
  4. use App\Module\File\Models\FileImg;
  5. use App\Module\File\Models\FileTemplate;
  6. /**
  7. * 文件仓库类
  8. *
  9. * 提供文件数据的访问和操作
  10. */
  11. class FileRepository
  12. {
  13. /**
  14. * 根据ID获取文件
  15. *
  16. * @param int $id 文件ID
  17. * @return FileFile|null 文件模型
  18. */
  19. public function getFileById(int $id)
  20. {
  21. return FileFile::find($id);
  22. }
  23. /**
  24. * 根据ID获取图片
  25. *
  26. * @param int $id 图片ID
  27. * @return FileImg|null 图片模型
  28. */
  29. public function getImageById(int $id)
  30. {
  31. return FileImg::find($id);
  32. }
  33. /**
  34. * 根据ID获取模板
  35. *
  36. * @param int $id 模板ID
  37. * @return FileTemplate|null 模板模型
  38. */
  39. public function getTemplateById(int $id)
  40. {
  41. return FileTemplate::find($id);
  42. }
  43. /**
  44. * 根据标识获取模板
  45. *
  46. * @param string $unid 模板标识
  47. * @return FileTemplate|null 模板模型
  48. */
  49. public function getTemplateByUnid(string $unid)
  50. {
  51. return FileTemplate::where('unid', $unid)->first();
  52. }
  53. /**
  54. * 获取用户的图片列表
  55. *
  56. * @param int $userId 用户ID
  57. * @param int $page 页码
  58. * @param int $pageSize 每页数量
  59. * @return \Illuminate\Pagination\LengthAwarePaginator 分页结果
  60. */
  61. public function getUserImages(int $userId, int $page = 1, int $pageSize = 20)
  62. {
  63. return FileImg::where('user_id', $userId)
  64. ->orderBy('id', 'desc')
  65. ->paginate($pageSize, ['*'], 'page', $page);
  66. }
  67. /**
  68. * 获取关联的图片列表
  69. *
  70. * @param string $reType 关联类型
  71. * @param int $reId 关联ID
  72. * @return \Illuminate\Database\Eloquent\Collection 图片集合
  73. */
  74. public function getRelatedImages(string $reType, int $reId)
  75. {
  76. return FileImg::where('re_type', $reType)
  77. ->where('re_id', $reId)
  78. ->orderBy('id', 'desc')
  79. ->get();
  80. }
  81. /**
  82. * 获取关联的文件列表
  83. *
  84. * @param string $reType 关联类型
  85. * @param int $reId 关联ID
  86. * @return \Illuminate\Database\Eloquent\Collection 文件集合
  87. */
  88. public function getRelatedFiles(string $reType, int $reId)
  89. {
  90. return FileFile::where('re_type', $reType)
  91. ->where('re_id', $reId)
  92. ->orderBy('id', 'desc')
  93. ->get();
  94. }
  95. /**
  96. * 获取模板列表
  97. *
  98. * @param string $group 分组
  99. * @param int $status 状态
  100. * @return \Illuminate\Database\Eloquent\Collection 模板集合
  101. */
  102. public function getTemplates(string $group = '', int $status = FileTemplate::STATUS_1)
  103. {
  104. $query = FileTemplate::query();
  105. if (!empty($group)) {
  106. $query->where('group', $group);
  107. }
  108. if ($status !== null) {
  109. $query->where('status', $status);
  110. }
  111. return $query->orderBy('id', 'desc')->get();
  112. }
  113. }