/** * Auther:langxi * * 用户输入邀请码获取加息劵,需要改变activity_code的状态值,1为被领取,0为未领取 */ public static function getCoupon($member_id, $name) { if (!$member_id || !is_numeric($member_id) || !is_int($member_id)) { $result = array('errorNum' => '1', 'errorMsg' => '参数错误', 'data' => null); return $result; } $name = Code::find()->where(['name' => $name, 'status' => '0'])->asArray()->one(); if (!$name) { $result = array('errorNum' => '1', 'errorMsg' => '邀请码不存在或已被使用', 'data' => null); return $result; } $id = $name['id']; //邀请码对应的加息劵id(code表的id) $coupon_id = $name['coupon_id']; //加息劵id //$validity_time = $name['validity_time']; //加息劵作用时间长度 $rate = $name['rate']; //利率 $use_at = $name['use_at']; //有效期时间起点 $use_end_time = $name['use_end_time']; //有效期时间结束点 //事物回滚 $transaction = \Yii::$app->db->beginTransaction(); try { //将邀请码获取到的加息劵放入到用户的加息劵中 $raise_card = new RaiseCard(); $raise_card->member_id = $member_id; $raise_card->coupon_id = $coupon_id; $raise_card->validity_start_at = $use_at; $raise_card->validity_out_at = $use_end_time; $raise_card->rate = $rate; $raise_card = $raise_card->save(); if (!$raise_card) { $result = array('errorNum' => '1', 'errorMsg' => '将邀请码获取到的加息劵放入到用户的加息劵中失败', 'data' => null); return $result; } //改变邀请码对应的加息劵的状态为被领取 $code = Code::findOne($id); $code->status = '1'; $code = $code->save(); if (!$code) { $result = array('errorNum' => '1', 'errorMsg' => '改变邀请码状态失败', 'data' => null); return $result; } $transaction->commit(); $result = array('errorNum' => '0', 'errorMsg' => 'success', 'data' => null); return $result; } catch (ErrorException $e) { $transaction->rollBack(); $remark = $e->getMessage(); $result = array('errorNum' => '1', 'errorMsg' => $remark, 'data' => null); return $result; } }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Code::find()->orderBy('id DESC'); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 10]]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'status' => $this->status, 'display' => $this->display]); $query->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'validity_time', $this->validity_time])->andFilterWhere(['like', 'rate', $this->rate])->andFilterWhere(['like', 'use_end_time', $this->use_end_time])->andFilterWhere(['like', 'created_at', $this->created_at])->andFilterWhere(['like', 'updated_at', $this->updated_at]); return $dataProvider; }
/** * Auther:langxi * * 用户输入邀请码获取加息劵,需要改变activity_code的状态值,1为被领取,0为未领取 */ public static function getCoupon($member_id, $name) { $name = Code::find()->where(['name' => $name, 'status' => '0'])->asArray()->one(); if (!$name) { throw new ErrorException('邀请码不存在或已被使用', 7001); } $id = $name['id']; //邀请码对应的加息劵id(code表的id) $coupon_id = $name['coupon_id']; //加息劵id //$validity_time = $name['validity_time']; //加息劵作用时间长度 $rate = $name['rate']; //利率 $use_at = $name['use_at']; //有效期时间起点 $use_end_time = $name['use_end_time']; //有效期时间结束点 //事物回滚 $transaction = \Yii::$app->db->beginTransaction(); try { //将邀请码获取到的加息劵放入到用户的加息劵中 $raise_card = new RaiseCard(); $raise_card->member_id = $member_id; $raise_card->coupon_id = $coupon_id; $raise_card->validity_start_at = $use_at; $raise_card->validity_out_at = $use_end_time; $raise_card->rate = $rate; $raise_card = $raise_card->save(); if (!$raise_card) { throw new ErrorException('将邀请码获取到的加息劵放入到用户的加息劵中失败', 7001); } //改变邀请码对应的加息劵的状态为被领取 $code = Code::findOne($id); $code->status = '1'; $code = $code->save(); if (!$code) { throw new ErrorException('改变邀请码状态失败', 7001); } $transaction->commit(); return true; } catch (ErrorException $e) { $transaction->rollBack(); } }
/** * Finds the Code model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Code the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Code::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
<p> </p> <?php echo GridView::widget(['dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [['class' => 'yii\\grid\\SerialColumn'], 'name', 'validity_time', ['attribute' => 'rate', 'value' => function ($model) { return $model->rate * 100; }], ['attribute' => 'use_end_time', 'format' => ['date', 'php:Y-m-d']], ['attribute' => 'status', 'format' => 'html', 'value' => function ($model) { if ($model->status === $model::STATUS_USE) { $class = 'label-success'; } elseif ($model->status === $model::STATUS_INACTIVE) { $class = 'label-warning'; } else { $class = 'label-danger'; } return '<span class="label ' . $class . '">' . $model->statusLabel . '</span>'; }, 'filter' => Html::activeDropDownList($searchModel, 'status', Code::labels(), ['class' => 'form-control', 'prompt' => Yii::t('app', 'Please Filter')])], ['attribute' => 'display', 'format' => 'html', 'value' => function ($model) { if ($model->display === $model::DISPLAY_INACTIVE) { $class = 'label-success'; } elseif ($model->display === $model::DISPLAY_USE) { $class = 'label-warning'; } else { $class = 'label-danger'; } return '<span class="label ' . $class . '">' . $model->displaysLabel . '</span>'; }, 'filter' => Html::activeDropDownList($searchModel, 'display', Code::displays(), ['class' => 'form-control', 'prompt' => Yii::t('app', 'Please Filter')])], ['class' => 'yii\\grid\\ActionColumn', 'header' => '操作', 'buttons' => ['delete' => function ($url, $model, $key) { return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, ['title' => '删除', 'data' => ['confirm' => '你确定要删除吗?', 'method' => 'post']]); }]]]]); ?> </div>