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

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