package models

import (
	"bwallet/pkg/setting"
	"github.com/jinzhu/gorm"
)

type AdminRole struct {
	Model

	Uid    uint8 `json:"uid"`
	RoleId uint8 `json:"role_id"`
}

func (c AdminRole) TableName() string {
	return setting.DatabaseSetting.Name_Manage + ".gli_admin_role"
}

func AddAdminRole(data map[string]interface{}) (error) {
	role := AdminRole{
		Uid:    data["uid"].(uint8),
		RoleId: data["role_id"].(uint8),
	}
	if err := db.Create(&role).Error; err != nil {
		return err
	}

	return nil
}

func DeleteAdminRole(id int) error {
	if err := db.Where("id = ?", id).Delete(AdminRole{}).Error; err != nil {
		return err
	}

	return nil
}

func GetAdminRoles(pageNum, pageSize int, maps interface{}) ([]*AdminRole, error) {
	var roles []*AdminRole
	err := db.Where(maps).Offset(pageNum).Limit(pageSize).Find(&roles).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}

	return roles, nil
}
