<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
/**
 * 关注
 */
class Userfollow extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];


    //关注某人，取关
    public function follow_one(){
        $follow_uid = input('doctor_id',0);

        if(!$follow_uid){
            $this->error(__('Invalid parameters'));
        }

        $checkuser = Db::name('doctor')->find($follow_uid);
        if(empty($checkuser)){
            $this->error('此医生不存在');
        }

        $map = [
            'uid' => $this->auth->id,
            'follow_uid' => $follow_uid,
        ];

        $check = Db::name('user_follow')->where($map)->find();
        if($check){
            //取关
            $rs = Db::name('user_follow')->where($map)->delete();
            $this->success('操作成功');
        }

        $id = Db::name('user_follow')->insertGetId($map);

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


    //我的关注uids
    public function my_follow_uids(){
        $list = Db::name('user_follow')->where('uid',$this->auth->id)->column('follow_uid');

        $list = array_flip($list);
        $list = array_flip($list);

        return $list;
    }
////////////////////////////////////////////////////////////

    //我的关注列表
    public function my_follow_list(){
        $user_id = input('user_id',0);
        if(empty($user_id)){
            $user_id = $this->auth->id;
        }

        //列表
        $list = Db::name('user_follow')
            ->alias('follow')
            ->join('user','follow.follow_uid = user.id','LEFT')
            ->field('user.id,user.username,user.nickname,user.avatar,user.birthday,user.gender')
            ->where('follow.uid',$user_id)->order('follow.id desc')->group('follow.follow_uid')->autopage()->select();

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

        //我的关注uids
        $my_follow_uids = $this->my_follow_uids();

        if(!empty($list)){
            foreach($list as $key => &$val){

                //是否关注
                $val['is_follow'] = in_array($val['id'],$my_follow_uids) ? 1 : 0;
            }
        }

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

    //我的粉丝列表
    public function my_fans_list(){
        $user_id = input('user_id',0);
        if(empty($user_id)){
            $user_id = $this->auth->id;
        }

        //列表
        $list = Db::name('user_follow')
            ->alias('follow')
            ->join('user','follow.uid = user.id','LEFT')
            ->field('user.id,user.username,user.nickname,user.avatar,user.birthday,user.gender')
            ->where('follow.follow_uid',$user_id)
            ->group('follow.uid')
            ->order('follow.id desc')->autopage()->select();

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

        //我的关注uids
        $my_follow_uids = $this->my_follow_uids();

        if(!empty($list)){
            foreach($list as $key => &$val){

                //是否关注
                $val['is_follow'] = in_array($val['id'],$my_follow_uids) ? 1 : 0;
            }
        }

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

    //我的好友,拿粉丝列表改的
    public function my_friend_list(){
        //我的关注uids
        $my_follow_uids = $this->my_follow_uids();
        //我的粉丝uids
        $my_fans_uids = $this->my_fans_uids();
        //合集就是朋友
        $friend_ids = array_intersect($my_follow_uids,$my_fans_uids);
        //dump($friend_ids);
        //好友列表
        $list = Db::name('user')->alias('user')
            ->field('user.id,user.nickname,user.avatar,a.requesttime')
            ->join('user_active a','user.id = a.user_id','LEFT')
            ->where('user.id','IN',$friend_ids)->autopage()->order('user.id asc')->select();
        if(empty($list)){
            $this->success(1,[]);
        }

        //dump($list);

        $list_ids = implode(',',array_column($list,'id'));
        //dump($list_ids);

        //亲密度列表
        $intimacy = Db::name('user_intimacy')->where('(uid = '.$this->auth->id.' and other_uid in ('.$list_ids.')) or (uid in ('.$list_ids.') and other_uid = '.$this->auth->id.')')->select();
        //dump($intimacy);

        foreach($list as $key => &$val){

            $val['intimacy'] = 0;
            $val['requesttime_text'] = get_last_time($val['requesttime']);

            foreach($intimacy as $k => $v){

                if($v['uid'] == $this->auth->id && $v['other_uid'] == $val['id']){
                    $val['intimacy'] = $v['value'];
                }
                if($v['uid'] == $val['id'] && $v['other_uid'] == $this->auth->id){
                    $val['intimacy'] = $v['value'];
                }
            }
        }

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

    }

    //我的粉丝uids
    private function my_fans_uids(){
        $list = Db::name('user_follow')->where('follow_uid',$this->auth->id)->column('uid');
        
        $list = array_flip($list);
        $list = array_flip($list);

        return $list;
    }

}
