system_user.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package model
  2. import (
  3. "fmt"
  4. operationPb "kpt-tmr-group/proto/go/backend/operation"
  5. "net/http"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. type SystemUser struct {
  11. Id int64 `json:"id"`
  12. Name string `json:"name"`
  13. EmployeeName string `json:"employee_name"`
  14. Phone string `json:"phone"`
  15. Password string `json:"password"`
  16. RoleIds string `json:"role_ids"`
  17. CreateUser string `json:"create_user"`
  18. IsShow operationPb.IsShow_Kind `json:"is_show"`
  19. IsDelete operationPb.IsShow_Kind `json:"is_delete"`
  20. CreatedAt int64 `json:"created_at"`
  21. UpdatedAt int64 `json:"updated_at"`
  22. }
  23. func (s *SystemUser) TableName() string {
  24. return "system_user"
  25. }
  26. func (s *SystemUser) SystemUserFormat(userRoles []*SystemRole, pastures []*operationPb.UserPasture) *operationPb.UserAuth {
  27. roles := make([]*operationPb.UserRole, len(userRoles))
  28. for k, v := range userRoles {
  29. roles[k] = &operationPb.UserRole{
  30. Id: int32(v.Id),
  31. Name: v.Name,
  32. }
  33. }
  34. return &operationPb.UserAuth{
  35. Code: http.StatusOK,
  36. Msg: "ok",
  37. Data: &operationPb.UserAuthData{
  38. UserName: s.Name,
  39. Phone: s.Phone,
  40. EmployeeName: s.EmployeeName,
  41. Roles: roles,
  42. Pastures: pastures,
  43. },
  44. }
  45. }
  46. func (s *SystemUser) SystemUserRoleFormat(req *operationPb.AddSystemUser) {
  47. roleIds := ""
  48. for _, role := range req.Roles {
  49. roleIds += fmt.Sprintf("%d,", role.Id)
  50. }
  51. if roleIds != "" {
  52. s.RoleIds = strings.TrimRight(roleIds, ",")
  53. }
  54. }
  55. func (s *SystemUser) SystemUserRoleToSlice() []int {
  56. roleIds := make([]int, 0)
  57. if s.RoleIds != "" {
  58. roleIdsStr := strings.Split(s.RoleIds, ",")
  59. for _, v := range roleIdsStr {
  60. roleIdsInt, _ := strconv.Atoi(v)
  61. roleIds = append(roleIds, roleIdsInt)
  62. }
  63. }
  64. return roleIds
  65. }
  66. type SystemUserSlice []*SystemUser
  67. func (s SystemUserSlice) ToPB(roleList []*SystemRole) []*operationPb.AddSystemUser {
  68. res := make([]*operationPb.AddSystemUser, len(s))
  69. for i, v := range s {
  70. res[i] = &operationPb.AddSystemUser{
  71. Id: int32(v.Id),
  72. Name: v.Name,
  73. Phone: v.Phone,
  74. EmployeeName: v.EmployeeName,
  75. CreateUser: v.CreateUser,
  76. IsShow: v.IsShow,
  77. CreatedAt: int32(v.CreatedAt),
  78. CreatedAtFormat: time.Unix(v.CreatedAt, 0).Format(LayoutTime),
  79. RoleName: strings.TrimRight(v.UserRoleFormat(roleList), ","),
  80. }
  81. }
  82. return res
  83. }
  84. func (s *SystemUser) UserRoleFormat(roleList []*SystemRole) string {
  85. ids := strings.Split(s.RoleIds, ",")
  86. roleListName := ""
  87. for _, id := range ids {
  88. for _, r := range roleList {
  89. if fmt.Sprintf("%d", r.Id) != id {
  90. continue
  91. }
  92. roleListName += fmt.Sprintf("%s,", r.Name)
  93. }
  94. }
  95. return roleListName
  96. }
  97. func (s *SystemUser) ToPb() *operationPb.AddSystemUser {
  98. roles := make([]int32, 0)
  99. roleIds := strings.Split(s.RoleIds, ",")
  100. for _, roleId := range roleIds {
  101. id, _ := strconv.Atoi(roleId)
  102. roles = append(roles, int32(id))
  103. }
  104. return &operationPb.AddSystemUser{
  105. Id: int32(s.Id),
  106. Name: s.Name,
  107. Phone: s.Phone,
  108. CreateUser: s.CreateUser,
  109. EmployeeName: s.EmployeeName,
  110. IsShow: s.IsShow,
  111. CreatedAt: int32(s.CreatedAt),
  112. CreatedAtFormat: time.Unix(s.CreatedAt, 0).Format(LayoutTime),
  113. RoleIds: roles,
  114. }
  115. }