<?php

namespace app\api\controller;

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



    //自己看列表
    //某用户的帖子列表
    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 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();
        }

        //关键字替换
        $content = Keyworld::sensitive($content);

        //判断
        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 adddongtai(){
        $content = input('content','', 'trim');
        $images = input('images','', 'trim');
        $show_real = input('show_real', 0, 'intval'); //是否标记真人:0=否,1=是
        //$address = input('address', '', 'trim'); //位置
        $topic_id = input('topic_id', 0, 'intval'); //热门话题id
        $type = input('type', 0, 'intval'); //类型:0=文字,1=图片,2=视频
        if(!$content && !$images){
            $this->error(__('Invalid parameters'));
        }
        if (!in_array($show_real, [0, 1])) {
            $this->error(__('Invalid parameters'));
        }
        if ($show_real == 1 && $this->auth->real_status != 1) { //验证是否已经通过真人认证
            $this->error('您尚未通过真人认证,暂不能标记真人');
        }
        /*if (iconv_strlen($address, 'utf-8') > 255) {
            $this->error('请选择正确位置');
        }*/
        if ($topic_id) {
            $topic_info = Db::name('topic_hub')->where(['id' => $topic_id])->find();
            if (!$topic_info) {
                $this->error('话题已过时,请重新选择');
            }
            if ($topic_info['status'] != 1) {
                $this->error('话题已过时,请重新选择');
            }
        }
        if (!in_array($type, [0, 1, 2])) {
            $this->error('您的网络开小差啦~');
        }

        //关键字替换
        $content = Keyworld::sensitive($content);

        $address = $this->ip_to_address();
        $address = $address['provincename'].$address['cityname'];

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


        Db::startTrans();
        $id = Db::name('topic_dongtai')->insertGetId($data);
        if (!$id) {
            Db::rollback();
            $this->error('您的网络开小差啦~');
        }
        //圈子新增一个贴
        if ($topic_id) {
            $rs = Db::name('topic_hub')->where('id', $topic_id)->setInc('t_number');
            if (!$rs) {
                Db::rollback();
                $this->error('您的网络开小差啦~');
            }
        }

        Db::commit();

        $this->success('发布成功',$id);
    }

    //动态列表
    public function dongtailist() {
        $type = input('type', 0, 'intval'); //类型:0热门 1最新
        $topic_id = input('topic_id', 0); //热门话题id

        if (!in_array($type, [0, 1])) {
            $this->error('您的网络开小差啦~');
        }
        if ($type == 0) {
//            $orderby = 'dt.goodnum desc';
            $orderby = 'dt.id desc';
        } else {
            $orderby = 'dt.id desc';
        }

        $where['dt.status'] = 0;
        $where['dt.auit_status'] = 1;
        $where['user.is_kefu'] = 0;
        if ($this->auth->gender == 1) {
            $where['user.gender'] = 0;
        } elseif ($this->auth->gender == 0) {
            $where['user.gender'] = 1;
        } else {
            $this->success('success',[]);
        }
        if ($topic_id) {
            $where['dt.topic_id'] = $topic_id;
            $orderby = 'dt.id desc';
        }

        $list = Db::name('topic_dongtai')->alias('dt')
            ->join('user','dt.user_id = user.id','LEFT')
            ->join('topic_hub th','dt.topic_id = th.id','LEFT')
            ->field('dt.*,user.nickname,user.avatar,user.gender,user.birthday,user.cityname,user.is_hideaddress,th.name,user.real_status')
            ->where($where)
            ->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($map)->select();
            $mt_user_greet = Db::name('user_greet'); //是否打过招呼
            $mt_gift_user_dongtai = Db::name('gift_user_dongtai');
            $mt_user_wallet = Db::name('user_wallet'); //钱包
            $mt_wealth_level = Db::name('wealth_level'); //财富等级
            $mt_charm_level = Db::name('charm_level'); //魅力等级

            foreach ($list as &$val) {
                $val['name'] = $val['name'] ? : '';
                $val['birthday'] = birthtime_to_age($val['birthday']);
                $val['createtime'] = get_last_time($val['createtime']);
                $val['cityname'] = $val['is_hideaddress'] ? '' : $val['address'] ;
                //是否点过赞:0否  1是
                $val['isgood'] = 0;
                foreach($good_list as $k => $v){
                    if($val['id'] == $v['dt_id']){
                        $val['isgood'] = 1;
                    }
                }
                //礼物数量
                $val['gift_count'] = $mt_gift_user_dongtai->where(['dt_id' => $val['id']])->count('id');
                //查询是否打过招呼
                $count = $mt_user_greet->where(['user_id' => $this->auth->id, 'user_to_id' => $val['user_id']])->count('id');
                if ($count) {
                    $val['is_chat'] = 1; //是否打过招呼: 1是  0否
                } else {
                    $val['is_chat'] = 0; //是否打过招呼: 1是  0否
                }
                //查询财富等级和魅力等级
                $wallet_info = $mt_user_wallet->where(['user_id' => $val['user_id']])->find();
                $wealth_level = $mt_wealth_level->where(['value' => ['elt', $wallet_info['pay_money']]])->order('id desc')->find();
                if ($wealth_level) {
                    $val['wealth_level'] = localpath_to_netpath($wealth_level['image']);
                } else {
                    $val['wealth_level'] = '';
                }
                $charm_level = $mt_charm_level->where(['value' => ['elt', $wallet_info['get_money']]])->order('id desc')->find();
                if ($charm_level) {
                    $val['charm_level'] = localpath_to_netpath($charm_level['image']);
                } else {
                    $val['charm_level'] = '';
                }

                //创建视频缩略图
                $val['images_thumb'] = '';
                if ($val['type'] == 2) {
                    $images_url = explode('.', $val['images']);
                    unset($images_url[count($images_url) - 1]);
                    $val['images_thumb'] = join('.', $images_url) . '_0.jpg';
                }
            }
        }

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

    //动态详情
    public function dongtaiinfo(){
        $id = input('id', 0, 'intval');
        if (!$id) {
            $this->error('您的网络开小差啦~');
        }

        $info = Db::name('topic_dongtai')->alias('dt')
            ->join('user','dt.user_id = user.id','LEFT')
            ->join('topic_hub th','dt.topic_id = th.id','LEFT')
            ->field('dt.*,user.nickname,user.avatar,user.gender,user.birthday,user.cityname,user.is_hideaddress,th.name,user.real_status')
            ->where('dt.id',$id)->find();
        if (!$info) {
            $this->error('您的网络开小差啦~');
        }
        if ($info['status'] != 0) {
            $this->error('您的网络开小差啦~');
        }

        $info = info_domain_image($info,['images','avatar']);
        $info['birthday'] = birthtime_to_age($info['birthday']);
        $info['createtime'] = get_last_time($info['createtime']);
        $info['cityname'] = $info['is_hideaddress'] ? '' : $info['address'];
        //是否点赞过
        $info['isgood'] = $this->is_good($id,$this->auth->id);
        //礼物数量
        $info['gift_count'] = Db::name('gift_user_dongtai')->where(['dt_id' => $info['id']])->count('id');
        //查询是否打过招呼
        $count = Db::name('user_greet')->where(['user_id' => $this->auth->id, 'user_to_id' => $info['user_id']])->count('id');
        if ($count) {
            $info['is_chat'] = 1; //是否打过招呼: 1是  0否
        } else {
            $info['is_chat'] = 0; //是否打过招呼: 1是  0否
        }
        //查询财富等级和魅力等级
        $wallet_info = Db::name('user_wallet')->where(['user_id' => $info['user_id']])->find();
        $wealth_level = Db::name('wealth_level')->where(['value' => ['elt', $wallet_info['pay_money']]])->order('id desc')->find();
        if ($wealth_level) {
            $info['wealth_level'] = localpath_to_netpath($wealth_level['image']);
        } else {
            $info['wealth_level'] = '';
        }
        $charm_level = Db::name('charm_level')->where(['value' => ['elt', $wallet_info['get_money']]])->order('id desc')->find();
        if ($charm_level) {
            $info['charm_level'] = localpath_to_netpath($charm_level['image']);
        } else {
            $info['charm_level'] = '';
        }
        //创建视频缩略图
        $info['images_thumb'] = '';
        if ($info['type'] == 2) {
            $images_url = explode('.', $info['images']);
            unset($images_url[count($images_url) - 1]);
            $info['images_thumb'] = join('.', $images_url) . '_0.jpg';
        }

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

    //点赞
    public function dongtaigood(){
        $id = input('id', 0, 'intval');
        if (!$id) {
            $this->error('您的网络开小差啦~');
        }
        $info = Db::name('topic_dongtai')->find($id);
        if (!$info) {
            $this->error('您的网络开小差啦~');
        }
        if ($info['status'] != 0) {
            $this->error('您的网络开小差啦~');
        }

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

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

        $where['createtime'] = time();
        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 dongtaigoodlist() {
        $id = input('id', 0, 'intval');
        if (!$id) {
            $this->error('您的网络开小差啦~');
        }
        //点赞只能查看异性
        $dongtaiWhere['td.id'] = $id;
        $dongtai = Db::name('topic_dongtai')->alias('td')->field('td.id,td.user_id,u.gender')
            ->join('user u','u.id = td.user_id','LEFT')
            ->where($dongtaiWhere)->find();
        $where['a.dt_id'] = $id;
        $gender = isset($dongtai['gender']) ? $dongtai['gender'] : 0;
        if ($gender == 1) {
            $where['user.gender'] = 0;
        } elseif ($gender == 0) {
            $where['user.gender'] = 1;
        }
        $list = Db::name('topic_dongtai_good')->alias('a')
            ->join('user', 'a.user_id = user.id', 'left')
            ->field('a.*, user.nickname,user.avatar,user.gender,user.birthday')
            ->where($where)
            ->order('a.id desc')
            ->autopage()->select();

        if (!$list) {
            $this->success('success', $list);
        }
        $list = list_domain_image($list,['avatar']);

        foreach ($list as &$v) {
            $v['birthday'] = birthtime_to_age($v['birthday']);
            $v['createtime'] = get_last_time($v['createtime']);
        }

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

    //动态赠送礼物
    public function givegiftdongtai() {
        // 接口防并发
        if (!$this->apiLimit(1, 1)) {
            $this->error(__('Operation frequently'));
        }

        $dt_id   = input('dt_id', 0, 'intval'); //动态id
        $gift_id = input('gift_id');// 礼物ID
        $number  = input('number',1,'intval');//数量

        if (!$dt_id || !$gift_id || $number < 1) {
            $this->error();
        }
        //查询动态
        $dongtai_info = Db::name('topic_dongtai')->find($dt_id);
        if (!$dongtai_info) {
            $this->error('您的网络开小差啦~');
        }
        if ($dongtai_info['status'] != 0) {
            $this->error('您的网络开小差啦~');
        }
        $user_id =  $dongtai_info['user_id'];

        // 不可以赠送给自己
        if($this->auth->id == $user_id) {
            $this->error("不可以赠送给自己");
        }

        // 获取礼物信息
        $giftinfo = Db::name('gift')->where('id',$gift_id)->find();
        if (!$giftinfo)
        {
            $this->error("请选择礼物");
        }
        $giftvalue = bcmul($giftinfo['value'],$number,2);

        //被赠送人信息
        $touserinfo = Db::name('user')->where('id',$user_id)->find();
        if (!$touserinfo) {
            $this->error("不存在的用户");
        }
        // 判断当前用户余额
        if($giftinfo['wallettype'] == 1){
            $user_gold = model('wallet')->getWallet($this->auth->id,'gold');
            if($user_gold < $giftvalue) {
                $this->error("您的金币不足");
            }
            $log_table = 'gift_user_dongtai';
        }else{
            $user_jewel = model('wallet')->getWallet($this->auth->id,'jewel');
            if($user_jewel < $giftvalue) {
                $this->error("您的钻石不足");
            }
            $log_table = 'gift_user_dongtai_jewel';
        }

        Db::startTrans();
        // 添加礼物赠送记录表
        $data = [
            'user_id' => $this->auth->id,
            'user_to_id' => $user_id,
            'dt_id' => $dt_id,
            'gift_id' => $giftinfo['id'],
            'gift_name' => $giftinfo['name'],
            'number' => $number,
            'price' => $giftvalue,
            'createtime' => time(),
        ];
        $log_id = Db::name($log_table)->insertGetId($data);
        if(!$log_id){
            Db::rollback();
            $this->error('赠送失败');
        }

        if($giftvalue > 0){
            // 扣除当前用户余额
            if($giftinfo['wallettype'] == 1){
                $wallet_rs = model('wallet')->lockChangeAccountRemain($this->auth->id,$user_id,'gold',-$giftvalue,59,'赠送礼物:'.$giftinfo["name"] . '*' . $number,'gift_user_dongtai',$log_id);
                if($wallet_rs['status'] === false){
                    Db::rollback();
                    $this->error($wallet_rs['msg']);
                }

                // 添加赠送用户余额
                $money_to_gold = config('site.money_to_gold');//送金币礼物得积分
                $gift_plat_scale = config('site.gift_plat_scale');

                $giftmoney = bcdiv($giftvalue,$money_to_gold,2);

                $money = bcdiv(bcmul($giftmoney,100 - $gift_plat_scale,2),100,2);
                $wallet_rs = model('wallet')->lockChangeAccountRemain($user_id,$this->auth->id,'money',$money,60,'获得礼物:'.$giftinfo["name"] . '*' . $number,'gift_user_dongtai',$log_id,2);
                if($wallet_rs['status'] === false){
                    Db::rollback();
                    $this->error($wallet_rs['msg']);
                }


                //增加赠送用户上级余额
                if ($touserinfo['intro_uid']) {
                    //获取返利比率
                    $is_agent = Db::name('user')->where(['id' => $touserinfo['intro_uid']])->value('is_agent');
                    $intro_income_rebate_rate = $is_agent ? (int)config('site.h_intro_income_rebate_rate') : (int)config('site.intro_income_rebate_rate'); //邀请人收礼物返利比率
                    if ($intro_income_rebate_rate > 0 && $intro_income_rebate_rate <= 100) {
                        //上级获得金额
                        $intro_uid_money = number_format($money * $intro_income_rebate_rate / 100, 2, '.', '');
                        if ($intro_uid_money > 0) {
                            $intro_result = model('Wallet')->lockChangeAccountRemain($touserinfo['intro_uid'],$user_id,'money',$intro_uid_money,68, '邀请人动态礼物获赠奖励','gift_user_dongtai',$log_id);
                            if($intro_result['status']===false)
                            {
                                Db::rollback();
                                $this->error($intro_result['msg']);
                            }
                        }
                    }
                }

                if ($this->auth->gender == 1 && $touserinfo['gender'] == 0) {
                //增加亲密度
                $user_intimacy_rs = addintimacy($this->auth->id, $user_id, $giftvalue);
                if (!$user_intimacy_rs['status']) {
                    Db::rollback();
                    $this->error('您的网络开小差啦~');
                }
            }
            }else{
                $wallet_rs = model('wallet')->lockChangeAccountRemain($this->auth->id,$user_id,'jewel',-$giftvalue,39,'赠送礼物:'.$giftinfo["name"] . '*' . $number,'gift_user_dongtai',$log_id);
                if($wallet_rs['status'] === false){
                    Db::rollback();
                    $this->error($wallet_rs['msg']);
                }
            }
        }



        Db::commit();

        //发送消息
        if (isset($user_intimacy_rs) && $user_intimacy_rs['level_remark']) {
            $tenim = new \app\api\controller\Tenim;
            $tenim->sendMessageToUser($this->auth->id, $user_id, $user_intimacy_rs['level_remark'], 1);
        }

        $return_data['money'] = $money; //获得金额
        $return_data['level_remark'] = isset($user_intimacy_rs) ? $user_intimacy_rs['level_remark'] : ''; //亲密度等级提示语
        $this->success('赠送成功', $return_data);
    }

    //动态收到礼物列表
    public function dongtaigiftlist() {
        $id = input('id', 0, 'intval');
        if (!$id) {
            $this->error('您的网络开小差啦~');
        }
        //点赞只能查看异性
        $dongtaiWhere['td.id'] = $id;
        $dongtai = Db::name('topic_dongtai')->alias('td')->field('td.id,td.user_id,u.gender')
            ->join('user u','u.id = td.user_id','LEFT')
            ->where($dongtaiWhere)->find();
        $where['a.dt_id'] = $id;
        $gender = isset($dongtai['gender']) ? $dongtai['gender'] : 0;
        if ($gender == 1) {
            $where['user.gender'] = 0;
        } elseif ($gender == 0) {
            $where['user.gender'] = 1;
        }
        $list = Db::name('gift_user_dongtai')->alias('a')->field('a.user_id, count(a.id) count')
            ->join('user', 'a.user_id = user.id', 'left')
            ->where($where)->group('a.user_id')->order('a.id desc')->autopage()->select();
        if (!$list) {
            $this->success('success', $list);
        }

        $mt_user = Db::name('user');
        foreach ($list as &$v) {
            $user_info = $mt_user->field('nickname, avatar, gender, birthday')->where(['id' => $v['user_id']])->find();
            $v['nickname'] = $user_info['nickname'];
            $v['avatar'] = one_domain_image($user_info['avatar']);
            $v['birthday'] = birthtime_to_age($user_info['birthday']);
        }

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

    //用户动态列表
    public function mydongtailist() {
        $user_id = input('user_id', $this->auth->id);
        if (!$user_id) {
            $this->error('您的网络开小差啦~');
        }
        $where['dt.user_id'] = $user_id;
        $where['dt.status'] = 0;
        $where['dt.auit_status'] = 1;
        $orderby = 'dt.id desc';

        $list = Db::name('topic_dongtai')->alias('dt')
            ->join('user','dt.user_id = user.id','LEFT')
            ->join('topic_hub th','dt.topic_id = th.id','LEFT')
            ->field('dt.*,user.nickname,user.avatar,user.gender,user.birthday,user.cityname,user.is_hideaddress,th.name,user.real_status')
            ->where($where)
            ->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($map)->select();
            $mt_user_greet = Db::name('user_greet'); //是否打过招呼
            $mt_gift_user_dongtai = Db::name('gift_user_dongtai');
            $mt_user_wallet = Db::name('user_wallet'); //钱包
            $mt_wealth_level = Db::name('wealth_level'); //财富等级
            $mt_charm_level = Db::name('charm_level'); //魅力等级

            foreach ($list as &$val) {
                $val['birthday'] = birthtime_to_age($val['birthday']);
                $val['createtime'] = get_last_time($val['createtime']);
                $val['cityname'] = $val['is_hideaddress'] ? '' : $val['address'];
                //是否点过赞:0否  1是
                $val['isgood'] = 0;
                foreach($good_list as $k => $v){
                    if($val['id'] == $v['dt_id']){
                        $val['isgood'] = 1;
                    }
                }
                //礼物数量
                $val['gift_count'] = $mt_gift_user_dongtai->where(['dt_id' => $val['id']])->count('id');
                //查询是否打过招呼
                $count = $mt_user_greet->where(['user_id' => $this->auth->id, 'user_to_id' => $val['user_id']])->count('id');
                if ($count) {
                    $val['is_chat'] = 1; //是否打过招呼: 1是  0否
                } else {
                    $val['is_chat'] = 0; //是否打过招呼: 1是  0否
                }
                //查询财富等级和魅力等级
                $wallet_info = $mt_user_wallet->where(['user_id' => $val['user_id']])->find();
                $wealth_level = $mt_wealth_level->where(['value' => ['elt', $wallet_info['pay_money']]])->order('id desc')->find();
                if ($wealth_level) {
                    $val['wealth_level'] = localpath_to_netpath($wealth_level['image']);
                } else {
                    $val['wealth_level'] = '';
                }
                $charm_level = $mt_charm_level->where(['value' => ['elt', $wallet_info['get_money']]])->order('id desc')->find();
                if ($charm_level) {
                    $val['charm_level'] = localpath_to_netpath($charm_level['image']);
                } else {
                    $val['charm_level'] = '';
                }

                //创建视频缩略图
                $val['images_thumb'] = '';
                if ($val['type'] == 2) {
                    $images_url = explode('.', $val['images']);
                    unset($images_url[count($images_url) - 1]);
                    $val['images_thumb'] = join('.', $images_url) . '_0.jpg';
                }
            }
        }

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

    //删除动态
    public function deldongtai() {
        $id = input('id', 0, 'intval');
        if (!$id) {
            $this->error('您的网络开小差啦~');
        }
        $info = Db::name('topic_dongtai')->find($id);
        if (!$info) {
            $this->error('您的网络开小差啦~');
        }
        if ($info['user_id'] != $this->auth->id) {
            $this->error('您的网络开小差啦~');
        }

        $rs = Db::name('topic_dongtai')->where(['id' => $id])->setField('status', 1);
        if (!$rs) {
            $this->error('您的网络开小差啦~');
        }

        $this->success('删除成功');
    }
}