BaseLogic.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | saiadmin [ saiadmin快速开发框架 ]
  4. // +----------------------------------------------------------------------
  5. // | Author: sai <1430792918@qq.com>
  6. // +----------------------------------------------------------------------
  7. namespace plugin\saiadmin\basic\eloquent;
  8. use support\Db;
  9. use plugin\saiadmin\basic\AbstractLogic;
  10. use plugin\saiadmin\exception\ApiException;
  11. /**
  12. * Laravel Eloquent 逻辑层基类
  13. */
  14. class BaseLogic extends AbstractLogic
  15. {
  16. /**
  17. * 数据库事务操作
  18. * @param callable $closure
  19. * @param bool $isTran
  20. * @return mixed
  21. */
  22. public function transaction(callable $closure, bool $isTran = true): mixed
  23. {
  24. return $isTran ? Db::transaction($closure) : $closure();
  25. }
  26. /**
  27. * 添加数据
  28. * @param array $data
  29. * @return mixed
  30. */
  31. public function add(array $data): mixed
  32. {
  33. $model = $this->model->create($data);
  34. return $model->getKey();
  35. }
  36. /**
  37. * 修改数据
  38. * @param mixed $id
  39. * @param array $data
  40. * @return mixed
  41. */
  42. public function edit($id, array $data): mixed
  43. {
  44. $model = $this->model->find($id);
  45. if (!$model) {
  46. throw new ApiException('数据不存在');
  47. }
  48. return $model->update($data);
  49. }
  50. /**
  51. * 读取数据
  52. * @param mixed $id
  53. * @return mixed
  54. */
  55. public function read($id): mixed
  56. {
  57. $model = $this->model->find($id);
  58. if (!$model) {
  59. throw new ApiException('数据不存在');
  60. }
  61. return $model;
  62. }
  63. /**
  64. * 删除数据
  65. * @param mixed $ids
  66. * @return bool
  67. */
  68. public function destroy($ids): bool
  69. {
  70. return $this->model->destroy($ids);
  71. }
  72. /**
  73. * 搜索器搜索
  74. * @param array $searchWhere
  75. * @return mixed
  76. */
  77. public function search(array $searchWhere = []): mixed
  78. {
  79. $withSearch = array_keys($searchWhere);
  80. $data = [];
  81. foreach ($searchWhere as $key => $value) {
  82. if ($value !== '' && $value !== null && $value !== []) {
  83. $data[$key] = $value;
  84. }
  85. }
  86. $withSearch = array_keys($data);
  87. return $this->model->withSearch($withSearch, $data);
  88. }
  89. /**
  90. * 分页查询数据
  91. * @param mixed $query
  92. * @return mixed
  93. */
  94. public function getList($query): mixed
  95. {
  96. $request = request();
  97. $saiType = $request ? $request->input('saiType', 'list') : 'list';
  98. $page = $request ? $request->input('page', 1) : 1;
  99. $limit = $request ? $request->input('limit', 10) : 10;
  100. $orderField = $request ? $request->input('orderField', '') : '';
  101. $orderType = $request ? $request->input('orderType', $this->orderType) : $this->orderType;
  102. if (empty($orderField)) {
  103. $orderField = $this->orderField !== '' ? $this->orderField : $this->model->getKeyName();
  104. }
  105. $query->orderBy($orderField, $orderType);
  106. if ($saiType === 'all') {
  107. return $query->get()->toArray();
  108. }
  109. $list = $query->paginate($limit, ['*'], 'page', $page);
  110. return [
  111. 'current_page' => $list->currentPage(),
  112. 'per_page' => $list->perPage(),
  113. 'last_page' => $list->lastPage(),
  114. 'has_more' => $list->hasMorePages(),
  115. 'total' => $list->total(),
  116. 'data' => $list->items(),
  117. ];
  118. }
  119. /**
  120. * 获取全部数据
  121. * @param mixed $query
  122. * @return mixed
  123. */
  124. public function getAll($query): mixed
  125. {
  126. $request = request();
  127. $orderField = $request ? $request->input('orderField', '') : '';
  128. $orderType = $request ? $request->input('orderType', $this->orderType) : $this->orderType;
  129. if (empty($orderField)) {
  130. $orderField = $this->orderField !== '' ? $this->orderField : $this->model->getKeyName();
  131. }
  132. $query->orderBy($orderField, $orderType);
  133. return $query->get()->toArray();
  134. }
  135. }