UserCouponController.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace addons\ElectronCoupon\backend\controllers;
  3. use addons\ElectronCoupon\common\models\ElectronCoupon;
  4. use addons\ElectronCoupon\common\models\ElectronCouponUserCoupon;
  5. use common\enums\StatusEnum;
  6. use common\models\user\User;
  7. use Yii;
  8. class UserCouponController extends BaseController
  9. {
  10. public $enableCsrfValidation = false;
  11. /**
  12. * 会员电子券
  13. *
  14. * @return string
  15. */
  16. public function actionIndex()
  17. {
  18. if (Yii::$app->request->isAjax && Yii::$app->request->isPost) {
  19. $params = Yii::$app->request->post();
  20. $res = Yii::$app->services->validate->validate($params, [
  21. [['page', 'user_id', 'coupon_type','e_coupon_id'], 'integer'],
  22. [['name', 'user_name', 'coupon_name'], 'string'],
  23. [['receive_time'], 'safe'],
  24. ]);
  25. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  26. $where = [
  27. ['=', 'mall_id', $this->mall_id],
  28. ['>=', 'status', StatusEnum::DISABLED],
  29. ];
  30. if ($params['user_id']) {
  31. $where[] = ['=', 'user_id', (int)$params['user_id']];
  32. }
  33. // 昵称、手机号
  34. if ($params['user_name']) {
  35. $user_ids = User::find()
  36. ->orWhere(['like', 'nickname', "%" . $params['user_name'] . "%", false])
  37. ->orWhere(['like', 'mobile', "%" . $params['user_name'] . "%", false])
  38. ->select('id')
  39. ->asArray()
  40. ->column();
  41. $where[] = ['in', 'user_id', $user_ids];
  42. }
  43. // 电子券名称
  44. if ($params['coupon_name']) {
  45. $coupon_ids = ElectronCoupon::find()
  46. ->where(['like', 'name', "%" . $params['coupon_name'] . "%", false])
  47. ->select('id')
  48. ->asArray()
  49. ->column();
  50. $where[] = ['in', 'e_coupon_id', $coupon_ids];
  51. }
  52. if(!empty($params['e_coupon_id']))$where[] = ['e_coupon_id'=>$params['e_coupon_id']];
  53. if ($params['coupon_type'] == 1) { // 免费券
  54. $where[] = ['=', 'price', 0];
  55. } else if ($params['coupon_type'] == 2) { // 激活券
  56. $where[] = ['>', 'price', 0];
  57. }
  58. // 领取时间
  59. $start_time = strtotime($params['receive_time'][0] ?? 0);
  60. $end_time = strtotime($params['receive_time'][1] ?? 0);
  61. if ($start_time > 0 && $end_time > 0 && $end_time >= $start_time) {
  62. $where[] = ['>=', 'last_receive_time', $start_time];
  63. $where[] = ['<=', 'last_receive_time', $end_time];
  64. }
  65. $condition = ['where' => $where, 'with' => ['user', 'properUser', 'giver', 'goods'], 'order' => 'id desc'];
  66. $page_data = ElectronCouponUserCoupon::getListPage($condition, $this->mall_id);
  67. $this->success('获取成功', compact('page_data'));
  68. } else {
  69. return $this->render('index');
  70. }
  71. }
  72. }