<?php
class memberModel extends CI_Model
{
    public static $baseInfo  = [];
    public static $loginData = [];
    public function __construct(){
        parent::__construct();
        $this->load->model('okeys');
        if (file_exists(APPPATH.'config/reason.php')) {
            $this->reason = include APPPATH.'config/reason.php';
        }
    }

    /**
     * 判断是否为游客
     * @param unknown $uid
     * @return number 1游客 0FB
     */
    public function isVistor( $uid ) {
        $userInfo = $this->getUserInfo($uid);
        if ($userInfo == 0) {
            return 1;
        }

        $siteUid = $userInfo['siteuid'];
        $account = $this->getAccount($siteUid);
        if ( $account <= 0 ) {
            return 1;
        }
        return 0;
    }

    /**
     * 获取用户基础信息
     * @param int $uid
     * @param string $array 需要获取用户信息的字段
     * @param string $flag true表示需要更新最后登录时间和用户VIP标志位，还有用户的昵称、性别、国家等信息
     * @param string $info 用户的FB信息
     * @return array
     */
    public function getUserInfo( $uid, $array = NULL, $flag = false, $info = NULL){
        if (!$uid = intval($uid)) {
            return 0;
        }
        //$userinfo = oo::commonOprRedis("games")->get(okeys::t_userinfo($uid));
        $userinfo = oo::commonOprRedis("games")->hGetAll(okeys::t_userinfoNew($uid));
        //从缓存中未找到数据则从数据库中读取
        if(empty($userinfo)){
            $sql = "SELECT * FROM ".otable::userinfo($uid)." WHERE uid='{$uid}'";
            $userinfo = oo::commonOprDb('slavedb')->getOne($sql,MYSQLI_ASSOC);
            if(empty($userinfo)){
                return 0;
            }
            $infoArr = oo::commonOprModel('member')->getAccount($userinfo['siteuid']);
            if (!empty($infoArr) && $infoArr['uid'] == $uid) {
                $userinfo['isFb'] = 1;
            } else {
                $userinfo['isFb'] = 0;
            }
            if (isset($userinfo['nick'])){
                $nickJson = json_encode($userinfo['nick']);
                if(empty($nickJson)){
                    $userinfo['nick'] = oo::functions()->phutil_utf8ize( $userinfo['nick']);
                }
                $userinfo['nick'] = oo::functions()->escapeNick($userinfo['nick']);
            }
            //设置进入新缓存
            oo::commonOprRedis('games')->hMset(okeys::t_userinfoNew($uid), $userinfo);
            oo::commonOprRedis('games')->expire(okeys::t_userinfoNew($uid), oo::redisRandomExpire(7*24*60*60));

        }else{
            if(!is_array($userinfo)){
                $userinfo =  json_decode($userinfo,true);
            }
        }

        if ($flag && !empty($userinfo['status'])){
            $now = time();
            if (empty($info)){
                $insert_sql = "lasttime='{$now}',";
            }else{
                if(isset($info['snick']) && empty($info['snick'])){
                    $info['snick'] = $uid;
                }
                $insert_sql = "lasttime='{$now}',";
                foreach($info as $k=>$v){
                    if($k[0] == 's'){
                        $v = oo::functions()->escape($v);
                    }
                    $insert_sql = $insert_sql.substr($k,1)."='{$v}',";
                }
            }
            $ip = oo::functions()->getIp();
            if(!empty($ip)){
                $insert_sql .= "locale='{$ip}'";
            }
            $u_sql = "UPDATE ".otable::userinfo($uid)." SET ".trim($insert_sql,',')." WHERE uid='{$uid}' LIMIT 1";
            oo::commonOprDb('games')->query($u_sql);
            oo::commonOprRedis("games")->delete(okeys::t_userinfo($uid));
            oo::commonOprRedis("games")->delete(okeys::t_userinfoNew($uid));
        }
        return $userinfo;
    }
    public function updateUserInfo( $uid, $array = NULL ){
        if(empty($array)) {
            return 0;
        }
        $userInfo  = self::getUserInfo($uid);
        if(empty($userInfo)){
            return 0;
        }
        foreach($array as $k=>$v){
            $userInfo[$k] = $v;
        }
        //全量字段更新
        oo::commonOprRedis('games')->hMset(okeys::t_userinfoNew($uid), $userInfo);
        oo::commonOprRedis('games')->expire(okeys::t_userinfoNew($uid), oo::redisRandomExpire(7*24*60*60));
        $updateSql = '';
        foreach($array as $k=>$v){
            $updateSql .= $k.'='."'{$v}',";
        }
        $updateSql = trim($updateSql, ',');
        $sql = "UPDATE ".otable::userinfo($uid)." SET {$updateSql} WHERE uid={$uid} LIMIT 1";
        $res = oo::commonOprDb('games')->query($sql);

        return $res === true ? 1 : -1;
    }
    /**
     * 根据siteuid返回uid
     * @param string $siteuid 平台uid，例如facebook、新浪平台的用户id
     * @return mixed|number
     */
    public function getAccount($siteuid){
        $siteuid = (int)$siteuid;
        if (!$siteuid) return 0;
        $cache = oo::commonOprRedis("games")->get(okeys::account($siteuid));
        if ($cache && isset($cache['uid'])) {
            return $cache;
        }
        $tb = otable::taccount($siteuid);
        $sql = "select siteuid, uid, createtime from {$tb} where siteuid='{$siteuid}' limit 1";
        $row = oo::commonOprDb('slavedb')->getOne($sql,1);
        if ($row) {
            $account = array('uid'=>(int)$row['uid'],'createtime'=>(int)$row['createtime']);
            oo::commonOprRedis("games")->setex(okeys::account($siteuid), $account,7*24*60*60);
            return $account;
        } else {
            return 0;
        }
    }
    public function getSiteuidByUid($uid){
        $tb = otable::taccount(0);
        $sql = "select * from {$tb} where uid='{$uid}' limit 1";
        $row = oo::commonOprDb('slavedb')->getOne($sql,1);
        if($row){
            return $row['siteuid'];
        }
        return 0;
    }
    /**
     * 获取玩家当前岛的建筑情况
     * @param $uid
     * @return array|bool
     * Created by: Owen
     * Created on: 2020/6/9 15:11
     */
    public function getUserLevel($uid) {
        if (empty($uid)) {
            return false;
        }
        $key = okeys::UserBuildLevel($uid);
        $cache = oo::commonOprRedis('games')->hGetAll($key);
        if(!$cache['levelId']){
            $tb = otable::userlevel($uid);
            $sql0 = "SELECT max(levelId) as levelId FROM {$tb} WHERE uid={$uid}";
            $lv = oo::commonOprDb('slavedb')->getOne($sql0,1);
            $sql = "SELECT * FROM {$tb} WHERE levelId={$lv['levelId']} AND uid={$uid} limit 1";
            $cache = oo::commonOprDb('slavedb')->getOne($sql,1);
            oo::commonOprRedis('games')->delete($key);
            if(!empty($cache)&&is_array($cache)){
                $temp = [
                    'levelId'   => $cache['levelId'],
                    'element'   => $cache['element'],
                    'is_reward' => $cache['is_reward'],
                ];
                oo::commonOprRedis('games')->hMset($key,$temp);
                oo::commonOprRedis('games')->expire($key,86400);
            }
        }
        $cache = !empty($cache)?funs::getArrayFromOne($cache):[];
        return $cache;
    }

    /**
     * 首冲
     * @param $uid
     * @return mixed
     * Created by: Owen
     * Created on: 2019/7/10 17:42
     * Description:
     */
    public function firstPay($uid){
        $firstPay = oo::commonOprRedis('games')->get(okeys::firstPay($uid));
        if($firstPay !== false){
            return $firstPay;
        }
        $tb = otable::payment($uid);
        $sql = "SELECT * FROM {$tb} WHERE uid={$uid} AND status=2 AND gid= 601 LIMIT 1";
        $ret =  oo::commonOprDb('slavedb')->getOne($sql,MYSQLI_ASSOC);
        empty($ret) ? oo::commonOprRedis('games')->setex(okeys::firstPay($uid),0) : oo::commonOprRedis('games')->setex(okeys::firstPay($uid),1);
        return empty($ret) ? 0 : 1;
    }

    public function getUserStatistics($uid){
        $key =  okeys::userBasicsStatistics($uid);
        $res =oo::commonOprRedis('games')->hGetAll($key);
        return $res;
    }

    /**
     * 获取用户的好友列表
     * @param $uid
     * @return array
     * Created by: Owen
     * Created on: 2020/7/23 16:16
     */
    public function getUserFriends($uid){
        $tb    = otable::userfriends($uid);
        $query = "select uid, isapper, notapper, game_list,poker ,updatetime, watchme from {$tb} where uid='{$uid}' limit 1";
        $ret   = oo::commonOprDb('slavedb')->getOne($query, MYSQLI_ASSOC);
        $gameList = explode(',',$ret['game_list']);
        $friends  = [];
        foreach ($gameList as $row){
            $friends[] = $row;
        }
        $poker= explode(',',$ret['poker']);
        foreach ($poker as $row){
            $friends[] = $row;
        }
        $friends = array_unique(array_filter($friends));
        sort($friends);
        return $friends;
    }

