<?php
defined('IN_WEB') or die('Include Error!');
/**
 * 用户相关
 */
class User
{
    /**
     * 开始新手引导
     * @param $param
     * @return false|string
     * Created by: Owen
     * Created on: 2020/3/27 20:09
     */
    public function beginGuide($param){
        $uid = oo::functions()->uint($param['uid']);
        $stage = oo::functions()->uint($param['stage']);
        $redisStage = oo::commonOprRedis('common')->hGet(okeys::beginGuide(),$uid);
        if($stage > 18){
            return json_encode(['code'=>-1,'msg'=>'stage error']);
        }

        $tb = otable::gh_gameserver($uid);
        $sql = "SELECT beginGuide FROM {$tb} WHERE mid={$uid} LIMIT 1";
        $ret = oo::commonOprDb('ghgames')->getOne($sql,1);
        if($ret['beginGuide'] >= 19){
            return json_encode(['code'=>-1]);
        }

        $out = ['code'=>1,'data'=>[]];
        $sql = "UPDATE {$tb} SET beginGuide = {$stage} WHERE mid = {$uid} LIMIT 1";
        oo::commonOprDb('ghgames')->query($sql);
        if($redisStage<$stage){
            oo::commonOprModel('member')->optProperty($uid,['spins'=>1],'-',101,"新手引导");
            oo::commonOprRedis('common')->hSet(okeys::beginGuide(),$uid,$stage);
            switch ($stage){
                case 1:
//                    $money = 0;
//                    oo::commonOprModel('member')->optPropertySet($uid,['money'=>$money],"新手引导");
                    oo::commonOprModel('member')->optProperty($uid,['money'=>10000],'+',101,"新手引导");
                    break;
                case 2:
                    oo::commonOprModel('member')->optProperty($uid,['money'=>80000],'+',101,"新手引导");
                    break;
                case 3:
                    oo::commonOprModel('member')->optProperty($uid, ['money' => 20000], '+',101,"新手引导");
                    break;
                case 4:
                    oo::commonOprModel('member')->optProperty($uid, ['money' => 150000], '+',101,"新手引导");
//                    oo::commonOprModel('member')->saveStatisticsByArray($uid, [
//                        'coinbya' => 150000,
//                        'damage' => 1
//                    ]);
                    break;
                case 5:
                    oo::commonOprModel('member')->optProperty($uid, ['money' => 5000], '+',101,"新手引导");
                    break;
                case 6:
                    oo::commonOprModel('member')->optProperty($uid, ['money' => 290000], '+',101,"新手引导");
//                    oo::commonOprModel('member')->saveStatisticsByArray($uid, [
//                        'allstealnum' => 1,
//                        'stealnum' => 1,
//                        'stealcoin' => 290000,
//                    ]);
                    break;
                case 7:
                    oo::commonOprModel('member')->optProperty($uid,['money'=>2000],'+',101,"新手引导");
                    break;
                case 8:
                    oo::commonOprModel('member')->optProperty($uid,['money'=>2000],'+',101,"新手引导");
                    oo::commonOprModel('member')->optProperty($uid,['spins'=>10],'+',101,"新手引导");
                    break;
                case 9:
                    oo::commonOprModel('member')->optProperty($uid,['money'=>80000],'+',101,"新手引导");
//                    oo::commonOprModel('member')->saveStatistics($uid,'damaged',1);
                    // 破壞建築
                    $tb  = otable::userlevel($uid);
                    $sql = "SELECT * FROM {$tb} WHERE levelId=(SELECT Max(levelId) as levelId FROM {$tb} WHERE uid={$uid}) AND uid={$uid}";
                    $ret = oo::commonOprDb('userlevel')->getOne($sql,MYSQLI_ASSOC);
                    $levelId = $ret['levelId'];
                    $element = json_decode($ret['element'],true);
                    foreach ($element as $key=>$row){
                        if($row['level']>0 && $row['status'] == 0){
                            $element[$key]['status'] = 1;
                            break;
                        }else if($row['level']>0 && $row['status'] == 1){
                            $element[$key]['status'] = 0;
                            $element[$key]['level'] = $element[$key]['level'] - 1;
                        }
                    }
                    oo::commonOprModel('member')->setUserLevel($uid,$levelId,$element);
                    break;
                case 10:
                    oo::commonOprModel('member')->optProperty($uid,['money'=>1000],'+',101,"新手引导");
                    break;
                case 11:
                    oo::commonOprModel('member')->optProperty($uid,['money'=>150000],'+',101,"新手引导");
//                    oo::commonOprModel('member')->saveStatisticsByArray($uid,[
//                        'coinbya' => 150000,
//                        'damage'  => 1
//                    ]);
                    break;
                case 12:
                    oo::commonOprModel('member')->optProperty($uid,['money'=>20000],'+',101,"新手引导");
                    break;
                case 13:
                    oo::commonOprModel('member')->optProperty($uid,['money'=>5000],'+',101,"新手引导");
                    break;
                case 14:
                    oo::commonOprModel('member')->optProperty($uid,['shield'=>1],'+',101,"新手引导");
                    oo::commonOprModel('member')->optProperty($uid,['money'=>50000],'+',101,"新手引导");
                    break;
                case 15:
                    oo::commonOprModel('member')->optProperty($uid,['money'=>80000],'+',101,"新手引导");
                    break;
                case 16:
                    oo::commonOprModel('member')->optProperty($uid,['money'=>1000],'+',101,"新手引导");
                    break;
                case 17:
                    oo::commonOprModel('member')->optProperty($uid,['money'=>2000],'+',101,"新手引导");
                    break;
                case 18:
                    oo::commonOprModel('member')->optProperty($uid,['money'=>5000],'+',101,"新手引导");
                    break;
            }
        }
        switch ($stage){
            case 1:
                $out['data'] = ['type'=>1, 'rid'=>5, 'rewardId'=>1, 'num'=>10000, 'curSpins'=>7];
                break;
            case 2:
                $out['data'] = ['type'=>1, 'rid'=>5, 'rewardId'=>6, 'num'=>80000, 'curSpins'=>6];
                break;
            case 3:
                $out['data'] = ['type'=>1, 'rid'=>9, 'rewardId'=>5, 'num'=>20000, 'curSpins'=>5];
                break;
            case 4:
                $out['data'] = ['rid'=>1, 'type'=>4,
                    'attackInfo'=>[
                        'levelId'=>1,
                        'element'=>[
                            ['id'=>1,'level'=>5,'maxLevel'=>5,'status'=>0],
                            ['id'=>2,'level'=>5,'maxLevel'=>5,'status'=>0],
                            ['id'=>3,'level'=>4,'maxLevel'=>5,'status'=>0],
                            ['id'=>4,'level'=>1,'maxLevel'=>3,'status'=>0],
                            ['id'=>5,'level'=>3,'maxLevel'=>5,'status'=>0],
                        ],
                        'createTime'=>time(), 'completionTime'=>null, 'is_reward'=>0, 'toUid'=>10000, 'guideCoins'=>150000, 'nick'=>"King", 's_picture'=>1,
                    ],
                    'lastTime'=>null, 'curSpins'=>4,
                ];
                break;
            case 5:
                $out['data'] = ['type'=>1, 'rid'=>8, 'rewardId'=>4, 'num'=>5000, 'curSpins'=>3];
                break;
            case 6:
                $out['data'] = ['rid'=>2, 'type'=>3,
                    'stealInfo'=>[
                        'levelId'=>3,
                        'element'=>[
                            ['id'=>1,'level'=>5,'maxLevel'=>5,'status'=>0],
                            ['id'=>2,'level'=>5,'maxLevel'=>5,'status'=>0],
                            ['id'=>3,'level'=>4,'maxLevel'=>5,'status'=>0],
                            ['id'=>4,'level'=>1,'maxLevel'=>3,'status'=>0],
                            ['id'=>5,'level'=>3,'maxLevel'=>5,'status'=>0],
                        ],
                        'createTime'=>time(), 'completionTime'=>null, 'is_reward'=>0, 'toUid'=>10000, 'coins'=>290000, 'nick'=>"King", 's_picture'=>1, 'u_flag'=>1, 'guideCoins'=>[174000,87000,29000]
                    ],
                    'lastTime'=>null, 'curSpins'=>2, 'coins'=>[174000,87000,29000]
                ];
                break;
            case 7:
                $out['data'] = ['type'=>1, 'rid'=>5, 'rewardId'=>1, 'num'=>2000, 'curSpins'=>1];
                break;
            case 8:
                $out['data'] = ['type'=>2, 'rid'=>3, 'num'=>10, 'curSpins'=>10, 'lastTime'=>null];
                break;
            case 9:
                $out['data'] = ['type'=>1, 'rid'=>5, 'rewardId'=>6, 'num'=>80000, 'curSpins'=>9];
                $userAssert = oo::commonOprModel('member')->getUserAssetsInfo($uid);
                oo::commonOprModel('Workerman')->push($uid,ocmd::$SVR_SELF_ATTACK_MESSAGE,array('nick'=>'KING','u_flag'=>1,'url'=>1,'shield'=>$userAssert['shield'],'isDefense'=>3,'star'=>100));
                break;
            case 10:
                $out['data'] = ['type'=>1, 'rid'=>5, 'rewardId'=>1, 'num'=>1000, 'curSpins'=>8];
                break;
            case 11:
                $out['data'] = ['rid'=>1, 'type'=>4, 'attackInfo'=>[
                    'levelId'=>2,
                    'element'=>[
                        ['id'=>1,'level'=>5,'maxLevel'=>5,'status'=>0],
                        ['id'=>2,'level'=>5,'maxLevel'=>5,'status'=>0],
                        ['id'=>3,'level'=>4,'maxLevel'=>5,'status'=>0],
                        ['id'=>4,'level'=>1,'maxLevel'=>3,'status'=>0],
                        ['id'=>5,'level'=>3,'maxLevel'=>5,'status'=>0],
                    ],
                    'createTime'=>time(), 'completionTime'=>null, 'is_reward'=>0, 'toUid'=>10001, 'guideCoins'=>150000, 'nick'=>"Queen", 's_picture'=>1,
                    'enemyInfo'=>[
                        'levelId'=>2,
                        'element'=>[
                            ['id'=>1,'level'=>5,'maxLevel'=>5,'status'=>0],
                            ['id'=>2,'level'=>5,'maxLevel'=>5,'status'=>0],
                            ['id'=>3,'level'=>4,'maxLevel'=>5,'status'=>0],
                            ['id'=>4,'level'=>5,'maxLevel'=>5,'status'=>0],
                            ['id'=>5,'level'=>4,'maxLevel'=>5,'status'=>0],
                        ],
                        'createTime'=>time(), 'completionTime'=>null, 'is_reward'=>0, 'guideCoins'=>10000, 'coins'=>150000, 'nick'=>"King", 's_picture'=>1,
                    ]
                ],
                    'lastTime'=>null,
                    'curSpins'=>7,
                ];
                break;
            case 12:
                $out['data'] = ['type'=>1, 'rid'=>9, 'rewardId'=>5, 'num'=>20000, 'curSpins'=>6];
                break;
            case 13:
                $out['data'] = ['type'=>1, 'rid'=>8, 'rewardId'=>4, 'num'=>5000, 'curSpins'=>5];
                break;
            case 14:
                $out['data'] = ['type'=>5, 'rid'=>4, 'num'=>1, 'curSpins'=>4];
                break;
            case 15:
                $out['data'] = ['type'=>1, 'rid'=>5, 'rewardId'=>6, 'num'=>80000, 'curSpins'=>3];
                break;
            case 16:
                $out['data'] = ['type'=>1, 'rid'=>5, 'rewardId'=>1, 'num'=>1000, 'curSpins'=>2];
                break;
            case 17:
                $out['data'] = ['type'=>1, 'rid'=>5, 'rewardId'=>1, 'num'=>2000, 'curSpins'=>1];
                break;
            case 18:
                $out['data'] = ['type'=>1, 'rid'=>8, 'rewardId'=>4, 'num'=>5000, 'curSpins'=>0];
                break;
        }
        return json_encode($out);
    }

