Criteria.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. namespace App\Base\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. trait Criteria
  5. {
  6. /**
  7. * 创建查询条件
  8. * @param $condition
  9. * @$conditionExample 有用到补充下例子
  10. * [
  11. * 'id' => [
  12. * ['notIn', [1,2,3]]
  13. * ],
  14. * 'score' => [
  15. * ['>', 60]
  16. * ]
  17. * 'create_time' => [
  18. * ['between',['2017-08-01','2017-08-30']
  19. * ]
  20. * 'user_id' => 10338,
  21. * '_string' => 'status=0 and type=1',
  22. * 'name' => [
  23. * ['like', "huyunnan"]
  24. * ]
  25. * ]
  26. * @return Model
  27. */
  28. public function buildQuery($condition)
  29. {
  30. if (is_string($condition)) {
  31. return $this->whereRaw($condition);
  32. }
  33. if (is_array($condition)) {
  34. $model = $this;
  35. foreach ($condition as $key => $item) {
  36. //laravel多条件写法 [ ['id','>',1],[...] ]
  37. if (is_int($key) && is_array($item)) {
  38. $model = $model->where([$item]);
  39. continue;
  40. }
  41. switch ($key) {
  42. case '_string':
  43. $model = $model->whereRaw($item);
  44. break;
  45. case '_null':
  46. $model = $this->buildNullQuery($model, $item);
  47. break;
  48. case '_notNull':
  49. $model = $this->buildNotNullQuery($model, $item);
  50. break;
  51. default:
  52. if (!is_array($item)) {
  53. $model = $model->where($key, $item);
  54. } else {
  55. $model = $this->buildItemQuery($model, $key, $item);
  56. }
  57. }
  58. }
  59. return $model;
  60. }
  61. return $this;
  62. }
  63. /**
  64. * 查询条件
  65. * @param $model
  66. * @param $key
  67. * @param $query
  68. * @return Model|mixed
  69. */
  70. private function buildItemQuery($model, $key, $query)
  71. {
  72. // 特别处理下: 'a' => ['!=', 2]这种写法
  73. if (isset($query[0]) && !is_array($query[0])) {
  74. $query = [$query];
  75. }
  76. foreach ($query as $index => $item) {
  77. if (count($item) < 2) {
  78. continue;
  79. }
  80. switch ($item[0]) {
  81. case 'in':
  82. $model = $this->buildInQuery($model, $key, $item[1]);
  83. break;
  84. case 'notIn':
  85. $model = $this->buildNotInQuery($model, $key, $item[1]);
  86. break;
  87. case 'like':
  88. $model = $this->buildLikeQuery($model, $key, $item[1]);
  89. break;
  90. case 'between':
  91. $model = $this->buildBetweenQuery($model, $key, $item[1]);
  92. break;
  93. case 'neq':
  94. $model = $this->buildNotInQuery($model, $key, is_array($item[1])?$item[1]:[$item[1]]);
  95. break;
  96. case '>':
  97. case '<':
  98. case '=':
  99. case '>=':
  100. case '<=':
  101. $model = $model->where($key, $item[0], $item[1]);
  102. break;
  103. default:
  104. if (!is_array($item[1])) {
  105. $model = $model->where($key, $item[0], $item[1]);
  106. } else {
  107. $model = $model->where($query);
  108. }
  109. break;
  110. }
  111. unset($query[$index]);
  112. }
  113. if (!empty($query) && count($query) >= 2) {
  114. $model = $model->where($key, $query[0], $query[0] == 'like' ? '%' . $query[1] . '%' : $query[1]);
  115. }
  116. return $model;
  117. }
  118. /**
  119. * in查询条件
  120. * @param $model
  121. * @param $key
  122. * @param $query
  123. * @return Model
  124. */
  125. private function buildInQuery($model, $key, $query)
  126. {
  127. if (is_array($query)) {
  128. $model = $model->whereIn($key, $query);
  129. } else {
  130. $model = $model->whereIn($key, [$query]);
  131. }
  132. return $model;
  133. }
  134. /**
  135. * not in
  136. * @param $model
  137. * @param $key
  138. * @param $query
  139. * @return mixed
  140. */
  141. private function buildNotInQuery($model, $key, $query)
  142. {
  143. if (is_array($query)) {
  144. $model = $model->whereNotIn($key, $query);
  145. } else {
  146. $model = $model->whereNotIn($key, [$query]);
  147. }
  148. return $model;
  149. }
  150. /**
  151. * null
  152. * @param $model
  153. * @param $query
  154. * @return mixed
  155. */
  156. private function buildNullQuery($model, $query)
  157. {
  158. if (is_array($query)) {
  159. foreach ($query as $item) {
  160. $model = $model->whereNull($item);
  161. }
  162. }
  163. return $model;
  164. }
  165. /**
  166. * not null
  167. * @param $model
  168. * @param $query
  169. * @return mixed
  170. */
  171. private function buildNotNullQuery($model, $query)
  172. {
  173. if (is_string($query)) {
  174. return $model->whereNotNull($query);
  175. }
  176. if (is_array($query)) {
  177. foreach ($query as $item) {
  178. $model = $model->whereNotNull($item);
  179. }
  180. }
  181. return $model;
  182. }
  183. /**
  184. * like
  185. * @param $model
  186. * @param $query
  187. * @return mixed
  188. */
  189. private function buildLikeQuery($model, $key, $query)
  190. {
  191. if (!is_array($query)) {
  192. $model = $model->where($key, 'like', '%' . $query . '%');
  193. }
  194. return $model;
  195. }
  196. /**
  197. * between
  198. * @param $model
  199. * @param $key
  200. * @param $query
  201. * @return mixed
  202. */
  203. private function buildBetweenQuery($model, $key, $query)
  204. {
  205. if (is_array($query)) {
  206. $model = $model->whereBetween($key, $query);
  207. }
  208. return $model;
  209. }
  210. public function joinRaw()
  211. {
  212. }
  213. }