    /**
     * 获取用户动态数据
     * @param $uid
     * @return array|false|mixed
     * Created by: Owen
     * Created on: 2020/7/23 14:37
     */
    public function getUserDynamicsData($uid){
        $cacheKey = okeys::UserDynamicsData($uid);
        $cache    = oo::commonOprRedis('games')->get($cacheKey);
        if(empty($cache)){
            $tb   = otable::UserDynamicsData();
            $cache= oo::commonOprDb('slavedb')->getOne("SELECT * FROM {$tb} WHERE uid={$uid} LIMIT 1",MYSQLI_ASSOC);
            if(empty($cache)){
                oo::commonOprDb('games')->query("INSERT INTO {$tb} (`uid`) VALUES ({$uid})");
                $cache= oo::commonOprDb('slavedb')->getOne("SELECT * FROM {$tb} WHERE uid={$uid} LIMIT 1",MYSQLI_ASSOC);
            }
        }else{
            $cache = json_decode($cache,true);
        }
        unset($cache['userCache']);
        return $cache;
    }

    /**
     * 更新用户动态数据
     * @param $uid
     * @param $updateInfo
     * @return mixed
     * Created by: Owen
     * Created on: 2020/7/23 14:38
     */
    public function updateUserDynamicsData($uid,$updateInfo){
        if(empty($updateInfo)){
            return false;
        }
        $userDynamicsData = self::getUserDynamicsData($uid);
        foreach ($updateInfo as $key=>$row){
            $userDynamicsData[$key] = $row;
        }
        return self::setUserDynamicsData($uid,$userDynamicsData);
    }

    /**
     * 追加用户动态数据
     * @param $uid
     * @param $updateInfo
     * @return bool
     * Created by: Owen
     * Created on: 2020/7/23 14:43
     */
    public function addUserDynamicsData($uid,$updateInfo){
        if(empty($updateInfo)){
            return false;
        }
        $userDynamicsData = self::getUserDynamicsData($uid);
        foreach ($updateInfo as $key=>$row){
            $userDynamicsData[$key] += $row;
        }
        return self::setUserDynamicsData($uid,$userDynamicsData);
    }

    /**
     * 设定用户动态数据
     * @param $uid
     * @param $dynamicsData
     * @return bool
     * Created by: Owen
     * Created on: 2020/7/23 14:54
     */
    public function setUserDynamicsData($uid,$dynamicsData){
        $dynamicsData['isLanding'] = 1;
        $cacheKey = okeys::UserDynamicsData($uid);
        oo::commonOprRedis('games')->setex($cacheKey,json_encode($dynamicsData),7*24*60*60);
        $ret = oo::commonOprRedis('games')->zAdd(okeys::Landing("DYNAMICS",$uid),time(),$uid);
        if($ret !== false){
            return true;
        }
        return false;
    }

    /**
     * 获取用户国家信息
     * @param $uid
     * @param bool $db
     * @return array
     * Created by: Owen
     * Created on: 2020/8/7 17:57
     */
    public function getUserCountry($uid,$db=false){
        $cache =[];
        $key = okeys::userCountryInfo($uid);
        if(!$db){
            $cache =  oo::commonOprRedis('games')->hGetAll($key);
        }
        if(empty($cache)){
            $tb = otable::useraddress();
            $sql = "SELECT * FROM {$tb} where uid={$uid}";
            $info = oo::commonOprDb('slavedb')->getOne($sql,1);
            $info['showCode'] = !empty($info['country_code'])?$info['country_code']:'OTHER';
            $cache = $info;
        }
        return $cache;
    }

    public function getUserAdjustGroup($uid){
        $cacheKey = okeys::UserAdjustGroup($uid);
        $cache    = oo::commonOprRedis('games')->get($cacheKey);
        if(empty($cache)){
            $tb  = otable::AdJustSta($uid);
            $ret = oo::commonOprDb('slavedb')->getOne("SELECT * FROM {$tb} WHERE uid={$uid} LIMIT 1",MYSQLI_ASSOC);
            $tb  = otable::AdJustData();
            if(empty($ret['ios']) && empty($ret['android'])){
                $group = "Not";
            }else if(!empty($ret['android']) || !empty($ret['ios'])) {
                $agps = trim($ret['android'], '"');
                if(!empty($agps)){
                    $where = "gps_adid='{$agps}'";
                }
                $igps = trim($ret['ios'], '"');
                if(!empty($igps)){
                    $where = "idfa='{$igps}'";
                }
                $ret = oo::commonOprDb('slavedb')->getOne("SELECT * FROM {$tb} WHERE {$where} LIMIT 1", MYSQLI_ASSOC);
                if(!empty($ret)){
                    if(strpos($ret['campaign_name'],'UAC2.0') !== false){
                        $group = 'UAC2.0';
                    }else if(strpos($ret['campaign_name'],'UAC2.5') !== false){
                        $group = 'UAC2.5';
                    }else if(strpos($ret['campaign_name'],'AEO') !== false){
                        $group = 'AEO';
                    }else if(strpos($ret['campaign_name'],'VO') !== false){
                        $group = 'VO';
                    }else if(strpos($ret['campaign_name'],'Recall') !== false){
                        $group = 'Recall';
                    }else{
                        $group = 'Not';
                    }
                }else{
                    $group = "Not";
                }
            }else{
                $group = "Not";
            }
            oo::commonOprRedis('games')->setex($cacheKey,$group,3*24*60*60);
        }else{
            $group = $cache;
        }
        return $group;
    }

    public function updateUserGroup(){
        $lSize     = oo::commonOprRedis('games')->lSize(okeys::UserGroupAction());
        $condition = array_reverse(oo::commonOprModel('config')->getUserGroupConditionConfig());
        for($i=0;$i<$lSize;$i++) {
            $uid = oo::commonOprRedis('games')->lPop(okeys::UserGroupAction());
            $userInfo = self::getUserInfo($uid);
            $userDynamicsData = self::getUserDynamicsData($uid);
            $id = 1;
            foreach ($condition as $row) {
                if ($row['platform'] != 0) {// 平台
                    if ($row['platform'] != $userInfo['login_plat']) {
                        continue;
                    }
                }
                if ($row['userType'] != 0) {// 用户类型
                    if (($row['userType'] == 1 && $userInfo['isFb'] != 1) || ($row['userType'] == 2 && $userInfo['isFb'] != 0)) {
                        continue;
                    }
                }
                if ($row['vip'] != 0) {//vip
                    if (($row['vip'] == 1 && $userInfo['vip'] != 0) || ($row['vip'] == 2 && $userInfo['vip'] != 1)) {
                        continue;
                    }
                }
                if ($row['point'] != 0) {//积分
                    $betweenPoint = explode(',', $row['point']);
                    $point = $userInfo['vipPoint'];
                    if ($betweenPoint[0] > $point || $point > $betweenPoint[1]) {
                        continue;
                    }
                }
                if ($row['levelId'] != 0) {
                    $betweenLevel = explode(',', $row['levelId']);
                    $levelId = self::getUserLevel($uid)['levelId'] ?? 1;
                    if ($betweenLevel[0] > $levelId || $levelId > $betweenLevel[1]) {
                        continue;
                    }
                }
                if ($row['firstPay'] != 0) {
                    $isFirstPay = self::firstPay($uid);
                    if (($row['firstPay'] == 1 && $isFirstPay != 0) || ($row['firstPay'] == 2 && $isFirstPay != 1)) {
                        continue;
                    }
                }
                if($row['active'] != 0){
                    $activeDay = $userDynamicsData['activeDay'] ?? 1;
                    $betweenActive = explode(',',$row['active']);
                    if($betweenActive[0] > $activeDay || $activeDay > $betweenActive[1]){
                        break;
                    }
                }
                if ($row['onlineTime'] != 0) {
                    $loginTime = intval(self::getUserStatistics($uid)['loginTime'] / 60) ?? 1;
                    $between = explode(',', $row['onlineTime']);
                    if ($between[0] > $loginTime || $loginTime > $between[1]) {
                        continue;
                    }
                }
                if ($row['regTime'] != 0) {
                    $loginTime = intval($userInfo['createtime'] / 84600) ?? 1;
                    $between = explode(',', $row['regTime']);
                    if ($between[0] > $loginTime || $loginTime > $between[1]) {
                        continue;
                    }
                }
                if ($row['friendNum'] != 0) {
                    $friends = count(self::getUserFriends($uid));
                    $between = explode(',', $row['friendNum']);
                    if ($between[0] > $friends || $friends > $between[1]) {
                        continue;
                    }
                }

                $userRankData = json_decode(oo::commonOprRedis('games')->get(okeys::UserRankData($uid)), true);

                if (empty($userRankData)) {
                    continue;
                }

                if ($row['friendRank'] != 0) {
                    $pos = $userRankData['friend'];
                    $between = explode(',', $row['friendRank']);
                    if ($between[0] > $pos || $pos > $between[1]) {
                        continue;
                    }
                }
                if ($row['countryRank'] != 0) {
                    $pos = $userRankData['country'];
                    $between = explode(',', $row['countryRank']);
                    if ($between[0] > $pos || $pos > $between[1]) {
                        continue;
                    }
                }
                if ($row['worldRank'] != 0) {
                    $pos = $userRankData['world'];
                    $between = explode(',', $row['worldRank']);
                    if ($between[0] > $pos || $pos > $between[1]) {
                        continue;
                    }
                }

                if ($row['countrySet'] != 0) {
                    $country = self::getUserCountry($uid);
                    $country = $country['country_code'];
                    $countrySet = json_decode($row['countrySet'],true);
                    if (!in_array($country,$countrySet)) {
                        continue;
                    }
                }

                if ($row['adjustGroup'] != 0) {
                    $userAdjustGroup = self::getUserAdjustGroup($uid);
                    $adjustGroup = json_decode($row['adjustGroup'],true);
                    if (!in_array($userAdjustGroup,$adjustGroup)) {
                        continue;
                    }
                }

                $id = $row['id'];
                break;
            }
            if ($userDynamicsData['groupId'] != $id) {
                self::updateUserDynamicsData($uid,['groupId'=>$id]);
            }
        }
    }

