BaseLogic.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | saiadmin [ saiadmin快速开发框架 ]
  4. // +----------------------------------------------------------------------
  5. // | Author: sai <1430792918@qq.com>
  6. // +----------------------------------------------------------------------
  7. namespace plugin\saiadmin\basic;
  8. use plugin\saiadmin\exception\ApiException;
  9. use think\facade\Db;
  10. /**
  11. * 逻辑层基础类
  12. * @package app\service
  13. * @method static where($data) think-orm的where方法
  14. * @method static find($id) think-orm的find方法
  15. * @method static findOrEmpty($id) think-orm的findOrEmpty方法
  16. * @method static hidden($data) think-orm的hidden方法
  17. * @method static order($data) think-orm的order方法
  18. * @method static save($data) think-orm的save方法
  19. * @method static create($data) think-orm的create方法
  20. * @method static saveAll($data) think-orm的saveAll方法
  21. * @method static update($data, $where, $allow = []) think-orm的update方法
  22. * @method static select() think-orm的select方法
  23. * @method static count($data) think-orm的count方法
  24. * @method static max($data) think-orm的max方法
  25. * @method static min($data) think-orm的min方法
  26. * @method static sum($data) think-orm的sum方法
  27. * @method static avg($data) think-orm的avg方法
  28. */
  29. class BaseLogic
  30. {
  31. /**
  32. * @var object 模型注入
  33. */
  34. protected $model;
  35. /**
  36. * @var object 管理员信息
  37. */
  38. protected $adminInfo;
  39. /**
  40. * 排序字段
  41. * @var string
  42. */
  43. protected string $orderField = '';
  44. /**
  45. * 排序方式
  46. * @var string
  47. */
  48. protected string $orderType = 'ASC';
  49. /**
  50. * 初始化
  51. * @param $user
  52. * @return void
  53. */
  54. public function init($user): void
  55. {
  56. $this->adminInfo = $user;
  57. }
  58. /**
  59. * 设置排序字段
  60. * @param $field
  61. * @return void
  62. */
  63. public function setOrderField($field): void
  64. {
  65. $this->orderField = $field;
  66. }
  67. /**
  68. * 设置排序方式
  69. * @param $type
  70. * @return void
  71. */
  72. public function setOrderType($type): void
  73. {
  74. $this->orderType = $type;
  75. }
  76. /**
  77. * 数据库事务操作
  78. * @param callable $closure
  79. * @param bool $isTran
  80. * @return mixed
  81. */
  82. public function transaction(callable $closure, bool $isTran = true): mixed
  83. {
  84. return $isTran ? Db::transaction($closure) : $closure();
  85. }
  86. /**
  87. * 添加数据
  88. * @param $data
  89. * @return mixed
  90. */
  91. public function add($data): mixed
  92. {
  93. $this->model->save($data);
  94. return $this->model->getKey();
  95. }
  96. /**
  97. * 修改数据
  98. * @param $id
  99. * @param $data
  100. * @return mixed
  101. */
  102. public function edit($id, $data): mixed
  103. {
  104. $model = $this->model->findOrEmpty($id);
  105. if ($model->isEmpty()) {
  106. throw new ApiException('数据不存在');
  107. }
  108. return $model->save($data);
  109. }
  110. /**
  111. * 读取数据
  112. * @param $id
  113. * @return mixed
  114. */
  115. public function read($id): mixed
  116. {
  117. $model = $this->model->findOrEmpty($id);
  118. if ($model->isEmpty()) {
  119. throw new ApiException('数据不存在');
  120. }
  121. return $model;
  122. }
  123. /**
  124. * 删除数据
  125. * @param $ids
  126. */
  127. public function destroy($ids)
  128. {
  129. $this->model->destroy($ids);
  130. }
  131. /**
  132. * 搜索器搜索
  133. * @param array $searchWhere
  134. * @return mixed
  135. */
  136. public function search(array $searchWhere = []): mixed
  137. {
  138. $withSearch = array_keys($searchWhere);
  139. $data = $searchWhere;
  140. foreach ($withSearch as $k => $v) {
  141. if ($data[$v] === '') {
  142. unset($data[$v]);
  143. unset($withSearch[$k]);
  144. }
  145. }
  146. return $this->model->withSearch($withSearch, $data);
  147. }
  148. /**
  149. * 分页查询数据
  150. * @param $query
  151. * @return mixed
  152. */
  153. public function getList($query): mixed
  154. {
  155. $saiType = request()->input('saiType', 'list');
  156. $page = request()->input('page', 1);
  157. $limit = request()->input('limit', 10);
  158. $orderBy = request()->input('orderBy', '');
  159. $orderType = request()->input('orderType', $this->orderType);
  160. if(empty($orderBy)) {
  161. $orderBy = $this->orderField !== '' ? $this->orderField : $this->model->getPk();
  162. }
  163. $query->order($orderBy, $orderType);
  164. if ($saiType === 'all') {
  165. return $query->select()->toArray();
  166. }
  167. return $query->paginate($limit, false, ['page' => $page])->toArray();
  168. }
  169. /**
  170. * 获取全部数据
  171. * @param $query
  172. * @return mixed
  173. */
  174. public function getAll($query): mixed
  175. {
  176. $orderBy = request()->input('orderBy', '');
  177. $orderType = request()->input('orderType', $this->orderType);
  178. if(empty($orderBy)) {
  179. $orderBy = $this->orderField !== '' ? $this->orderField : $this->model->getPk();
  180. }
  181. $query->order($orderBy, $orderType);
  182. return $query->select()->toArray();
  183. }
  184. /**
  185. * 获取上传的导入文件
  186. * @param $file
  187. * @return string
  188. */
  189. public function getImport($file): string
  190. {
  191. $full_dir = runtime_path() . '/resource/';
  192. if (!is_dir($full_dir)) {
  193. mkdir($full_dir, 0777, true);
  194. }
  195. $ext = $file->getUploadExtension() ?: null;
  196. $full_path = $full_dir. md5(time()). '.'. $ext;
  197. $file->move($full_path);
  198. return $full_path;
  199. }
  200. /**
  201. * 方法调用
  202. * @param $name
  203. * @param $arguments
  204. * @return mixed
  205. */
  206. public function __call($name, $arguments)
  207. {
  208. // TODO: Implement __call() method.
  209. return call_user_func_array([$this->model, $name], $arguments);
  210. }
  211. }