    /**
     * 结束新手引导
     * @param $param
     * @return false|string
     * Created by: Owen
     * Created on: 2019/8/29 15:15
     * Description:
     */
    public function endGuide($param){
        $uid = oo::functions()->uint($param['uid']);
        if(empty($uid)){
            return oo::response(-1);
        }
        $dump  = oo::functions()->escape($param['dump']);
        $build = $param['build'];
        return oo::commonOprModel('member')->endGuide2($uid,$build, $dump);
    }

    /**
     * 删除云端数据
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/3/27 20:10
     */
    public function delFromCloud($param){
        $uid = oo::functions()->uint($param['uid']);
        if( empty($uid) ){
            return oo::response(-1);
        }
        $key = $param['keys'];
        $key = (array)json_decode($key,true);
        $userDynamicsData = oo::commonOprModel('member')->getUserDynamicsData($uid);
        $userDynamicsData = (array)$userDynamicsData['userCache'];
        foreach ($key as $row){
            oo::commonOprRedis('common')->hDel(okeys::UserCache($uid),$row);
            unset($userDynamicsData[$key]);
        }
        oo::commonOprModel('member')->updateUserDynamicsData($uid,['userCache'=>(array)$userDynamicsData]);

        return oo::response(1,$ret??[]);
    }

    /**
     * 获取云端全部数据
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/3/27 20:13
     */
    public function getAllFromCloud($param){
        $uid = oo::functions()->uint($param['uid']);
        if( empty($uid) ){
            return oo::response(-1);
        }
        $ret = oo::commonOprRedis('common')->hGetAll(okeys::UserCache($uid));
        foreach ($ret as $key=>$row){
            if(!$row){
                $ret[$key] = null;
            }else{
                $ret[$key] = json_decode($row);
            }
        }

        $userDynamicsData = oo::commonOprModel('member')->getUserDynamicsData($uid);
        $userDynamicsData = (array)$userDynamicsData['userCache'];
        foreach ($userDynamicsData as $key=>$row){
            !$row ? $ret[$key] = null : $ret[$key] = $row;
        }

        return oo::response(1,$ret??[]);
    }