    /**
     * 是否是推金币进来的用户
     * @param $uid
     * @return false|int
     * Created by: Owen
     * Created on: 2020/11/25 14:50
     */
    public function isPushCoinsUser($uid){
        return 0;
        $cacheKey = okeys::isPushCoinsUser($uid);
        $cache    = oo::commonOprRedis('games')->get($cacheKey);
        if($cache === false){
            $isPush = 0;
            $tb  = otable::AdJustSta($uid);
            $ret = oo::commonOprDb('slavedb')->getOne("SELECT * FROM {$tb} WHERE uid={$uid} LIMIT 1",MYSQLI_ASSOC);
            $tb  = otable::AdJustData();
            if(!empty($ret['android']) || !empty($ret['ios'])) {
                $agps = trim($ret['android'], '"');
                if(!empty($agps)){$where = "gps_adid='{$agps}'";}
                $igps = trim($ret['ios'], '"');
                if(!empty($igps)){$where = "idfa='{$igps}'";}
                $ret = oo::commonOprDb('slavedb')->getOne("SELECT * FROM {$tb} WHERE {$where} LIMIT 1", MYSQLI_ASSOC);
                if(!empty($ret) && strpos($ret['campaign_name'],'PUSH') !== false){
                    $isPush = 1;
                }
            }
            oo::commonOprRedis('games')->setex($cacheKey,$isPush,3*24*60*60);
        }else{
            $isPush = $cache;
        }
        return intval($isPush);
    }

    public function updateUserGroup2($actionUid = 0){
        if($actionUid){
            $lSize = 1;
            $log   = true;
            $nameArr = [];
        }else{
            $lSize = oo::commonOprRedis('games')->lSize(okeys::UserGroupAction(2));
            $log   = false;
            $nameArr = [];
        }
        $userGroupConfig = oo::commonOprModel('config')->getUserGroupConfig2();
        array_multisort(array_column($userGroupConfig,'sort'),SORT_DESC,$userGroupConfig);
        for($i=0;$i<$lSize;$i++) {
            $actionGroupId     = 0;
            $actionGroupName   = "";
            $actionGroupIdSort = 0;
            self::$baseInfo    = [];
            if(!$actionUid){
                $uid = oo::commonOprRedis('games')->lPop(okeys::UserGroupAction(2));
            }else{
                $uid = $actionUid;
            }
            if(!$uid||$uid==1){
                continue;
            }
            //*********临时代码*********star
            oo::commonOprModel('ssdata')->updateSGroup($uid);//数数分组
            continue;
            //*********临时代码*********end

            $userDynamicsData = self::getUserDynamicsData($uid);
            self::$baseInfo["userDynamics"] = $userDynamicsData;
            $curGroupId = explode(',',$userDynamicsData['groupId2']);
            foreach ($curGroupId as $k=>$g){
                oo::commonOprRedis('games')->hIncrBy(okeys::UserGroupTotal(),$g,-1);
                unset($curGroupId[$k]);
            }
            $idArr      = [];
            $rfmUnique  = false;
            foreach ($userGroupConfig as $config){
                if($config['update'] == 1){
                    continue;
                }
                if($config['cid'] == 2 && $rfmUnique){
                    if($log){
                        echo "<br/>------已加入RFM分组--跳过：".$config['name']."<br/>";
                    }
                    continue;
                }
                if($log){
                    echo "<br/>------开始计算：".$config['name']."<br/>";
                }
                $and = [];
                $or  = [];
                $not = [];
                foreach ($config['condition'] as $row){
                    if($row['action'] == 0){
                        $and[] = $row;
                    }else if($row['action'] == 1){
                        $or[]  = $row;
                    }else if($row['action'] == 2){
                        $not[] = $row;
                    }
                }
                if(!$this->updateUserGroupAndCondition($and,$uid,$log)){
                    if($log){
                        echo "<br/>------退出分组：".$config['name']."<br/>";
                    }
                    continue;
                }
                if(!$this->updateUserGroupOrCondition($or,$uid,$log)){
                    if($log){
                        echo "<br/>------退出分组：".$config['name']."<br/>";
                    }
                    continue;
                }
                if(!$this->updateUserGroupNotCondition($not,$uid,$log)){
                    if($log){
                        echo "<br/>------退出分组：".$config['name']."<br/>";
                    }
                    continue;
                }
                $idArr[] = $config['id'];
                if($config['cid'] == 2){
                    $rfmUnique = true;
                }
                oo::commonOprRedis('games')->hIncrBy(okeys::UserGroupTotal(),$config['id'],1);
                if($log){
                    echo "<br/>------加入分组：".$config['name']."<br/>";
                    $nameArr[] = $config['name']."(序号：{$config['sort']})";
                    if($actionGroupIdSort <= $config['sort']){
                        $actionGroupIdSort = $config['sort'];
                        $actionGroupId     = $config['id'];
                        $actionGroupName   = $config['name'];
                    }
                }
            }
            $idArr = array_reverse($idArr);
            foreach ($idArr as $row){
                $curGroupId[] = $row;
            }
            $curGroupId = array_filter(array_unique($curGroupId));
            self::updateUserDynamicsData($uid,['groupId2'=>implode(',',$curGroupId)]);

            $this->UserGroupSta($uid,$curGroupId,self::$baseInfo);
            if($log){
                echo "<br/>------所有分组：".implode(',',$nameArr)."<br/>";
                echo "<br/>------广告执行分组：".$actionGroupName."<br/>";
                return $actionGroupId;
            }
            oo::commonOprModel('ssdata')->updateSGroup($uid);//数数分组
        }
    }

    public function updateUserGroupByUid(){
        $id = oo::commonOprRedis('games')->sRandMember(okeys::UserGroupActionGroupId());
        if(!$id){
            return true;
        }
        $config = oo::commonOprModel('config')->getUserGroupConfig2($id);
        for($i=0;$i<1000;$i++) {
            self::$baseInfo = [];
            $uid   = oo::commonOprRedis('games')->sPop(okeys::UserGroupActionByUid($id));
            if(!$uid){
                oo::commonOprRedis('games')->sRemove(okeys::UserGroupActionGroupId(),$id);
                return true;
            }
            $userDynamicsData = self::getUserDynamicsData($uid);
            $curGroupId = explode(',',$userDynamicsData['groupId2']);
            if($config['update'] == 1){
                continue;
            }
            $and = [];
            $or  = [];
            $not = [];
            foreach ($config['condition'] as $row){
                if($row['action'] == 0){
                    $and[] = $row;
                }else if($row['action'] == 1){
                    $or[]  = $row;
                }else if($row['action'] == 2){
                    $not[] = $row;
                }
            }

            foreach ($curGroupId as $k=>$g){
                if($g == $config['id']){
                    unset($curGroupId[$k]);
                }
            }
            $flag = true;
            if($flag && !$this->updateUserGroupAndCondition($and,$uid)){
                $flag = false;
            }
            if($flag && !$this->updateUserGroupOrCondition($or,$uid)){
                $flag = false;
            }
            if($flag && !$this->updateUserGroupNotCondition($not,$uid)){
                $flag = false;
            }
            if($flag){
                $curGroupId[] = $config['id'];
            }
            $curGroupId = array_unique(array_filter($curGroupId));
            self::updateUserDynamicsData($uid,['groupId2'=>trim(implode(',',$curGroupId),',')]);
        }
    }

    public function getUserUpdateUserGroupBaseInfo($uid,$key,$param = []){
        if(!empty(self::$baseInfo[$key])){
            return self::$baseInfo[$key];
        }
        if($key == "userInfo"){
            $data = self::getUserInfo($uid);
        }else if($key == "levelId"){
            $data = self::getUserLevel($uid)['levelId'] ?? 1;
        }else if($key == "isFirstPay"){
            $data = self::firstPay($uid);
        }else if($key == "loginTime"){
            $data = intval(self::getUserStatistics($uid)['loginTime'] / 60) ?? 1;
        }else if($key == "friends"){
            $data = count(self::getUserFriends($uid));
        }else if($key == "userRankData"){
            $data = json_decode(oo::commonOprRedis('games')->get(okeys::UserRankData($uid)), true);
        }else if($key == "country"){
            $data = self::getUserCountry($uid);
        }else if($key == "userAdjustGroup"){
            $data = self::getUserAdjustGroup($uid);
        }else if($key == "userDynamics"){
            $data = self::getUserDynamicsData($uid);
        }else if($key == "pay"){
            $data = self::getUserPayData($uid);
        }else if($key == "regPay" || $key == "timePay"){
            return self::getUserPayDataByParam($uid,$param);
        }else if($key == "loginNum"){
            $data = self::getUserRecentLoginDay($uid,$param);
        }
        self::$baseInfo[$key] = $data;
        return $data;
    }

