system_user.go 5.1 KB

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