system_user.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package model
  2. import (
  3. "fmt"
  4. "net/http"
  5. "strings"
  6. "time"
  7. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  8. operationPb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/operation"
  9. )
  10. type SystemUser struct {
  11. Id int64 `json:"id"`
  12. Name string `json:"name"`
  13. NickName string `json:"nickName"`
  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. Gender pasturePb.Genders_Kind `json:"gender"`
  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. CreatedAt int64 `json:"created_at"`
  26. UpdatedAt int64 `json:"updated_at"`
  27. }
  28. func (s *SystemUser) TableName() string {
  29. return "system_user"
  30. }
  31. func (s *SystemUser) SystemUserFormat(userRoles []*SystemRole, pastures []*operationPb.UserPasture) *operationPb.UserAuth {
  32. roles := make([]*operationPb.UserRole, len(userRoles))
  33. for k, v := range userRoles {
  34. roles[k] = &operationPb.UserRole{
  35. Id: int32(v.Id),
  36. Name: v.Name,
  37. }
  38. }
  39. return &operationPb.UserAuth{
  40. Code: http.StatusOK,
  41. Msg: "ok",
  42. Data: &operationPb.UserAuthData{
  43. UserName: s.Name,
  44. Roles: roles,
  45. Pastures: pastures,
  46. },
  47. }
  48. }
  49. type UserModel struct {
  50. SystemUser *SystemUser
  51. AppPasture *AppPastureList
  52. }
  53. type SystemUserSlice []*SystemUser
  54. func (s SystemUserSlice) ToPB(deptList []*SystemDept, roleList []*SystemRole) []*pasturePb.SearchUserRequest {
  55. deptMap := make(map[string]*SystemDept)
  56. for _, v := range deptList {
  57. deptMap[fmt.Sprintf("%d", v.Id)] = v
  58. }
  59. roleMap := make(map[string]*SystemRole)
  60. for _, v := range roleList {
  61. roleMap[fmt.Sprintf("%d", v.Id)] = v
  62. }
  63. res := make([]*pasturePb.SearchUserRequest, len(s))
  64. for i, v := range s {
  65. userDeptList := make([]*pasturePb.IdName, 0)
  66. if v.DeptIds != "" {
  67. deptIds := strings.Split(v.DeptIds, ",")
  68. for _, d := range deptIds {
  69. if de, ok := deptMap[d]; ok {
  70. userDeptList = append(userDeptList, &pasturePb.IdName{
  71. Id: int32(de.Id),
  72. Name: de.Name,
  73. })
  74. }
  75. }
  76. }
  77. userRoleList := make([]*pasturePb.IdName, 0)
  78. if v.RoleIds != "" {
  79. roleIds := strings.Split(v.RoleIds, ",")
  80. for _, r := range roleIds {
  81. if ro, ok := roleMap[r]; ok {
  82. userRoleList = append(userRoleList, &pasturePb.IdName{
  83. Id: int32(ro.Id),
  84. Name: ro.Name,
  85. })
  86. }
  87. }
  88. }
  89. res[i] = &pasturePb.SearchUserRequest{
  90. Id: int32(v.Id),
  91. Name: v.Name,
  92. Mobile: v.Mobile,
  93. NickName: v.NickName,
  94. Avatar: v.Avatar,
  95. Gender: v.Gender,
  96. IsShow: v.IsShow,
  97. IsDelete: v.IsDelete,
  98. Remarks: v.Remarks,
  99. DepthList: userDeptList,
  100. RoleList: userRoleList,
  101. CreatedAtFormat: time.Unix(v.CreatedAt, 0).Local().Format(LayoutTime),
  102. UpdatedAtFormat: time.Unix(v.UpdatedAt, 0).Local().Format(LayoutTime),
  103. }
  104. }
  105. return res
  106. }
  107. func (s *SystemUser) ToPb() *operationPb.AddSystemUser {
  108. return &operationPb.AddSystemUser{
  109. Id: int32(s.Id),
  110. Name: s.Name,
  111. CreatedAt: int32(s.CreatedAt),
  112. CreatedAtFormat: time.Unix(s.CreatedAt, 0).Local().Format(LayoutTime),
  113. RoleIds: []int32{},
  114. }
  115. }
  116. func (s SystemUserSlice) ToPB2() []*pasturePb.ConfigOptionsList {
  117. res := make([]*pasturePb.ConfigOptionsList, len(s))
  118. for i, d := range s {
  119. res[i] = &pasturePb.ConfigOptionsList{
  120. Value: int32(d.Id),
  121. Label: d.Name,
  122. Disabled: true,
  123. }
  124. }
  125. return res
  126. }