package models

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

type AdminRole struct {
	Model

	Uid    int32 `json:"uid"`
	RoleId int32 `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"].(int32),
		RoleId: data["role_id"].(int32),
	}
	if err := db.Create(&role).Error; err != nil {
		return err
	}

	return nil
}

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

	return nil
}

func RemoveAdminRole(uid int32) error {
	if err := db.Where("uid = ? ", uid).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
}
