<?php


namespace app\lumaapi\service;


use app\lumaapi\model\MemberModel;
use think\Db;

class UserServices
{

    public static function registerLumaUser($user_id){
        $member=Db::name('member')->where(['user_id'=>$user_id])->fetchSql(false)->find();
        $user = Db::connect('db_con2')->name('users')->where(['user_id'=>$user_id])->find();
        $uid = $member['id'];$uid1=0;$uid2=0;
        if(empty($member) && $user_id>0){
            $uid=MemberModel::innerData($user['user_id'],$user['nickname'],$user['password'],$user['account'],
                $user['mobile'],$user['email'],$user['face']);
            $fuid1=Db::name('member')->where(['user_id'=>$user['fuid1']])->find();
            if($user['fuid1'] > 0&&$fuid1==null){
                $user1 = Db::connect('db_con2')->name('users')->where(['user_id'=>$user['fuid1']])->find();
                $uid1=MemberModel::innerData($user1['user_id'],$user1['nickname'],$user1['password'],$user1['account'],
                    $user1['mobile'],$user1['email'],$user1['face']);
            }else{
                $uid1=$fuid1['id'];
            }
            $fuid2=Db::name('member')->where(['user_id'=>$user['fuid2']])->find();
            if($user['fuid2'] > 0&&$fuid2 ==null){
                $user2 = Db::connect('db_con2')->name('users')->where(['user_id'=>$user['fuid2']])->find();
                $uid2=MemberModel::innerData($user2['user_id'],$user2['nickname'],$user2['password'],$user2['account'],
                    $user2['mobile'],$user2['email'],$user2['face']);
            }else{
                $uid2=$fuid2['id'];
            }
        }else{
            $fuid1=Db::name('member')->where(['user_id'=>$user['fuid1']])->fetchSql(false)->find();
            if($user['fuid1'] > 0 && $fuid1 ==null){
                $user1 = Db::connect('db_con2')->name('users')->where(['user_id'=>$user['fuid1']])->find();
                $uid1=MemberModel::innerData($user1['user_id'],$user1['nickname'],$user1['password'],$user1['account'],
                    $user1['mobile'],$user1['email'],$user1['face']);
            }else{
                $uid1=$fuid1['id'];
            }
            $fuid2=Db::name('member')->where(['user_id'=>$user['fuid2']])->find();
            if($user['fuid2'] > 0 &&$fuid2 ==null){
                $user2 = Db::connect('db_con2')->name('users')->where(['user_id'=>$user['fuid2']])->find();
                $uid2=MemberModel::innerData($user2['user_id'],$user2['nickname'],$user2['password'],$user2['account'],
                    $user2['mobile'],$user2['email'],$user2['face']);
            }else{
                $uid2=$fuid2['id'];
            }
        }
        self::_yaoqing($uid,$uid1?$uid1:0,$uid2?$uid2:0);
        return $uid;
    }

// 2018年2月27日 19:59:28
// 邀请
   public static  function _yaoqing($uid,$s1=0,$s0=0){
        if(!$uid ){//|| !$s1
            return;
        }
        $user=Db::name('yaoqing')->where(['uid'=>$uid])->find();
        if(!empty($user)){
            return;
        }
        // 设置师傅s1 师公s0
        Db::name('yaoqing')->insert(['uid'=>$uid,'s1'=>$s1,'s0'=>$s0]);
        // 更新师傅的徒弟数量
        Db::name('yaoqing')->where('uid',$s1)->setInc('tudi_num');
        if($s0){
            // 更新师公的徒孙数量
            Db::name('yaoqing')->where('uid',$s0)->setInc('tuxun_num');
        }

        $yaoqing=config()['yaoqing'];
        if($yaoqing['money_yaoqing']){
            // 记录徒弟奖励
            _moneyDb([
                'uid'   =>  $uid,
                'money' =>[
                    'money'     =>  $yaoqing['money_yaoqing']*100,
                    'content'   =>  '邀请码注册',
                ]
            ]);
        }
        if($yaoqing['money_yaoqing_s1']){
            // 记录师傅奖励
            _moneyDb([
                'uid'   =>  $s1,
                'money' =>[
                    'money'     =>  $yaoqing['money_yaoqing_s1']*100,
                    'content'   =>  'id:'.$uid.'成功邀请徒弟',
                ]
            ]);
        }

        if($yaoqing['money_yaoqing_s0'] && $s0){
            // 记录师公奖励
            _moneyDb([
                'uid'   =>  $s0,
                'money' =>[
                    'money'     =>  $yaoqing['money_yaoqing_s0']*100,
                    'content'   =>  'id:'.$uid.'成功邀请徒孙',
                ]
            ]);

        }
        //注册师傅的金币
        if($yaoqing['jinbi_yaoqing_s1']){
            _moneyDb([
                'uid'   =>  $s1,
                'jinbi' =>[
                    'money'     =>  $yaoqing['jinbi_yaoqing_s1'],
                    'content'   =>  'id:'.$uid.'成功邀请徒弟',
                ]
            ]);
            // 记录师傅奖励
            // Db::name('yaoqing_records')->insert(['uid'=>$uid,'content'=>'id:'.$this->member['id'].'邀请徒弟','money'=>$yaoqing['money_yaoqing_s1']*100,'time'=>$time,'ip'=>$ip]);
        }
        //注册师公的金币
        if($yaoqing['jinbi_yaoqing_s0'] && $s0){
            _moneyDb([
                'uid'   =>  $s0,
                'jinbi' =>[
                    'money'     =>  $yaoqing['jinbi_yaoqing_s0'],
                    'content'   =>  'id:'.$uid.'成功邀请徒孙',
                ]
            ]);

        }
        //注册得金币
        if($yaoqing['jinbi_yaoqing']){
            _moneyDb([
                'uid'=>$uid,
                'jinbi'=>[
                    'money'=>$yaoqing['jinbi_yaoqing'],
                    'content'=>'注册奖励金币'
                ]
            ]);

        }
        return ;
    }
}