ProjectService.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace App\Services;
  3. use App\Models\Project;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Facades\Log;
  6. use Slowlyo\OwlAdmin\Services\AdminService;
  7. /**
  8. * 项目服务
  9. *
  10. * @method Project getModel()
  11. * @method Project|\Illuminate\Database\Query\Builder query()
  12. */
  13. class ProjectService extends AdminService
  14. {
  15. protected string $modelName = Project::class;
  16. /**
  17. * 获取项目列表
  18. */
  19. public function getProjects(int $page, int $perPage, ?string $keyword = null): array
  20. {
  21. try {
  22. $query = $this->query();
  23. // 如果有关键词,添加搜索条件
  24. if ($keyword) {
  25. $query->where(function ($q) use ($keyword) {
  26. $q->where('title', 'like', "%{$keyword}%")
  27. ->orWhere('subtitle', 'like', "%{$keyword}%");
  28. });
  29. }
  30. $total = $query->count();
  31. $items = $query->forPage($page, $perPage)
  32. ->orderBy('id', 'desc')
  33. ->get();
  34. return [
  35. 'items' => $items,
  36. 'total' => $total,
  37. ];
  38. } catch (\Exception $e) {
  39. Log::error('获取项目列表失败', [
  40. 'error' => $e->getMessage(),
  41. 'params' => func_get_args(),
  42. ]);
  43. throw $e;
  44. }
  45. }
  46. /**
  47. * 创建项目
  48. *
  49. * @return mixed
  50. */
  51. public function createProject(array $data)
  52. {
  53. try {
  54. return DB::transaction(function () use ($data) {
  55. // 创建项目记录
  56. return $this->query()->create($data);
  57. });
  58. } catch (\Exception $e) {
  59. Log::error('创建项目失败', [
  60. 'error' => $e->getMessage(),
  61. 'data' => $data,
  62. ]);
  63. throw $e;
  64. }
  65. }
  66. /**
  67. * 更新项目
  68. *
  69. * @return mixed
  70. */
  71. public function updateProject(int $id, array $data)
  72. {
  73. try {
  74. return DB::transaction(function () use ($id, $data) {
  75. $project = $this->query()->findOrFail($id);
  76. // 更新项目信息
  77. $project->update($data);
  78. return $project;
  79. });
  80. } catch (\Exception $e) {
  81. Log::error('更新项目失败', [
  82. 'error' => $e->getMessage(),
  83. 'id' => $id,
  84. 'data' => $data,
  85. ]);
  86. throw $e;
  87. }
  88. }
  89. /**
  90. * 删除项目
  91. */
  92. public function deleteProject(int $id): bool
  93. {
  94. try {
  95. return DB::transaction(function () use ($id) {
  96. $project = $this->query()->findOrFail($id);
  97. // 删除项目
  98. return $project->delete();
  99. });
  100. } catch (\Exception $e) {
  101. Log::error('删除项目失败', [
  102. 'error' => $e->getMessage(),
  103. 'id' => $id,
  104. ]);
  105. throw $e;
  106. }
  107. }
  108. /**
  109. * 获取项目详情
  110. *
  111. * @return mixed
  112. */
  113. public function getProject(int $id)
  114. {
  115. try {
  116. return $this->query()->findOrFail($id);
  117. } catch (\Exception $e) {
  118. Log::error('获取项目详情失败', [
  119. 'error' => $e->getMessage(),
  120. 'id' => $id,
  121. ]);
  122. throw $e;
  123. }
  124. }
  125. }