CouponController.php 2.6 KB

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