    public function updateUserGroupAndCondition($condition,$uid,$log = false){
        if(empty($condition)){
            return true;
        }
        $userInfo = self::getUserUpdateUserGroupBaseInfo($uid,"userInfo");
        foreach ($condition as $row){
            if (isset($row['platform']) && $row['platform'] != 0) {// 平台
                if ($row['platform'] != $userInfo['login_plat']) {
                    $log && $this->log("并且","platform",$userInfo['login_plat'],$row['platform']);
                    return false;
                }
            }
            if (isset($row['userType']) && $row['userType'] != 0) {// 用户类型
                if (($row['userType'] == 1 && $userInfo['isFb'] != 1) || ($row['userType'] == 2 && $userInfo['isFb'] != 0)) {
                    $log && $this->log("并且","userType",$userInfo['isFb'],$row['userType']);
                    return false;
                }
            }
            if (isset($row['vip']) && $row['vip'] != 0) {//vip
                if (($row['vip'] == 1 && $userInfo['vip'] != 0) || ($row['vip'] == 2 && $userInfo['vip'] != 1)) {
                    $log && $this->log("并且","vip",$userInfo['vip'],$row['vip']);
                    return false;
                }
            }
            if (isset($row['point']) && $row['point'] != 0) {//积分
                $betweenPoint = $row['point'];
                $point = $userInfo['vipPoint'];
                if ($betweenPoint[0] > $point || $point > $betweenPoint[1]) {
                    $log && $this->log("并且","point",$userInfo['vipPoint'],$betweenPoint);
                    return false;
                }
            }
            if (isset($row['levelId']) && $row['levelId'] != 0) {
                $betweenLevel = $row['levelId'];
                $levelId      = self::getUserUpdateUserGroupBaseInfo($uid,"levelId");
                if ($betweenLevel[0] > $levelId || $levelId > $betweenLevel[1]) {
                    $log && $this->log("并且","levelId",$levelId,$betweenLevel);
                    return false;
                }
            }
            if (isset($row['firstPay']) && $row['firstPay'] != 0) {
                $isFirstPay = self::getUserUpdateUserGroupBaseInfo($uid,"isFirstPay");
                if (($row['firstPay'] == 1 && $isFirstPay != 0) || ($row['firstPay'] == 2 && $isFirstPay != 1)) {
                    $log && $this->log("并且","firstPay",$isFirstPay,$row['firstPay']);
                    return false;
                }
            }
            if(isset($row['active']) && $row['active'] != 0){
                $activeDay = self::getUserUpdateUserGroupBaseInfo($uid,"userDynamics")['activeDay'] ?? 1;
                $betweenActive = $row['active'];
                if($betweenActive[0] > $activeDay || $activeDay > $betweenActive[1]){
                    $log && $this->log("并且","active",$activeDay,$betweenActive);
                    return false;
                }
            }
            if (isset($row['onlineTime']) && $row['onlineTime'] != 0) {
                $loginTime        = self::getUserUpdateUserGroupBaseInfo($uid,"loginTime");
                $between = $row['onlineTime'];
                if ($between[0] > $loginTime || $loginTime > $between[1]) {
                    $log && $this->log("并且","onlineTime",$loginTime,$between);
                    return false;
                }
            }
            if (isset($row['regTime']) && $row['regTime'] != 0) {
                $loginTime = intval((time() - $userInfo['createtime']) / 84600) ?? 1;
                $between = $row['regTime'];
                if ($between[0] > $loginTime || $loginTime > $between[1]) {
                    $log && $this->log("并且","regTime",$loginTime,$between);
                    return false;
                }
            }
            if (isset($row['friendNum']) && $row['friendNum'] != 0) {
                $friends = self::getUserUpdateUserGroupBaseInfo($uid,"friends");
                $between = $row['friendNum'];
                if ($between[0] > $friends || $friends > $between[1]) {
                    $log && $this->log("并且","regTime",$friends,$between);
                    return false;
                }
            }

            if (isset($row['countrySet']) && $row['countrySet'] != 0) {
                $country = self::getUserUpdateUserGroupBaseInfo($uid,"country");
                $country = $country['country_code'];
                $countrySet = $row['countrySet'];
                if (!in_array($country,$countrySet)) {
                    $log && $this->log("并且","countrySet",$country,$countrySet);
                    return false;
                }
            }
            if (isset($row['adjustGroup']) && $row['adjustGroup'] != 0) {
                $userAdjustGroup = self::getUserUpdateUserGroupBaseInfo($uid,"userAdjustGroup");
                $adjustGroup = $row['adjustGroup'];
                if (!in_array($userAdjustGroup,$adjustGroup)) {
                    $log && $this->log("并且","userAdjustGroup",$userAdjustGroup,$adjustGroup);
                    return false;
                }
            }
            if (isset($row['friendRank']) && $row['friendRank'] != 0) {
                $between = $row['friendRank'];
                $userRankData = self::getUserUpdateUserGroupBaseInfo($uid,"userRankData");
                if(empty($userRankData)){
                    $log && $this->log("并且","friendRank","加载失败",$between);
                    return false;
                }
                $pos = $this->RankIntFormat($userRankData['friend']);
                if ($between[0] > $pos || $pos > $between[1]) {
                    $log && $this->log("并且","friendRank",$userRankData['friend'],$between);
                    return false;
                }
            }
            if (isset($row['countryRank']) && $row['countryRank'] != 0) {
                $between = $row['countryRank'];
                $userRankData = self::getUserUpdateUserGroupBaseInfo($uid,"userRankData");
                if(empty($userRankData)){
                    $log && $this->log("并且","countryRank","加载失败",$between);
                    return false;
                }
                $pos = $this->RankIntFormat($userRankData['country']);
                if ($between[0] > $pos || $pos > $between[1]) {
                    $log && $this->log("并且","countryRank",$userRankData['country'],$between);
                    return false;
                }
            }
            if (isset($row['worldRank']) && $row['worldRank'] != 0) {
                $between = $row['worldRank'];
                $userRankData = self::getUserUpdateUserGroupBaseInfo($uid,"userRankData");
                if(empty($userRankData)){
                    $log && $this->log("并且","worldRank","加载失败",$between);
                    return false;
                }
                $pos     = $this->RankIntFormat($userRankData['world']);
                if ($between[0] > $pos || $pos > $between[1]) {
                    $log && $this->log("并且","worldRank",$userRankData['world'],$between);
                    return false;
                }
            }
            if (isset($row['worldRank']) && $row['worldRank'] != 0) {
                $between = $row['worldRank'];
                $userRankData = self::getUserUpdateUserGroupBaseInfo($uid,"userRankData");
                if(empty($userRankData)){
                    $log && $this->log("并且","worldRank","加载失败",$between);
                    return false;
                }
                $pos     = $this->RankIntFormat($userRankData['world']);
                if ($between[0] > $pos || $pos > $between[1]) {
                    $log && $this->log("并且","worldRank",$userRankData['world'],$between);
                    return false;
                }
            }
            if (isset($row['rfm'])) {
                $rfm = $row['rfm'];
                $payData = self::getUserUpdateUserGroupBaseInfo($uid,"pay");
                if(empty($payData)){
                    $log && $this->log("并且","rfm","加载失败",[]);
                    return false;
                }
                if($rfm["r"][0] > $payData['lastPayTime'] || $payData['lastPayTime'] > $rfm["r"][1]){
                    $log && $this->log("并且","rfm","R:".$payData['lastPayTime'],$rfm["r"]);
                    return false;
                }
                if($rfm["f"][0] > $payData['payTotalTimes'] || $payData['payTotalTimes'] > $rfm["f"][1]){
                    $log && $this->log("并且","rfm","F:".$payData['payTotalTimes'],$rfm["f"]);
                    return false;
                }
                if($rfm["m"][0] > $payData['payNum'] || $payData['payNum'] >= $rfm["m"][1]){
                    $log && $this->log("并且","rfm","M:".$payData['payNum'],$rfm["m"]);
                    return false;
                }
            }
            if (isset($row['channel'])) {
                $channel = $userInfo['channel'];
                if($channel == 0){
                    $channel = $userInfo['login_plat'];
                }else{
                    $channel = 2 + $channel;
                }
                if($channel != $row['channel']){
                    $log && $this->log("并且","channel",$channel,$row['channel']);
                    return false;
                }
            }
            if (isset($row['vipLv']) && $row['vipLv'] != 0) {
                $vipLv = $userInfo['vipLv'];
                if (!in_array($vipLv,$row['vipLv'])) {
                    $log && $this->log("并且","vipLv",$vipLv,$row['vipLv']);
                    return false;
                }
            }
            if (isset($row['payNum']) && $row['payNum'] != 0) {
                $betweenPayNum = $row['payNum'];
                $payTotal      = self::getUserUpdateUserGroupBaseInfo($uid,"pay")['payTotal'];
                if ($betweenPayNum[0] > $payTotal || $payTotal > $betweenPayNum[1]) {
                    $log && $this->log("并且","payNum",$payTotal,$betweenPayNum);
                    return false;
                }
            }

            if (isset($row['payNum']) && $row['payNum'] != 0) {
                $betweenPayNum = $row['payNum'];
                $payTotal      = self::getUserUpdateUserGroupBaseInfo($uid,"pay")['payTotal'];
                if ($betweenPayNum[0] > $payTotal || $payTotal > $betweenPayNum[1]) {
                    $log && $this->log("并且","payNum",$payTotal,$betweenPayNum);
                    return false;
                }
            }
            if (isset($row['regPay'])) {
                $param = $row['regPay'];
                $param["type"] = "reg";
                $sum = self::getUserUpdateUserGroupBaseInfo($uid,"regPay",$param);
                if ($row['regPay']['num'][0] > $sum || $sum > $row['regPay']['num'][1]) {
                    $log && $this->log("并且","regPay",$sum,$row['regPay']['num']);
                    return false;
                }
            }
            if (isset($row['timePay'])) {
                $param = $row['timePay'];
                $param["type"] = "time";
                $sum = self::getUserUpdateUserGroupBaseInfo($uid,"timePay",$param);
                if ($row['timePay']['num'][0] > $sum || $sum > $row['timePay']['num'][1]) {
                    $log && $this->log("并且","timePay",$sum,$row['timePay']['num']);
                    return false;
                }
            }
            if (isset($row['loginNum'])) {
                $needLoginNum = $row['loginNum'][1];
                $dayArr = [];
                for($i=1;$i<=intval($row['loginNum'][0]);$i++){
                    $dayArr[] = date("Ymd",strtotime("-{$i} day"));
                }
                $userLoginNum = self::getUserUpdateUserGroupBaseInfo($uid,"loginNum",$dayArr);
                if ($userLoginNum < $needLoginNum) {
                    $log && $this->log("并且","loginNum",$userLoginNum,"最近{$row['loginNum'][0]}天大于等于{$needLoginNum}天登录");
                    return false;
                }
            }
        }
        return true;
    }

