123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- package model
- import (
- operationPb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/operation"
- "time"
- )
- const (
- Level1 = iota + 1
- Level2
- Level3
- )
- type SystemMenu struct {
- Id int64 `json:"id,omitempty"`
- Name string `json:"name,omitempty"`
- MenuType int32 `json:"menu_type,omitempty"`
- Level int32 `json:"level,omitempty"`
- Title string `json:"title,omitempty"`
- Path string `json:"path,omitempty"`
- IsShow operationPb.IsShow_Kind `json:"is_show,omitempty"`
- IsDelete operationPb.IsShow_Kind `json:"is_delete,omitempty"`
- Component string `json:"component,omitempty"`
- Icon string `json:"icon,omitempty"`
- Sort int32 `json:"sort,omitempty"`
- ParentId int64 `json:"parent_id"`
- Redirect string `json:"redirect,omitempty"`
- CreatedAt int64 `json:"created_at,omitempty"`
- UpdatedAt int64 `json:"updated_at,omitempty"`
- }
- func (s *SystemMenu) TableName() string {
- return "system_menu"
- }
- func NewSystemMenu(req *operationPb.AddMenuRequest) *SystemMenu {
- return &SystemMenu{
- Name: req.Name,
- MenuType: req.MenuType,
- Level: req.Level,
- Title: req.Title,
- Path: req.Path,
- Component: req.Component,
- Icon: req.Icon,
- Sort: req.Sort,
- Redirect: req.Redirect,
- ParentId: int64(req.ParentId),
- IsShow: operationPb.IsShow_OK,
- IsDelete: operationPb.IsShow_OK,
- }
- }
- type SystemMenuSlice []*SystemMenu
- func (s SystemMenuSlice) ToPB() []*operationPb.AddMenuRequest {
- level := make(map[int32][]*operationPb.AddMenuRequest, 0)
- for _, menu := range s {
- if _, ok := level[menu.Level]; !ok {
- level[menu.Level] = make([]*operationPb.AddMenuRequest, 0)
- }
- level[menu.Level] = append(level[menu.Level], &operationPb.AddMenuRequest{
- Id: int32(menu.Id),
- Name: menu.Name,
- ParentId: int32(menu.ParentId),
- MenuType: menu.MenuType,
- Title: menu.Title,
- Path: menu.Path,
- IsShow: menu.IsShow,
- Component: menu.Component,
- Icon: menu.Icon,
- Sort: menu.Sort,
- Redirect: menu.Redirect,
- CreatedAt: int32(menu.CreatedAt),
- CreatedAtFormat: time.Unix(menu.CreatedAt, 0).Format(LayoutTime),
- Level: menu.Level,
- Affix: true,
- KeepAlive: true,
- Children: make([]*operationPb.AddMenuRequest, 0),
- })
- }
- for _, leve3Data := range level[Level3] {
- for _, leve2Data := range level[Level2] {
- if leve3Data.ParentId == leve2Data.Id {
- if leve2Data.Children == nil {
- leve2Data.Children = make([]*operationPb.AddMenuRequest, 0)
- }
- leve2Data.Children = append(leve2Data.Children, leve3Data)
- }
- }
- }
- for _, leve2Data := range level[Level2] {
- for _, leve1Data := range level[Level1] {
- if leve2Data.ParentId == leve1Data.Id {
- if leve1Data.Children == nil {
- leve1Data.Children = make([]*operationPb.AddMenuRequest, 0)
- }
- leve1Data.Children = append(leve1Data.Children, leve2Data)
- }
- }
- }
- return level[Level1]
- }
- func (s *SystemMenu) ToPb() *operationPb.AddMenuRequest {
- return &operationPb.AddMenuRequest{
- Id: int32(s.Id),
- Name: s.Name,
- MenuType: s.MenuType,
- Level: s.Level,
- Title: s.Title,
- Path: s.Path,
- Component: s.Component,
- Icon: s.Icon,
- Sort: s.Sort,
- Redirect: s.Redirect,
- ParentId: int32(s.ParentId),
- IsShow: s.IsShow,
- CreatedAt: int32(s.CreatedAt),
- CreatedAtFormat: time.Unix(s.CreatedAt, 0).Format(LayoutTime),
- }
- }
|