package models

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

type Role struct {
	Model

	RoleName    string `json:"role_name"`
	Description string `json:"description"`
	PlatformId  int64  `json:"platform_id"`
}

func (c Role) TableName() string {
	return setting.DatabaseSetting.Name_Manage + ".gli_role"
}

func GetRole(id int32) (*Role, error) {
	var role Role
	err := db.Where("id = ?", id).First(&role).Error
	if err != nil {
		return nil, err
	}

	return &role, nil
}

func AddRole(data map[string]interface{}) (error) {
	role := Role{
		RoleName:    data["role_name"].(string),
		Description: data["description"].(string),
		PlatformId:  data["platform_id"].(int64),
	}
	if err := db.Create(&role).Error; err != nil {
		return err
	}

	return nil
}

func EditRole(id int32, data interface{}) error {
	if err := db.Model(&Role{}).Where("id = ?", id).Updates(data).Error; err != nil {
		return err
	}

	return nil
}

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

	return nil
}

func GetRoles(maps interface{}) ([]*Role, error) {
	var roles []*Role
	err := db.Where(maps).Find(&roles).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}

	return roles, nil
}
