system_user.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package model
  2. import (
  3. "encoding/json"
  4. "strconv"
  5. "strings"
  6. "time"
  7. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  8. )
  9. type SystemUser struct {
  10. Id int64 `json:"id"`
  11. Name string `json:"name"`
  12. NickName string `json:"nickName"`
  13. Gender pasturePb.Genders_Kind `json:"gender"`
  14. Mobile string `json:"mobile"`
  15. Password string `json:"password"`
  16. Avatar string `json:"avatar"`
  17. RoleIds string `json:"roleIds"`
  18. DeptIds string `json:"deptIds"`
  19. IndicatorsKinds string `json:"indicatorsKinds"`
  20. PastureIds string `json:"pastureIds"`
  21. IsShow pasturePb.IsShow_Kind `json:"isShow"`
  22. IsDelete pasturePb.IsShow_Kind `json:"isDelete"`
  23. Remarks string `json:"remarks"`
  24. Spare string `json:"spare"`
  25. CreatedAt int64 `json:"created_at"`
  26. UpdatedAt int64 `json:"updated_at"`
  27. }
  28. func (s *SystemUser) TableName() string {
  29. return "system_user"
  30. }
  31. func (s *SystemUser) UserUpdate(req *pasturePb.SearchUserRequest, pastureIds string) {
  32. s.Name = req.Name
  33. s.NickName = req.NickName
  34. s.Mobile = req.Mobile
  35. s.Gender = req.Gender
  36. s.Remarks = req.Remarks
  37. s.IsShow = req.IsShow
  38. s.PastureIds = pastureIds
  39. }
  40. func NewSystemUser(req *pasturePb.SearchUserRequest, pastureIds string, pastureDepthList []*pasturePb.PastureDepthDetail) *SystemUser {
  41. spare := ""
  42. if len(pastureDepthList) > 0 {
  43. bt, _ := json.Marshal(pastureDepthList)
  44. spare = string(bt)
  45. }
  46. return &SystemUser{
  47. Name: req.Name,
  48. NickName: req.NickName,
  49. Gender: req.Gender,
  50. Mobile: req.Mobile,
  51. Password: req.Password,
  52. Avatar: "https://avatars.githubusercontent.com/u/9510375",
  53. IndicatorsKinds: DefaultFocusIndicators,
  54. PastureIds: pastureIds,
  55. IsShow: pasturePb.IsShow_Ok,
  56. IsDelete: pasturePb.IsShow_Ok,
  57. Remarks: req.Remarks,
  58. Spare: spare,
  59. }
  60. }
  61. func (s *SystemUser) GetPastureIds() []int32 {
  62. res := make([]int32, 0)
  63. if s.PastureIds != "" {
  64. pastureIds := strings.Split(s.PastureIds, ",")
  65. for _, idStr := range pastureIds {
  66. id, _ := strconv.Atoi(idStr)
  67. res = append(res, int32(id))
  68. }
  69. }
  70. return res
  71. }
  72. type UserModel struct {
  73. SystemUser *SystemUser
  74. AppPasture *AppPastureList
  75. }
  76. type SystemUserSlice []*SystemUser
  77. func (s SystemUserSlice) ToPB(deptList []*SystemDept, roleList []*SystemRole, appPastureList []*AppPastureList, systemUserDepthRoleMap map[int64]*SystemUserDepthRole) []*pasturePb.SearchUserRequest {
  78. deptMap := make(map[int32]*SystemDept)
  79. for _, v := range deptList {
  80. deptMap[int32(v.Id)] = v
  81. }
  82. roleMap := make(map[int32]*SystemRole)
  83. for _, v := range roleList {
  84. roleMap[int32(v.Id)] = v
  85. }
  86. appPastureMap := make(map[int32]*AppPastureList)
  87. for _, v := range appPastureList {
  88. appPastureMap[int32(v.Id)] = v
  89. }
  90. res := make([]*pasturePb.SearchUserRequest, len(s))
  91. for i, v := range s {
  92. userDepthName := make([]string, 0)
  93. deptIds := make([]int32, 0)
  94. if ud, ok := systemUserDepthRoleMap[v.Id]; ok {
  95. deptIds = ud.GetDepthIds()
  96. }
  97. for _, d := range deptIds {
  98. if de, ok := deptMap[d]; ok {
  99. userDepthName = append(userDepthName, de.Name)
  100. }
  101. }
  102. userRoleName := make([]string, 0)
  103. roleIds := make([]int32, 0)
  104. if rd, ok := systemUserDepthRoleMap[v.Id]; ok {
  105. roleIds = rd.GetRoleIds()
  106. }
  107. for _, r := range roleIds {
  108. if ro, ok := roleMap[r]; ok {
  109. userRoleName = append(userRoleName, ro.Name)
  110. }
  111. }
  112. pastureName := make([]string, 0)
  113. for _, p := range v.GetPastureIds() {
  114. if pa, ok := appPastureMap[p]; ok {
  115. pastureName = append(pastureName, pa.Name)
  116. }
  117. }
  118. pastureDepthList := make([]*pasturePb.PastureDepthDetail, 0)
  119. if len(v.Spare) > 0 {
  120. json.Unmarshal([]byte(v.Spare), &pastureDepthList)
  121. }
  122. res[i] = &pasturePb.SearchUserRequest{
  123. Id: int32(v.Id),
  124. Name: v.Name,
  125. NickName: v.NickName,
  126. Mobile: v.Mobile,
  127. Avatar: v.Avatar,
  128. Gender: v.Gender,
  129. IsShow: v.IsShow,
  130. IsDelete: v.IsDelete,
  131. Remarks: v.Remarks,
  132. PastureDepthList: pastureDepthList,
  133. DepthId: deptIds,
  134. DeptName: userDepthName,
  135. RoleList: nil,
  136. RoleId: roleIds,
  137. RoleName: userRoleName,
  138. PastureId: v.GetPastureIds(),
  139. PastureName: pastureName,
  140. CreatedAtFormat: time.Unix(v.CreatedAt, 0).Local().Format(LayoutTime),
  141. UpdatedAtFormat: time.Unix(v.UpdatedAt, 0).Local().Format(LayoutTime),
  142. }
  143. }
  144. return res
  145. }
  146. func (s SystemUserSlice) ToPB2() []*pasturePb.ConfigOptionsList {
  147. res := make([]*pasturePb.ConfigOptionsList, len(s))
  148. for i, d := range s {
  149. res[i] = &pasturePb.ConfigOptionsList{
  150. Value: int32(d.Id),
  151. Label: d.Name,
  152. Disabled: true,
  153. }
  154. }
  155. return res
  156. }