    public function updateUserGroupOrCondition($condition,$uid,$log = false){
        if(empty($condition)){
            return true;
        }
        $userInfo = self::getUserUpdateUserGroupBaseInfo($uid,"userInfo");
        foreach ($condition as $row){
            if (isset($row['platform']) && $row['platform'] != 0) {// 平台
                if ($row['platform'] == $userInfo['login_plat']) {
                    return true;
                }else{
                    $log && $this->log("或者","platform",$userInfo['login_plat'],$row['platform']);
                }
            }
            if (isset($row['userType']) && $row['userType'] != 0) {// 用户类型
                if (($row['userType'] == 1 && $userInfo['isFb'] == 1) || ($row['userType'] == 2 && $userInfo['isFb'] == 0)) {
                    return true;
                }else{
                    $log && $this->log("或者","userType",$userInfo['isFb'],$row['userType']);
                }
            }
            if (isset($row['vip']) && $row['vip'] != 0) {//vip
                if (($row['vip'] == 1 && $userInfo['vip'] == 0) || ($row['vip'] == 2 && $userInfo['vip'] == 1)) {
                    return true;
                }else{
                    $log && $this->log("或者","vip",$userInfo['vip'],$row['vip']);
                }
            }
            if (isset($row['point']) && $row['point'] != 0) {//积分
                $betweenPoint = $row['point'];
                $point = $userInfo['vipPoint'];
                if ($betweenPoint[0] < $point && $point < $betweenPoint[1]) {
                    return true;
                }else{
                    $log && $this->log("或者","point",$userInfo['vipPoint'],$betweenPoint);
                }
            }
            if (isset($row['levelId']) && $row['levelId'] != 0) {
                $betweenLevel = $row['levelId'];
                $levelId      = self::getUserUpdateUserGroupBaseInfo($uid,"levelId");
                if ($betweenLevel[0] < $levelId && $levelId < $betweenLevel[1]) {
                    return true;
                }else{
                    $log && $this->log("或者","levelId",$levelId,$betweenLevel);
                }
            }
            if (isset($row['firstPay']) && $row['firstPay'] != 0) {
                $isFirstPay = self::getUserUpdateUserGroupBaseInfo($uid,"isFirstPay");
                if (($row['firstPay'] == 1 && $isFirstPay == 0) || ($row['firstPay'] == 2 && $isFirstPay == 1)) {
                    return true;
                }else{
                    $log && $this->log("或者","firstPay",$isFirstPay,$row['firstPay']);
                }
            }
            if(isset($row['active']) && $row['active'] != 0){
                $activeDay = self::getUserUpdateUserGroupBaseInfo($uid,"userDynamics")['activeDay'] ?? 1;
                $betweenActive = $row['active'];
                if($betweenActive[0] < $activeDay && $activeDay < $betweenActive[1]){
                    return true;
                }else{
                    $log && $this->log("或者","active",$activeDay,$betweenActive);
                }
            }
            if (isset($row['onlineTime']) && $row['onlineTime'] != 0) {
                $loginTime        = self::getUserUpdateUserGroupBaseInfo($uid,"loginTime");
                $between = $row['onlineTime'];
                if ($between[0] < $loginTime && $loginTime < $between[1]) {
                    return true;
                }else{
                    $log && $this->log("或者","onlineTime",$loginTime,$between);
                }
            }
            if (isset($row['regTime']) && $row['regTime'] != 0) {
                $loginTime = intval((time() - $userInfo['createtime']) / 84600) ?? 1;
                $between = $row['regTime'];
                if ($between[0] < $loginTime && $loginTime < $between[1]) {
                    return true;
                }else{
                    $log && $this->log("或者","regTime",$loginTime,$between);
                }
            }
            if (isset($row['friendNum']) && $row['friendNum'] != 0) {
                $friends = self::getUserUpdateUserGroupBaseInfo($uid,"friends");
                $between = $row['friendNum'];
                if ($between[0] < $friends && $friends < $between[1]) {
                    return true;
                }else{
                    $log && $this->log("或者","regTime",$friends,$between);
                }
            }

            if (isset($row['countrySet']) && $row['countrySet'] != 0) {
                $country = self::getUserUpdateUserGroupBaseInfo($uid,"country");
                $country = $country['country_code'];
                $countrySet = $row['countrySet'];
                if (in_array($country,$countrySet)) {
                    return true;
                }else{
                    $log && $this->log("或者","countrySet",$country,$countrySet);
                }
            }
            if (isset($row['adjustGroup']) && $row['adjustGroup'] != 0) {
                $userAdjustGroup = self::getUserUpdateUserGroupBaseInfo($uid,"userAdjustGroup");
                $adjustGroup = $row['adjustGroup'];
                if (in_array($userAdjustGroup,$adjustGroup)) {
                    return true;
                }else{
                    $log && $this->log("或者","userAdjustGroup",$userAdjustGroup,$adjustGroup);
                }
            }

            if (isset($row['friendRank']) && $row['friendRank'] != 0) {
                $between = $row['friendRank'];
                $userRankData = self::getUserUpdateUserGroupBaseInfo($uid,"userRankData");
                if (empty($userRankData)) {
                    $log && $this->log("或者","friendRank","加载失败",$between);
                    return true;
                }
                $pos     = $this->RankIntFormat($userRankData['friend']);
                if ($between[0] < $pos && $pos < $between[1]) {
                    return true;
                }else{
                    $log && $this->log("或者","friendRank",$userRankData['friend'],$between);
                }
            }
            if (isset($row['countryRank']) && $row['countryRank'] != 0) {
                $between = $row['countryRank'];
                $userRankData = self::getUserUpdateUserGroupBaseInfo($uid,"userRankData");
                if (empty($userRankData)) {
                    $log && $this->log("或者","countryRank","加载失败",$between);
                    return true;
                }
                $pos     = $this->RankIntFormat($userRankData['country']);
                if ($between[0] < $pos && $pos < $between[1]) {
                    return true;
                }else{
                    $log && $this->log("或者","countryRank",$userRankData['country'],$between);
                }
            }
            if (isset($row['worldRank']) && $row['worldRank'] != 0) {
                $between = $row['worldRank'];
                $userRankData = self::getUserUpdateUserGroupBaseInfo($uid,"userRankData");
                if (empty($userRankData)) {
                    $log && $this->log("或者","worldRank","加载失败",$between);
                    return true;
                }
                $pos     = $this->RankIntFormat($userRankData['world']);
                if ($between[0] < $pos && $pos < $between[1]) {
                    return true;
                }else{
                    $log && $this->log("或者","worldRank",$userRankData['world'],$between);
                }
            }
            if (isset($row['rfm'])) {
                $rfm = $row['rfm'];
                $payData = self::getUserUpdateUserGroupBaseInfo($uid,"pay");
                if(empty($payData)){
                    $log && $this->log("或者","rfm","加载失败",[]);
                    return true;
                }
                if($rfm["r"][0] <= $payData['lastPayTime'] && $payData['lastPayTime'] < $rfm["r"][1]){
                    return true;
                }else{
                    $log && $this->log("或者","rfm","R:".$payData['lastPayTime'],$rfm["r"]);
                }
                if($rfm["f"][0] <= $payData['payTotalTimes'] && $payData['payTotalTimes'] < $rfm["f"][1]){
                    return true;
                }else{
                    $log && $this->log("或者","rfm","F:".$payData['payTotalTimes'],$rfm["f"]);
                }
                if($rfm["m"][0] <= $payData['payNum'] && $payData['payNum'] < $rfm["m"][1]){
                    return true;
                }else{
                    $log && $this->log("或者","rfm","M:".$payData['payNum'],$rfm["m"]);
                }
            }
            if (isset($row['channel'])) {
                $channel = $userInfo['channel'];
                if($channel == 0){
                    $channel = $userInfo['login_plat'];
                }else{
                    $channel = 2 + $channel;
                }
                if($channel == $row['channel']){
                    return true;
                }else{
                    $log && $this->log("或者","channel",$channel,$row['channel']);
                }
            }
            if (isset($row['vipLv']) && $row['vipLv'] != 0) {
                $vipLv = $userInfo['vipLv'];
                if (in_array($vipLv,$row['vipLv'])) {
                    return true;
                }else{
                    $log && $this->log("或者","vipLv",$vipLv,$row['vipLv']);
                }
            }
            if (isset($row['payNum']) && $row['payNum'] != 0) {
                $betweenPayNum = $row['payNum'];
                $payTotal      = self::getUserUpdateUserGroupBaseInfo($uid,"pay")['payTotal'];
                if ($betweenPayNum[0] < $payTotal && $payTotal < $betweenPayNum[1]) {
                    return true;
                }else{
                    $log && $this->log("或者","payNum",$payTotal,$betweenPayNum);
                }
            }
            if (isset($row['regPay'])) {
                $param = $row['regPay'];
                $param["type"] = "reg";
                $sum = self::getUserUpdateUserGroupBaseInfo($uid,"regPay",$param);
                if ($row['regPay']['num'][0] <= $sum && $sum < $row['regPay']['num'][1]) {
                    return true;
                }else{
                    $log && $this->log("或者","regPay",$sum,$row['regPay']['num']);
                }
            }
            if (isset($row['timePay'])) {
                $param = $row['timePay'];
                $param["type"] = "time";
                $sum = self::getUserUpdateUserGroupBaseInfo($uid,"timePay",$param);
                if ($row['timePay']['num'][0] <= $sum && $sum < $row['timePay']['num'][1]) {
                    return true;
                }else{
                    $log && $this->log("或者","timePay",$sum,$row['timePay']['num']);
                }
            }
            if (isset($row['loginNum'])) {
                $needLoginNum = $row['loginNum'][1];
                $dayArr = [];
                for($i=1;$i<=intval($row['loginNum'][0]);$i++){
                    $dayArr[] = date("Ymd",strtotime("-{$i} day"));
                }
                $userLoginNum = self::getUserUpdateUserGroupBaseInfo($uid,"loginNum",$dayArr);
                if ($userLoginNum >= $needLoginNum) {
                    return true;
                }else{
                    $log && $this->log("或者","loginNum",$userLoginNum,"最近{$row['loginNum'][0]}天大于等于{$needLoginNum}天登录");
                }
            }
        }
        return false;
    }

