<?php

namespace app\common\model;

use app\common\library\Auth;
use app\common\library\Sms;
use app\common\model\school\classes\Teacher;
use app\common\model\school\classes\Verification;
use fast\Random;
use think\Db;
use think\Model;

/**
 * 会员模型
 */
class User extends BaseModel
{

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    // 追加属性
    protected $append = [
        'url',
    ];

    /**
     * 获取个人URL
     * @param string $value
     * @param array  $data
     * @return string
     */
    public function getUrlAttr($value, $data)
    {
        return "/u/" . $data['id'];
    }

    /**
     * 获取头像
     * @param string $value
     * @param array  $data
     * @return string
     */
    public function getAvatarAttr($value, $data)
    {
        if (!$value) {
            //如果不需要启用首字母头像,请使用
            //$value = '/assets/img/avatar.png';
            $value = letter_avatar($data['nickname']);
        }
        return cdnurl($value,true);
    }

    /**
     * 获取会员的组别
     */
    public function getGroupAttr($value, $data)
    {
        return UserGroup::get($data['group_id']);
    }

    /**
     * 获取验证字段数组值
     * @param string $value
     * @param array  $data
     * @return  object
     */
    public function getVerificationAttr($value, $data)
    {
        $value = array_filter((array)json_decode($value, true));
        $value = array_merge(['email' => 0, 'mobile' => 0, 'wechat_miniapp' => 0], $value);
        return (object)$value;
    }

    /**
     * 设置验证字段
     * @param mixed $value
     * @return string
     */
    public function setVerificationAttr($value)
    {
        $value = is_object($value) || is_array($value) ? json_encode($value) : $value;
        return $value;
    }

