| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- <?php
- /**
- * 厦门云联储网络科技有限公司
- * https://www.baokuaiyun.com
- * Copyright (c) 2023 爆块云 All rights reserved.
- */
- namespace app\models;
- use yii\db\ActiveRecord;
- /**
- * This is the model class for table "{{%q_and_a_qa}}".
- *
- * @property int $id
- * @property int $store_id
- * @property int $is_delete
- * @property int $user_id
- * @property string $question
- * @property string $answer
- * @property int $created_at
- * @property int $use_integral
- * @property int $answer_time
- */
- class QAndAQa extends \yii\db\ActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%q_and_a_qa}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['store_id', 'user_id'], 'integer'],
- [['question', 'answer'], 'string'],
- [['use_integral'], 'default', 'value' => 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('积分不足, 请先充值后再提问!');
- }
- }
- }
- }
- }
|