    public function updateUserGroupNotCondition($condition,$uid,$log = false){
        if(empty($condition)){
            return true;
        }
        $userInfo = self::getUserUpdateUserGroupBaseInfo($uid,"userInfo");
        foreach ($condition as $row){
            if (isset($row['platform']) && $row['platform'] != 0) {// 平台
                if ($row['platform'] == $userInfo['login_plat']) {
                    $log && $this->log("非","platform",$userInfo['login_plat'],$row['platform']);
                    return false;
                }
            }
            if (isset($row['userType']) && $row['userType'] != 0) {// 用户类型
                if (($row['userType'] == 1 && $userInfo['isFb'] == 1) || ($row['userType'] == 2 && $userInfo['isFb'] == 0)) {
                    $log && $this->log("非","userType",$userInfo['isFb'],$row['userType']);
                    return false;
                }
            }
            if (isset($row['vip']) && $row['vip'] != 0) {//vip
                if (($row['vip'] == 1 && $userInfo['vip'] == 0) || ($row['vip'] == 2 && $userInfo['vip'] == 1)) {
                    $log && $this->log("非","vip",$userInfo['vip'],$row['vip']);
                    return false;
                }
            }
            if (isset($row['point']) && $row['point'] != 0) {//积分
                $betweenPoint = $row['point'];
                $point = $userInfo['vipPoint'];
                if ($betweenPoint[0] < $point && $point < $betweenPoint[1]) {
                    $log && $this->log("非","point",$userInfo['vipPoint'],$betweenPoint);
                    return false;
                }
            }
            if (isset($row['levelId']) && $row['levelId'] != 0) {
                $betweenLevel = $row['levelId'];
                $levelId      = self::getUserUpdateUserGroupBaseInfo($uid,"levelId");
                if ($betweenLevel[0] < $levelId && $levelId < $betweenLevel[1]) {
                    $log && $this->log("非","levelId",$levelId,$betweenLevel);
                    return false;
                }
            }
            if (isset($row['firstPay']) && $row['firstPay'] != 0) {
                $isFirstPay = self::getUserUpdateUserGroupBaseInfo($uid,"isFirstPay");
                if (($row['firstPay'] == 1 && $isFirstPay == 0) || ($row['firstPay'] == 2 && $isFirstPay == 1)) {
                    $log && $this->log("非","firstPay",$isFirstPay,$row['firstPay']);
                    return false;
                }
            }
            if(isset($row['active']) && $row['active'] != 0){
                $activeDay = self::getUserUpdateUserGroupBaseInfo($uid,"userDynamics")['activeDay'] ?? 1;
                $betweenActive = $row['active'];
                if($betweenActive[0] < $activeDay && $activeDay < $betweenActive[1]){
                    $log && $this->log("非","active",$activeDay,$betweenActive);
                    return false;
                }
            }
            if (isset($row['onlineTime']) && $row['onlineTime'] != 0) {
                $loginTime = self::getUserUpdateUserGroupBaseInfo($uid,"loginTime");
                $between = $row['onlineTime'];
                if ($between[0] < $loginTime && $loginTime < $between[1]) {
                    $log && $this->log("非","onlineTime",$loginTime,$between);
                    return false;
                }
            }
            if (isset($row['regTime']) && $row['regTime'] != 0) {
                $loginTime = intval( (time() - $userInfo['createtime']) / 84600) ?? 1;
                $between = $row['regTime'];
                if ($between[0] < $loginTime && $loginTime < $between[1]) {
                    $log && $this->log("非","regTime",$loginTime,$between);
                    return false;
                }
            }
            if (isset($row['friendNum']) && $row['friendNum'] != 0) {
                $friends = self::getUserUpdateUserGroupBaseInfo($uid,"friends");
                $between = $row['friendNum'];
                if ($between[0] < $friends && $friends < $between[1]) {
                    $log && $this->log("非","regTime",$friends,$between);
                    return false;
                }
            }

            if (isset($row['countrySet']) && $row['countrySet'] != 0) {
                $country = self::getUserUpdateUserGroupBaseInfo($uid,"country");
                $country = $country['country_code'];
                $countrySet = $row['countrySet'];
                if (in_array($country,$countrySet)) {
                    $log && $this->log("非","countrySet",$country,$countrySet);
                    return false;
                }
            }
            if (isset($row['adjustGroup']) && $row['adjustGroup'] != 0) {
                $userAdjustGroup = self::getUserUpdateUserGroupBaseInfo($uid,"userAdjustGroup");
                $adjustGroup = $row['adjustGroup'];
                if (in_array($userAdjustGroup,$adjustGroup)) {
                    $log && $this->log("非","adjustGroup",$userAdjustGroup,$adjustGroup);
                    return false;
                }
            }

            if (isset($row['friendRank']) && $row['friendRank'] != 0) {
                $between = $row['friendRank'];
                $userRankData = self::getUserUpdateUserGroupBaseInfo($uid,"userRankData");
                if (empty($userRankData)) {
                    $log && $this->log("非","friendRank","加载失败",$between);
                    return true;
                }
                $pos     = $this->RankIntFormat($userRankData['friend']);
                if ($between[0] < $pos && $pos < $between[1]) {
                    $log && $this->log("非","friendRank",$userRankData['friend'],$between);
                    return false;
                }
            }
            if (isset($row['countryRank']) && $row['countryRank'] != 0) {
                $between = $row['countryRank'];
                $userRankData = self::getUserUpdateUserGroupBaseInfo($uid,"userRankData");
                if (empty($userRankData)) {
                    $log && $this->log("非","countryRank","加载失败",$between);
                    return true;
                }
                $pos     = $this->RankIntFormat($userRankData['country']);
                if ($between[0] < $pos && $pos < $between[1]) {
                    $log && $this->log("非","countryRank",$userRankData['country'],$between);
                    return false;
                }
            }
            if (isset($row['worldRank']) && $row['worldRank'] != 0) {
                $between = $row['worldRank'];
                $userRankData = self::getUserUpdateUserGroupBaseInfo($uid,"userRankData");
                if (empty($userRankData)) {
                    $log && $this->log("非","worldRank","加载失败",$between);
                    return true;
                }
                $pos     = $this->RankIntFormat($userRankData['world']);
                if ($between[0] < $pos && $pos < $between[1]) {
                    $log && $this->log("非","worldRank",$userRankData['world'],$between);
                    return false;
                }
            }
            if (isset($row['rfm'])) {
                $rfm = $row['rfm'];
                $payData = self::getUserUpdateUserGroupBaseInfo($uid,"pay");
                if(empty($payData)){
                    $log && $this->log("非","rfm","加载失败",[]);
                    return true;
                }
                if($rfm["r"][0] <= $payData['lastPayTime'] && $payData['lastPayTime'] < $rfm["r"][1]){
                    $log && $this->log("非","rfm","R:".$payData['lastPayTime'],$rfm["r"]);
                    return false;
                }
                if($rfm["f"][0] <= $payData['payTotalTimes'] && $payData['payTotalTimes'] < $rfm["f"][1]){
                    $log && $this->log("非","rfm","F:".$payData['payTotalTimes'],$rfm["f"]);
                    return false;
                }
                if($rfm["m"][0] <= $payData['payNum'] && $payData['payNum'] < $rfm["m"][1]){
                    $log && $this->log("非","rfm","M:".$payData['payNum'],$rfm["m"]);
                    return false;
                }
            }
            if (isset($row['channel'])) {
                $channel = $userInfo['channel'];
                if($channel == 0){
                    $channel = $userInfo['login_plat'];
                }else{
                    $channel = 2 + $channel;
                }
                if($channel == $row['channel']){
                    $log && $this->log("非","channel",$channel,$row['channel']);
                    return false;
                }
            }
            if (isset($row['vipLv']) && $row['vipLv'] != 0) {
                $vipLv = $userInfo['vipLv'];
                if (in_array($vipLv,$row['vipLv'])) {
                    $log && $this->log("非","vipLv",$vipLv,$row['vipLv']);
                    return false;
                }
            }
            if (isset($row['payNum']) && $row['payNum'] != 0) {
                $betweenPayNum = $row['payNum'];
                $payTotal      = self::getUserUpdateUserGroupBaseInfo($uid,"pay")['payTotal'];
                if ($betweenPayNum[0] < $payTotal && $payTotal < $betweenPayNum[1]) {
                    $log && $this->log("非","payNum",$payTotal,$betweenPayNum);
                    return false;
                }
            }
            if (isset($row['regPay'])) {
                $param = $row['regPay'];
                $param["type"] = "reg";
                $sum = self::getUserUpdateUserGroupBaseInfo($uid,"regPay",$param);
                if ($row['regPay']['num'][0] <= $sum && $sum < $row['regPay']['num'][1]) {
                    $log && $this->log("非","regPay",$sum,$row['regPay']['num']);
                    return false;
                }
            }
            if (isset($row['timePay'])) {
                $param = $row['timePay'];
                $param["type"] = "time";
                $sum = self::getUserUpdateUserGroupBaseInfo($uid,"timePay",$param);
                if ($row['timePay']['num'][0] <= $sum && $sum < $row['timePay']['num'][1]) {
                    $log && $this->log("非","timePay",$sum,$row['timePay']['num']);
                    return false;
                }
            }
            if (isset($row['loginNum'])) {
                $needLoginNum = $row['loginNum'][1];
                $dayArr = [];
                for($i=1;$i<=intval($row['loginNum'][0]);$i++){
                    $dayArr[] = date("Ymd",strtotime("-{$i} day"));
                }
                $userLoginNum = self::getUserUpdateUserGroupBaseInfo($uid,"loginNum",$dayArr);
                if ($userLoginNum >= $needLoginNum) {
                    $log && $this->log("非","loginNum",$userLoginNum,"最近{$row['loginNum'][0]}天小于{$needLoginNum}天登录");
                    return false;
                }
            }
        }
        return true;
    }

