package svc import ( "applet/app/db" "applet/app/md/member_center" "applet/app/utils" "code.fnuoos.com/EggPlanet/egg_models.git/src/implement" "code.fnuoos.com/EggPlanet/egg_models.git/src/model" "strings" "time" "xorm.io/xorm" ) func UserManagementGetUsers(engine *xorm.Engine, req *md.UserManagementGetUserListReq) (*[]md.UserInfo, int64, error) { users := make([]md.UserInfo, 0) session := engine.Table("user").Alias("a").Distinct("a.id"). Join("LEFT OUTER", []string{"user", "b"}, "a.parent_uid = b.id"). Join("LEFT OUTER", "user_tag_records", "user_tag_records.uid = a.id") if req.ID != 0 { session = session.Where("a.id = ?", req.ID) } if req.Nickname != "" { session = session.Where("a.nickname = ?", req.Nickname) } if req.RecommendID != 0 { session = session.Where("a.parent_uid = ?", req.RecommendID) } if req.Tag != 0 { session = session.Where("user_tag_records.tag_id = ?", req.Tag) } if req.Phone != "" { session = session.Where("a.phone = ?", req.Phone) } if req.InviteCode != "" { session = session.Where("a.system_invite_code = ? or a.custom_invite_code = ?", req.InviteCode, req.InviteCode) } if req.ParentPhone != "" { session = session.Where("b.phone= ?", req.ParentPhone) } if req.ParentInviteCode != "" { session = session.Where("b.system_invite_code = ? or b.custom_invite_code = ?", req.ParentInviteCode, req.ParentInviteCode) } if req.Sex != 0 { session = session.Where("a.sex = ?") } if req.UnionId != "" { session = session.Where("a.union_id = ?", req.UnionId) } if req.Level != 0 { session = session.Where("a.level = ?", req.Level) } if req.RegisterType != 0 { session = session.Where("a.register_type = ?", req.RegisterType) } if req.State != 0 { session = session.Where("a.state = ?", req.State) } if req.IsRealName != 0 { session = session.Where("a.is_real_name = ?", req.IsRealName) } if req.Memo != "" { session = session.Where("a.memo = ?", req.Memo) } if req.RegisterBefore != "" && req.RegisterAfter != "" { session = session.Where("a.create_at > ? and a.create_at < ?", req.RegisterBefore, req.RegisterAfter) } if req.LoginBefore != "" && req.LoginAfter != "" { session = session.Where("a.update_at > ? and a.update_at < ?", req.LoginBefore, req.LoginAfter) } total, err := session.Limit(req.Limit, (req.Page-1)*req.Limit).Asc("a.id").FindAndCount(&users) if err != nil { return nil, 0, err } return &users, total, nil } func UserManagementUpdateUserInfo(engine *xorm.Engine, req *md.UserManagementUpdateUserInfoReq) (int64, error) { session := engine.NewSession() defer session.Close() session.Begin() userDb := implement.NewUserDb(db.Db) var affected int64 var err1 error forceColumns := make([]string, 0) user := model.User{ Id: req.UID, Phone: req.Phone, UnionId: req.UnionId, Nickname: req.Nickname, Avatar: req.Avatar, LastLoginIp: req.LastLoginIp, Memo: req.Memo, Password: req.Password, } if req.Level != "" { user.Level = utils.StrToInt(req.Level) } if req.State != "" { user.State = utils.StrToInt(req.State) } if req.Sex != "" { user.Sex = utils.StrToInt(req.Sex) } if req.ParentUid != "" { user.ParentUid = utils.StrToInt64(req.ParentUid) } else { user.ParentUid = 0 } forceColumns = []string{"sex", "parent_uid"} affected, err1 = userDb.UserUpdateBySession(session, req.UID, &user, forceColumns...) if err1 != nil { session.Rollback() return 0, err1 } relateDb := implement.NewUserRelateDb(db.Db) relateForceColumns := []string{"sex", "parent_uid"} relate := &model.UserRelate{ ParentUid: user.ParentUid, } affected, err1 = relateDb.UpdateUserRelateBySession(session, req.UID, relate, relateForceColumns...) if err1 != nil { session.Rollback() return 0, err1 } // 获取用户所有标签记录 recordsDb := implement.NewUserTagRecordsDb(db.Db) records, err := recordsDb.UserTagRecordsFindByParams(map[string]interface{}{ "key": "uid", "value": req.UID, }) if err != nil { return 0, err } if records != nil { recordIds := make([]int, 0) for _, record := range *records { recordIds = append(recordIds, record.TagId) } // 删除所有用户的所有标签记录 _, err := recordsDb.UserTagRecordsDeleteBySession(session, map[string]interface{}{ "key": "id", "value": recordIds, }) if err != nil { return 0, err } } // 新增用户标签记录 if req.Tag != "" { newRecords := make([]*model.UserTagRecords, 0) now := time.Now() tags := strings.Split(req.Tag, ",") for _, tag := range tags { newRecord := &model.UserTagRecords{ TagId: utils.StrToInt(tag), Uid: req.UID, Memo: req.Memo, CreateAt: now.Format("2006-01-02 15:04:05"), } newRecords = append(newRecords, newRecord) } affected, err = recordsDb.UserTagRecordsBatchInsert(newRecords) if err != nil { return 0, err } } err3 := session.Commit() if err3 != nil { session.Rollback() return 0, err3 } return affected, nil }