CollectionController.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace app\admin\controller\client;
  3. use app\controller\Curd;
  4. use app\model\ClientFavorite;
  5. use support\Request;
  6. use support\Response;
  7. class CollectionController extends Curd
  8. {
  9. public function __construct()
  10. {
  11. $this->model = new ClientFavorite();
  12. }
  13. public function select(Request $request): Response
  14. {
  15. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  16. $order = $request->get('order', 'desc');
  17. $field = $field ?? 'client_favorite_addtimes';
  18. $query = $this->doSelect($where, $field, $order);
  19. return $this->doFormat($query, $format, $limit);
  20. }
  21. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  22. {
  23. $model = $this->model->with([
  24. 'member' => function ($query) {
  25. $query->select('member_id', 'member_mobile');
  26. },
  27. 'goods' => function ($query) {
  28. $query->select('goods_id', 'goods_name');
  29. }
  30. ]);
  31. foreach ($where as $column => $value) {
  32. if (is_array($value)) {
  33. if ($value[0] === 'like' || $value[0] === 'not like') {
  34. $model = $model->where($column, $value[0], "%$value[1]%");
  35. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  36. $model = $model->where($column, $value[0], $value[1]);
  37. } elseif ($value[0] == 'in' && !empty($value[1])) {
  38. $valArr = $value[1];
  39. if (is_string($value[1])) {
  40. $valArr = explode(",", trim($value[1]));
  41. }
  42. $model = $model->whereIn($column, $valArr);
  43. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  44. $valArr = $value[1];
  45. if (is_string($value[1])) {
  46. $valArr = explode(",", trim($value[1]));
  47. }
  48. $model = $model->whereNotIn($column, $valArr);
  49. } elseif ($value[0] == 'null') {
  50. $model = $model->whereNull($column);
  51. } elseif ($value[0] == 'not null') {
  52. $model = $model->whereNotNull($column);
  53. } elseif ($value[0] !== '' || $value[1] !== '') {
  54. $model = $model->whereBetween($column, $value);
  55. }
  56. } else {
  57. $model = $model->where($column, $value);
  58. }
  59. }
  60. if ($field) {
  61. $model = $model->orderBy($field, $order);
  62. }
  63. return $model;
  64. }
  65. }