<?php
class friendModel extends CI_Model
{
	public function getUserAllFriend($uid,$type = 'all'){
        $friends = oo::commonOprRedis('games')->get(okeys::userfriends($uid,$type));
        if(!empty($friends)){
            $friends = json_decode($friends,true);
        }else{
            $this->getUserFriends($uid,$temp);
            $friends = [];
            if($type == 'game' || $type == 'all'){
                $gameList = explode(',',$temp['game_list']);
                foreach ($gameList as $row){
                    $row != $uid && $friends[] = $row;
                }
            }
            if($type == 'fb' || $type == 'all'){
                $poker= explode(',',$temp['poker']);
                foreach ($poker as $row){
                    $row != $uid && $friends[] = $row;
                }
            }
            $friends = array_unique(array_filter($friends));
            sort($friends);
            oo::commonOprRedis('games')->setex(okeys::userfriends($uid,$type),json_encode($friends),7*24*60*60);
        }

        foreach ($friends as $key=>$row){
            if($row == $uid){
                unset($friends[$key]);
            }
        }

        return array_values($friends);
    }

    /**
     * 获取用户的好友列表
     * @param $uid
     * @param $friends
     * @return int 0,失败; =0,没有记录; >0,有记录
     * Created by: Owen
     * Created on: 2020/9/2 15:15
     */
    public function getUserFriends($uid, &$friends){
        if ($uid) {
            return -1;
        }
        $tb = otable::userfriends($uid);
        $query = "select uid, isapper, notapper, game_list,poker ,updatetime, watchme from {$tb} where uid='{$uid}' limit 1";
        $friends = oo::commonOprDb('slavedb')->getOne($query, MYSQLI_ASSOC);
        if ($friends == false) {
            $friends = array('uid' => $uid, 'isapper' => '', 'notapper' => '', 'game_list' => '', 'updatetime' => 0, 'watchme' => '');
        }
        return 1;
    }
}
