1、新增自定义用户私人价格2、优化api服务鉴权缓存3、新增管理员端对公充值
This commit is contained in:
63
apps/user/internal/logic/user/getuserlistlogic.go
Normal file
63
apps/user/internal/logic/user/getuserlistlogic.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package userlogic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"tianyuan-api/apps/user/internal/svc"
|
||||
"tianyuan-api/apps/user/user"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type GetUserListLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewGetUserListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserListLogic {
|
||||
return &GetUserListLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetUserListLogic) GetUserList(in *user.UserListRequest) (*user.UserListResponse, error) {
|
||||
list, total, err := l.svcCtx.UserModel.FindUserPageList(l.ctx, in.Page, in.PageSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
userIds := make([]int64, len(list))
|
||||
for i, userItem := range list {
|
||||
userIds[i] = userItem.Id
|
||||
}
|
||||
|
||||
walletMap, err := l.svcCtx.WalletsModel.FindWalletsByUserIds(l.ctx, userIds)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var userList []*user.UserItem
|
||||
for _, userItem := range list {
|
||||
balance := float64(0)
|
||||
if wallet, ok := walletMap[userItem.Id]; ok {
|
||||
balance = wallet.Balance
|
||||
}
|
||||
|
||||
userList = append(userList, &user.UserItem{
|
||||
Id: userItem.Id,
|
||||
Username: userItem.Username,
|
||||
Phone: userItem.Phone,
|
||||
Disable: userItem.Disable,
|
||||
QuotaExceeded: userItem.QuotaExceeded,
|
||||
Balance: balance,
|
||||
CreatedAt: userItem.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
UpdatedAt: userItem.UpdatedAt.Format("2006-01-02 15:04:05"),
|
||||
})
|
||||
}
|
||||
|
||||
return &user.UserListResponse{
|
||||
List: userList,
|
||||
Total: total,
|
||||
}, nil
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"errors"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
"tianyuan-api/apps/sentinel/client/product"
|
||||
"tianyuan-api/apps/sentinel/sentinel"
|
||||
"tianyuan-api/apps/user/internal/model"
|
||||
"time"
|
||||
|
||||
@@ -40,7 +41,13 @@ func (l *UpdateWalletLogic) UpdateWallet(in *user.UpdateWalletRequest) (*user.Up
|
||||
if getProductByCodeErr != nil {
|
||||
return nil, getProductByCodeErr
|
||||
}
|
||||
|
||||
userProduct, getUserProductErr := l.svcCtx.UserProductRpc.GetUserProduct(l.ctx, &sentinel.UserProuctRequest{
|
||||
UserId: in.UserId,
|
||||
ProductId: consumeProduct.Id,
|
||||
})
|
||||
if getUserProductErr != nil {
|
||||
return nil, getUserProductErr
|
||||
}
|
||||
// 检查是否已经扣款
|
||||
deduction, FindOneByTransactionIdErr := l.svcCtx.DeductionsModel.FindOneByTransactionId(l.ctx, in.TransactionId)
|
||||
if FindOneByTransactionIdErr != nil {
|
||||
@@ -59,7 +66,7 @@ func (l *UpdateWalletLogic) UpdateWallet(in *user.UpdateWalletRequest) (*user.Up
|
||||
var err error
|
||||
// 尝试多次更新余额(用于乐观锁)
|
||||
for i := 0; i < maxRetries; i++ {
|
||||
err = l.svcCtx.WalletsModel.UpdateBalance(session, l.ctx, in.UserId, -consumeProduct.ProductPrice)
|
||||
err = l.svcCtx.WalletsModel.UpdateBalance(session, l.ctx, in.UserId, -userProduct.ProductPrice)
|
||||
if err == nil {
|
||||
// 成功,退出循环
|
||||
break
|
||||
@@ -82,7 +89,7 @@ func (l *UpdateWalletLogic) UpdateWallet(in *user.UpdateWalletRequest) (*user.Up
|
||||
_, err = l.svcCtx.DeductionsModel.InsertDeductionsTrans(ctx, &model.Deductions{
|
||||
TransactionId: in.TransactionId,
|
||||
UserId: in.UserId,
|
||||
Amount: consumeProduct.ProductPrice,
|
||||
Amount: userProduct.ProductPrice,
|
||||
}, session)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user