<?php
declare (strict_types = 1);

namespace app\model\api;

use Closure;
use Firebase\JWT\JWT;
use library\basic\BaseModel;
use library\traits\JwtAuthModelTrait;
use library\traits\ModelTrait;
use think\facade\Cache;
use think\facade\Env;
use think\Model;

/**
 * @mixin \think\Model
 */
class Member extends BaseModel
{
    use ModelTrait;
    use JwtAuthModelTrait;





    /**
     * 获取列表数据
     * @param $page
     * @param $where
     * @param $pageCount
     * @param $desc
     */
    public function getList($page,$where = [],$pageCount = 20,$filed = '*',$desc = ''){
        $data =  $this

            ->field("*,(SELECT count(*) from table_order where uid = u.uid and status > 0) as order_count,
                (select name from table_member_level where id = u.levelid) as lavel_name
            ")
            ->alias("u")
            ->when(!empty($where),function ($query) use($where){
                foreach ($where as $k=>$v) {
                    if($v instanceof Closure) {
                        $v($query);
                    } else {
                        if(is_array($v)) {
                            //whereLike
                            if($v[1] == 'whereLike') {
                                if(!empty($v[0]))
                                    $query->whereLike($k,$v[0]);
                                continue;
                            }
                            $bool = false;
                            eval('$bool = '.$v[1].'($v[0]);');
                            if($bool) {
                                $query->where($k,$v[0]);
                            }
                        } else {
                            $query->where($k,$v);
                        }
                    }
                }
            })
            ->order($desc)
            ->paginate(['list_rows'=>$pageCount,'page'=>$page])
            ->toArray();
        return [$data['total'],$data['data']];
    }




    /**
     * 获取列表数据
     * @param $page
     * @param $where
     * @param $pageCount
     * @param $desc
     */
    public function getItem($uid){
        $data =  $this

            ->field("*,(SELECT count(*) from table_order where uid = u.uid and status > 0) as order_count,
                (select name from table_member_level where id = u.levelid) as lavel_name
            ")
            ->alias("u")
            ->where('uid',$uid)
            ->toArray();
        return $data;
    }



    public static function login($userName,$passWord,$sassid) {
        $info = self::where('mobile',$userName)->where('sassid',$sassid)->find();
        if(empty($info)) return self::setErrorInfo('请输入正确的账号和密码');
        if (!$info['status']) return self::setErrorInfo('账户被停用了!');
        if($info['password'] != md5($passWord)) {
            return self::setErrorInfo('请输入正确的密码');
        }
        return $info;
    }

    /**
     * 生成token存放令牌 | 存放redis
     * @param SystemAdmin $info
     * @param $secret_key
     * @return array|bool
     */
    public static function createToken(Member $info,$secret_key)
    {
        $params['exp'] = 30;
        $params['time'] = time();
        $params['token'] = base64_encode(crypto_encrypt($info->getData('uid'),$secret_key));
        $params['uid'] = $info->getData('uid');
        $params['sassid'] = $info->getData('sassid');
        $token = JWT::encode($params, Env::get('app.app_key', 'default'));
        return $token;
    }

}
