<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
/**
 * 圈子中心
 */
class Topichub extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];


    //圈子广场，全部圈子
    public function lists(){
        $list = Db::name('topic_hub')->where('status',1)->order('weight desc,id desc')->autopage()->select();
        $list = list_domain_image($list,['image']);
        $this->success('success',$list);
    }

    //某个圈子详情
    public function info(){
        $id = input('id',0);
        $info = Db::name('topic_hub')->where(['status'=>1,'id'=>$id])->find();
        $info = info_domain_image($info,['image']);

        //圈子成员
        $user_list = Db::name('topic_user')->alias('tp')
            ->field('tp.user_id,user.avatar,user.nickname,user.gender,user.cityname,user.longitude,user.latitude')
            ->join('user','tp.user_id = user.id','LEFT')
            ->where('topic_id',$id)->autopage()->select();
        $user_list = list_domain_image($user_list,['avatar']);

        $info['userlist'] = $user_list;

        //我是否已加入
        $isjoin = Db::name('topic_user')->where(['topic_id'=>$id,'user_id'=>$this->auth->id])->find();
        $info['isjoin'] = $isjoin ? 1 : 0;
        $this->success('success',$info);
    }

    //加入某个圈子
    public function join_topic(){
        $id = input('topic_id',0);
        if(empty($id)){
            $this->error();
        }

        $check = Db::name('topic_user')->where(['user_id'=>$this->auth->id,'topic_id'=>$id])->find();
        if($check){
            $this->error('已经关注了此圈子');
        }

        $data = [
            'user_id' => $this->auth->id,
            'topic_id'=> $id,
        ];

        Db::startTrans();
        $new_id = Db::name('topic_user')->insertGetId($data);
        $rs = Db::name('topic_hub')->where('id',$id)->setInc('p_number');

        if($new_id && $rs){
            Db::commit();
            $this->success();
        }

        Db::rollback();
        $this->error('操作失败');
    }
    //退出某个圈子
    public function leave_topic(){
        $id = input('topic_id',0);
        if(empty($id)){
            $this->error();
        }

        $check = Db::name('topic_user')->where(['user_id'=>$this->auth->id,'topic_id'=>$id])->find();
        if(!$check){
            $this->error('没有关注此圈子');
        }


        Db::startTrans();
        $del = Db::name('topic_user')->where(['user_id'=>$this->auth->id,'topic_id'=>$id])->delete();
        $rs = Db::name('topic_hub')->where('id',$id)->setDec('p_number');

        if($del && $rs){
            Db::commit();
            $this->success();
        }

        Db::rollback();
        $this->error('操作失败');
    }

    //我的圈子列表
    public function my_lists(){
        $list = Db::name('topic_user')->alias('tu')
                ->field('th.*')
                ->join('topic_hub th','tu.topic_id = th.id','LEFT')
                ->where(['tu.user_id' => $this->auth->id,'th.status'=>1])->order('tu.id desc,th.weight desc,th.id desc')->autopage()->select();
        $list = list_domain_image($list,['image']);
        $this->success('success',$list);
    }

    //热门圈子。头部圈子N个，每个圈子拿出最热的一个（评论最多的一个）
    public function top_list(){
        $list = Db::name('topic_hub')->field('id,name')->where(['status'=>1])->order('weight desc,p_number desc,t_number desc')->autopage()->select();

        if(empty($list)){
            $this->success('success',$list);
        }

        foreach($list as $key => $val){
            $child = Db::name('topic_dongtai')->alias('dt')
                ->field('dt.id,dt.content,dt.images,user.nickname')
                ->join('user','dt.user_id = user.id','LEFT')
                ->where('dt.topic_id',$val['id'])
                ->order('goodnum desc')->find();
            $child = info_domain_image($child,['images']);

            if(!empty($child)){
                $child['images'] = explode(',',$child['images'])[0];
            }

            $list[$key]['child'] = $child;
        }

        $this->success('success',$list);
    }

    //某个圈子里的动态列表，全部，最新，最热
    //放到 topicdongtai.php里了

}
