<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use app\common\library\Tenim;
/**
 * 好友
 */
class Friend extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    //好友列表
    public function friend_lists(){

        $ids = Db::name('friend')->where('user_id',$this->auth->id)->column('to_user_id');
        $ids2 = Db::name('friend')->where('to_user_id',$this->auth->id)->column('user_id');

        $ids3 = array_merge($ids,$ids2);
        $ids4 = array_unique($ids3);
        /*dump($ids);
        dump($ids2);
        dump($ids3);
        dump($ids4);*/

        $list = [];
        if(!empty($ids4)){
            $order = 'field(id,'.implode(',',$ids4).')';
            $list = Db::name('user')->field('id,nickname,avatar,mobile')->where('id','IN',$ids4)->orderRaw($order)->autopage()->select();
            $list = list_domain_image($list,['avatar']);
        }

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

    //没做防重复的好友列表，废弃
    public function aaa(){

        $field = '
            friend.id,friend.user_id,friend.to_user_id,
            u.avatar as u_avatar,u.nickname as u_nickname,u.mobile as u_mobile,
            tu.avatar as tu_avatar,tu.nickname as tu_nickname,tu.mobile as tu_mobile
            ';
        $list = Db::name('friend')->field($field)
            ->join('user u'  ,'friend.user_id    = u.id','LEFT')
            ->join('user tu' ,'friend.to_user_id = tu.id','LEFT')
            ->where('(friend.user_id = '.$this->auth->id.') or (friend.to_user_id = '.$this->auth->id.')')
            ->order('friend.id desc')->autopage()
            ->select();
        $result = [];

        if(!empty($list)){
            foreach($list as $key => $val){
                $newval = [];
                if($val['user_id'] == $this->auth->id){
                    $newval = [
                        'id'       => $val['to_user_id'],
                        'nickname' => $val['tu_nickname'],
                        'avatar'   => localpath_to_netpath($val['tu_avatar']),
                        'mobile'   => $val['tu_mobile'],
                    ];
                }else{
                    $newval = [
                        'id'       => $val['user_id'],
                        'nickname' => $val['u_nickname'],
                        'avatar'   => localpath_to_netpath($val['u_avatar']),
                        'mobile'   => $val['u_mobile'],
                    ];
                }
                $result[] = $newval;
            }
        }

        $this->success(1,$result);
    }

    //移除好友
    public function remove(){
        $user_id = input('user_id',0);
        if(empty($user_id)){$this->error();}

        $rs3 = Db::name('friend')->where('user_id',$user_id)->where('to_user_id',$this->auth->id)->delete();
        $rs4 = Db::name('friend')->where('user_id',$this->auth->id)->where('to_user_id',$user_id)->delete();

        //IM删除好友
        /*$tenim = new Tenim();
        $rs = $tenim->friend_del();*/

        $this->success('操作成功');
    }

    //申请列表
    public function apply_list(){
        $list = Db::name('friend_apply')->field('user.id,user.avatar,user.nickname,user.mobile')
            ->join('user','friend_apply.user_id = user.id','LEFT')
            ->where('friend_apply.to_user_id',$this->auth->id)->where('friend_apply.status',0)->autopage()->select();

        $list = list_domain_image($list,['avatar']);

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

    //同意与拒绝
    public function apply_audit(){
        $user_id = input('user_id',0);
        $status = input('status',0);
        if(empty($user_id) || empty($status)){
            $this->error();
        }

        if($status == 1){
            //同意
            Db::startTrans();
            $rs1 = Db::name('friend_apply')->where('user_id',$user_id)->where('to_user_id',$this->auth->id)->delete();
            $rs2 = Db::name('friend_apply')->where('user_id',$this->auth->id)->where('to_user_id',$user_id)->delete();
            $rs3 = Db::name('friend')->where('user_id',$user_id)->where('to_user_id',$this->auth->id)->delete();
            $rs4 = Db::name('friend')->where('user_id',$this->auth->id)->where('to_user_id',$user_id)->delete();

            $data = [
                'user_id'    => $user_id,
                'to_user_id' => $this->auth->id,
            ];
            $rs5 = Db::name('friend')->insertGetId($data);

            if($rs1 !== false && $rs2 !== false && $rs3 !== false && $rs4 !== false && $rs5 !== false){
                Db::commit();

                //IM添加好友
                /*$tenim = new Tenim();
                $rs = $tenim->friend_add();*/

                $this->success('操作成功');
            }else{
                Db::rollback();
                $this->error('操作失败');
            }
        }else{
            //拒绝
            Db::name('friend_apply')->where('user_id',$user_id)->where('to_user_id',$this->auth->id)->delete();
            $this->success();
        }

    }

    //搜索
    public function search(){
        $keyword = input('keyword',0);
        if(!$keyword){$this->error();}

        $list = Db::name('user')->field('id,nickname,mobile,avatar')->where('mobile',$keyword)->where('id','neq',$this->auth->id)->select();
        $list = list_domain_image($list,['avatar']);

        if(!empty($list)){
            foreach($list as $key => $val){
                //是否申请过了
                $val['is_apply'] = 0;
                $check_apply = Db::name('friend_apply')->where('user_id',$this->auth->id)->where('to_user_id',$val['id'])->find();
                if($check_apply){
                    $val['is_apply'] = 1;
                }

                //是否是好友
                $val['is_friend'] = 0;
                $check_friend1 = Db::name('friend')->where('user_id',$this->auth->id)->where('to_user_id',$val['id'])->find();
                $check_friend2 = Db::name('friend')->where('user_id',$val['id'])->where('to_user_id',$this->auth->id)->find();
                if($check_friend1 || $check_friend2){
                    $val['is_friend'] = 1;
                }

                //
                $list[$key] = $val;
            }
        }

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

    //申请好友
    public function apply(){
        $user_id = input('user_id',0);
        if(!$user_id){$this->error();}

        if($user_id == $this->auth->id){
            $this->error('不能添加自己为好友');
        }

        $check = Db::name('friend_apply')->where('user_id',$this->auth->id)->where('to_user_id',$user_id)->find();
        if(!$check){
            $data = [
                'user_id' => $this->auth->id,
                'to_user_id' => $user_id,
            ];

            $id = Db::name('friend_apply')->insertGetId($data);
        }

        $this->success();
    }

}