    /**
     * 设置云端数据
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/3/27 20:29
     */
    public function setToCloud($param){
        $uid = oo::functions()->uint($param['uid']);
        if(empty($uid)){
            return oo::response(-1);
        }
        $KVDataList = $param['KVDataList'];
        $KVDataList = json_decode($KVDataList,true);
        $mSet = [];
        foreach ($KVDataList as $key=>$row){
            if(is_array($row)){
                $row = json_encode($row, JSON_UNESCAPED_UNICODE);
            }
            $mSet[$key]= stripcslashes($row);
        }
        oo::commonOprRedis('common')->hMset(okeys::UserCache($uid),$mSet);

        $userDynamicsData = oo::commonOprModel('member')->getUserDynamicsData($uid);
        $userDynamicsData = (array)$userDynamicsData['userCache'];
        foreach ($KVDataList as $key=>$row){
            $userDynamicsData[$key] = $row;
        }
        oo::commonOprModel('member')->updateUserDynamicsData($uid,['userCache'=>(array)$userDynamicsData]);

        return oo::response(1);
    }

    /**
     * 获取仇人列表
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/3/27 20:17
     */
    public function enemyList($param){
        $uid = oo::functions()->uint($param['uid']);
        if(empty($uid)){
            return oo::response(-1);
        }
//        $enemyList   = oo::commonOprModel('member')->getDefaultEnemyList($uid);//过滤后的仇人列表
//        $myEnemyList = oo::commonOprModel('member')->getMyEnemyList($uid);
//        $enemyList   = array_values($enemyList);
//        $i = 0;
//        foreach ($enemyList as $k => $v){
//            $listInfo[$k] = oo::commonOprModel('member')->getUserBasic($v);
//            if(!empty($myEnemyList) && in_array($v,$myEnemyList) && $i != 0){
//                $listInfo[$k]['type'] = 2;
//            }else{
//                $listInfo[$k]['type'] = 1;
//            }
//            $i++;
//        }
        $enemyList = oo::commonOprModel('attack')->getEnemyList($uid);
        foreach ($enemyList as $k => $v){
            $listInfo[$k] = oo::commonOprModel('member')->getUserBasic($v);
            if($k==0){
                $listInfo[$k]['type'] = 1;
                $robotList = oo::commonOprModel("robot")->getAllRobot();
                if(in_array($v, $robotList)){
                   $info= oo::commonOprModel('attack')->getRobotInfo($uid);
                    $listInfo[$k]['element'] = $info['element'];
                    $listInfo[$k]['levelId'] = $info['levelId'];
                    $listInfo[$k]['star'] = $info['star'];
                }
            }else{
                $listInfo[$k]['type'] = 2;
            }
        }
        return oo::response(1,array_values($listInfo),"",true);
    }
    /**
     * 触发偷取对象
     * @param $param
     * @return false|string
     * Created by: Owen
     * Created on: 2019/6/12 11:39
     * Description:
     */
    public function richestKing($param){
        $uid = oo::functions()->uint($param['uid']);
        $bet = oo::functions()->uint($param['bet']);
        if(empty($uid)){
            return oo::response(-1);
        }
        if(empty($bet)){
            $bet = 1;
        }
        return oo::commonOprModel('user')->getRichestKing($uid,$bet);
    }

