<?php
/**
 * Created by PhpStorm.
 * User: xmy 280564871@qq.com
 * Date: 2017/3/24
 * Time: 10:54
 */
namespace App\Model;

use Org\UcenterSDK\Ucservice;

class UserModel extends BaseModel {

	protected $_auto = [
		['password', 'think_psw_md5', self::MODEL_BOTH, 'function', UC_AUTH_KEY],
	];

	/**
	 * 获取用户信息
	 * @param $user_id
	 * @param array $field
	 * @return mixed
	 * author: xmy 280564871@qq.com
	 */
	public function getUserInfo($account,$field=['id','account','nickname','promote_id','phone','balance','head_img','ifnull(sex,0) as sex','point','idcard','real_name','age_status','alipay','alipay_real_name','point','gold_coin']){
		$map['account'] = $account;
		$data = $this->field($field)->where($map)->find();
		if (empty($data)){
			return $data;
		}
		
		if(is_numeric($data['head_img'])){
			$head_img = get_img_url($data['head_img']);
			$data['head_img'] = $head_img == false?"":$head_img;
		}
		
		/* $head_img = get_img_url($data['head_img']);
		$data['head_img'] = $head_img;
		if($head_img == false){
			$data['head_img'] = "";
		} */
        if ($data['age_status']==0 && !empty($data['idcard']) && !empty($data['real_name'])){
            $data['age_status'] = 4;
        }
        $pointmap['type_id']=5;
	    $pointmap['user_id']=get_user_id($account);
	    $find=M('point_record','tab_')->field('id')->where('create_time'.total(1))->where($pointmap)->find();
	     $data['age_status']=empty($data['age_status'])?0:$data['age_status'];
	    if(null==$find){
	    	$data['status']=1;//未签到
	    }else{
	    	$data['status']=0;//已签到
	    }		

			if(empty($data['phone'])) {
				$data['phone'] = '';
			}
			if(empty($data['real_name'])) {
				$data['real_name'] = '';
			}
			
			
			/* 通过充值量设置等级 */			
			
			
			$money = 0;			
			
			
			$money += M('deposit','tab_')->where(['pay_status'=>1,'user_id'=>$data['id']])->sum('pay_amount');			
			
			
			$money += M('spend','tab_')->where(['pay_status'=>1,'user_id'=>$data['id']])->sum('pay_amount');						
			
			
			$data['vip_level'] = get_vip_level($money);		


			
		return $data;
	}

	/**
	 * 获取用户信息
	 * @param $account
	 * @param array $filed
	 * @return mixed
	 * author: xmy 280564871@qq.com
	 */
	public function getUserByAccount($account,$filed=['*']){
		$map['account'] = $account;
		$data = $this->field($filed)->where($map)->find();
		return $data;
	}

	/**
	 * 更新用户信息
	 * @param $account
	 * @param string $nickname
	 * @param string $sex
	 * @param string $password
	 * @return bool
	 * author: xmy 280564871@qq.com
	 */
	public function updateUserInfo($account,$nickname="",$sex=""){
		$data['account'] = $account;
		empty($nickname) || $data['nickname'] = $nickname;
		$sex == "" || $data['sex'] = $sex;
		return $this->where(['account'=>$account])->save($data);
	}

	/**
	 * 修改密码
	 * @param $phone
	 * @param $old_pwd  旧密码
	 * @param $new_pwd  新密码
	 * @return bool
	 * author: xmy 280564871@qq.com
	 */
	public function changePwd($account, $old_pwd, $new_pwd)
	{
		
		$user = $this->getUserByAccount($account);

		if(!empty($user) &&think_psw_md5($old_pwd, UC_AUTH_KEY) !== $user['password']){
			$result=2;	
		}else if(!empty($user) && think_psw_md5($old_pwd, UC_AUTH_KEY) === $user['password']){
			$user['password'] = think_psw_md5($new_pwd, UC_AUTH_KEY);
			$result = $this->save($user);
		}


		return $result;
	}

	/**
	 * 忘记密码
	 * @param $account
	 * @param $pwd
	 * @return bool
	 * author: xmy 280564871@qq.com
	 */
	public function forgetPwd($account,$pwd){
		
		$user = $this->getUserByAccount($account);
		if(!empty($user)){
			$user['password'] = think_psw_md5($pwd, UC_AUTH_KEY);
			$result =  $this->save($user);
		}
		return $result;
	}

	/**
	 * 修改UC密码
	 * @param $account
	 * @param string $old_pwd   旧密码
	 * @param $new_pwd          新密码
	 * @param $type             0 修改密码 1 忘记密码
	 * @return bool
	 * author: xmy 280564871@qq.com
	 */
	private function changeUcPwd($account,$old_pwd="",$new_pwd,$type){
		//修改UC密码
		if(C('UC_SET') == 1){
			$uc = new Ucservice();
			$data_uc = $uc->get_uc($account);
			if (is_array($data_uc)) {
				$result = $uc->uc_edit($account, $old_pwd, $new_pwd,'',$type);
				if($result < 0){
					return false;
				}
				return true;
			}else{
				return true;
			}
		}else{
			return true;
		}
	}


	/**
	 * 绑币记录
	 * @param $user_id
	 * @param $p
	 * @return mixed
	 * author: xmy 280564871@qq.com
	 */
	public function getUserBindCoin($user_id,$p,$row=10){
		$map['user_id'] = $user_id;
		$page = intval($p);
		$page = $page ? $page : 1; //默认显示第一页数据
		$data = M("user_play","tab_")
			->field("tab_game.game_name,game_id,bind_balance,icon")
			->join('tab_game on tab_game.id = tab_user_play.game_id')
			->where($map)
			->group("game_id")
			->page($page,$row)
			->select();
		return $data;
	}
	/**
	 * 绑定手机号
	 * @param $user_id
	 * @param $phone
	 * @return bool
	 * author: xmy 280564871@qq.com
	 */
	public function bindPhone($user_id,$phone){
		$data['id'] = $user_id;
		$data['phone'] = $phone;
		return $this->save($data);
	}
	
	
	public function thirdRegister($user){
		$data = $this->create($user);
		if(!$data){
			return false;
		}
		return $this->add($data);
	}
	
}