package entity

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

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

// SchemaMenu Menu对象
type SchemaMenu schema.Menu

// ToMenu 转换为Menu实体
func (a SchemaMenu) ToMenu() *Menu {
	item := &Menu{
		RecordID:    a.RecordID,
		Name:        a.Name,
		Path:        a.Path,
		Component:   a.Component,
		ParentId:    a.ParentId,
		Icon:        a.Meta.Icon,
		Title:       a.Meta.Title,
		IsLink:      a.Meta.IsLink,
		IsHide:      a.Meta.IsHide,
		IsFull:      a.Meta.IsFull,
		IsAffix:     a.Meta.IsAffix,
		IsKeepAlive: a.Meta.IsKeepAlive,
		Sequence:    &a.Sequence,
		CreatorId:   a.CreatorId,
	}
	return item
}

// ToSchemaMenu 转换为Menu对象
func (a Menu) ToSchemaMenu() *schema.Menu {
	item := &schema.Menu{
		RecordID:  a.RecordID,
		Name:      a.Name,
		Path:      a.Path,
		Component: a.Component,
		ParentId:  a.ParentId,
		Sequence:  *a.Sequence,
		CreatorId: a.CreatorId,
		CreatedAt: a.CreatedAt,
		Meta: schema.MenuMeta{
			Icon:        a.Icon,
			Title:       a.Title,
			IsLink:      a.IsLink,
			IsHide:      a.IsHide,
			IsFull:      a.IsFull,
			IsAffix:     a.IsAffix,
			IsKeepAlive: a.IsKeepAlive,
		},
	}
	return item
}

// Menu Menu实体
type Menu struct {
	gorm.Model
	RecordID    string `gorm:"column:record_id;size:32;index;comment:'记录内码'"`
	Name        string `gorm:"column:name;size:50;index;comment:'菜单名称'"`
	Path        string `gorm:"column:path;size:50;"`            // 路径
	Component   string `gorm:"column:component;size:50;"`       // 组件
	ParentId    string `gorm:"column:parent_id;size:32;index;"` // 父级id
	Icon        string `gorm:"column:icon;size:30;"`            // 图标
	Title       string `gorm:"column:title;size:50;"`           // 标题
	IsLink      string `gorm:"column:is_link;size:10;"`         // 是否外链
	IsHide      bool   `gorm:"column:is_hide;"`                 // 是否隐藏
	IsFull      bool   `gorm:"column:is_full;"`                 // 是否全屏
	IsAffix     bool   `gorm:"column:is_affix;"`                // 是否固定
	IsKeepAlive bool   `gorm:"column:is_keep_alive;"`           // 是否缓存
	Sequence    *int   `gorm:"column:sequence;auto_increment;"` // 排序值
	CreatorId   string `gorm:"column:creator_id;size:32;"`      // 创建人
}

// Menus Menu列表
type Menus []*Menu

// ToSchemaMenus 转换为Menu对象列表
func (a Menus) ToSchemaMenus() []*schema.Menu {
	list := make([]*schema.Menu, len(a))
	for i, item := range a {
		list[i] = item.ToSchemaMenu()
	}
	return list
}
