system_user.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. func (s *SystemUser) GetRoleIds() []int64 {
  62. res := make([]int64, 0)
  63. if s.RoleIds != "" {
  64. roleIds := strings.Split(s.RoleIds, ",")
  65. for _, idStr := range roleIds {
  66. id, _ := strconv.Atoi(idStr)
  67. res = append(res, int64(id))
  68. }
  69. }
  70. return res
  71. }
  72. func (s *SystemUser) GetDepthIds() []int64 {
  73. res := make([]int64, 0)
  74. if s.DeptIds != "" {
  75. depthIds := strings.Split(s.DeptIds, ",")
  76. for _, idStr := range depthIds {
  77. id, _ := strconv.Atoi(idStr)
  78. res = append(res, int64(id))
  79. }
  80. }
  81. return res
  82. }
  83. type UserModel struct {
  84. SystemUser *SystemUser
  85. AppPasture *AppPastureList
  86. }
  87. type SystemUserSlice []*SystemUser
  88. func (s SystemUserSlice) ToPB(deptList []*SystemDept, roleList []*SystemRole) []*pasturePb.SearchUserRequest {
  89. deptMap := make(map[string]*SystemDept)
  90. for _, v := range deptList {
  91. deptMap[fmt.Sprintf("%d", v.Id)] = v
  92. }
  93. roleMap := make(map[string]*SystemRole)
  94. for _, v := range roleList {
  95. roleMap[fmt.Sprintf("%d", v.Id)] = v
  96. }
  97. res := make([]*pasturePb.SearchUserRequest, len(s))
  98. for i, v := range s {
  99. userDeptList := make([]*pasturePb.IdName, 0)
  100. if v.DeptIds != "" {
  101. deptIds := strings.Split(v.DeptIds, ",")
  102. for _, d := range deptIds {
  103. if de, ok := deptMap[d]; ok {
  104. userDeptList = append(userDeptList, &pasturePb.IdName{
  105. Id: int32(de.Id),
  106. Name: de.Name,
  107. })
  108. }
  109. }
  110. }
  111. userRoleList := make([]*pasturePb.IdName, 0)
  112. if v.RoleIds != "" {
  113. roleIds := strings.Split(v.RoleIds, ",")
  114. for _, r := range roleIds {
  115. if ro, ok := roleMap[r]; ok {
  116. userRoleList = append(userRoleList, &pasturePb.IdName{
  117. Id: int32(ro.Id),
  118. Name: ro.Name,
  119. })
  120. }
  121. }
  122. }
  123. res[i] = &pasturePb.SearchUserRequest{
  124. Id: int32(v.Id),
  125. Name: v.Name,
  126. Mobile: v.Mobile,
  127. NickName: v.NickName,
  128. Avatar: v.Avatar,
  129. Gender: v.Gender,
  130. IsShow: v.IsShow,
  131. IsDelete: v.IsDelete,
  132. Remarks: v.Remarks,
  133. DepthList: userDeptList,
  134. RoleList: userRoleList,
  135. CreatedAtFormat: time.Unix(v.CreatedAt, 0).Local().Format(LayoutTime),
  136. UpdatedAtFormat: time.Unix(v.UpdatedAt, 0).Local().Format(LayoutTime),
  137. }
  138. }
  139. return res
  140. }
  141. func (s *SystemUser) ToPb() *operationPb.AddSystemUser {
  142. return &operationPb.AddSystemUser{
  143. Id: int32(s.Id),
  144. Name: s.Name,
  145. CreatedAt: int32(s.CreatedAt),
  146. CreatedAtFormat: time.Unix(s.CreatedAt, 0).Local().Format(LayoutTime),
  147. RoleIds: []int32{},
  148. }
  149. }
  150. func (s SystemUserSlice) ToPB2() []*pasturePb.ConfigOptionsList {
  151. res := make([]*pasturePb.ConfigOptionsList, len(s))
  152. for i, d := range s {
  153. res[i] = &pasturePb.ConfigOptionsList{
  154. Value: int32(d.Id),
  155. Label: d.Name,
  156. Disabled: true,
  157. }
  158. }
  159. return res
  160. }