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

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