<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
/**
 * 圈子动态
 */
class Topicdongtai extends Api
{
    protected $noNeedLogin = ['info','floor_info','topic_list'];
    protected $noNeedRight = ['*'];

    //发布动态
    public function addone(){
        $content = input('content','');
        $images = input('images','');
        $topic_id = input('topic_id','');
        if(!$content && !$images){
            $this->error(__('Invalid parameters'));
        }

        $data = [
            'topic_id' => $topic_id,
            'user_id' => $this->auth->id,
            'content' => $content,
            'images' => $images,
            'longitude' => input('longitude',''),
            'latitude'  => input('latitude',''),
            'createtime' => time(),
            'updatetime' => time(),
        ];


        Db::startTrans();
        $id = Db::name('topic_dongtai')->insertGetId($data);
        //圈子新增一个贴
        $rs = Db::name('topic_hub')->where('id',$topic_id)->setInc('t_number');

        //tag任务赠送金币
        //发动态奖励
        $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,16);
        if($task_rs === false){
            Db::rollback();
            $this->error('完成任务赠送奖励失败');
        }

        Db::commit();

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

    //自己看列表
    //某用户的帖子列表
    public function my_lists(){

        $uid = input('uid',$this->auth->id);
        $list = Db::name('topic_dongtai')->alias('dt')
            ->join('user','dt.user_id = user.id','LEFT')
            ->field('dt.*,user.nickname,user.avatar')
            ->where('dt.user_id',$uid)
            ->order('dt.id desc')->autopage()->select();
        $list = list_domain_image($list,['images','avatar']);

        if(!empty($list)){
            foreach($list as $key => $val){
                $list[$key]['isgood'] = $this->is_good($val['id'],$this->auth->id);
            }
        }

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

    //我回复的帖子列表
    public function answer_dt_lists(){
        $map = [
            'answer.user_id' => $this->auth->id,
            'answer.level' => 1,
        ];
        $lists = Db::name('topic_dongtai_answer')->alias('answer')
            ->field('answer.id,answer.content as answer_content,answer.createtime,user.nickname,user.avatar,dt.id as dt_id,dt.content as dt_content')
            ->join('user','answer.user_id = user.id','LEFT')
            ->join('topic_dongtai dt','answer.dt_id = dt.id','LEFT')
            ->where($map)->autopage()->select();

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

    //是否点赞
    private function is_good($dt_id,$uid){
        $where = [
            'dt_id' => $dt_id,
            'user_id'  => $uid,
        ];
        $check = Db::name('topic_dongtai_good')->where($where)->find();
        if($check){
            return 1;
        }else{
            return 0;
        }
    }

    //详情
    public function info(){
        $id = input('id');

        $info = Db::name('topic_dongtai')->alias('dt')
            ->join('user','dt.user_id = user.id','LEFT')
            ->field('dt.*,user.nickname,user.avatar')
            ->where('dt.id',$id)->find();
        $info = info_domain_image($info,['images','avatar']);

        //是否点赞过
        if($info){
            $info['isgood'] = $this->is_good($id,$this->auth->id);
        }

        //评论
        if($info){
            $info['answer'] = $this->answer_list($id);
        }

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

    //点赞
    public function good(){
        $id = input('id');

        $where = [
            'dt_id' => $id,
            'user_id'  => $this->auth->id,
        ];
        $check = Db::name('topic_dongtai_good')->where($where)->find();

        if($check){
            $this->error('已经赞过了');
        }

        Db::startTrans();
        $rs = Db::name('topic_dongtai_good')->insertGetId($where);
        $up = Db::name('topic_dongtai')->where('id',$id)->setInc('goodnum');

        //tag任务赠送金币
        //点赞奖励
        $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,14);
        if($task_rs === false){
            Db::rollback();
            $this->error('完成任务赠送奖励失败');
        }

        if($rs && $up !== false){
            Db::commit();
            $this->success('点赞成功');
        }
        Db::rollback();
        $this->error('点赞失败');
    }

    //评论
    public function answer(){
        $id = input('id',0);
        $content = input('content','');
        $to_user_id = input('to_user_id',0);
        $level = input('level',1);  //回复类型:1=层主回复楼主,2=层中回复
        $floor = input('floor',0);

        if(empty($content) || empty($id)){
            $this->error();
        }

        //判断
        if($level == 2 && $floor == 0){
            $this->error('楼层错误');
        }

        //回复楼主，最新楼层
        if($level == 1 || $floor == 0){
            $to_user_id = 0;
            $floor = 1;  //默认一楼

            $last_floor = Db::name('topic_dongtai_answer')->where(['dt_id'=>$id,'level'=>1])->order('floor desc')->value('floor');
            if($last_floor){
                $floor = $last_floor + 1;
            }
        }

        //判断user_id
        if($to_user_id){
            $to_user = Db::name('user')->where('id',$to_user_id)->value('id');
            if(empty($to_user)){
                $this->error('被回复的用户不存在');
            }
        }

        //data
        $data = [
            'dt_id' => $id,
            'floor' => $floor,
            'user_id' => $this->auth->id,
            'content' => $content,
            'to_user_id' => $to_user_id,
            'level' => $level,
            'createtime' => time(),
            'updatetime' => time(),
        ];

        Db::startTrans();
        $rs = Db::name('topic_dongtai_answer')->insertGetId($data);

        //tag任务赠送金币
        //评论奖励
        $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,13);
        if($task_rs === false){
            Db::rollback();
            $this->error('完成任务赠送奖励失败');
        }

        Db::commit();
        $this->success('评价成功');
    }

    //评论列表
    private function answer_list($dt_id){
        //楼
        $floor_list = Db::name('topic_dongtai_answer')
            ->alias('a')
            ->field('a.*,user.nickname,user.avatar')
            ->join('user','a.user_id = user.id','LEFT')
            ->where(['a.dt_id'=>$dt_id,'a.level'=>1])->order('id asc')->autopage()->select();
        $floor_list = list_domain_image($floor_list,['avatar']);
        if(empty($floor_list)){
            return [];
        }

        //层
        $floors = array_column($floor_list,'floor');
        $child_lists = Db::name('topic_dongtai_answer')->alias('a')
            ->field('a.*,user.nickname,user.avatar,tuser.nickname as to_nickname,tuser.avatar as to_avatar')
            ->join('user','a.user_id = user.id','LEFT')
            ->join('user tuser','a.to_user_id = tuser.id','LEFT')
            ->where(['a.dt_id'=>$dt_id,'a.floor'=>['IN',$floors],'a.level'=>2])->order('id asc')->select();
        $child_lists = list_domain_image($child_lists,['avatar','to_avatar']);
        /*if(empty($child_lists)){
            return $floor_list;
        }*/

        //合并
        foreach($floor_list as $key => $val){
            $child = [];
            foreach($child_lists as $k => $v){
                if($val['floor'] == $v['floor']){
                    $child[] = $v;
                }
            }
            //追加到外循环
            $floor_list[$key]['childcount'] = 0;

            if(count($child) > 4){
                $floor_list[$key]['childcount'] = count($child) - 4;
            }
            $floor_list[$key]['child'] = array_slice($child,0,4);
        }

        return $floor_list;
    }

    //单独某一层的详细
    public function floor_info(){
        $floor_id = input('floor_id');

        //楼
        $floor_info = Db::name('topic_dongtai_answer')
            ->alias('a')
            ->field('a.*,user.nickname,user.avatar')
            ->join('user','a.user_id = user.id','LEFT')
            ->where(['a.id'=>$floor_id])->find();
        $floor_info = info_domain_image($floor_info,['avatar']);

        //层
        $floors = $floor_info['floor'];
        $child_lists = Db::name('topic_dongtai_answer')->alias('a')
            ->field('a.*,user.nickname,user.avatar,tuser.nickname as to_nickname,tuser.avatar as to_avatar')
            ->join('user','a.user_id = user.id','LEFT')
            ->join('user tuser','a.to_user_id = tuser.id','LEFT')
            ->where(['a.floor'=>$floors,'a.level'=>2])->order('id asc')->autopage()->select();
        $child_lists = list_domain_image($child_lists,['avatar','to_avatar']);

        //合并
        $floor_info['child'] = $child_lists;

        $this->success('success',$floor_info);

    }

    //某个圈子里的动态列表，全部，最新，最热
    public function topic_list(){
        $topic_id = input('topic_id',0);
        $order = input('orderby','new');

        $orderby  = 'dt.id desc';
        if($order == 'hot'){
            $orderby = 'dt.goodnum desc';
        }

        $list = Db::name('topic_dongtai')->alias('dt')
            ->join('user','dt.user_id = user.id','LEFT')
            ->field('dt.*,user.nickname,user.avatar')
            ->where('dt.topic_id',$topic_id)
            ->order($orderby)->autopage()->select();
        $list = list_domain_image($list,['images','avatar']);

        //追加是否点赞
        if(!empty($list)){
            $ids = array_column($list,'id');
            $map = [
                'dt_id' => ['IN',$ids],
                'user_id'  => $this->auth->id,
            ];
            $good_list = Db::name('topic_dongtai_good')->where('dt_id','IN',$ids)->select();

            foreach($list as $key => $val){
                $list[$key]['isgood'] = 0;

                foreach($good_list as $k => $v){
                    if($val['id'] == $v['dt_id']){
                        $list[$key]['isgood'] = 1;
                    }
                }
            }
        }



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



}
