2023-07-14 10:16:58 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-08-30 13:25:57 +08:00
|
|
|
"time"
|
|
|
|
|
2023-07-28 11:15:44 +08:00
|
|
|
"go.uber.org/zap"
|
2023-07-14 10:16:58 +08:00
|
|
|
"joylink.club/bj-rtsts-server/db/dbquery"
|
2023-08-30 13:25:57 +08:00
|
|
|
"joylink.club/bj-rtsts-server/db/model"
|
2023-07-14 10:16:58 +08:00
|
|
|
"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 {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
2023-07-14 10:16:58 +08:00
|
|
|
}
|
2023-08-30 13:25:57 +08:00
|
|
|
return &dto.PageDto{Total: int(total), PageQueryDto: query.PageQueryDto, Records: linkUserRole(records)}, err
|
2023-07-14 10:16:58 +08:00
|
|
|
}
|
2023-07-28 11:15:44 +08:00
|
|
|
|
2023-07-28 15:49: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))
|
2023-07-28 15:49:44 +08:00
|
|
|
findCounter, _ := uq.Count()
|
|
|
|
if findCounter > 0 {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.DataAlreadyExist, Message: "重复的手机号"})
|
2023-07-28 11:15:44 +08:00
|
|
|
}
|
|
|
|
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 {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.DataNotExist, Message: "没有获取到对应的用户信息"})
|
2023-07-28 11:15:44 +08:00
|
|
|
}
|
|
|
|
return user
|
|
|
|
}
|
2023-08-30 13:25:57 +08:00
|
|
|
|
|
|
|
// 查询关联用户角色信息
|
|
|
|
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
|
|
|
|
}
|