system_user.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package model
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "time"
  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, deptIds, pastureIds string) {
  33. s.Name = req.Name
  34. s.NickName = req.NickName
  35. s.Mobile = req.Mobile
  36. s.Gender = req.Gender
  37. s.DeptIds = deptIds
  38. s.Remarks = req.Remarks
  39. s.IsShow = req.IsShow
  40. s.PastureIds = pastureIds
  41. }
  42. func NewSystemUser(req *pasturePb.SearchUserRequest, deptIds, 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. RoleIds: "",
  56. DeptIds: deptIds,
  57. IndicatorsKinds: fmt.Sprintf("%s,%s,%s,%s,%s,%s", AllCow, OutNumber, InputNumber, SalesVolume, FattenCattleNumber, AdultCow),
  58. PastureIds: pastureIds,
  59. IsShow: pasturePb.IsShow_Ok,
  60. IsDelete: pasturePb.IsShow_Ok,
  61. Remarks: req.Remarks,
  62. Spare: spare,
  63. }
  64. }
  65. func (s *SystemUser) GetPastureIds() []int32 {
  66. res := make([]int32, 0)
  67. if s.PastureIds != "" {
  68. pastureIds := strings.Split(s.PastureIds, ",")
  69. for _, idStr := range pastureIds {
  70. id, _ := strconv.Atoi(idStr)
  71. res = append(res, int32(id))
  72. }
  73. }
  74. return res
  75. }
  76. func (s *SystemUser) GetRoleIds() []int32 {
  77. res := make([]int32, 0)
  78. if s.RoleIds != "" {
  79. roleIds := strings.Split(s.RoleIds, ",")
  80. for _, idStr := range roleIds {
  81. id, _ := strconv.Atoi(idStr)
  82. res = append(res, int32(id))
  83. }
  84. }
  85. return res
  86. }
  87. func (s *SystemUser) GetDepthIds() []int32 {
  88. res := make([]int32, 0)
  89. if s.DeptIds != "" {
  90. depthIds := strings.Split(s.DeptIds, ",")
  91. for _, idStr := range depthIds {
  92. id, _ := strconv.Atoi(idStr)
  93. res = append(res, int32(id))
  94. }
  95. }
  96. return res
  97. }
  98. type UserModel struct {
  99. SystemUser *SystemUser
  100. AppPasture *AppPastureList
  101. }
  102. type SystemUserSlice []*SystemUser
  103. func (s SystemUserSlice) ToPB(deptList []*SystemDept, roleList []*SystemRole, appPastureList []*AppPastureList) []*pasturePb.SearchUserRequest {
  104. deptMap := make(map[int32]*SystemDept)
  105. for _, v := range deptList {
  106. deptMap[int32(v.Id)] = v
  107. }
  108. roleMap := make(map[int32]*SystemRole)
  109. for _, v := range roleList {
  110. roleMap[int32(v.Id)] = v
  111. }
  112. appPastureMap := make(map[int32]*AppPastureList)
  113. for _, v := range appPastureList {
  114. appPastureMap[int32(v.Id)] = v
  115. }
  116. res := make([]*pasturePb.SearchUserRequest, len(s))
  117. for i, v := range s {
  118. userDepthName := make([]string, 0)
  119. deptIds := v.GetDepthIds()
  120. for _, d := range deptIds {
  121. if de, ok := deptMap[d]; ok {
  122. userDepthName = append(userDepthName, de.Name)
  123. }
  124. }
  125. userRoleName := make([]string, 0)
  126. roleIds := v.GetRoleIds()
  127. for _, r := range roleIds {
  128. if ro, ok := roleMap[r]; ok {
  129. userRoleName = append(userRoleName, ro.Name)
  130. }
  131. }
  132. pastureName := make([]string, 0)
  133. for _, p := range v.GetPastureIds() {
  134. if pa, ok := appPastureMap[p]; ok {
  135. pastureName = append(pastureName, pa.Name)
  136. }
  137. }
  138. pastureDepthList := make([]*pasturePb.PastureDepthDetail, 0)
  139. if len(v.Spare) > 0 {
  140. json.Unmarshal([]byte(v.Spare), &pastureDepthList)
  141. }
  142. res[i] = &pasturePb.SearchUserRequest{
  143. Id: int32(v.Id),
  144. Name: v.Name,
  145. NickName: v.NickName,
  146. Mobile: v.Mobile,
  147. Avatar: v.Avatar,
  148. Gender: v.Gender,
  149. IsShow: v.IsShow,
  150. IsDelete: v.IsDelete,
  151. Remarks: v.Remarks,
  152. PastureDepthList: pastureDepthList,
  153. DepthId: deptIds,
  154. DeptName: userDepthName,
  155. RoleList: nil,
  156. RoleId: v.GetRoleIds(),
  157. RoleName: userRoleName,
  158. PastureId: v.GetPastureIds(),
  159. PastureName: pastureName,
  160. CreatedAtFormat: time.Unix(v.CreatedAt, 0).Local().Format(LayoutTime),
  161. UpdatedAtFormat: time.Unix(v.UpdatedAt, 0).Local().Format(LayoutTime),
  162. }
  163. }
  164. return res
  165. }
  166. func (s SystemUserSlice) ToPB2() []*pasturePb.ConfigOptionsList {
  167. res := make([]*pasturePb.ConfigOptionsList, len(s))
  168. for i, d := range s {
  169. res[i] = &pasturePb.ConfigOptionsList{
  170. Value: int32(d.Id),
  171. Label: d.Name,
  172. Disabled: true,
  173. }
  174. }
  175. return res
  176. }