Example #1
0
 /**
  * Finds the MeritLog model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return MeritLog the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = MeritLog::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
Example #2
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = MeritLog::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $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, 'user_id' => $this->user_id, 'merit_template_id' => $this->merit_template_id, 'type' => $this->type, 'action_type' => $this->action_type, 'increment' => $this->increment, 'created_at' => $this->created_at]);
     $query->andFilterWhere(['like', 'description', $this->description])->andFilterWhere(['like', 'username', $this->username]);
     return $dataProvider;
 }
Example #3
0
 /**
  * @param MeritTemplate $meritTemplate
  * @throws Exception
  */
 public function update(MeritTemplate $meritTemplate)
 {
     $meritLog = new MeritLog();
     $user = \Yii::$app->user->identity;
     $transaction = \Yii::$app->db->beginTransaction();
     try {
         /** @var Merit $userMerit */
         $userMerit = Merit::findOne(['user_id' => $user->getId(), 'type' => $meritTemplate->type]);
         // is sub 判断是否是减法
         $actionSub = $meritTemplate->action_type == MeritTemplate::ACTIVE_TYPE_SUB;
         if ($userMerit) {
             $merit = call_user_func($actionSub ? 'bcsub' : 'bcadd', $userMerit->merit, $meritTemplate->increment);
             $userMerit->setAttributes(['merit' => (int) $merit]);
         } else {
             $userMerit = new Merit();
             $userMerit->setAttributes(['merit' => ($actionSub ? '-' : '') . $meritTemplate->increment, 'user_id' => $user->getId(), 'username' => $user->username, 'type' => $meritTemplate->type]);
         }
         if (!$userMerit->save()) {
             Yii::error('Merit 操作失败' . json_encode(array_values($userMerit->getFirstErrors())), 'error');
             throw new Exception(array_values($userMerit->getFirstErrors())[0]);
         }
         $description = $meritTemplate->title . ': ' . MeritTemplate::getActionTypes()[$meritTemplate->action_type] . $meritTemplate->increment . MeritTemplate::getTypes()[$meritTemplate->type];
         $meritLog->setAttributes(['user_id' => $user->getId(), 'username' => $user->username, 'merit_template_id' => $meritTemplate->id, 'type' => $meritTemplate->type, 'description' => $description, 'action_type' => $meritTemplate->action_type, 'increment' => $meritTemplate->increment, 'created_at' => time()]);
         if (!$meritLog->save()) {
             throw new Exception(array_values($meritLog->getFirstErrors())[0]);
         }
         $transaction->commit();
     } catch (Exception $e) {
         $transaction->rollBack();
     }
 }
Example #4
0
 public function actionPoint($username = '')
 {
     $user = $this->user($username);
     $dataProvider = new ActiveDataProvider(['query' => MeritLog::find()->where(['user_id' => $user->id, 'type' => 1])->orderBy(['created_at' => SORT_DESC])]);
     return $this->render('show', ['user' => $user, 'dataProvider' => $dataProvider]);
 }