0], [['created_at', 'is_delete', 'answer_time'], 'safe'] ]; } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id' => 'ID', 'store_id' => 'Store ID', 'user_id' => '用户id', 'question' => '问题', 'answer' => '答案', 'created_at' => '添加时间', 'is_delete' => '是否删除', 'answer_time' => '回答时间', ]; } public function afterSave($insert, $changedAttributes) { parent::afterSave($insert, $changedAttributes); if ($insert) { $setting = QAndASetting::find()->where(['store_id' => get_store_id()])->one(); if ($setting && $setting->q_integral > 0) { $user = User::findOne($this->user_id); if ($user && $user->integral >= $setting->q_integral) { $old_integral = $user->integral; $user->integral -= $setting->q_integral; if ($user->save()) { $log = new AccountLog(); $log->store_id = get_store_id(); $log->user_id = $user->id; $log->type = AccountLog::TYPE_INTEGRAL; $log->log_type = AccountLog::LOG_TYPE_EXPEND; $log->amount = $setting->q_integral; $log->desc = "问答提问消耗积分, 提问id:{$this->id}"; $log->before = $old_integral; $log->after = $user->integral; $log->operator = ''; $log->operator_id = 0; $log->operator_type = AccountLog::TYPE_OPERATOR_NORMAL; $log->created_at = time(); $log->order_id = $this->id; $log->order_type = AccountLog::TYPE_QUESTION_INTEGRAL; $log->save(); } } else { throw new \Exception('积分不足, 请先充值后再提问!'); } } } } }