ShortVideoComment.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace addons\ShortVideo\common\models;
  3. use common\enums\StatusEnum;
  4. use common\models\user\User;
  5. use Yii;
  6. /**
  7. * This is the model class for table "{{%addons_short_video_comment}}".
  8. *
  9. * @property int $id
  10. * @property int $mall_id
  11. * @property int $user_id
  12. * @property int $video_id 视频id
  13. * @property string $content 评论内容
  14. * @property int $like_num 点赞量
  15. * @property int $status 状态[-1:删除;0:禁用;1启用]
  16. * @property int $created_at
  17. * @property int $updated_at
  18. * @property int $is_top
  19. */
  20. class ShortVideoComment extends \common\models\base\BaseActiveRecord
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public static function tableName()
  26. {
  27. return '{{%addons_short_video_comment}}';
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function rules()
  33. {
  34. return [
  35. [['mall_id', 'user_id', 'video_id', 'content'], 'required'],
  36. [['mall_id', 'user_id', 'video_id', 'like_num', 'status', 'created_at', 'updated_at', 'is_top'], 'integer'],
  37. [['content'], 'string', 'max' => 255],
  38. ];
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function attributeLabels()
  44. {
  45. return [
  46. 'id' => 'ID',
  47. 'mall_id' => 'Mall ID',
  48. 'user_id' => 'User ID',
  49. 'video_id' => 'Video ID',
  50. 'content' => 'Content',
  51. 'like_num' => 'Like Num',
  52. 'status' => 'Status',
  53. 'is_top' => 'is_top',
  54. 'created_at' => 'Created At',
  55. 'updated_at' => 'Updated At',
  56. ];
  57. }
  58. public function getUser()
  59. {
  60. return $this->hasOne(User::class, ['id' => 'user_id']);
  61. }
  62. public static function updateData($params)
  63. {
  64. $t = Yii::$app->db->beginTransaction();
  65. try {
  66. if (empty($params['attributes']) || !is_array($params['attributes'])) throw new \Exception('参数有误!');
  67. $attributes = $params['attributes'];
  68. $condition = ['id' => $params['id'], 'mall_id' => $params['mall_id']];
  69. self::updateAll($attributes, $condition);
  70. $comment_list = self::lists(['where' => [['id' => $params['id']]]]);
  71. $video_id_arr = array_column($comment_list, 'video_id');
  72. $list = self::lists(['where' => [['video_id' => $video_id_arr], ['status' => StatusEnum::ENABLED]], 'select' => 'count(*) nums,video_id', 'group' => 'video_id', 'index' => 'video_id']);
  73. $video_list = ShortVideo::lists(['where' => [['id' => $video_id_arr]]], false);
  74. foreach ($video_list as $video) {
  75. $video->comment_num = $list[$video['id']]['nums'] ?? 0;
  76. $video->save();
  77. }
  78. $t->commit();
  79. return true;
  80. } catch (\Exception $e) {
  81. $t->rollBack();
  82. self::setStaticError($e->getMessage());
  83. return false;
  84. }
  85. }
  86. public static function setData(int $mall_id, array $data)
  87. {
  88. if (!empty($data['id'])) {
  89. $model = self::findOne(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'id' => $data['id']]);
  90. if (empty($model)) {
  91. self::$static_error = '评论不存在';
  92. return false;
  93. }
  94. } else {
  95. $model = new self();
  96. $model->loadDefaultValues();
  97. $model->mall_id = $mall_id;
  98. }
  99. foreach ($data as $key => $val) {
  100. $model->$key = $val;
  101. }
  102. $transaction = \Yii::$app->db->beginTransaction();
  103. if (!$model->save()) {
  104. $transaction->rollBack();
  105. self::$static_error = '保存数据失败:' . $model->getErrorMessage();
  106. return false;
  107. }
  108. $video = ShortVideo::findOne(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'id' => $data['video_id']]);
  109. $video->comment_num += 1;
  110. $res_video = $video->save();
  111. if (false === $res_video) {
  112. $transaction->rollBack();
  113. self::$static_error = '保存数据失败:' . $video->getError();
  114. return false;
  115. }
  116. $transaction->commit();
  117. return $model;
  118. }
  119. public static function commentLike(int $mall_id, array $data)
  120. {
  121. $comment = ShortVideoComment::findOne(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'id' => $data['comment_id']]);
  122. if (empty($comment)) {
  123. self::$static_error = '点赞的评论不存在';
  124. return false;
  125. }
  126. $like_times = 0;
  127. $tatus = 1;
  128. if ('praise' == $data['type']) {
  129. $like_times = $comment->like_num + 1;
  130. $status = StatusEnum::ENABLED;
  131. } elseif ('unpraise' == $data['type']) {
  132. $like_times = max($comment->like_num - 1, 0);
  133. $status = StatusEnum::DELETE;
  134. }
  135. $comment_params = [
  136. 'id' => $data['comment_id'],
  137. 'like_num' => $like_times,
  138. ];
  139. $transaction = \Yii::$app->db->beginTransaction();
  140. $res_comment = self::setData($mall_id, $comment_params);
  141. if (false === $res_comment) {
  142. $transaction->rollBack();
  143. return false;
  144. }
  145. $liek_params = [
  146. 'user_id' => $data['user_id'],
  147. 'comment_id' => $data['comment_id'],
  148. 'status' => $status,
  149. ];
  150. $res_like = ShortVideoCommentLike::setData($mall_id, $liek_params);
  151. if (false === $res_like) {
  152. $transaction->rollBack();
  153. self::$static_error = ShortVideoCommentLike::getStaticError();
  154. return false;
  155. }
  156. $transaction->commit();
  157. return true;
  158. }
  159. }