    /**
     * 变更会员余额
     * @param int    $money   余额
     * @param int    $user_id 会员ID
     * @param string $memo    备注
     */
    public static function money($money, $user_id, $memo,$type='',$ext = [])
    {
        Db::startTrans();
        try {
            $user = self::lock(true)->find($user_id);
            if ($user && $money != 0) {
                $before = $user->money;
                //$after = $user->money + $money;
                $after = function_exists('bcadd') ? bcadd($user->money, $money, 2) : $user->money + $money;
                //更新会员信息
                $user->save(['money' => $after]);
                //写入日志
                MoneyLog::create(['user_id' => $user_id, 'money' => $money, 'before' => $before, 'after' => $after, 'memo' => $memo, 'ext' => $ext?:"","type" =>$type]);
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
        }
    }

    /**
     * 变更会员积分
     * @param int    $score   积分
     * @param int    $user_id 会员ID
     * @param string $memo    备注
     */
    public static function score($score, $user_id, $memo,$type='',$ext = [])
    {
        Db::startTrans();
        try {
            $user = self::lock(true)->find($user_id);
            if ($user && $score != 0) {
                $before = $user->score;
                $after = $user->score + $score;
                $level = self::nextlevel($after);
                //更新会员信息
                $user->save(['score' => $after, 'level' => $level]);
                //写入日志
                ScoreLog::create(['user_id' => $user_id, 'score' => $score, 'before' => $before, 'after' => $after, 'memo' => $memo, 'ext' => $ext?:"","type" =>$type]);
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
        }
    }

    /**
     * 根据积分获取等级
     * @param int $score 积分
     * @return int
     */
    public static function nextlevel($score = 0)
    {
        $lv = array(1 => 0, 2 => 30, 3 => 100, 4 => 500, 5 => 1000, 6 => 2000, 7 => 3000, 8 => 5000, 9 => 8000, 10 => 10000);
        $level = 1;
        foreach ($lv as $key => $value) {
            if ($score >= $value) {
                $level = $key;
            }
        }
        return $level;
    }


    /**是否有核销权限
     * @return boolean
     */
    public static function verificationAuth($user_id){
        $have_auth = false;
        //判断用户是否有核销权限
        //判断是否是员工
        $verification = Verification::where("user_id",$user_id)->where("status",'1')->find();
        if($verification)$have_auth = true;
        //判断是否是教师
        $teacher = Teacher::where("user_id",$user_id)->find();
        if($teacher)$have_auth = true;
      return $have_auth;
    }

    /**是否有老师权限
     * @return boolean
     */
    public static function teacherAuth($user_id){
        $have_auth = false;
        //判断是否是教师
        $teacher = Teacher::where("user_id",$user_id)->find();
        if($teacher)$have_auth = true;
        return $have_auth;
    }




    /**检测用户设置密码情况
     * @param $user_id
     * @param $password
     * @param bool $check
     */
    public static function checkPayPassword($user_id,$password=null,$check=true){
        $user = self::where('id',$user_id)->find();
        if(!$user) throw new \Exception("用户不存在!");
//       if(!$user["mobile"]) throw new \Exception("您未绑定手机号,请您先去绑定手机号!");
        $mobile = $user["mobile"];
        $auth = Auth::instance();
        //是否设置支付密码
        $is_setting = false;
        //密码是否正确
        $is_right = false;
        //检测是否设置支付密码
        if($user['pay_password'])$is_setting = true;
        if($check){
            //校验密码是否正确
            $pay_password = $auth->getEncryptPassword($password, $user['pay_salt']);
            if($user['pay_password']==$pay_password)$is_right = true;
        }
        return compact('is_setting','is_right','mobile');
    }



    /** 设置用户支付密码
     * @param $user_id
     * @param $code
     * @param $password
     * @param bool $trans
     * @return bool
     * @throws \Exception
     */
    public static function setPayPassword($user_id, $code,$password,$trans=false,$check_sms=true){
        $user = self::where('id',$user_id)->find();
        if(!$user) throw new \Exception("用户不存在!");
        if($check_sms){
            $ret = Sms::check($user['mobile'], $code, 'pay_password');
            if (!$ret)throw new \Exception("验证码错误");
        }
        if(!is_numeric($password) || strlen($password)!=6)throw new \Exception("支付密碼只能設置六位數字!");
        //判断逻辑
        if($trans){
            self::beginTrans();
        }
        $res = true;
        try{
            $auth = Auth::instance();
            //事务逻辑
            $user['pay_salt'] =   Random::alnum();
            $user['pay_password'] = $auth->getEncryptPassword($password, $user['pay_salt']);
            $user->save();
            //清除验证码
            if($check_sms)Sms::flush($user['mobile'], 'pay_password');
            if($trans){
                self::commitTrans();
            }
        }catch (\Exception $e){
            if($trans){
                self::rollbackTrans();
            }
            throw new \Exception($e->getMessage());
        }
        return $res;
    }




    /**添加用户:通过 手机号 和昵称
     * @param $mobile
     * @param $nickname
     */
    public function addUserByMobile($mobile,$nickname,$check=true){
        //去除空格
        $nickname = trim($nickname);
        $mobile = trim($mobile);
        if(!is_numeric($mobile))throw new \Exception("不是合法手机号!");
        if(strlen($mobile)!=11)throw new \Exception("请输入11位手机号!");
        if($check){
            if (!preg_match("/[\x7f-\xff]/", $nickname)) throw new \Exception("名称必须是汉字,请去除空格和其他特殊符号!");
            preg_match_all("/[\x{4e00}-\x{9fa5}]/u","$nickname",$result);
            if(!$result || join('',$result[0])!=$nickname)throw new \Exception("名称必须是汉字,请去除空格和其他特殊符号!");
        }


        //判断用户存不存在
        $user = self::where("mobile",$mobile)->find();
        if($user){
            //如果存在,直接返回
            return $user;
        }else{
            //如果不存在,创建并返回
            //得到认证实例
            $auth = Auth::instance();
            //拼装提交参数
//            $extend = $this->getUserDefaultFields();
            $extend=[];
            $extend['nickname'] = $nickname;
            $extend['avatar'] =  config("site.default_avatar");
            //调用注册方法
            $user = $auth->registerNoLogin("{$mobile}_user", rand(100000,999999), "{$mobile}@user.com", $mobile,$extend);
            if(!$user)throw new \Exception($auth->getError());
            //更新已认证方式
            $user = self::where("id", $user->id)->find();
            $verification = $user->verification;
            $verification->mobile = 1;
            $user->verification = $verification;
            $user->save();
            //返回对象
            return self::where("id", $user->id)->find();
        }
    }



}