<?php
namespace app\model;

class Member extends Base
{
    public static function getList($limit = 40)
    {
        $items = self::alias('m')
            ->leftjoin('auth_group g','g.id=m.group_id')
            ->field('m.id,m.username,m.login_time,m.group_id,g.title')
            ->order('m.id', 'asc')
            ->paginate($limit);
        return $items;
    }

    /**
     * 根据角色分组返回用户
     * @param int $groupId 角色分组ID
     * @param int $limit 每页数量
     */
    public static function getListByGroup($groupId, $limit = 40)
    {
        $items = self::alias('m')
            ->leftjoin('auth_group g','g.id=m.group_id')
            ->field('m.id,m.username,m.login_time,m.group_id,g.title')
            ->where('m.group_id', '=', $groupId)
            ->order('m.id', 'asc')
            ->paginate($limit);
        return $items;
    }

    /**
     * 根据角色分组返回用户
     * @param  int  $limit  每页数量
     * @return mixed
     */
    public static function getListNotAdmin(int $limit = 40)
    {
        return self::alias('m')
            ->leftjoin('auth_group g','g.id=m.group_id')
            ->field('m.id,m.username,m.login_time,m.group_id,g.title')
            ->where('m.group_id', '<>', 1)
            ->order('m.id', 'asc')
            ->paginate($limit);
    }

    //根据用户名获取管理账号
    public static function getByUserName($username)
    {
        return self::where('username', trim($username))
        ->findOrEmpty()
        ->toArray();
    }

    //根据ID获取管理账户和相关权限
    public static function getMemberAndRulesByID($memberId)
    {
        return self::alias('m')
        ->join('auth_group g', 'm.group_id = g.id', 'LEFT')
        ->field('m.group_id,g.rules')
        ->where('m.id', $memberId)
        ->findOrEmpty()
        ->toArray();
    }

    public static function updateCates($id, $cates)
    {
        $cates = implode(',', $cates);
        $data = ['cates' => $cates];
        self::updateById($id, $data);
    }
}
