BaseLogic.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | saiadmin [ saiadmin快速开发框架 ]
  4. // +----------------------------------------------------------------------
  5. // | Author: sai <1430792918@qq.com>
  6. // +----------------------------------------------------------------------
  7. namespace plugin\saiadmin\basic\think;
  8. use support\think\Db;
  9. use plugin\saiadmin\basic\AbstractLogic;
  10. use plugin\saiadmin\exception\ApiException;
  11. /**
  12. * ThinkORM 逻辑层基类
  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->findOrEmpty($id);
  45. if ($model->isEmpty()) {
  46. throw new ApiException('数据不存在');
  47. }
  48. return $model->save($data);
  49. }
  50. /**
  51. * 读取数据
  52. * @param mixed $id
  53. * @return mixed
  54. */
  55. public function read($id): mixed
  56. {
  57. $model = $this->model->findOrEmpty($id);
  58. if ($model->isEmpty()) {
  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->getPk();
  104. }
  105. $query->order($orderField, $orderType);
  106. if ($saiType === 'all') {
  107. return $query->select()->toArray();
  108. }
  109. return $query->paginate([
  110. 'page' => $page,
  111. 'list_rows' => $limit,
  112. ])->toArray();
  113. }
  114. /**
  115. * 获取全部数据
  116. * @param mixed $query
  117. * @return mixed
  118. */
  119. public function getAll($query): mixed
  120. {
  121. $request = request();
  122. $orderField = $request ? $request->input('orderField', '') : '';
  123. $orderType = $request ? $request->input('orderType', $this->orderType) : $this->orderType;
  124. if (empty($orderField)) {
  125. $orderField = $this->orderField !== '' ? $this->orderField : $this->model->getPk();
  126. }
  127. $query->order($orderField, $orderType);
  128. return $query->select()->toArray();
  129. }
  130. }