    public function log($con,$type,$cur,$need){
        $log = "=========>不满足{$con}条件=>";
        switch ($type){
            case 'platform':
                $typeZh = "平台 ";
                $log.= $typeZh;
                $arr = [1=>'安卓',2=>'IOS'];
                $log.="用户{$typeZh}：{$arr[$cur]} ";
                $log.="要求{$typeZh}：{$arr[$need]} ";
                break;
            case 'userType':
                $typeZh = "用户类型 ";
                $log.=$typeZh;
                $arr = [1=>'FB',2=>'游客'];
                $log.="用户{$typeZh}：{$arr[$cur==1?1:2]} ";
                $log.="要求{$typeZh}：{$arr[$need]} ";
                break;
            case 'vip':
                $typeZh = "vip ";
                $log.=$typeZh;
                $arr = [1=>'非VIP',2=>'VIP'];
                $log.="用户{$typeZh}：{$arr[$cur==1?2:1]} ";
                $log.="要求{$typeZh}：{$arr[$need]} ";
                break;
            case 'point':
                $typeZh = "积分 ";
                $log.=$typeZh;
                $log.="用户{$typeZh}：{$cur} ";
                $log.="要求{$typeZh}：{$need[0]}-{$need[1]} ";
                break;
            case 'levelId':
                $typeZh = "等级 ";
                $log.=$typeZh;
                $log.="用户{$typeZh}：{$cur} ";
                $log.="要求{$typeZh}：{$need[0]}-{$need[1]} ";
                break;
            case 'firstPay':
                $typeZh = "首冲 ";
                $log.=$typeZh;
                $arr = [1=>'非首冲',2=>'首冲'];
                $log.="用户{$typeZh}：{$arr[$cur==1?2:1]} ";
                $log.="要求{$typeZh}：{$arr[$need]} ";
                break;
            case 'active':
                $typeZh = "活跃天数 ";
                $log.=$typeZh;
                $log.="用户{$typeZh}：{$cur} ";
                $log.="要求{$typeZh}：{$need[0]}-{$need[1]} ";
                break;
            case 'onlineTime':
                $typeZh = "累积游戏时长 ";
                $log.=$typeZh;
                $log.="用户{$typeZh}：{$cur} ";
                $log.="要求{$typeZh}：{$need[0]}-{$need[1]} ";
                break;
            case 'regTime':
                $typeZh = "注册天数 ";
                $log.=$typeZh;
                $log.="用户{$typeZh}：{$cur} ";
                $log.="要求{$typeZh}：{$need[0]}-{$need[1]} ";
                break;
            case 'friendNum':
                $typeZh = "好友数量 ";
                $log.=$typeZh;
                $log.="用户{$typeZh}：{$cur} ";
                $log.="要求{$typeZh}：{$need[0]}-{$need[1]} ";
                break;
            case 'friendRank':
                $typeZh = "好友排行 ";
                $log.=$typeZh;
                $log.="用户{$typeZh}：{$cur} ";
                $log.="要求{$typeZh}：{$need[0]}-{$need[1]} ";
                break;
            case 'countryRank':
                $typeZh = "国家排行 ";
                $log.=$typeZh;
                $log.="用户{$typeZh}：{$cur} ";
                $log.="要求{$typeZh}：{$need[0]}-{$need[1]} ";
                break;
            case 'worldRank':
                $typeZh = "世界排行 ";
                $log.=$typeZh;
                $log.="用户{$typeZh}：{$cur} ";
                $log.="要求{$typeZh}：{$need[0]}-{$need[1]} ";
                break;
            case 'countrySet':
                $typeZh = "国家分组 ";
                $log.=$typeZh;
                $log.="用户{$typeZh}：{$cur} ";
                $log.="要求{$typeZh}：".implode(',',$need);
                break;
            case 'userAdjustGroup':
                $typeZh = "Adjust来源分组 ";
                $log.=$typeZh;
                $log.="用户{$typeZh}：{$cur} ";
                $log.="要求{$typeZh}：".implode(',',$need);;
                break;
            case 'rfm':
                $typeZh = "RFM ";
                $log.=$typeZh;
                $log.="用户{$typeZh}：{$cur} ";
                $log.="要求{$typeZh}：{$need[0]}-{$need[1]} ";
                break;
            case 'channel':
                $typeZh = "渠道 ";
                $log.=$typeZh;
                $arr = [1=>'GooglePay',2=>'AppStore',3=>'VIVO'];
                $log.="用户{$typeZh}：{$arr[$cur]} ";
                $log.="要求{$typeZh}：{$arr[$need]} ";
                break;
            case 'vipLv':
                $typeZh = "vip等级 ";
                $log.=$typeZh;
                $log.="用户{$typeZh}：{$cur} ";
                $log.="要求{$typeZh}：".implode(',',$need);
                break;
            case 'payNum':
                $typeZh = "支付金额 ";
                $log.=$typeZh;
                $log.="用户{$typeZh}：{$cur} ";
                $log.="要求{$typeZh}：{$need[0]}-{$need[1]} ";
                break;
            case 'regPay':
                $typeZh = "注册区间金额 ";
                $log.=$typeZh;
                $log.="用户{$typeZh}：{$cur} ";
                $log.="要求{$typeZh}：{$need[0]}-{$need[1]} ";
                break;
            case 'timePay':
                $typeZh = "时间区间金额 ";
                $log.=$typeZh;
                $log.="用户{$typeZh}：{$cur} ";
                $log.="要求{$typeZh}：{$need[0]}-{$need[1]} ";
                break;
            case 'loginNum':
                $typeZh = "最近登录天数 ";
                $log.=$typeZh;
                $log.="用户{$typeZh}：{$cur} ";
                $log.="要求{$typeZh}：{$need} ";
                break;
            default:
                echo $type;
        }
        echo $log."<br/>";
    }

    public function UserGroupSta($uid,$groupId,$data){
        $groupId = "#".implode('#',$groupId)."#";
        $date = date("Ymd");
        $data = addslashes(json_encode($data));
        $tb   = otable::UserGroupSta();
        $sql  = "REPLACE INTO {$tb} SET date='{$date}',uid='{$uid}',groupId='{$groupId}',data='{$data}'";
        oo::commonOprDb('statistics')->query($sql);
    }

    public function RankIntFormat($pot){
        if(strpos($pot,">") !== false){
            return intval(str_replace('>','',$pot)) + 1;
        }else{
            return $pot;
        }
    }