    /**
     * 用户统计
     * @param $param
     * @return false|string
     * Created by: Owen
     * Created on: 2019/8/29 15:14
     * Description:
     */
    public function satistics($param){
        $uid = oo::functions()->uint($param['uid']);
        if(empty($uid)){
            return oo::response(-1);
        }
        $data = [];
//        $statistics = oo::commonOprModel('member')->getStatistics($uid);
        $outputKey  = [
            'coins'     =>"",
            'money'     =>"",
            'damage'    =>"",
            'destroy'   =>"",
            'coinbya'   =>"",
            'stealnum'  =>"",
            'allstealnum'=>"stealperfect",
            'stealcoin' =>"",
            'defense'   =>"",
            'damaged'   =>"",
            'destroyed' =>"",
            'stealed'   =>"",
        ];
//        foreach ($outputKey as $key=>$row){
//            (empty($row))?$data[$key] = $statistics[$key]:$data[$row] = $statistics[$key];
//        }
//        if(isset($data['money'])){
//            $data['coins'] = $data['money'];
//        }
        $data = $outputKey;
        return oo::response(1,$data,"",true);
    }

    /**
     * 修改用户信息
     * @param $param
     * @return false|int|string
     * Created by: Owen
     * Created on: 2020/3/27 20:23
     */
    public function modifyInfo($param) {
        $uid = oo::functions()->uint($param['uid']);
        if(empty($uid)){
            return oo::response(-1);
        }
        $updateInfo = array();
        //更改昵称
        $nick = isset($param['nick']) ? htmlspecialchars($param['nick']) : '';
        if (!empty($nick)) {
            $code = mb_detect_encoding($nick);
            if (strtolower($code) != 'utf-8') {
                $nick = mb_convert_encoding($nick, 'UTF-8');
            }
            if (mb_strlen($nick) > 127) { // 留一位做全是单音节以.结束
                $nick = mb_substr($nick, 0, 127);
            }
            $nick = oo::functions()->escapeNick($nick);
            $updateInfo['nick'] = addslashes($nick);
        }
        //更改签名
        $signature = isset($param['signature']) ? htmlspecialchars($param['signature']) : '';
        if (!empty($signature)) {
            $code = mb_detect_encoding($nick);
            if (strtolower($code) != 'utf-8') {
                $signature = mb_convert_encoding($signature, 'UTF-8');
            }
            $updateInfo['signature'] = addslashes($signature);
        }
        //更改旗帜
        $u_flag = isset($param['u_flag']) ? htmlspecialchars($param['u_flag']) : '';
        if (!empty($u_flag)) {
            $f_id = oo::functions()->uint($u_flag);
            if($f_id && $f_id>0){
                $updateInfo['u_flag'] = (int)$f_id;
            }
        }
        //更改性别
        $sex = isset($param['sex']) ? htmlspecialchars($param['sex']) : '';
        if (!empty($sex)) {
            if(in_array($sex,['f','m'])){
                $updateInfo['sex'] = $sex;
            }
        }
        //更改头像
        $s_picture = isset($param['s_picture']) ? htmlspecialchars($param['s_picture']) : '';
        if (!empty($s_picture)) {
            if(is_numeric($s_picture)){
                $picture_id = oo::functions()->uint($s_picture);
                if($picture_id && $picture_id>0){
                    $updateInfo['s_picture'] = (int)$picture_id;
                }
            }else{
                $preg = "/^http(s)?:\\/\\/.+/";
                if(preg_match($preg,$s_picture))
                {
                    $updateInfo['s_picture'] = $s_picture;
                }
            }
        }
        //更改头像框
        $updateInfo['avatar'] = isset($param['avatar']) ? intval($param['avatar']) :0;
        $result = oo::commonOprModel('member')->updateUserInfo($uid, $updateInfo);
        $userInfo = oo::commonOprModel('user')->showUserInfo($uid);
        return oo::response($result?1:-2,$userInfo,$result?"修改成功":"修改失败",true);
    }

