BaseModel.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. namespace app\common\model;
  3. use think\db\Query;
  4. use think\Model;
  5. use think\helper\Str;
  6. class BaseModel extends Model
  7. {
  8. protected $message = '';
  9. protected $data = [];
  10. protected $query;
  11. protected array $select = [];
  12. protected int $is_status_search = 0;// 默认使用 status = 1 筛选
  13. protected int $is_delete_search = 0;// 默认使用 is_delete = 0 筛选
  14. /**
  15. * 筛选条件
  16. *
  17. * @param Query $query
  18. * @param $value
  19. * @param array $params
  20. * @return mixed
  21. */
  22. public function searchIdAttribute($query, $value, array $params)
  23. {
  24. if (empty($value)) {
  25. return $query;
  26. }
  27. return $query->where('id', $value);
  28. }
  29. public function searchIdInAttribute($query, $value, array $params)
  30. {
  31. if ($value === []){
  32. return $query->whereIn('id', []);
  33. }
  34. if (empty($value)) {
  35. return $query;
  36. }
  37. return $query->whereIN('id', $value);
  38. }
  39. /**
  40. * 设置 select
  41. * @param array $select
  42. * @return BaseModel
  43. */
  44. public function setSelect(array $select = ['*'])
  45. {
  46. $this->select = $select;
  47. return $this;
  48. }
  49. /**
  50. * 设置 默认使用 status = 1 筛选
  51. * @param int $is_status_search
  52. * @return $this
  53. */
  54. public function setIsStatusSearchValue(int $is_status_search = 1)
  55. {
  56. $this->is_status_search = $is_status_search;
  57. return $this;
  58. }
  59. /**
  60. * 设置 默认使用 is_delete = 0 筛选
  61. * @param int $is_delete_search
  62. * @return $this
  63. */
  64. public function setIsDeleteSearchValue(int $is_delete_search = 1)
  65. {
  66. $this->is_delete_search = $is_delete_search;
  67. return $this;
  68. }
  69. /**
  70. * 列表查询
  71. *
  72. * @param array $params
  73. * @param array $orderBy
  74. * @param array $select
  75. * @param array $with
  76. * @return mixed
  77. */
  78. public function getList(array $params = [], array $orderBy = [], array $select = [], array $with = [])
  79. {
  80. $query = $this->catchSearch($params)
  81. ->catchPages($params)
  82. ->sortTool($orderBy)
  83. ->getQueryObj()
  84. ->field($select ?: $this->select);
  85. // 模型关联
  86. count($with) > 0 && $query->with($with);
  87. // 规避 禁用 和 删除 数据
  88. $this->is_status_search === 1 && $query->where('status', 1);
  89. $this->is_delete_search === 1 && $query->whereNull('deletetime');
  90. return $this->catchData(json_decode(json_encode($query->select()),true));
  91. }
  92. /**
  93. * 单条数据查询
  94. * @param array $params
  95. * @param array $orderBy
  96. * @param array $select
  97. * @param array $with
  98. * @return array
  99. */
  100. public function getDetail(array $params = [], array $orderBy = [], array $select = [], array $with = [])
  101. {
  102. $query = $this->catchSearch($params)
  103. ->catchPages($params)
  104. ->sortTool($orderBy)
  105. ->getQueryObj()
  106. ->field($select ?: $this->select);
  107. // 模型关联
  108. count($with) > 0 && $query->with($with);
  109. // 规避 禁用 和 删除 数据
  110. $this->is_status_search === 1 && $query->where('status', 'normal');
  111. $this->is_delete_search === 1 && $query->whereNull('deletetime');
  112. $detail = $query->find();
  113. if ($detail){
  114. $detail = json_decode(json_encode($detail),true);
  115. }else{
  116. $detail = [];
  117. }
  118. return $this->catchData($detail);
  119. }
  120. /**
  121. * 查询器
  122. *
  123. * @param array $params
  124. * @return $this
  125. */
  126. protected function catchSearch(array $params = [])
  127. {
  128. $this->query = !empty($this->query) ? $this->query : $this;
  129. if (empty($params)) {
  130. return $this;
  131. }
  132. foreach ($params as $field => $value) {
  133. $method = 'search' . Str::studly($field) . 'Attribute';
  134. if ($value !== null && $value !== '' && method_exists($this, $method)) {
  135. $this->query = $this->$method($this->query,$value,$params);
  136. }
  137. }
  138. return $this;
  139. }
  140. /**
  141. * 分页器
  142. *
  143. * @param $params
  144. * @return $this|BaseModel
  145. */
  146. protected function catchPages($params)
  147. {
  148. $this->query = !empty($this->query) ? $this->query : $this;
  149. if (empty($params['page']) && empty($params['list_row'])){
  150. return $this;
  151. }
  152. $page = $params['page'] ?? 1;
  153. $size = $params['list_row'] ?? 15;
  154. $this->query = $this->query->page($page,$size);
  155. return $this;
  156. }
  157. /**
  158. * 排序工具
  159. *
  160. * @param $orderBy
  161. * @return $this|BaseModel
  162. */
  163. protected function sortTool($orderBy)
  164. {
  165. $this->query = !empty($this->query) ? $this->query : $this;
  166. if (empty($orderBy)){
  167. return $this;
  168. }
  169. foreach ($orderBy as $sort=>$order){
  170. if (empty($sort) || empty($order)){
  171. continue;
  172. }
  173. $this->query = $this->query->order($sort,$order);
  174. }
  175. return $this;
  176. }
  177. /**
  178. * 数据集处理器
  179. * @param array $data
  180. * @return array
  181. */
  182. protected function catchData(array $data): array
  183. {
  184. if (isset($data[0])){
  185. foreach ($data as $key=>$val){
  186. foreach ($val as $k=>$v){
  187. $method = 'data' . Str::studly($k) . 'Attribute';
  188. if (method_exists($this, $method)) {
  189. $data[$key][$k] = $this->$method($v,$data);
  190. }
  191. }
  192. }
  193. }else{
  194. foreach ($data as $key=>$val){
  195. $method = 'data' . Str::studly($key) . 'Attribute';
  196. if (method_exists($this, $method)) {
  197. $data[$key] = $this->$method($val,$data);
  198. }
  199. }
  200. }
  201. return $data;
  202. }
  203. /**
  204. * @return mixed
  205. */
  206. protected function getQueryObj()
  207. {
  208. return $this->query;
  209. }
  210. protected function success($message = '', $data = [])
  211. {
  212. $this->message = $message;
  213. $this->data = $data;
  214. return true;
  215. }
  216. protected function error($message = '', $data = [])
  217. {
  218. $this->message = $message;
  219. $this->data = $data;
  220. return false;
  221. }
  222. public function getMessage()
  223. {
  224. return $this->message;
  225. }
  226. public function getData($name = '')
  227. {
  228. return !empty($name) ? $this->data[$name] ?? '' : $this->data;
  229. }
  230. }