| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- <?php
- namespace addons\ShortVideo\common\models;
- use common\enums\StatusEnum;
- use common\models\user\User;
- use Yii;
- /**
- * This is the model class for table "{{%addons_short_video_comment}}".
- *
- * @property int $id
- * @property int $mall_id
- * @property int $user_id
- * @property int $video_id 视频id
- * @property string $content 评论内容
- * @property int $like_num 点赞量
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at
- * @property int $updated_at
- * @property int $is_top
- */
- class ShortVideoComment extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_short_video_comment}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'user_id', 'video_id', 'content'], 'required'],
- [['mall_id', 'user_id', 'video_id', 'like_num', 'status', 'created_at', 'updated_at', 'is_top'], 'integer'],
- [['content'], 'string', 'max' => 255],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => 'Mall ID',
- 'user_id' => 'User ID',
- 'video_id' => 'Video ID',
- 'content' => 'Content',
- 'like_num' => 'Like Num',
- 'status' => 'Status',
- 'is_top' => 'is_top',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- public function getUser()
- {
- return $this->hasOne(User::class, ['id' => 'user_id']);
- }
- public static function updateData($params)
- {
- $t = Yii::$app->db->beginTransaction();
- try {
- if (empty($params['attributes']) || !is_array($params['attributes'])) throw new \Exception('参数有误!');
- $attributes = $params['attributes'];
- $condition = ['id' => $params['id'], 'mall_id' => $params['mall_id']];
- self::updateAll($attributes, $condition);
- $comment_list = self::lists(['where' => [['id' => $params['id']]]]);
- $video_id_arr = array_column($comment_list, 'video_id');
- $list = self::lists(['where' => [['video_id' => $video_id_arr], ['status' => StatusEnum::ENABLED]], 'select' => 'count(*) nums,video_id', 'group' => 'video_id', 'index' => 'video_id']);
- $video_list = ShortVideo::lists(['where' => [['id' => $video_id_arr]]], false);
- foreach ($video_list as $video) {
- $video->comment_num = $list[$video['id']]['nums'] ?? 0;
- $video->save();
- }
- $t->commit();
- return true;
- } catch (\Exception $e) {
- $t->rollBack();
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- public static function setData(int $mall_id, array $data)
- {
- if (!empty($data['id'])) {
- $model = self::findOne(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'id' => $data['id']]);
- if (empty($model)) {
- self::$static_error = '评论不存在';
- return false;
- }
- } else {
- $model = new self();
- $model->loadDefaultValues();
- $model->mall_id = $mall_id;
- }
- foreach ($data as $key => $val) {
- $model->$key = $val;
- }
- $transaction = \Yii::$app->db->beginTransaction();
- if (!$model->save()) {
- $transaction->rollBack();
- self::$static_error = '保存数据失败:' . $model->getErrorMessage();
- return false;
- }
- $video = ShortVideo::findOne(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'id' => $data['video_id']]);
- $video->comment_num += 1;
- $res_video = $video->save();
- if (false === $res_video) {
- $transaction->rollBack();
- self::$static_error = '保存数据失败:' . $video->getError();
- return false;
- }
- $transaction->commit();
- return $model;
- }
- public static function commentLike(int $mall_id, array $data)
- {
- $comment = ShortVideoComment::findOne(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'id' => $data['comment_id']]);
- if (empty($comment)) {
- self::$static_error = '点赞的评论不存在';
- return false;
- }
- $like_times = 0;
- $tatus = 1;
- if ('praise' == $data['type']) {
- $like_times = $comment->like_num + 1;
- $status = StatusEnum::ENABLED;
- } elseif ('unpraise' == $data['type']) {
- $like_times = max($comment->like_num - 1, 0);
- $status = StatusEnum::DELETE;
- }
- $comment_params = [
- 'id' => $data['comment_id'],
- 'like_num' => $like_times,
- ];
- $transaction = \Yii::$app->db->beginTransaction();
- $res_comment = self::setData($mall_id, $comment_params);
- if (false === $res_comment) {
- $transaction->rollBack();
- return false;
- }
- $liek_params = [
- 'user_id' => $data['user_id'],
- 'comment_id' => $data['comment_id'],
- 'status' => $status,
- ];
- $res_like = ShortVideoCommentLike::setData($mall_id, $liek_params);
- if (false === $res_like) {
- $transaction->rollBack();
- self::$static_error = ShortVideoCommentLike::getStaticError();
- return false;
- }
- $transaction->commit();
- return true;
- }
- }
|