1], [['limit'], 'default', 'value' => 10], [['ball_number'], 'string'], [['user_id', 'page', 'user_id', 'coupon_id','store_id'], 'integer'], [['user_id'], 'required', 'on' => 'user_coupon'], [['user_id', 'coupon_id'], 'required', 'on' => 'get_coupon'], [['user_id', 'user_coupon_id'], 'required', 'on' => 'check_user_coupon'], [['user_id'], 'required', 'on' => 'check_run_coupon'], [['user_id','ball_number'], 'required', 'on' => 'check_use_coupon'], ]; } public function attributeLabels() { return [ 'user_id' => 'ID', 'coupon_id' => '卡券ID', 'user_coupon_id' => '我的卡券ID', 'store_id' => '球车所属门店', ]; } //卡券中心 public function search() { if (!$this->validate()) { return $this->getModelError(); } $query = Coupon::find() ->select('id,title,price,end_time,num,get_num,user_get_num') ->where(['is_delete' => 0]) ->andWhere(['<=', 'get_start_time', time()]) ->andWhere(['>', 'get_end_time', time()]); $count = $query->count(); $pagination = new Pagination(['pageSize' => $this->limit, 'totalCount' => $count, 'page' => $this->page - 1]); $list = $query->offset($pagination->offset)->limit($pagination->limit)->orderBy(['price' => SORT_DESC])->asArray()->all(); foreach ($list as $index => &$item) { $item['date_cn'] = date('Y/m/d', $item['end_time']); $item['subhead'] = $item['date_cn'] . '前可用'; $item['plan'] = '无限'; $item['status'] = false; if ($item['num'] != '-1') { $item['plan'] = ($item['get_num'] / $item['num']) * 100; } if(!empty($this->user_id)){ $count_c = UserCoupon::find()->where(['user_id' => $this->user_id, 'coupon_id' => $item['id'], 'is_admin_send' => 0])->count(); $count_c = empty($count_c) ? 0 : $count_c; if ($count_c >= $item['user_get_num'] && !empty($item['user_get_num'])) { $item['status'] = true; } } } //是否已经全部加载 $end_flag = $this->page >= $pagination->pageCount ? true : false; return [ 'code' => 0, 'msg' => 'ok', 'data' => $list, 'count' => $count, 'page_size' => $this->limit, 'page_count' => $pagination->pageCount, 'page_no' => $this->page, 'end_flag' => $end_flag ]; } //我的卡包 public function userCoupon() { if (!$this->validate()) { return $this->getModelError(); } $query = UserCoupon::find()->alias('uc') ->select('uc.id,uc.coupon_id,c.title,c.price,c.start_time,c.end_time,c.type,c.store_ids as store_ids') ->leftJoin(['c' => Coupon::tableName()], 'uc.coupon_id=c.id') ->where(['uc.is_delete' => 0, 'uc.status' => 0, 'uc.user_id' => $this->user_id]); if(!empty($this->store_id)){ $query->andWhere([ 'OR', ['c.store_ids' => ''], new Expression("FIND_IN_SET(:store_ids, store_ids)",[":store_ids"=>$this->store_id]) ]); } $count = $query->count(); $pagination = new Pagination(['pageSize' => $this->limit, 'totalCount' => $count, 'page' => $this->page - 1]); $list = $query->offset($pagination->offset)->limit($pagination->limit)->orderBy(['uc.id' => SORT_DESC])->asArray()->all(); foreach ($list as $index => &$item) { $item['date_cn'] = date('Y/m/d', $item['start_time']) . ' - ' . date('Y/m/d', $item['end_time']); $item['subhead'] = ''; if(!empty($item['type'])){ $item['subhead'] = '立减' . $item['price'] . '元'; } } //是否已经全部加载 $end_flag = $this->page >= $pagination->pageCount ? true : false; return [ 'code' => 0, 'msg' => 'ok', 'data' => $list, 'count' => $count, 'page_size' => $this->limit, 'page_count' => $pagination->pageCount, 'page_no' => $this->page, 'end_flag' => $end_flag ]; } //领取卡券 public function getCoupon() { if (!$this->validate()) { return $this->getModelError(); } // $user_info = UserInformation::findOne(['user_id' => $this->user_id]); // if($user_info == null){ // return $this->apiReturnError('需完善会员资料', [], 4);//跳转会员注册页面 // } $coupon = Coupon::findOne(['id' => $this->coupon_id, 'is_delete' => 0]); if ($coupon == null) { return $this->apiReturnError('卡券已被下架', ['status' => 0,'msg'=>'卡券已被下架'], 2);//刷新 } $count = UserCoupon::find()->where(['user_id' => $this->user_id, 'coupon_id' => $this->coupon_id, 'is_admin_send' => 0])->count(); if ($count >= $coupon->user_get_num) { return $this->apiReturnError('您已领完此卡券,不能再次领取', ['status' => 0,'msg'=>'您已领完此卡券,不能再次领取']);//不刷新,只弹框提示 } if (time() < $coupon->get_start_time) { return $this->apiReturnError('还未到此卡券领取日期', ['status' => 0,'msg'=>'还未到此卡券领取日期']);//不刷新,只弹框提示 } if (time() >= $coupon->get_end_time) { return $this->apiReturnError('此卡券领取时间已过期,无法领取', ['status' => 0,'msg'=>'此卡券领取时间已过期,无法领取'], 2); //刷新 } if ($coupon->num != '-1' && $coupon->get_num >= $coupon->num) { return $this->apiReturnError('卡券已被抢完!', ['status' => 0,'msg'=>'卡券已被抢完'], 3);//不刷新,按钮切换为已领完 } $transaction = \Yii::$app->db->beginTransaction(); $model = new UserCoupon(); $model->user_id = $this->user_id; $model->coupon_id = $coupon->id; $model->status = 0; $model->created_at = time(); $model->updated_at = 0; $model->is_delete = 0; $model->deleted_at = 0; $model->is_admin_send = 0; if (!$model->save()) { $transaction->rollBack(); $error = $this->getModelError($model); return $this->apiReturnError($error['msg'], ['status' => 0,'msg'=>$error['msg']], 1);//不刷新,提示 } $coupon->get_num += 1; $coupon->updated_at = time(); if (!$coupon->save()) { $transaction->rollBack(); $error = $this->getModelError($coupon); return $this->apiReturnError($error['msg'], ['status' => 0,'msg'=>$error['msg']], 1);//不刷新,提示 } $transaction->commit(); if ($coupon->num != '-1' && $coupon->get_num >= $coupon->num) { return $this->apiReturnError('领取成功', ['status' => 1,'msg'=>'领取成功'], 3);//不刷新,按钮切换为已领完 } if ($count+1 >= $coupon->user_get_num) { return $this->apiReturnError('您已领完此卡券,不能再次领取', ['status' => 1,'msg'=>'领取成功'],5);//不刷新,按钮改为已领取 } return $this->apiReturnSuccess('领取成功', ['status' => 1,'msg'=>'领取成功']); } //校验用户卡券是否可使用 public function checkUserCoupon() { if (!$this->validate()) { return $this->getModelError(); } $userCoupon = UserCoupon::findOne(['id' => $this->user_coupon_id, 'user_id' => $this->user_id, 'status' => 0, 'is_delete' => 0]); if ($userCoupon == null) { return $this->apiReturnError('卡券不存在或已被使用', [], 2);//刷新页面 } $coupon = Coupon::findOne(['id' => $userCoupon->coupon_id, 'is_delete' => 0]); if ($coupon == null) { $userCoupon->is_delete = 1; $userCoupon->deleted_at = time(); if (!$userCoupon->save()) { return $this->getModelError('卡券异常!');//弹框提示 } return $this->apiReturnError('卡券已被下架', [], 2);//刷新页面 } if (time() < $coupon->start_time) { return $this->apiReturnError('此卡券未到使用日期');//弹框提示 } if (time() >= $coupon->end_time) { $userCoupon->is_delete = 1; $userCoupon->deleted_at = time(); if (!$userCoupon->save()) { return $this->getModelError('卡券异常!');//弹框提示 } return $this->apiReturnError('此卡券已过期', [], 2);//弹框提示,刷新页面 } return $this->apiReturnSuccess('可使用'); } /** *获取用户优惠券信息 */ public function getUserCoupon() { $userCoupon = UserCoupon::findOne(['id' => $this->user_coupon_id, 'user_id' => $this->user_id, 'status' => 0, 'is_delete' => 0]); $coupon_id = $userCoupon['coupon_id']; $couponArr = Coupon::findOne(['id' => $coupon_id]); if ($couponArr == null) { return $this->apiReturnError('优惠券错误');//弹框提示 } return $couponArr; } public function checkRunCoupon() { if (!$this->validate()) { return $this->getModelError(); } $redis_name = "api:cxaibc:coupon:check:run".$this->user_id; $redis = \Yii::$app->redis; if($redis->get($redis_name)){ return $this->apiReturnSuccess('ok'); } $list = Coupon::find() ->select('id,title,price,end_time,num,get_num,user_get_num') ->where(['is_delete' => 0]) ->andWhere(['<=', 'get_start_time', time()]) ->andWhere(['>', 'get_end_time', time()]) ->orderBy(['type' => SORT_ASC,'price' => SORT_DESC]) ->asArray() ->all(); foreach ($list as $index => &$item) { $item['date_cn'] = date('Y/m/d', $item['end_time']); $item['subhead'] = $item['date_cn'] . '前可用'; $item['plan'] = '无限'; $item['status'] = false; $item['price'] = floor($item['price']); if ($item['num'] != '-1') { $item['plan'] = ($item['get_num'] / $item['num']) * 100; } $count_c = UserCoupon::find()->where(['user_id' => $this->user_id, 'coupon_id' => $item['id'], 'is_admin_send' => 0])->count(); $count_c = empty($count_c) ? 0 : $count_c; if ($count_c >= $item['user_get_num'] && !empty($item['user_get_num'])) { unset($list[$index]); } if($item['num'] != '-1' && $item['num'] == $item['get_num']){ unset($list[$index]); } } if(!empty($list)){ $redis->setex($redis_name,600,$this->user_id); } $list = array_values($list); return $this->apiReturnSuccess('ok',$list); } public function checkUseCoupon() { if (!$this->validate()) { return $this->getModelError(); } $ball = BallCart::findOne(['ball_number' => $this->ball_number,'is_delete' => 0]); if($ball == null){ return $this->apiReturnSuccess(); } $this->store_id = $ball->store_id; $data = UserCoupon::find()->alias('uc') ->select('uc.id,c.title,c.store_ids,c.type,c.price') ->leftJoin(['c' => Coupon::tableName()], 'uc.coupon_id=c.id') ->where(['uc.is_delete' => 0, 'uc.status' => 0, 'uc.user_id' => $this->user_id,'c.is_delete'=>0]) ->andWhere(['<=', 'start_time', time()]) ->andWhere(['>', 'end_time', time()]) ->andWhere([ 'OR', ['c.store_ids' => ''], new Expression("FIND_IN_SET(:store_ids, store_ids)",[":store_ids"=>$this->store_id]) ]) ->orderBy(['c.type' => SORT_ASC,'c.price' => SORT_DESC]) ->asArray() ->one(); if(empty($data)){ return $this->apiReturnSuccess(); } unset($data['store_ids']); return $this->apiReturnSuccess('ok',$data); } }