SettingItemService.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace App\Services;
  3. use App\Models\SettingItem;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Facades\Log;
  6. use Slowlyo\OwlAdmin\Services\AdminService;
  7. /**
  8. * 设置项
  9. *
  10. * @method SettingItem getModel()
  11. * @method SettingItem|\Illuminate\Database\Query\Builder query()
  12. */
  13. class SettingItemService extends AdminService
  14. {
  15. protected string $modelName = SettingItem::class;
  16. /**
  17. * 获取设置项列表
  18. *
  19. * @return mixed
  20. */
  21. public function getItemList(array $params)
  22. {
  23. try {
  24. $query = $this->query();
  25. if (! empty($params['keyword'])) {
  26. $query->where(function ($q) use ($params) {
  27. $q->where('code', 'like', "%{$params['keyword']}%")
  28. ->orWhere('name', 'like', "%{$params['keyword']}%");
  29. });
  30. }
  31. if (! empty($params['group_id'])) {
  32. $query->where('group_id', $params['group_id']);
  33. }
  34. return $query->orderBy('sort')->get();
  35. } catch (\Exception $e) {
  36. Log::error('获取设置项列表失败', ['error' => $e->getMessage(), 'params' => $params]);
  37. throw $e;
  38. }
  39. }
  40. /**
  41. * 创建设置项
  42. *
  43. * @return mixed
  44. */
  45. public function createItem(array $data)
  46. {
  47. try {
  48. return DB::transaction(function () use ($data) {
  49. // 检查code是否已存在
  50. abort_if(
  51. $this->query()->where('code', $data['code'])->exists(),
  52. 422,
  53. '设置项代码已存在'
  54. );
  55. return $this->getModel()::create($data);
  56. });
  57. } catch (\Exception $e) {
  58. Log::error('创建设置项失败', ['error' => $e->getMessage(), 'data' => $data]);
  59. throw $e;
  60. }
  61. }
  62. /**
  63. * 更新设置项
  64. *
  65. * @param int $id
  66. * @return mixed
  67. */
  68. public function updateItem($id, array $data)
  69. {
  70. try {
  71. return DB::transaction(function () use ($id, $data) {
  72. $item = $this->query()->findOrFail($id);
  73. // 检查code是否已存在(排除自身)
  74. abort_if(
  75. $this->query()->where('code', $data['code'])->where('id', '!=', $id)->exists(),
  76. 422,
  77. '设置项代码已存在'
  78. );
  79. return $item->update($data);
  80. });
  81. } catch (\Exception $e) {
  82. Log::error('更新设置项失败', ['error' => $e->getMessage(), 'id' => $id, 'data' => $data]);
  83. throw $e;
  84. }
  85. }
  86. /**
  87. * 删除设置项
  88. *
  89. * @param int $id
  90. * @return mixed
  91. */
  92. public function deleteItem($id)
  93. {
  94. try {
  95. return DB::transaction(function () use ($id) {
  96. return $this->query()->findOrFail($id)->delete();
  97. });
  98. } catch (\Exception $e) {
  99. Log::error('删除设置项失败', ['error' => $e->getMessage(), 'id' => $id]);
  100. throw $e;
  101. }
  102. }
  103. /**
  104. * 获取设置项详情
  105. *
  106. * @param string $id_or_code ID或代码
  107. * @return mixed
  108. */
  109. public function getItemDetail($id_or_code)
  110. {
  111. try {
  112. $query = $this->query();
  113. // 判断是否为数字ID
  114. if (is_numeric($id_or_code)) {
  115. $item = $query->find($id_or_code);
  116. } else {
  117. // 通过code查询
  118. $item = $query->where('code', $id_or_code)->first();
  119. }
  120. // 如果未找到记录则抛出异常
  121. abort_if(! $item, 404, '设置项不存在');
  122. return $item;
  123. } catch (\Exception $e) {
  124. Log::error('获取设置项详情失败', [
  125. 'error' => $e->getMessage(),
  126. 'id_or_code' => $id_or_code,
  127. ]);
  128. throw $e;
  129. }
  130. }
  131. }