<?php

namespace app\admin\model;

use think\Model;

class User extends Model
{
    protected $pk = 'user_id';

    public $page_info;

    public function uorder()
    {
        return $this->hasMany('Order', 'user_id', 'user_id');
    }

    //模糊查询时使用,返回地用户的ID数组
    public function getIdsByEmailLike($email)
    {
        $return = [] ;
        $ret = $this->field('user_id')->where(['user_email'=>['like', "%$email%"]])->select();
        if ($ret){
            foreach ($ret as $val){
                $return[] = $val->user_id;
            }
        }
        return $return ;
    }

    /**
     * 用户列表
     * @access public
     * @author csdeshang
     * @param array $condition 条件
     * @param string $field 字段
     * @param number $page 分页
     * @param string $order 排序
     * @return array
     */
    public function getUserList($condition = array(), $field = '*', $page = 0, $order = 'user_addTime desc')
    {
        if ($page) {
            $user_list = db('user')
                ->alias('a')
                ->join('userinfo b','b.user_id = a.user_id')
                ->where($condition)
                ->order($order)
                ->paginate($page, false, ['query' => request()->param()]);
            $this->page_info = $user_list;
            return $user_list->items();
        } else {
            return db('user')->where($condition)->order($order)->select();
        }
    }

    /**
     * 取单个用户
     */
    public function getUserInfo($user_id)
    {
        $userinfo = db('user')
            ->alias('a')
            ->join('userinfo b','b.user_id = a.user_id')
            ->where('b.user_id',$user_id)
            ->find();
        return $userinfo;
    }

}
