Exemplo n.º 1
0
 /**
  * 获取演员评论信息
  * @param $actor_id
  * @param $index
  * @param $count
  * @return array|int|\yii\mongodb\ActiveRecord
  */
 public function commentList($actor_id, $index, $count)
 {
     if (!is_numeric($actor_id)) {
         return ErrorConstant::PARAM_ERROR;
     }
     $index = is_numeric($index) ? $index : self::PAGE_INDEX_DEFAULT;
     $count = is_numeric($count) ? $count : self::PAGE_COUNT_DEFAULT;
     $offset = ($index - 1) * $count;
     //判断演员是否存在
     $actor = ActorModel::find()->where(['status' => ActorModel::ACTOR_STATUS_OK, 'id' => $actor_id])->asArray()->one();
     if ($actor === []) {
         return ErrorConstant::ACTOR_NOT_EXISTS;
     }
     //获取评论列表
     $commentList = ActorCommentModel::find()->where(['actor_id' => (int) $actor_id])->offset($offset)->limit($count)->asArray()->orderBy('create_time DESC')->all();
     if ($commentList === []) {
         return $commentList;
     }
     $user_id = array_unique(array_merge(ArrayHelper::getColumn($commentList, 'user_id'), ArrayHelper::getColumn($commentList, 'target_user_id')));
     //获取用户信息
     $comment_user = UserModel::find()->where(['in', 'id', $user_id])->asArray()->all();
     $comment_user = ArrayHelper::index($comment_user, 'id');
     //获取用户评论的评论信息
     $comment_id = array_unique(ArrayHelper::getColumn($commentList, 'target_comment_id'));
     $target_comment = ActorCommentModel::find()->where(['in', '_id', $comment_id])->asArray()->all();
     $target_comment = ArrayHelper::index($target_comment, '_id');
     foreach ($commentList as $key => $comment) {
         $commentList[$key]['user_name'] = $comment_user[$comment['user_id']]['nick_name'];
         $commentList[$key]['icon_url'] = $comment_user[$comment['user_id']]['icon_url'];
         if ($comment['target_comment_id'] >= 0) {
             $commentList[$key]['target_user_name'] = $comment_user[$comment['target_user_id']]['nick_name'];
             $commentList[$key]['target_user_icon_url'] = $comment_user[$comment['target_user_id']]['icon_url'];
             $commentList[$key]['target_comment_content'] = $target_comment[$comment['target_user_id']]['content'];
         }
     }
     return $commentList;
 }