QAndAQa.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * 厦门云联储网络科技有限公司
  4. * https://www.baokuaiyun.com
  5. * Copyright (c) 2023 爆块云 All rights reserved.
  6. */
  7. namespace app\models;
  8. use yii\db\ActiveRecord;
  9. /**
  10. * This is the model class for table "{{%q_and_a_qa}}".
  11. *
  12. * @property int $id
  13. * @property int $store_id
  14. * @property int $is_delete
  15. * @property int $user_id
  16. * @property string $question
  17. * @property string $answer
  18. * @property int $created_at
  19. * @property int $use_integral
  20. * @property int $answer_time
  21. */
  22. class QAndAQa extends \yii\db\ActiveRecord
  23. {
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public static function tableName()
  28. {
  29. return '{{%q_and_a_qa}}';
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function rules()
  35. {
  36. return [
  37. [['store_id', 'user_id'], 'integer'],
  38. [['question', 'answer'], 'string'],
  39. [['use_integral'], 'default', 'value' => 0],
  40. [['created_at', 'is_delete', 'answer_time'], 'safe']
  41. ];
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function attributeLabels()
  47. {
  48. return [
  49. 'id' => 'ID',
  50. 'store_id' => 'Store ID',
  51. 'user_id' => '用户id',
  52. 'question' => '问题',
  53. 'answer' => '答案',
  54. 'created_at' => '添加时间',
  55. 'is_delete' => '是否删除',
  56. 'answer_time' => '回答时间',
  57. ];
  58. }
  59. public function afterSave($insert, $changedAttributes)
  60. {
  61. parent::afterSave($insert, $changedAttributes);
  62. if ($insert) {
  63. $setting = QAndASetting::find()->where(['store_id' => get_store_id()])->one();
  64. if ($setting && $setting->q_integral > 0) {
  65. $user = User::findOne($this->user_id);
  66. if ($user && $user->integral >= $setting->q_integral) {
  67. $old_integral = $user->integral;
  68. $user->integral -= $setting->q_integral;
  69. if ($user->save()) {
  70. $log = new AccountLog();
  71. $log->store_id = get_store_id();
  72. $log->user_id = $user->id;
  73. $log->type = AccountLog::TYPE_INTEGRAL;
  74. $log->log_type = AccountLog::LOG_TYPE_EXPEND;
  75. $log->amount = $setting->q_integral;
  76. $log->desc = "问答提问消耗积分, 提问id:{$this->id}";
  77. $log->before = $old_integral;
  78. $log->after = $user->integral;
  79. $log->operator = '';
  80. $log->operator_id = 0;
  81. $log->operator_type = AccountLog::TYPE_OPERATOR_NORMAL;
  82. $log->created_at = time();
  83. $log->order_id = $this->id;
  84. $log->order_type = AccountLog::TYPE_QUESTION_INTEGRAL;
  85. $log->save();
  86. }
  87. } else {
  88. throw new \Exception('积分不足, 请先充值后再提问!');
  89. }
  90. }
  91. }
  92. }
  93. }