package entity

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

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

// SchemaRole Role对象
type SchemaRole schema.Role

// ToRole 转换为Role实体
func (a SchemaRole) ToRole() *Role {
	item := &Role{
		RecordID: a.RecordID,
		Name:     a.Name,
		Code:     a.Code,
		ChartNum: a.ChartNum,
	}
	return item
}

// ToSchemaRole 转换为Role对象
func (a Role) ToSchemaRole() *schema.Role {
	item := &schema.Role{
		RecordID:  a.RecordID,
		Name:      a.Name,
		Code:      a.Code,
		ChartNum:  a.ChartNum,
		Creator:   a.CreatorId,
		CreatedAt: a.CreatedAt,
	}
	return item
}

// Role Role实体
type Role struct {
	gorm.Model
	RecordID  string `gorm:"column:record_id;size:32;index;"`
	Name      string `gorm:"column:name;size:30;"` // 角色名称
	Code      string `gorm:"column:code;size:5;"`  // 角色编码(11 系统管理员 12 企业管理员  99 企业员工)
	ChartNum  int    `gorm:"column:chart_num;"`    // 对话轮次/日
	CreatorId string `gorm:"column:creator_id;size:32;"`
}

// Roles Role列表
type Roles []*Role

// ToSchemaRoles 转换为Role对象列表
func (a Roles) ToSchemaRoles() []*schema.Role {
	list := make([]*schema.Role, len(a))
	for i, item := range a {
		list[i] = item.ToSchemaRole()
	}
	return list
}