    /**
     * 上报firebase Token
     * @param $param
     * @return mixed
     * Created by: Owen
     * Created on: 2020/3/27 20:27
     */
    public function firebaseToken($param){
        return oo::commonOprModel('user')->recordPushToken($param);
    }

    /**
     * 设置新手引导阶段
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/3/27 20:30
     */
    public function setGuide($param){
        $uid   = oo::functions()->uint($param['uid']);
        if(empty($uid)){
            return oo::response(-1);
        }
        $stage = 18;
        $tb = otable::gh_gameserver($uid);
        $sql = "SELECT beginGuide FROM {$tb} WHERE mid={$uid} LIMIT 1";
        $ret = oo::commonOprDb('ghgames')->getOne($sql,1);
        if($ret['beginGuide'] >= 19){
            return oo::response(-1);
        }
        $sql = "UPDATE {$tb} SET beginGuide = {$stage} WHERE mid = {$uid} LIMIT 1";
        oo::commonOprDb('ghgames')->query($sql);
        oo::commonOprRedis('common')->hSet(okeys::beginGuide(),$uid,$stage);
        return oo::response(1);
    }

    /**
     * 消息列表
     * @param $param
     * @return false|string
     * Created by: Owen
     * Created on: 2020/3/27 20:33
     */
    public function news($param){
        $uid = oo::functions()->uint($param['uid']);
        if(empty($uid)){
            return oo::response(-1);
        }
        $data = oo::commonOprModel('message')->getMessageList($uid);
        return json_encode($data,JSON_NUMERIC_CHECK);
    }

