<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class UserGroup extends Model
{
    protected $table = 'tb_user_group';
    protected $primaryKey = 'id';

    public $keyType = 'int';
    public $incrementing = true;
    public $timestamps = true;

    const CREATED_AT = 'created_at';
    const UPDATED_AT = 'updated_at';

    protected $guarded = [];

    public function user()
    {
        return $this->hasMany(User::class);
    }

    /**
     * 회원그룹 조회
     */
    public function data(): object
    {
        $query = $this->query();
        $query->orderBy('sort');
        $list = $query->get();
        $total = $this->count();
        $rows = $list->count();

        return (object)[
            'total' => $total,
            'rows' => $rows,
            'list' => $list
        ];
    }

    /**
     * 전체 사용 회원그룹 조회
     */
    public function getAllGroup()
    {
        return $this->where('is_use', 1)->orderBy('sort', 'ASC')->get();
    }

    /**
     * 회원그룹 조회하여 배열로 반환
     */
    public function findUserGroup(int $userGroupID)
    {
        return $this->where('id', $userGroupID)->where('is_use', 1)->orderBy('sort', 'ASC')->get();
    }
}
