蛋蛋星球 后台端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

177 lines
4.8 KiB

  1. package svc
  2. import (
  3. "applet/app/db"
  4. "applet/app/md/member_center"
  5. "applet/app/utils"
  6. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  7. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  8. "time"
  9. "xorm.io/xorm"
  10. )
  11. func UserManagementGetUsers(engine *xorm.Engine, req *md.UserManagementGetUserListReq) (*[]md.UserInfo, int64, error) {
  12. users := make([]md.UserInfo, 0)
  13. session := engine.Table("user").Alias("a").Distinct("a.id").
  14. Join("LEFT OUTER", []string{"user", "b"}, "a.parent_uid = b.id").
  15. Join("LEFT OUTER", "user_tag_records", "user_tag_records.uid = a.id")
  16. if req.ID != 0 {
  17. session = session.Where("a.id = ?", req.ID)
  18. }
  19. if req.Nickname != "" {
  20. session = session.Where("a.nickname = ?", req.Nickname)
  21. }
  22. if req.RecommendID != 0 {
  23. session = session.Where("a.parent_uid = ?", req.RecommendID)
  24. }
  25. if req.Tag != 0 {
  26. session = session.Where("user_tag_records.tag_id = ?", req.Tag)
  27. }
  28. if req.Phone != "" {
  29. session = session.Where("a.phone = ?", req.Phone)
  30. }
  31. if req.InviteCode != "" {
  32. session = session.Where("a.system_invite_code = ? or a.custom_invite_code = ?", req.InviteCode, req.InviteCode)
  33. }
  34. if req.ParentPhone != "" {
  35. session = session.Where("b.phone= ?", req.ParentPhone)
  36. }
  37. if req.ParentInviteCode != "" {
  38. session = session.Where("b.system_invite_code = ? or b.custom_invite_code = ?", req.ParentInviteCode, req.ParentInviteCode)
  39. }
  40. if req.Sex != 0 {
  41. session = session.Where("a.sex = ?")
  42. }
  43. if req.UnionId != "" {
  44. session = session.Where("a.union_id = ?", req.UnionId)
  45. }
  46. if req.Level != 0 {
  47. session = session.Where("a.level = ?", req.Level)
  48. }
  49. if req.RegisterType != 0 {
  50. session = session.Where("a.register_type = ?", req.RegisterType)
  51. }
  52. if req.State != 0 {
  53. session = session.Where("a.state = ?", req.State)
  54. }
  55. if req.IsRealName != 0 {
  56. session = session.Where("a.is_real_name = ?", req.IsRealName)
  57. }
  58. if req.Memo != "" {
  59. session = session.Where("a.memo = ?", req.Memo)
  60. }
  61. if req.RegisterBefore != "" && req.RegisterAfter != "" {
  62. session = session.Where("a.create_at > ? and a.create_at < ?", req.RegisterBefore, req.RegisterAfter)
  63. }
  64. if req.LoginBefore != "" && req.LoginAfter != "" {
  65. session = session.Where("a.update_at > ? and a.update_at < ?", req.LoginBefore, req.LoginAfter)
  66. }
  67. total, err := session.Limit(req.Limit, (req.Page-1)*req.Limit).Asc("a.id").FindAndCount(&users)
  68. if err != nil {
  69. return nil, 0, err
  70. }
  71. return &users, total, nil
  72. }
  73. func UserManagementUpdateUserInfo(engine *xorm.Engine, req *md.UserManagementUpdateUserInfoReq) (int64, error) {
  74. session := engine.NewSession()
  75. defer session.Close()
  76. session.Begin()
  77. userDb := implement.NewUserDb(db.Db)
  78. var affected int64
  79. var err1 error
  80. forceColumns := make([]string, 0)
  81. user := model.User{
  82. Id: req.UID,
  83. Phone: req.Phone,
  84. UnionId: req.UnionId,
  85. Nickname: req.Nickname,
  86. Avatar: req.Avatar,
  87. LastLoginIp: req.LastLoginIp,
  88. Memo: req.Memo,
  89. }
  90. if req.Level != "" {
  91. user.Level = utils.StrToInt(req.Level)
  92. }
  93. if req.State != "" {
  94. user.State = utils.StrToInt(req.State)
  95. }
  96. if req.Sex != "" {
  97. user.Sex = utils.StrToInt(req.Sex)
  98. }
  99. if req.ParentUid != "" {
  100. user.ParentUid = utils.StrToInt64(req.ParentUid)
  101. } else {
  102. user.ParentUid = 0
  103. }
  104. forceColumns = []string{"sex", "parent_uid"}
  105. affected, err1 = userDb.UserUpdateBySession(session, req.UID, &user, forceColumns...)
  106. if err1 != nil {
  107. session.Rollback()
  108. return 0, err1
  109. }
  110. relateDb := implement.NewUserRelateDb(db.Db)
  111. relateForceColumns := []string{"sex", "parent_uid"}
  112. relate := &model.UserRelate{
  113. ParentUid: user.ParentUid,
  114. }
  115. affected, err1 = relateDb.UpdateUserRelateBySession(session, req.UID, relate, relateForceColumns...)
  116. if err1 != nil {
  117. session.Rollback()
  118. return 0, err1
  119. }
  120. // 获取用户所有标签记录
  121. recordsDb := implement.NewUserTagRecordsDb(db.Db)
  122. records, err := recordsDb.UserTagRecordsFindByParams(map[string]interface{}{
  123. "key": "uid",
  124. "value": req.UID,
  125. })
  126. if err != nil {
  127. return 0, err
  128. }
  129. if records != nil {
  130. recordIds := make([]int, 0)
  131. for _, record := range *records {
  132. recordIds = append(recordIds, record.TagId)
  133. }
  134. // 删除所有用户的所有标签记录
  135. _, err := recordsDb.UserTagRecordsDeleteBySession(session, map[string]interface{}{
  136. "key": "id",
  137. "value": recordIds,
  138. })
  139. if err != nil {
  140. return 0, err
  141. }
  142. }
  143. // 新增用户标签记录
  144. if len(req.Tag) > 0 {
  145. newRecords := make([]*model.UserTagRecords, 0)
  146. now := time.Now()
  147. for _, tag := range req.Tag {
  148. newRecord := &model.UserTagRecords{
  149. TagId: utils.StrToInt(tag),
  150. Uid: req.UID,
  151. Memo: req.Memo,
  152. CreateAt: now.Format("2006-01-02 15:04:05"),
  153. }
  154. newRecords = append(newRecords, newRecord)
  155. }
  156. affected, err = recordsDb.UserTagRecordsBatchInsert(newRecords)
  157. if err != nil {
  158. return 0, err
  159. }
  160. }
  161. err3 := session.Commit()
  162. if err3 != nil {
  163. session.Rollback()
  164. return 0, err3
  165. }
  166. return affected, nil
  167. }