CartController.php 2.6 KB

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