rts-sim-testing-service/service/user.go
2023-09-01 16:10:29 +08:00

133 lines
3.5 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)
// 查找关联关系
aru := dbquery.AuthRoleUser
arus, err1 := aru.Distinct(aru.Rid).Select(aru.Rid).Where(aru.UID.Eq(user.ID)).Find()
if err1 != nil {
panic(dto.ErrorDto{Code: dto.DataNotExist, Message: err1.Error()})
}
if len(arus) == 0 {
return rspUser
}
rids := make([]int32, len(arus))
for i, v := range arus {
rids[i] = v.Rid
}
roles, err2 := dbquery.AuthRole.Where(dbquery.AuthRole.ID.In(rids...)).Find()
if err2 != nil {
panic(dto.ErrorDto{Code: dto.DataNotExist, Message: err2.Error()})
}
if len(roles) == 0 {
return rspUser
}
for _, r := range roles {
rspUser.Roles = append(rspUser.Roles, &dto.AuthRoleRspDto{Id: r.ID, Name: r.Name})
}
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
}