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" "errors" "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.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) 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 user model.User get, err2 := session.Where("id=?", req.UID).Get(&user) if get == false || err2 != nil { session.Rollback() return 0, errors.New("用户不存在") } if user.Phone != req.Phone { //判断下有没有重复的 count, _ := session.Where("id<>? and phone=?", req.UID, req.Phone).Count(&model.User{}) if count > 0 { session.Rollback() return 0, errors.New("手机号已被绑定") } user.Phone = req.Phone } var affected int64 var err1 error user.Nickname = req.Nickname user.WechatAccount = req.WechatAccount user.Avatar = req.Avatar user.Memo = req.Memo if req.Password != "" { user.Password = utils.Md5(req.Password) } if req.Level != "" && req.Level != "undefined" { 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 utils.StrToInt(req.ParentUid) > 0 { if utils.StrToInt64(req.ParentUid) != user.ParentUid { session.Rollback() return 0, errors.New("不支持修改推荐人") } } forceColumns := []string{"phone", "nickname", "wechat_account", "password", "level", "state", "sex"} affected, err1 = userDb.UserUpdateBySession(session, req.UID, &user, forceColumns...) 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 }