AddonsOperatingNoteComment.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace addons\OperatingNote\common\models;
  3. use addons\OperatingNote\common\enums\NoteEnum;
  4. use common\enums\StatusEnum;
  5. use common\models\user\User;
  6. use Yii;
  7. use yii\db\Exception;
  8. /**
  9. * This is the model class for table "{{%addons_operating_note_comment}}".
  10. *
  11. * @property int $id
  12. * @property int $mall_id 商城id
  13. * @property int $user_id 用户id
  14. * @property int $note_content_id 回复的笔记id
  15. * @property int $level 第n级回复
  16. * @property int $reply_id 一级评论id
  17. * @property int $content 评论内容
  18. * @property int $is_top 是否置顶
  19. * @property int $top_time 置顶时间
  20. * @property int $status -1是删除,0是禁用 1是启用
  21. * @property int $created_at
  22. * @property int $updated_at
  23. */
  24. class AddonsOperatingNoteComment extends \common\models\base\BaseActiveRecord
  25. {
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public static function tableName()
  30. {
  31. return '{{%addons_operating_note_comment}}';
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function rules()
  37. {
  38. return [
  39. [['mall_id', 'user_id', 'note_content_id'], 'required'],
  40. [['mall_id', 'user_id', 'note_content_id', 'level', 'reply_id', 'is_top', 'status', 'created_at', 'updated_at','top_time'], 'integer'],
  41. [['content'], 'string'],
  42. ];
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function attributeLabels()
  48. {
  49. return [
  50. 'id' => 'ID',
  51. 'mall_id' => '商城id',
  52. 'user_id' => '用户id',
  53. 'note_content_id' => '回复的笔记id',
  54. 'level' => '第n级回复',
  55. 'reply_id' => '一级评论id',
  56. 'content' => '评论内容',
  57. 'is_top' => '是否置顶',
  58. 'top_time' => '置顶时间',
  59. 'status' => '-1是删除,0是禁用 1是启用',
  60. 'created_at' => 'Created At',
  61. 'updated_at' => 'Updated At',
  62. ];
  63. }
  64. public function getUser()
  65. {
  66. return $this->hasOne(User::class, ['id' => 'user_id'])->select('id,mobile,nickname,avatar_url,parent_id,username');
  67. }
  68. public static function updateData($params)
  69. {
  70. $t = Yii::$app->db->beginTransaction();
  71. try {
  72. if (empty($params['attributes']) || !is_array($params['attributes'])) throw new \Exception('参数有误!');
  73. $attributes = $params['attributes'];
  74. $condition = ['id' => $params['id'], 'mall_id' => $params['mall_id']];
  75. self::updateAll($attributes, $condition);
  76. $comment_list = self::lists(['where' => [['id' => $params['id']]]]);
  77. $note_content_id_arr = array_column($comment_list, 'note_content_id');
  78. $list = self::lists(['where' => [['note_content_id' => $note_content_id_arr], ['status' => StatusEnum::ENABLED]], 'select' => 'count(*) nums,note_content_id', 'group' => 'note_content_id', 'index' => 'note_content_id']);
  79. $note_list = AddonsOperatingNoteContent::lists(['where' => [['id' => $note_content_id_arr]]], false);
  80. foreach ($note_list as $note) {
  81. $note->reply_num = $list[$note['id']]['nums']??0;
  82. $note->save();
  83. }
  84. $t->commit();
  85. return true;
  86. } catch (\Exception $e) {
  87. $t->rollBack();
  88. self::setStaticError($e->getMessage());
  89. return false;
  90. }
  91. }
  92. public static function setData($params)
  93. {
  94. $transaction = \Yii::$app->db->beginTransaction();
  95. try {
  96. $content = AddonsOperatingNoteContent::getOne([['id'=>$params['note_content_id']]]);
  97. if($content->hidden_comment == 0){
  98. return false;
  99. }
  100. $params['level'] = 1;
  101. if (isset($params['reply_id']) && $params['reply_id'] > 0) {
  102. $params['level'] = 2;
  103. }
  104. $model = new self();
  105. if (!$model->load($params, '') || !$model->save()) {
  106. throw new Exception($model->getErrorMessage());
  107. }
  108. $content->reply_num++;
  109. $content->save();
  110. $transaction->commit();
  111. return $model->toArray();
  112. } catch (Exception $e) {
  113. $transaction->rollBack();
  114. var_dump($e->getMessage());
  115. throw new Exception($e->getMessage());
  116. }
  117. }
  118. public static function editComment($params)
  119. {
  120. $transaction = \Yii::$app->db->beginTransaction();
  121. try {
  122. $detail = self::getOne([['id' => $params['note_comment_id']]]);
  123. if ($params['type'] == 1) {
  124. if ($detail->is_top == NoteEnum::STATUS_ONE) {
  125. $detail->is_top = NoteEnum::STATUS_ZERO;
  126. $detail->top_time = 0;
  127. } else {
  128. $detail->is_top = NoteEnum::STATUS_ONE;
  129. $detail->top_time = time();
  130. }
  131. } else {
  132. $detail->status = StatusEnum::DELETE;
  133. }
  134. $detail->save();
  135. $transaction->commit();
  136. return true;
  137. } catch (Exception $e) {
  138. $transaction->rollBack();
  139. var_dump($e->getMessage());
  140. throw new Exception($e->getMessage());
  141. }
  142. }
  143. }