system_user.go 5.0 KB

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