package service import ( "fmt" "sort" "time" "joylink.club/bj-rtsts-server/db/dbquery" "joylink.club/bj-rtsts-server/db/model" "joylink.club/bj-rtsts-server/dto" "joylink.club/bj-rtsts-server/sys_error" ) // 分页查询用户列表 func PagingQueryUser(query *dto.PageUserReqDto) (*dto.PageDto[*dto.UserRspDto], error) { u := dbquery.User uq := u.Where() if query.Name != "" { uq = uq.Where(u.Name.Like(fmt.Sprintf("%%%s%%", query.Name))) } if query.Mobile != "" { uq = uq.Where(u.Mobile.Like(fmt.Sprintf("%%%s%%", query.Mobile))) } records, total, err := uq.Debug().Order(u.RegisterTime).FindByPage(query.Offset(), query.Size) if err != nil { panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()}) } return &dto.PageDto[*dto.UserRspDto]{Total: int(total), PageQueryDto: query.PageQueryDto, Records: linkUserRole(records)}, err } func Register(user *dto.RegisterUser) error { u := dbquery.User uq := u.Where() uq = uq.Where(u.Mobile.Eq(user.Mobile)) findCounter, err := uq.Count() if err != nil { return sys_error.New("数据服务异常", err) } if findCounter > 0 { return sys_error.New("手机号已存在") } user.RegisterTime = time.Now() err = u.Save(user) if err != nil { return sys_error.New("数据服务异常", err) } return nil } func FindUserInfo(userId int32) *dto.UserRspDto { user, _ := dbquery.User.Where(dbquery.User.ID.Eq(userId)).First() if user == nil { panic(dto.ErrorDto{Code: dto.DataNotExist, Message: "没有获取到对应的用户信息"}) } rspUser := dto.ConvertFromUserDto(user) roles := QueryAuthRoleByUid(user.ID) rids := make([]int32, len(roles)) if len(roles) == 0 { // 如果没有指派用户时,指定普通用户信息 rspUser.Roles = append(rspUser.Roles, dto.GetDefaultAuthRole()) rids = append(rids, int32(dto.USER)) } else { for i, r := range roles { rids[i] = r.ID rspUser.Roles = append(rspUser.Roles, &dto.AuthRoleRspDto{Id: r.ID, Name: r.Name}) } } // 查询权限路径 rspUser.Paths = QueryAuthApiPathByRids(rids) return rspUser } // 查询关联用户角色信息 func linkUserRole(users []*model.User) []*dto.UserRspDto { un := len(users) if un == 0 { return []*dto.UserRspDto{} } uids := make([]int32, un) userMap := make(map[int32]*dto.UserRspDto, un) for i, u := range users { uids[i] = u.ID userMap[u.ID] = &dto.UserRspDto{ ID: u.ID, Name: u.Name, Mobile: u.Mobile, RegisterTime: u.RegisterTime, Roles: []*dto.AuthRoleRspDto{}, } } // 获取角色信息 roles, err1 := dbquery.AuthRole.Find() if err1 != nil { panic(dto.ErrorDto{Code: dto.DataOperationError, Message: err1.Error()}) } roleMap := make(map[int32]*model.AuthRole, len(roles)) for _, r := range roles { roleMap[r.ID] = r } // 用户角色关联信息 utls, err2 := dbquery.AuthRoleUser.Where(dbquery.AuthRoleUser.UID.In(uids...)).Find() if err2 != nil { panic(dto.ErrorDto{Code: dto.DataOperationError, Message: err2.Error()}) } // 为用户赋值角色 for _, ul := range utls { u, r := userMap[ul.UID], roleMap[ul.Rid] if u == nil || r == nil { continue } u.Roles = append(u.Roles, &dto.AuthRoleRspDto{Id: r.ID, Name: r.Name}) } userArr := make([]*dto.UserRspDto, un) i := 0 for _, u := range userMap { if len(u.Roles) == 0 { u.Roles = append(u.Roles, dto.GetDefaultAuthRole()) } userArr[i] = u i++ } sort.SliceStable(userArr, func(i, j int) bool { return userArr[i].RegisterTime.Before(userArr[j].RegisterTime) }) return userArr }