// 保存评论
public function saveComment($data)
{
Db::startTrans();
try{
//计算嵌套循环
$query = self::withTrashed()->order('rgt','desc');
$parent_id = $data['parent_id'] ?? 0;
if($parent_id){
$query->where('id',$parent_id);
}
$parent = $query->find();
if($parent_id>0 && $parent){
if($parent['rgt'] == $parent['lft'] + 1){
//叶子节点
$rgt = $parent['lft'];
}else{
//子节点
$rgt = self::withTrashed()->where('parent_id',$parent->id)->order('rgt','desc')->lock(true)->value('rgt');
// $rgt = $rgt>0 ? $rgt : 0;
}
$parent_deep = $parent['deep'] ?? 0;
}else{
$rgt = $parent['rgt'] ?? 0;
$parent_deep = 0;
}
self::withTrashed()->where('rgt','>',$rgt)->order('rgt','desc')->inc('rgt',2)->update();
self::withTrashed()->where('lft','>',$rgt)->order('lft','desc')->inc('lft',2)->update();
$this->user_id = $data['user_id'];
$this->topic_id = $data['topic_id'];
$this->content = $data['content'];
$this->status = BaseModel::ENABLE;
$this->parent_id = $parent_id;
$this->lft = $rgt + 1;
$this->rgt = $rgt + 2;
$this->deep = $parent_deep + 1;
$result = $this->save();
if($result){
//计算活跃度
$active_num = Config::get('topic.active_num_comment',5);
//增加话题评论数
$result = Topic::where('id',$this->topic_id)->inc('comment_num')->inc('active_num',$active_num)->update();
//增加mongo话题评论数
$result2 = \app\topic\mongo\Topic::where('id',(int)$this->topic_id)->inc('comment_num')->inc('active_num',$active_num)->update();
//增加上级评论数
if($parent_id > 0){
self::where('id',$parent_id)->inc('comment_num')->update();
}
}
$topic_user_id = 0;
$topic_info = (new Topic())->where('')->find();
if($topic_info){
$topic_user_id = $topic_info['id'];
}
UserEvent::create(['user_id'=>$data['user_id'],'topic_id'=>$data['topic_id'],'create_time'=>time(),'status'=>1,'topic_user_id'=>$topic_user_id,'comment_msg'=>$data['content'],'is_see'=>0]);
Db::commit();
}catch (\Throwable $e){
Db::rollback();
return false;
}
return true;
}
暂无评论