    //返回广告渠道
    public function getUserAdjustChannel($uid){
        $tb  = otable::AdJustSta($uid);
        $ret = oo::commonOprDb('slavedb')->getOne("SELECT * FROM {$tb} WHERE uid={$uid} LIMIT 1",MYSQLI_ASSOC);
        $tb  = otable::AdJustData();
        if(empty($ret['ios']) || empty($ret['android'])){
            $channel = "";
        }else if(!empty($ret['android']) || !empty($ret['ios'])) {
            $agps = trim($ret['android'], '"');
            $igps = trim($ret['ios'], '"');
            $ret = oo::commonOprDb('slavedb')->getOne("SELECT * FROM {$tb} WHERE gps_adid='{$agps}' OR idfa='{$igps}' LIMIT 1", MYSQLI_ASSOC);
            if(!empty($ret)){
                $channel = $ret['network_name'];
            }else{
                $channel = "";
            }
        }else{
            $channel = "";
        }
        return $channel;
    }

    //返回用户在线时间
    public function getUserLoginTime($uid){
        $ghtable = otable::statistics($uid);
        $updateSrv = "SELECT * FROM {$ghtable} WHERE uid={$uid}";
        $ret = oo::commonOprDb('slavedb')->getOne($updateSrv,1);
        return empty($ret['loginTime'])?0:$ret['loginTime'];
    }

    public function delUserGroupAction(){
        $idArr = oo::commonOprRedis('games')->sMembers(okeys::DelUserGroupAction());
        oo::commonOprRedis('games')->delete(okeys::DelUserGroupAction());
        if(empty($idArr)){
            return true;
        }
        $tb    = otable::UserDynamicsData();
        $uidArr= [];
        $notUid= [];
        foreach ($idArr as $id){
            if(empty($notUid)){
                $uidSql = "";
            }else{
                $notUidTemp = implode(',',$notUid);
                $uidSql = "uid NOT IN ({$notUidTemp})";
            }
            $sql = "SELECT uid,groupId2 FROM {$tb} WHERE {$uidSql} groupId2 LIKE '%{$id}%'";
            $ret = oo::commonOprDb('slavedb')->getAll($sql,MYSQLI_ASSOC);
            foreach ($ret as $r){
                $uidArr[$r["uid"]] = $r["groupId2"];
                $notUid[] = $r["uid"];
            }
        }
        foreach ($uidArr as $uid=>$groupId){
            $groupId = explode(',',$groupId);
            $action  = false;
            foreach ($groupId as $k=>$r){
                if(in_array($r,$idArr)){
                    unset($groupId[$k]);
                    $action = true;
                }
            }
            if($action){
                self::updateUserDynamicsData($uid,['groupId2'=>implode(',',$groupId)]);
            }
        }
    }

    /**
     * 支付数据
     * @param $uid
     * @return array|mixed
     * Created by: Owen
     * Created on: 2021/1/20 11:52
     */
    public function getUserPayData($uid){
        $cacheKey = okeys::CMSUserPayData($uid);
        $cache    = oo::commonOprRedis('games')->hGet($cacheKey,"payData");
        if($cache){
            //return json_decode($cache,true);
        }
        $data = [
            'payNum'     => oo::commonOprModel('payment')->getUserTotalPayNum($uid,90),
            'payTotal'   => oo::commonOprModel('payment')->getUserTotalPayNum($uid),
            'lastPayTime'=> intval((time() - oo::commonOprModel('payment')->getUserLastPayTime($uid)) / (24 * 60 * 60)),
            'payTotalTimes'=> intval(oo::commonOprModel('payment')->getUserPayTimes($uid,90)),
        ];
        oo::commonOprRedis('games')->hSet($cacheKey,"payData",json_encode($data));
        oo::commonOprRedis('games')->expire($cacheKey,7*24*60*60);
        return $data;
    }

    /**
     * 根据区间获取支付金额
     * @param $uid
     * @param $param
     * @return float|int
     * Created by: Owen
     * Created on: 2021/1/20 17:07
     */
    public function getUserPayDataByParam($uid,$param){
        $cacheKey = okeys::CMSUserPayData($uid);
        $cache    = oo::commonOprRedis('games')->hGet($cacheKey,$param["type"]."-".$param['time'][0]."-".$param['time'][1]);
        if($cache){
            return intval($cache);
        }
        $tb = otable::payment($uid);
        $stime    = $param['time'][0];
        $etime    = $param['time'][1];
        if($param["type"] == "reg"){
            $createTime = self::getUserUpdateUserGroupBaseInfo($uid,'userInfo')['createtime'];
            $stime    = $stime * 24*60*60 + $createTime;
            $etime    = $etime * 24*60*60 + $createTime;
        }
        $sql = "SELECT COALESCE(SUM(pusd),0) AS sum FROM {$tb} WHERE uid={$uid} AND status=2 AND pay_time>={$stime} AND pay_time<{$etime}";
        $ret = oo::commonOprDb('slavedb')->getOne($sql,MYSQLI_ASSOC);
        $sum = round($ret['sum'],2);
        oo::commonOprRedis('games')->hSet($cacheKey,$param["type"]."-".$param['time'][0]."-".$param['time'][1],$sum);
        oo::commonOprRedis('games')->expire($cacheKey,7*24*60*60);
        return $sum;
    }

    /**
     * 给定时间内登录天数
     * @param $uid
     * @param $dayArr
     * @return int
     * Created by: Owen
     * Created on: 2021/3/4 17:09
     */
    public function getUserRecentLoginDay($uid,$dayArr){
        $num = 0;
        foreach ($dayArr as $day){
            if(!isset(self::$loginData[$day])){
                self::$loginData[$day] = oo::commonOprModel('statistics')->getAllPlayer($day,2);
            }
            if (in_array($uid,self::$loginData[$day])){
                $num ++ ;
            }
        }
        return $num;
    }

    /**
     * Notes:定时根据数数分组列表,获取各分组的用户列表
     * User: wsc
     * Time: 2021/5/29 18:02
     */
    public function getUserListFromSS(){
        $config = oo::commonOprModel('config')->getUserGroupConfigByCid(5);
        foreach ($config as $v){
            if($v['groupId'] == 0 || $v['name'] == '数数-默认分组'){
                continue;
            }

            $key = okeys::userListSSGroup($v['groupId']);
            oo::commonOprRedis('statistics')->delete($key);

            $uidList = oo::commonOprModel('SSdata')->getUidListByGroupName($v['name']);
            if(!empty($uidList)){
                foreach ($uidList as $uid){
                    oo::commonOprRedis('statistics')->zAdd($key,1,$uid);
                }
            }
        }
    }

    /**
     * Notes:更新用户数数分组(弃用)
     * User: wsc
     * Time: 2021/5/29 17:57
     */
//    public function upDateUserGroup3(){
//        $group = "";
//        $list =  [];
//        foreach ($list as $uid){
//            $uGroupArr = [];
//            $config = oo::commonOprModel('config')->getUserGroupConfigByCid(5);
//            foreach ($config as $v){
//                $key = okeys::userListSSGroup($v['groupId']);
//                $res = oo::commonOprRedis('statistics')->zScore($key,$uid);
//                if($res!==false){
//                     array_push($uGroupArr,$v['groupId']);
//                }
//            }
//            if($uGroupArr){
//                $group = implode(',',$uGroupArr);
//                $this->updateUserDynamicsData($uid,["groupId3"=>$group]);
//            }else{
//                $this->updateUserDynamicsData($uid,["groupId3"=>"0"]);
//            }
//        }
//    }

    /**
     * Notes:注册分群 数数
     * User: hss
     * Time: 2021/09/16 10:55
     * @param $uid
     * @return boolean
     */
    public function regSSgroup($uid = 0){
        if(empty($uid)){
            return false;
        }
        //190 lastnumber1 id尾号为1
        //191 lastnumber0 id尾号为0
        //192 lastnumber89 id尾号8&9
        //193 lastnumber67 id尾号6&7
        //194 lastnumber45 id尾号4&5
        //195 lastnumber23 id尾号2&3
        //196 lastnumber01 id尾号0&1
        $end = $uid % 10;
        $groupId = [];
        switch ($end){
            case 0:
                $groupId = [191,196];
                break;
            case 1:
                $groupId = [190,196];
                break;
            case 2:
            case 3:
                $groupId = [195];
                break;
            case 4:
            case 5:
                $groupId = [194];
                break;
            case 6:
            case 7:
                $groupId = [193];
                break;
            case 8:
            case 9:
                $groupId = [192];
                break;
        }
        return $groupId;
    }
    /**
     * Notes:获取所有机器人
     * User: wsc
     * Time: 2022/10/18 16:58
     * @return array
     */
    public function getAllRobot(){
        $userList = oo::commonOprRedis('games')->get(okeys::GetRobotUserList());
        if(!empty($userList)){
            $userList = json_decode($userList,true);
            if(!empty($userList)&&is_array($userList)){
                return $userList;
            }
        }
        $tb  = otable::Robot();
        $sql = "SELECT * FROM {$tb} WHERE 1";
        $ret = oo::commonOprDb('slavedb')->getAll($sql,1);
        $robot = [];
        foreach ($ret as $row){
            $robot[] = $row['uid'];
        }
        oo::commonOprRedis('games')->set(okeys::GetRobotUserList(),json_encode($robot));
        return $robot;
    }
}
