system_user.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. Language string
  78. }
  79. type SystemUserSlice []*SystemUser
  80. func (s SystemUserSlice) ToPB(
  81. deptList []*SystemDept,
  82. roleList []*SystemRole,
  83. appPastureList []*AppPastureList,
  84. systemUserDepthRoleMap map[int64]*SystemUserDepthRole,
  85. ) []*pasturePb.SearchUserRequest {
  86. deptMap := make(map[int32]*SystemDept)
  87. for _, v := range deptList {
  88. deptMap[int32(v.Id)] = v
  89. }
  90. roleMap := make(map[int32]*SystemRole)
  91. for _, v := range roleList {
  92. roleMap[int32(v.Id)] = v
  93. }
  94. appPastureMap := make(map[int32]*AppPastureList)
  95. for _, v := range appPastureList {
  96. appPastureMap[int32(v.Id)] = v
  97. }
  98. res := make([]*pasturePb.SearchUserRequest, len(s))
  99. for i, v := range s {
  100. userDepthName := make([]string, 0)
  101. deptIds := make([]int32, 0)
  102. if ud, ok := systemUserDepthRoleMap[v.Id]; ok {
  103. deptIds = ud.GetDepthIds()
  104. }
  105. for _, d := range deptIds {
  106. if de, ok := deptMap[d]; ok {
  107. userDepthName = append(userDepthName, de.Name)
  108. }
  109. }
  110. userRoleName := make([]string, 0)
  111. roleIds := make([]int32, 0)
  112. if rd, ok := systemUserDepthRoleMap[v.Id]; ok {
  113. roleIds = rd.GetRoleIds()
  114. }
  115. for _, r := range roleIds {
  116. if ro, ok := roleMap[r]; ok {
  117. userRoleName = append(userRoleName, ro.Name)
  118. }
  119. }
  120. pastureName := make([]string, 0)
  121. for _, p := range v.GetPastureIds() {
  122. if pa, ok := appPastureMap[p]; ok {
  123. pastureName = append(pastureName, pa.Name)
  124. }
  125. }
  126. pastureDepthList := make([]*pasturePb.PastureDepthDetail, 0)
  127. if len(v.Spare) > 0 {
  128. json.Unmarshal([]byte(v.Spare), &pastureDepthList)
  129. }
  130. res[i] = &pasturePb.SearchUserRequest{
  131. Id: int32(v.Id),
  132. Name: v.Name,
  133. NickName: v.NickName,
  134. Mobile: v.Mobile,
  135. Avatar: v.Avatar,
  136. Gender: v.Gender,
  137. IsShow: v.IsShow,
  138. IsDelete: v.IsDelete,
  139. Remarks: v.Remarks,
  140. PastureDepthList: pastureDepthList,
  141. DepthId: deptIds,
  142. DeptName: userDepthName,
  143. RoleList: nil,
  144. RoleId: roleIds,
  145. RoleName: userRoleName,
  146. PastureId: v.GetPastureIds(),
  147. PastureName: pastureName,
  148. CreatedAtFormat: time.Unix(v.CreatedAt, 0).Local().Format(LayoutTime),
  149. UpdatedAtFormat: time.Unix(v.UpdatedAt, 0).Local().Format(LayoutTime),
  150. }
  151. }
  152. return res
  153. }
  154. func (s SystemUserSlice) ToPB2() []*pasturePb.ConfigOptionsList {
  155. res := make([]*pasturePb.ConfigOptionsList, len(s))
  156. for i, d := range s {
  157. res[i] = &pasturePb.ConfigOptionsList{
  158. Value: int32(d.Id),
  159. Label: d.Name,
  160. Disabled: true,
  161. }
  162. }
  163. return res
  164. }