    /**
     * 设置已读状态
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/12/25 15:21
     */
    public function setNewsReadStatus($param){
        $uid = oo::functions()->uint($param['uid']);
        if(empty($uid)){
            return oo::response(-1);
        }
        return oo::commonOprModel('message')->changeMessageReadStatus($uid);
    }

    /**
     * 用户设置
     * @param $param
     * @return false|string
     * Created by: Owen
     * Created on: 2019/8/29 15:14s
     * Description:
     */
    public function setting($param){
        $uid = oo::functions()->uint($param['uid']);
        $type = oo::functions()->escape($param['type']);
        $lock = max(oo::functions()->uint($param['lock']),0);
        if(empty($uid) || empty($type)){
            return oo::response(-1);
        }
        if($type == 'ALL'){
            if($lock == 0){
                oo::commonOprModel('member')->updateUserDynamicsData($uid,['pushLock'=>""]);
            }else if($lock == 1){
                oo::commonOprModel('member')->updateUserDynamicsData($uid,['pushLock'=>"stealAndAttack,ALL"]);
            }
        }else{
            $pushLock = oo::commonOprModel('member')->getUserSettingLock($uid);
            if($lock == 0){
                foreach ($pushLock as $key=>$row){
                    if($row == $type){
                        unset($pushLock[$key]);
                    }
                }
            }else if($lock == 1){
                $pushLock[] = $type;
            }
            $pushLock   = implode(',',array_unique($pushLock));
            oo::commonOprModel('member')->updateUserDynamicsData($uid,['pushLock'=>$pushLock]);
        }
        return oo::response(1);
    }

    /**
     * 账号映射
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/5/22 18:11
     */
    public function accountSync($param){
        $uid           = oo::functions()->uint($param['uid']);
        $guestUid      = oo::functions()->uint($param['guestUid']);
        $transformType = oo::functions()->uint($param['transformType']);
        $macAddress    = $param['macAddress'];
        $token         = $param['fbToken'];
        $deviceId      = $param['deviceId'] ?? "";
        if(empty($uid) || empty($guestUid) || empty($transformType) || empty($macAddress) || empty($token)){
            return oo::response(-1);
        }
        if(!funs::concurrentLock($uid,"ACCOUNTSYNC",6)){
            return oo::response(-2);
        }
        $fbUid = oo::functions()->uint($param['fbUid']);
        if(!empty($fbUid)){
            $uid = $fbUid;
        }
        return oo::commonOprModel('user')->accountSynchronize($transformType,$macAddress,$token,$uid,$guestUid,$deviceId);
    }

    /**
     * 获取玩家token
     */
    public function getToken($param) {
        $uid = oo::functions()->uint($param['uid']);
        $token = oo::commonOprRedis('common')->get(okeys::UserToken($uid));
        return oo::response(1,['saToken' => $token]);
    }

