system_menu.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package model
  2. import (
  3. operationPb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/operation"
  4. "time"
  5. )
  6. const (
  7. Level1 = iota + 1
  8. Level2
  9. Level3
  10. )
  11. type SystemMenu struct {
  12. Id int64 `json:"id,omitempty"`
  13. Name string `json:"name,omitempty"`
  14. MenuType int32 `json:"menu_type,omitempty"`
  15. Level int32 `json:"level,omitempty"`
  16. Title string `json:"title,omitempty"`
  17. Path string `json:"path,omitempty"`
  18. IsShow operationPb.IsShow_Kind `json:"is_show,omitempty"`
  19. IsDelete operationPb.IsShow_Kind `json:"is_delete,omitempty"`
  20. Component string `json:"component,omitempty"`
  21. Icon string `json:"icon,omitempty"`
  22. Sort int32 `json:"sort,omitempty"`
  23. ParentId int64 `json:"parent_id"`
  24. Redirect string `json:"redirect,omitempty"`
  25. CreatedAt int64 `json:"created_at,omitempty"`
  26. UpdatedAt int64 `json:"updated_at,omitempty"`
  27. }
  28. func (s *SystemMenu) TableName() string {
  29. return "system_menu"
  30. }
  31. func NewSystemMenu(req *operationPb.AddMenuRequest) *SystemMenu {
  32. return &SystemMenu{
  33. Name: req.Name,
  34. MenuType: req.MenuType,
  35. Level: req.Level,
  36. Title: req.Title,
  37. Path: req.Path,
  38. Component: req.Component,
  39. Icon: req.Icon,
  40. Sort: req.Sort,
  41. Redirect: req.Redirect,
  42. ParentId: int64(req.ParentId),
  43. IsShow: operationPb.IsShow_OK,
  44. IsDelete: operationPb.IsShow_OK,
  45. }
  46. }
  47. type SystemMenuSlice []*SystemMenu
  48. func (s SystemMenuSlice) ToPB() []*operationPb.AddMenuRequest {
  49. level := make(map[int32][]*operationPb.AddMenuRequest, 0)
  50. for _, menu := range s {
  51. if _, ok := level[menu.Level]; !ok {
  52. level[menu.Level] = make([]*operationPb.AddMenuRequest, 0)
  53. }
  54. level[menu.Level] = append(level[menu.Level], &operationPb.AddMenuRequest{
  55. Id: int32(menu.Id),
  56. Name: menu.Name,
  57. ParentId: int32(menu.ParentId),
  58. MenuType: menu.MenuType,
  59. Title: menu.Title,
  60. Path: menu.Path,
  61. IsShow: menu.IsShow,
  62. Component: menu.Component,
  63. Icon: menu.Icon,
  64. Sort: menu.Sort,
  65. Redirect: menu.Redirect,
  66. CreatedAt: int32(menu.CreatedAt),
  67. CreatedAtFormat: time.Unix(menu.CreatedAt, 0).Format(LayoutTime),
  68. Level: menu.Level,
  69. Affix: true,
  70. KeepAlive: true,
  71. Children: make([]*operationPb.AddMenuRequest, 0),
  72. })
  73. }
  74. for _, leve3Data := range level[Level3] {
  75. for _, leve2Data := range level[Level2] {
  76. if leve3Data.ParentId == leve2Data.Id {
  77. if leve2Data.Children == nil {
  78. leve2Data.Children = make([]*operationPb.AddMenuRequest, 0)
  79. }
  80. leve2Data.Children = append(leve2Data.Children, leve3Data)
  81. }
  82. }
  83. }
  84. for _, leve2Data := range level[Level2] {
  85. for _, leve1Data := range level[Level1] {
  86. if leve2Data.ParentId == leve1Data.Id {
  87. if leve1Data.Children == nil {
  88. leve1Data.Children = make([]*operationPb.AddMenuRequest, 0)
  89. }
  90. leve1Data.Children = append(leve1Data.Children, leve2Data)
  91. }
  92. }
  93. }
  94. return level[Level1]
  95. }
  96. func (s *SystemMenu) ToPb() *operationPb.AddMenuRequest {
  97. return &operationPb.AddMenuRequest{
  98. Id: int32(s.Id),
  99. Name: s.Name,
  100. MenuType: s.MenuType,
  101. Level: s.Level,
  102. Title: s.Title,
  103. Path: s.Path,
  104. Component: s.Component,
  105. Icon: s.Icon,
  106. Sort: s.Sort,
  107. Redirect: s.Redirect,
  108. ParentId: int32(s.ParentId),
  109. IsShow: s.IsShow,
  110. CreatedAt: int32(s.CreatedAt),
  111. CreatedAtFormat: time.Unix(s.CreatedAt, 0).Format(LayoutTime),
  112. }
  113. }