system_role.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package model
  2. import (
  3. "fmt"
  4. operationPb "kpt-tmr-group/proto/go/backend/operation"
  5. "strconv"
  6. "strings"
  7. "time"
  8. )
  9. type SystemRole struct {
  10. Id int64 `json:"id,omitempty"`
  11. Name string `json:"name,omitempty"`
  12. Remarks string `json:"remarks,omitempty"`
  13. MenuId string `json:"menu_id"`
  14. MobileId string `json:"mobile_id"`
  15. PastureId string `json:"pasture_id"`
  16. IsShow operationPb.IsShow_Kind `json:"is_show,omitempty"`
  17. CreateUser string `json:"create_user,omitempty"`
  18. CreatedAt int64 `json:"created_at,omitempty"`
  19. UpdatedAt int64 `json:"updated_at,omitempty"`
  20. }
  21. func (s *SystemRole) TableName() string {
  22. return "system_role"
  23. }
  24. const LayoutTime = "2006-01-02 15:04:05"
  25. func NewSystemRole(req *operationPb.AddRoleRequest) *SystemRole {
  26. systemRole := &SystemRole{
  27. Name: req.Name,
  28. Remarks: req.Remarks,
  29. IsShow: operationPb.IsShow_OK,
  30. CreateUser: req.CreateUser,
  31. }
  32. systemRole.RolePermissionsFormat(req)
  33. return systemRole
  34. }
  35. func (r *SystemRole) RolePermissionsFormat(req *operationPb.AddRoleRequest) {
  36. pastureId := ""
  37. for _, v := range req.PastureId {
  38. pastureId += fmt.Sprintf("%d,", v)
  39. }
  40. menuId := ""
  41. for _, v := range req.MenuId {
  42. menuId += fmt.Sprintf("%d,", v)
  43. }
  44. mobileId := ""
  45. for _, v := range req.MobileId {
  46. mobileId += fmt.Sprintf("%d", v)
  47. }
  48. if pastureId != "" {
  49. r.PastureId = strings.TrimRight(pastureId, ",")
  50. }
  51. if mobileId != "" {
  52. r.MobileId = strings.TrimRight(mobileId, ",")
  53. }
  54. if menuId != "" {
  55. r.MenuId = strings.TrimRight(menuId, ",")
  56. }
  57. }
  58. func (s *SystemRole) RoleForMenuToSlice() []int {
  59. menuIds := make([]int, 0)
  60. if s.MenuId != "" {
  61. menuIdsStr := strings.Split(s.MenuId, ",")
  62. for _, v := range menuIdsStr {
  63. menuIdsInt, _ := strconv.Atoi(v)
  64. menuIds = append(menuIds, menuIdsInt)
  65. }
  66. }
  67. return menuIds
  68. }
  69. func (s *SystemRole) RoleForMobileToSlice() []int {
  70. mobileIds := make([]int, 0)
  71. if s.MobileId != "" {
  72. mobileIdsStr := strings.Split(s.MenuId, ",")
  73. for _, v := range mobileIdsStr {
  74. mobileIdsInt, _ := strconv.Atoi(v)
  75. mobileIds = append(mobileIds, mobileIdsInt)
  76. }
  77. }
  78. return mobileIds
  79. }
  80. func (s *SystemRole) RoleForPastureToSlice() []int {
  81. pastureIds := make([]int, 0)
  82. if s.PastureId != "" {
  83. pastureIdsStr := strings.Split(s.MenuId, ",")
  84. for _, v := range pastureIdsStr {
  85. pastureIdsInt, _ := strconv.Atoi(v)
  86. pastureIds = append(pastureIds, pastureIdsInt)
  87. }
  88. }
  89. return pastureIds
  90. }
  91. type SystemRoleSlice []*SystemRole
  92. func (s SystemRoleSlice) ToPB() []*operationPb.AddRoleRequest {
  93. res := make([]*operationPb.AddRoleRequest, len(s))
  94. for i, v := range s {
  95. res[i] = &operationPb.AddRoleRequest{
  96. Id: v.Id,
  97. Name: v.Name,
  98. Remarks: v.Remarks,
  99. CreateUser: v.CreateUser,
  100. IsShow: v.IsShow,
  101. CreatedAt: v.CreatedAt,
  102. CratedAtFormat: time.Unix(v.CreatedAt, 0).Format(LayoutTime),
  103. }
  104. }
  105. return res
  106. }
  107. func (s *SystemRole) ToPb() *operationPb.AddRoleRequest {
  108. return &operationPb.AddRoleRequest{
  109. Id: s.Id,
  110. Name: s.Name,
  111. CreateUser: s.CreateUser,
  112. IsShow: s.IsShow,
  113. CreatedAt: s.CreatedAt,
  114. }
  115. }
  116. type SystemRoleResponse struct {
  117. Page int32 `json:"page"`
  118. Total int32 `json:"total"`
  119. List []*operationPb.AddRoleRequest `json:"list"`
  120. }
  121. func (s *SystemRoleResponse) ToPB() *operationPb.SearchRoleResponse {
  122. return &operationPb.SearchRoleResponse{
  123. Page: s.Page,
  124. Total: s.Total,
  125. List: s.List,
  126. }
  127. }