    /**
     * 支付数据
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/6/28 15:06
     */
    public function getUserPayNum($param){
        $uid     = oo::functions()->uint($param['uid']);
        $giftDay = oo::functions()->uint($param['giftDay']);
        $payDay  = oo::functions()->uint($param['payDay']);
        if(empty($uid) || empty($giftDay) || empty($payDay)){
            return oo::response(-1);
        }
        $data = [
            'giftNum'    => oo::commonOprModel('payment')->getBuyGiftNum($uid,$giftDay),
            'payNum'     => oo::commonOprModel('payment')->getUserTotalPayNum($uid,$payDay),
            'payTotal'   => oo::commonOprModel('payment')->getUserTotalPayNum($uid),
            'lastPayTime'=> oo::commonOprModel('payment')->getUserLastPayTime($uid),
            'payTotalTimes'=> intval(oo::commonOprModel('payment')->getUserPayTimes($uid)),
        ];
        return oo::response(1,$data);
    }

    /**
     * 设置email
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/6/28 15:28
     */
    public function setUserEmail($param){
        $uid   = oo::functions()->uint($param['uid']);
        $email = oo::functions()->escape($param['email']);
        if(empty($uid) || empty($email)){
            return oo::response(-1);
        }
        if(funs::isEmail($email)){
            oo::commonOprModel('member')->getUserInfo($uid, NULL, TRUE,['semail'=>$email]);
            return oo::response(1);
        }
        return oo::response(-2,[],"email 输入格式异常");
    }

    /**
     * 账号绑定
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/8/24 16:34
     */
    public function accountBind($param){
        $uid        = oo::functions()->uint($param['uid']);
        $macAddress = $param['macAddress'];
        $fbToken    = $param['fbToken'];
        $appleUserId= $param['appleUserId'];
        $appleIdentityToken = $param['appleIdentityToken'];
        $deviceId      = $param['deviceId'] ?? "";
        if(empty($uid) || empty($fbToken) || empty($macAddress)){
            return oo::response(-1);
        }
        $del = true;
        if(!empty($appleUserId) && !empty($appleIdentityToken)){
            $ret = oo::commonOprModel('ios')->appleJwtVerify($appleIdentityToken);
            $auds = ['com.astak.crazyspin', 'com.newsky.crazycoin'];
            if(!$ret['status'] || !in_array($ret['data']['aud'], $auds) || $ret['data']['sub'] != $appleUserId){
                return oo::response(-2,[],'apple 用户验证异常');
            }
            $macAddress = base64_encode("{$appleUserId}_superant");
            $del = false;
        }
        return oo::commonOprModel('user')->accountBind($uid,$macAddress,$fbToken,$del,$deviceId);
    }

    /**
     * 邀请成功的用户上报数数
     */
    public function newAdjustReport($param) {
        $deviceId  = oo::functions()->escape($param['deviceId']);
        $uid = oo::functions()->uint($param['uid']);
        if(empty($deviceId) || strlen($deviceId) > 36 || empty($uid)){
            return oo::response(errorCode::REQUEST_PARAM_ERROR);
        }

        $info = oo::commonOprModel('member')->getUserInfo($uid);
        if(empty($info)){
            return oo::response(errorCode::USER_NOT_EXISTS);
        }

        $ret = oo::commonOprModel('member')->getDataFromAdjust($deviceId);
        $taData = [];
        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';
            }
            $taData = [
                "network_name"  =>$ret['network_name'],
                "campaign_name" =>$ret['campaign_name'],
                "adgroup_name"  =>$ret['adgroup_name'],
                "creative_name" =>$ret['creative_name'],
                "network_id"    => oo::commonOprModel('member')->getAdjustChannel($ret['network_name']),
                "campaign_id"   => oo::commonOprModel('member')->getAdjustId($ret['campaign_name']),
                "adgroup_id"    => oo::commonOprModel('member')->getAdjustId($ret['adgroup_name']),
                "creative_id"   => oo::commonOprModel('member')->getAdjustId($ret['creative_name']),
            ];
            !empty($group) && $taData["user_source"] = 2;
        }
        $labelUid = intval(base64_decode($ret['label']));
        if($labelUid > 0 || (IS_DEBUF && $labelUid != 0)){
            $taData["user_source"] = 1;
        }
        !empty($taData) && oo::commonOprModel('ta')->setEventData($uid,'user_set',$taData);
        return oo::response(errorCode::SUCCESS);
    }
}
