rts-sim-testing-service/service/user.go

112 lines
2.6 KiB
Go
Raw Normal View History

package service
import (
"fmt"
"time"
2023-07-28 11:15:44 +08:00
"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().FindByPage(query.Offset(), query.Size)
if err != nil {
panic(err)
}
return &dto.PageDto{Total: int(total), PageQueryDto: query.PageQueryDto, Records: linkUserRole(records)}, err
}
2023-07-28 11:15:44 +08:00
func Register(user *dto.RegisterUser) {
2023-07-28 11:15:44 +08:00
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 {
2023-07-28 11:15:44 +08:00
panic("重复的手机号")
}
user.RegisterTime = time.Now()
u.Save(user)
}
func FindUserInfo(userId int32) *dto.RegisterUser {
defer func() {
err := recover()
if err != nil {
zap.S().Warn("用户详情查找错误 userId:"+string(userId), err)
panic(err)
}
}()
u := dbquery.User
uq := u.Where()
uq = uq.Where(u.ID.Eq(userId))
user, _ := uq.First()
if user == nil {
panic("没有获取到对应的用户信息")
}
return user
}
// 查询关联用户角色信息
func linkUserRole(users []*model.User) []*dto.UserRspDto {
un := len(users)
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)
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++
}
return userArr
}