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