system_user.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package model
  2. import (
  3. "fmt"
  4. "net/http"
  5. "strconv"
  6. "strings"
  7. "time"
  8. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  9. operationPb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/operation"
  10. )
  11. type SystemUser struct {
  12. Id int64 `json:"id"`
  13. Name string `json:"name"`
  14. NickName string `json:"nickName"`
  15. Mobile string `json:"mobile"`
  16. Password string `json:"password"`
  17. Avatar string `json:"avatar"`
  18. RoleIds string `json:"roleIds"`
  19. DeptIds string `json:"deptIds"`
  20. IndicatorsKinds string `json:"indicatorsKinds"`
  21. Gender pasturePb.Genders_Kind `json:"gender"`
  22. PastureIds string `json:"pastureIds"`
  23. IsShow pasturePb.IsShow_Kind `json:"isShow"`
  24. IsDelete pasturePb.IsShow_Kind `json:"isDelete"`
  25. Remarks string `json:"remarks"`
  26. CreatedAt int64 `json:"created_at"`
  27. UpdatedAt int64 `json:"updated_at"`
  28. }
  29. func (s *SystemUser) TableName() string {
  30. return "system_user"
  31. }
  32. func (s *SystemUser) SystemUserFormat(userRoles []*SystemRole, pastures []*operationPb.UserPasture) *operationPb.UserAuth {
  33. roles := make([]*operationPb.UserRole, len(userRoles))
  34. for k, v := range userRoles {
  35. roles[k] = &operationPb.UserRole{
  36. Id: int32(v.Id),
  37. Name: v.Name,
  38. }
  39. }
  40. return &operationPb.UserAuth{
  41. Code: http.StatusOK,
  42. Msg: "ok",
  43. Data: &operationPb.UserAuthData{
  44. UserName: s.Name,
  45. Roles: roles,
  46. Pastures: pastures,
  47. },
  48. }
  49. }
  50. func (s *SystemUser) GetPastureIds() []int64 {
  51. res := make([]int64, 0)
  52. if s.PastureIds != "" {
  53. pastureIds := strings.Split(s.PastureIds, ",")
  54. for _, idStr := range pastureIds {
  55. id, _ := strconv.Atoi(idStr)
  56. res = append(res, int64(id))
  57. }
  58. }
  59. return res
  60. }
  61. type UserModel struct {
  62. SystemUser *SystemUser
  63. AppPasture *AppPastureList
  64. }
  65. type SystemUserSlice []*SystemUser
  66. func (s SystemUserSlice) ToPB(deptList []*SystemDept, roleList []*SystemRole) []*pasturePb.SearchUserRequest {
  67. deptMap := make(map[string]*SystemDept)
  68. for _, v := range deptList {
  69. deptMap[fmt.Sprintf("%d", v.Id)] = v
  70. }
  71. roleMap := make(map[string]*SystemRole)
  72. for _, v := range roleList {
  73. roleMap[fmt.Sprintf("%d", v.Id)] = v
  74. }
  75. res := make([]*pasturePb.SearchUserRequest, len(s))
  76. for i, v := range s {
  77. userDeptList := make([]*pasturePb.IdName, 0)
  78. if v.DeptIds != "" {
  79. deptIds := strings.Split(v.DeptIds, ",")
  80. for _, d := range deptIds {
  81. if de, ok := deptMap[d]; ok {
  82. userDeptList = append(userDeptList, &pasturePb.IdName{
  83. Id: int32(de.Id),
  84. Name: de.Name,
  85. })
  86. }
  87. }
  88. }
  89. userRoleList := make([]*pasturePb.IdName, 0)
  90. if v.RoleIds != "" {
  91. roleIds := strings.Split(v.RoleIds, ",")
  92. for _, r := range roleIds {
  93. if ro, ok := roleMap[r]; ok {
  94. userRoleList = append(userRoleList, &pasturePb.IdName{
  95. Id: int32(ro.Id),
  96. Name: ro.Name,
  97. })
  98. }
  99. }
  100. }
  101. res[i] = &pasturePb.SearchUserRequest{
  102. Id: int32(v.Id),
  103. Name: v.Name,
  104. Mobile: v.Mobile,
  105. NickName: v.NickName,
  106. Avatar: v.Avatar,
  107. Gender: v.Gender,
  108. IsShow: v.IsShow,
  109. IsDelete: v.IsDelete,
  110. Remarks: v.Remarks,
  111. DepthList: userDeptList,
  112. RoleList: userRoleList,
  113. CreatedAtFormat: time.Unix(v.CreatedAt, 0).Local().Format(LayoutTime),
  114. UpdatedAtFormat: time.Unix(v.UpdatedAt, 0).Local().Format(LayoutTime),
  115. }
  116. }
  117. return res
  118. }
  119. func (s *SystemUser) ToPb() *operationPb.AddSystemUser {
  120. return &operationPb.AddSystemUser{
  121. Id: int32(s.Id),
  122. Name: s.Name,
  123. CreatedAt: int32(s.CreatedAt),
  124. CreatedAtFormat: time.Unix(s.CreatedAt, 0).Local().Format(LayoutTime),
  125. RoleIds: []int32{},
  126. }
  127. }
  128. func (s SystemUserSlice) ToPB2() []*pasturePb.ConfigOptionsList {
  129. res := make([]*pasturePb.ConfigOptionsList, len(s))
  130. for i, d := range s {
  131. res[i] = &pasturePb.ConfigOptionsList{
  132. Value: int32(d.Id),
  133. Label: d.Name,
  134. Disabled: true,
  135. }
  136. }
  137. return res
  138. }