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

svc_user_management.go 5.4 KiB

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