package entity

import (
	"context"
	"gorm.io/gorm"
	"yx-dataset-server/app/schema"
)

// GetUserRoleDB 获取User存储
func GetUserRoleDB(ctx context.Context, defDB *gorm.DB) *gorm.DB {
	return getDBWithModel(ctx, defDB, UserRole{})
}

// SchemaUserRole UserRole对象
type SchemaUserRole schema.UserRole

// ToUserRole 转换为UserRole实体
func (a SchemaUserRole) ToUserRole() *UserRole {
	item := &UserRole{
		RecordId: a.RecordId,
		UserID:   a.UserId,
		RoleID:   a.RoleId,
	}
	return item
}

// ToSchemaUserRole 转换为UserRole对象
func (a UserRole) ToSchemaUserRole() *schema.UserRole {
	item := &schema.UserRole{
		RecordId: a.RecordId,
		UserId:   a.UserID,
		RoleId:   a.RoleID,
	}
	return item
}

// UserRole 用户角色关联实体
type UserRole struct {
	gorm.Model
	RecordId string `gorm:"column:record_id;size:32;index;"` // 记录id
	UserID   string `gorm:"column:user_id;size:32;index;"`   // 用户id
	RoleID   string `gorm:"column:role_id;size:32;index;"`   // 角色id
}

// UserRoles UserRole列表
type UserRoles []*UserRole

// ToSchemaUsersRoles 转换为UserRole对象列表
func (a UserRoles) ToSchemaUserRoles() []*schema.UserRole {
	list := make([]*schema.UserRole, len(a))
	for i, item := range a {
		list[i] = item.ToSchemaUserRole()
	}
	return list
}
