BrowseController.php 2.6 KB

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