Model.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace App\Model;
  12. use Hyperf\DbConnection\Model\Model as BaseModel;
  13. use Hyperf\Stringable\Str;
  14. abstract class Model extends BaseModel
  15. {
  16. protected $query;
  17. protected array $select = [];
  18. protected int $is_status_search = 1;// 默认使用 status = 1 筛选
  19. protected int $is_delete_search = 1;// 默认使用 is_delete = 0 筛选
  20. protected string $message = '';
  21. protected array $data = [];
  22. /**
  23. * 筛选条件
  24. *
  25. * @param $query
  26. * @param $value
  27. * @param array $params
  28. * @return mixed
  29. */
  30. public function searchIdAttribute($query, $value, array $params): mixed
  31. {
  32. if (empty($value)) {
  33. return $query;
  34. }
  35. return $query->where('id', $value);
  36. }
  37. /**
  38. * 设置 select
  39. * @param array $select
  40. * @return BaseModel
  41. */
  42. public function setSelect(array $select = ['*'])
  43. {
  44. $this->select = $select;
  45. return $this;
  46. }
  47. /**
  48. * 设置 默认使用 status = 1 筛选
  49. * @param int $is_status_search
  50. * @return $this
  51. */
  52. public function setIsStatusSearchValue(int $is_status_search = 1)
  53. {
  54. $this->is_status_search = $is_status_search;
  55. return $this;
  56. }
  57. /**
  58. * 设置 默认使用 is_delete = 0 筛选
  59. * @param int $is_delete_search
  60. * @return $this
  61. */
  62. public function setIsDeleteSearchValue(int $is_delete_search = 1)
  63. {
  64. $this->is_delete_search = $is_delete_search;
  65. return $this;
  66. }
  67. /**
  68. * 列表查询
  69. *
  70. * @param array $params
  71. * @param array $orderBy
  72. * @param array $select
  73. * @param array $with
  74. * @return array
  75. */
  76. public function getList(array $params = [], array $orderBy = [], array $select = [], array $with = [])
  77. {
  78. $query = $this->catchSearch($params)
  79. ->catchPages($params)
  80. ->sortTool($orderBy)
  81. ->getQueryObj()
  82. ->select(!empty($select) ? $select : $this->select);
  83. // 模型关联
  84. count($with) > 0 && $query->with($with);
  85. // 规避 禁用 和 删除 数据
  86. $this->is_status_search === 1 && $query->where('status', 1);
  87. $this->is_delete_search === 1 && $query->where('is_delete', 0);
  88. return $this->catchData($query->get()->toArray());
  89. }
  90. /**
  91. * 获取总数
  92. * @param array $params
  93. * @return mixed
  94. */
  95. public function getTotal(array $params = [])
  96. {
  97. $query = $this->catchSearch($params)->getQueryObj();
  98. // 规避 禁用 和 删除 数据
  99. $this->is_status_search === 1 && $query->where('status', 1);
  100. $this->is_delete_search === 1 && $query->where('is_delete', 0);
  101. return $query->count();
  102. }
  103. /**
  104. * 单条数据查询
  105. * @param array $params
  106. * @param array $orderBy
  107. * @param array $select
  108. * @param array $with
  109. * @return array
  110. */
  111. public function getDetail(array $params = [], array $orderBy = [], array $select = [], array $with = [])
  112. {
  113. $query = $this->catchSearch($params)
  114. ->catchPages($params)
  115. ->sortTool($orderBy)
  116. ->getQueryObj()
  117. ->select(!empty($select) ? $select : $this->select);
  118. // 模型关联
  119. count($with) > 0 && $query->with($with);
  120. // 规避 禁用 和 删除 数据
  121. $this->is_status_search === 1 && $query->where('status', 1);
  122. $this->is_delete_search === 1 && $query->where('is_delete', 0);
  123. $detail = $query->first();
  124. if ($detail){
  125. $detail = $detail->toArray();
  126. }else{
  127. $detail = [];
  128. }
  129. return $this->catchData($detail);
  130. }
  131. /**
  132. * 查询器
  133. *
  134. * @param array $params
  135. * @return $this
  136. */
  137. protected function catchSearch(array $params = [])
  138. {
  139. $this->query = !empty($this->query) ? $this->query : $this;
  140. if (empty($params)) {
  141. return $this;
  142. }
  143. foreach ($params as $field => $value) {
  144. $method = 'search' . Str::studly($field) . 'Attribute';
  145. if ($value !== null && $value !== '' && method_exists($this, $method)) {
  146. $this->query = $this->$method($this->query,$value,$params);
  147. }
  148. }
  149. return $this;
  150. }
  151. /**
  152. * 分页器
  153. *
  154. * @param $params
  155. * @return $this
  156. */
  157. protected function catchPages($params)
  158. {
  159. $this->query = !empty($this->query) ? $this->query : $this;
  160. if (empty($params['page']) && empty($params['list_rows'])){
  161. return $this;
  162. }
  163. $page = intval($params['page'] ?? 1);
  164. $size = intval($params['list_rows'] ?? 15);
  165. $this->query = $this->query->offset(($page - 1) * $size)->limit($size);
  166. return $this;
  167. }
  168. /**
  169. * 排序工具
  170. *
  171. * @param $orderBy
  172. * @return $this
  173. */
  174. protected function sortTool($orderBy)
  175. {
  176. $this->query = !empty($this->query) ? $this->query : $this;
  177. if (empty($orderBy)){
  178. return $this;
  179. }
  180. foreach ($orderBy as $sort=>$order){
  181. if (empty($sort) || empty($order)){
  182. continue;
  183. }
  184. $this->query = $this->query->orderBy($sort,$order);
  185. }
  186. return $this;
  187. }
  188. /**
  189. * 数据集处理器
  190. * @param array $data
  191. * @return array
  192. */
  193. protected function catchData(array $data): array
  194. {
  195. if (isset($data[0])){
  196. foreach ($data as $key=>$val){
  197. foreach ($val as $k=>$v){
  198. $method = 'data' . Str::studly($k) . 'Attribute';
  199. if (method_exists($this, $method)) {
  200. $data[$key][$k] = $this->$method($v,$data);
  201. }
  202. }
  203. }
  204. }else{
  205. foreach ($data as $key=>$val){
  206. $method = 'data' . Str::studly($key) . 'Attribute';
  207. if (method_exists($this, $method)) {
  208. $data[$key] = $this->$method($val,$data);
  209. }
  210. }
  211. }
  212. return $data;
  213. }
  214. /**
  215. * @return mixed
  216. */
  217. protected function getQueryObj()
  218. {
  219. return $this->query;
  220. }
  221. /**
  222. * 返回成功结果
  223. * @param string $message
  224. * @param array $data
  225. * @return bool
  226. */
  227. protected function success(string $message = 'success',array $data = []): bool
  228. {
  229. $this->message = $message;
  230. $this->data = $data;
  231. return true;
  232. }
  233. /**
  234. * 返回失败结果
  235. * @param string $message
  236. * @param array $data
  237. * @return bool
  238. */
  239. protected function error(string $message = 'error',array $data = []): bool
  240. {
  241. $this->message = $message;
  242. $this->data = $data;
  243. return false;
  244. }
  245. /**
  246. * 获取成功数据
  247. * @return array
  248. */
  249. public function getData(): array
  250. {
  251. return $this->data;
  252. }
  253. /**
  254. * 获取消息
  255. * @return string
  256. */
  257. public function getMessage(): string
  258. {
  259. return $this->message;
  260. }
  261. }