rts-sim-testing-service/service/user.go
2023-09-01 17:34:41 +08:00

121 lines
3.3 KiB
Go

package service
import (
"fmt"
"sort"
"time"
"go.uber.org/zap"
"joylink.club/bj-rtsts-server/db/dbquery"
"joylink.club/bj-rtsts-server/db/model"
"joylink.club/bj-rtsts-server/dto"
)
// 分页查询用户列表
func PagingQueryUser(query *dto.PageUserReqDto) (*dto.PageDto, 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{Total: int(total), PageQueryDto: query.PageQueryDto, Records: linkUserRole(records)}, err
}
func Register(user *dto.RegisterUser) {
defer func() {
err := recover()
if err != nil {
zap.S().Warn("用户注册失败", err)
panic(err)
}
}()
u := dbquery.User
uq := u.Where()
uq = uq.Where(u.Mobile.Eq(user.Mobile))
findCounter, _ := uq.Count()
if findCounter > 0 {
panic(dto.ErrorDto{Code: dto.DataAlreadyExist, Message: "重复的手机号"})
}
user.RegisterTime = time.Now()
u.Save(user)
}
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)
if len(roles) == 0 { // 如果没有指派用户时,指定普通用户信息
roles = []*model.AuthRole{{ID: int32(dto.USER), Name: "普通用户"}}
}
rids := make([]int32, len(roles))
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 {
userArr[i] = u
i++
}
sort.SliceStable(userArr, func(i, j int) bool {
return userArr[i].RegisterTime.Before(userArr[j].RegisterTime)
})
return userArr
}