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" "fmt" "strings" "time" "xorm.io/xorm" ) func UserManagementGetUsers(req *md.UserManagementGetUserListReq) ([]*md.UserInfo, int64, error) { users := make([]*md.UserInfo, 0) pageSess := userManagementGetUsersBindQuery(db.Db, req) countSess := userManagementGetUsersBindQuery(db.Db, req) total, err := countSess.Distinct("a.id").Count(&md.UserInfo{}) if err != nil { fmt.Println(err.Error()) return nil, 0, err } err = pageSess.Distinct("a.*").Limit(req.Limit, (req.Page-1)*req.Limit).Desc("a.id").Find(&users) if err != nil { return nil, 0, err } return users, total, nil } func userManagementGetUsersBindQuery(engine *xorm.Engine, req *md.UserManagementGetUserListReq) *xorm.Session { session := engine.Table("user").Alias("a"). 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.IsRecycle == "1" { session = session.In("a.state", []string{"4"}) } else { session = session.In("a.state", []string{"1", "2"}) } 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) } return session } 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: utils.Md5(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 } oldRecordIds := make([]string, 0) for _, record := range *records { oldRecordIds = append(oldRecordIds, utils.IntToStr(record.Id)) } newRecordIds := strings.Split(req.Tag, ",") // 需要删除的标签 delTags := utils.DiffArray(oldRecordIds, newRecordIds) addTags := utils.DiffArray(newRecordIds, oldRecordIds) if delTags != nil { // 删除不需要的标签记录 _, err := recordsDb.UserTagRecordsDeleteBySession(session, map[string]interface{}{ "key": "id", "value": delTags, }) if err != nil { return 0, err } } // 新增用户标签记录 if req.Tag != "" { newRecords := make([]*model.UserTagRecords, 0) now := time.Now() for _, tag := range addTags { newRecord := &model.UserTagRecords{ TagId: utils.StrToInt(tag), Uid: req.UID, CreateAt: now.Format("2006-01-02 15:04:05"), } newRecords = append(newRecords, newRecord) } affected, err = recordsDb.UserTagRecordsBatchInsertBySession(session, newRecords) if err != nil { session.Rollback() return 0, err } } err3 := session.Commit() if err3 != nil { session.Rollback() return 0, err3 } return affected, nil }