蛋蛋星球 后台端
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.
 
 
 
 

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