system_user.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package model
  2. import (
  3. "fmt"
  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. CreatedAt int64 `json:"created_at"`
  25. UpdatedAt int64 `json:"updated_at"`
  26. }
  27. func (s *SystemUser) TableName() string {
  28. return "system_user"
  29. }
  30. func (s *SystemUser) UserUpdate(req *pasturePb.SearchUserRequest, deptIds, pastureIds string) {
  31. s.Name = req.Name
  32. s.NickName = req.NickName
  33. s.Mobile = req.Mobile
  34. s.Gender = req.Gender
  35. s.DeptIds = deptIds
  36. s.Remarks = req.Remarks
  37. s.IsShow = req.IsShow
  38. s.PastureIds = pastureIds
  39. }
  40. func NewSystemUser(req *pasturePb.SearchUserRequest, deptIds, pastureIds string) *SystemUser {
  41. return &SystemUser{
  42. Name: req.Name,
  43. NickName: req.NickName,
  44. Gender: req.Gender,
  45. Mobile: req.Mobile,
  46. Password: req.Password,
  47. Avatar: "",
  48. RoleIds: "",
  49. DeptIds: deptIds,
  50. IndicatorsKinds: fmt.Sprintf("%s,%s,%s,%s,%s,%s", AllCow, OutNumber, InputNumber, SalesVolume, FattenCattleNumber, AdultCow),
  51. PastureIds: pastureIds,
  52. IsShow: pasturePb.IsShow_Ok,
  53. IsDelete: pasturePb.IsShow_Ok,
  54. Remarks: req.Remarks,
  55. }
  56. }
  57. func (s *SystemUser) GetPastureIds() []int64 {
  58. res := make([]int64, 0)
  59. if s.PastureIds != "" {
  60. pastureIds := strings.Split(s.PastureIds, ",")
  61. for _, idStr := range pastureIds {
  62. id, _ := strconv.Atoi(idStr)
  63. res = append(res, int64(id))
  64. }
  65. }
  66. return res
  67. }
  68. func (s *SystemUser) GetRoleIds() []int32 {
  69. res := make([]int32, 0)
  70. if s.RoleIds != "" {
  71. roleIds := strings.Split(s.RoleIds, ",")
  72. for _, idStr := range roleIds {
  73. id, _ := strconv.Atoi(idStr)
  74. res = append(res, int32(id))
  75. }
  76. }
  77. return res
  78. }
  79. func (s *SystemUser) GetDepthIds() []int64 {
  80. res := make([]int64, 0)
  81. if s.DeptIds != "" {
  82. depthIds := strings.Split(s.DeptIds, ",")
  83. for _, idStr := range depthIds {
  84. id, _ := strconv.Atoi(idStr)
  85. res = append(res, int64(id))
  86. }
  87. }
  88. return res
  89. }
  90. type UserModel struct {
  91. SystemUser *SystemUser
  92. AppPasture *AppPastureList
  93. }
  94. type SystemUserSlice []*SystemUser
  95. func (s SystemUserSlice) ToPB(deptList []*SystemDept, roleList []*SystemRole) []*pasturePb.SearchUserRequest {
  96. deptMap := make(map[string]*SystemDept)
  97. for _, v := range deptList {
  98. deptMap[fmt.Sprintf("%d", v.Id)] = v
  99. }
  100. roleMap := make(map[string]*SystemRole)
  101. for _, v := range roleList {
  102. roleMap[fmt.Sprintf("%d", v.Id)] = v
  103. }
  104. res := make([]*pasturePb.SearchUserRequest, len(s))
  105. for i, v := range s {
  106. userDeptList := make([]*pasturePb.IdName, 0)
  107. if v.DeptIds != "" {
  108. deptIds := strings.Split(v.DeptIds, ",")
  109. for _, d := range deptIds {
  110. if de, ok := deptMap[d]; ok {
  111. userDeptList = append(userDeptList, &pasturePb.IdName{
  112. Id: int32(de.Id),
  113. Name: de.Name,
  114. })
  115. }
  116. }
  117. }
  118. userRoleList := make([]*pasturePb.IdName, 0)
  119. if v.RoleIds != "" {
  120. roleIds := strings.Split(v.RoleIds, ",")
  121. for _, r := range roleIds {
  122. if ro, ok := roleMap[r]; ok {
  123. userRoleList = append(userRoleList, &pasturePb.IdName{
  124. Id: int32(ro.Id),
  125. Name: ro.Name,
  126. })
  127. }
  128. }
  129. }
  130. res[i] = &pasturePb.SearchUserRequest{
  131. Id: int32(v.Id),
  132. Name: v.Name,
  133. Mobile: v.Mobile,
  134. NickName: v.NickName,
  135. Avatar: v.Avatar,
  136. Gender: v.Gender,
  137. IsShow: v.IsShow,
  138. IsDelete: v.IsDelete,
  139. Remarks: v.Remarks,
  140. //PastureDepthList: userDeptList,
  141. //RoleList: userRoleList,
  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. }