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
|
||||
|
||||
@@ -18,6 +18,7 @@ type (
|
||||
usersModel
|
||||
UpdateUserTrans(ctx context.Context, user *Users, session sqlx.Session) (sql.Result, error)
|
||||
FindOneTrans(ctx context.Context, userId int64, session sqlx.Session) (*Users, error)
|
||||
FindUserPageList(ctx context.Context, page, pageSize int64) ([]Users, int64, error)
|
||||
}
|
||||
|
||||
customUsersModel struct {
|
||||
@@ -76,3 +77,23 @@ func (m *defaultUsersModel) FindOneTrans(ctx context.Context, userId int64, sess
|
||||
// 返回查询结果
|
||||
return &user, nil
|
||||
}
|
||||
func (m *customUsersModel) FindUserPageList(ctx context.Context, page, pageSize int64) ([]Users, int64, error) {
|
||||
offset := (page - 1) * pageSize
|
||||
var users []Users
|
||||
|
||||
query := fmt.Sprintf("SELECT * FROM %s ORDER BY created_at DESC LIMIT ?,?", m.table)
|
||||
err := m.QueryRowsNoCacheCtx(ctx, &users, query, offset, pageSize)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
// 查询总数量
|
||||
var total int64
|
||||
countQuery := fmt.Sprintf("SELECT COUNT(*) FROM %s", m.table)
|
||||
err = m.QueryRowNoCacheCtx(ctx, &total, countQuery)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return users, total, nil
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"fmt"
|
||||
"github.com/zeromicro/go-zero/core/stores/cache"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var _ WalletsModel = (*customWalletsModel)(nil)
|
||||
@@ -19,6 +20,7 @@ type (
|
||||
InsertWalletsTrans(ctx context.Context, wallets *Wallets, session sqlx.Session) (sql.Result, error)
|
||||
UpdateBalance(session sqlx.Session, ctx context.Context, userId int64, amount float64) error
|
||||
TransCtx(ctx context.Context, fn func(ctx context.Context, session sqlx.Session) error) error
|
||||
FindWalletsByUserIds(ctx context.Context, userIds []int64) (map[int64]*Wallets, error)
|
||||
}
|
||||
|
||||
customWalletsModel struct {
|
||||
@@ -88,3 +90,36 @@ func (m *customWalletsModel) InsertWalletsTrans(ctx context.Context, wallets *Wa
|
||||
|
||||
return ret, err
|
||||
}
|
||||
func (m *customWalletsModel) FindWalletsByUserIds(ctx context.Context, userIds []int64) (map[int64]*Wallets, error) {
|
||||
if len(userIds) == 0 {
|
||||
return make(map[int64]*Wallets), nil
|
||||
}
|
||||
|
||||
queryBuilder := strings.Builder{}
|
||||
queryBuilder.WriteString(fmt.Sprintf("SELECT user_id, balance FROM %s WHERE user_id IN (", m.table))
|
||||
|
||||
placeholders := make([]string, len(userIds))
|
||||
args := make([]interface{}, len(userIds))
|
||||
for i, userId := range userIds {
|
||||
placeholders[i] = "?"
|
||||
args[i] = userId
|
||||
}
|
||||
|
||||
queryBuilder.WriteString(strings.Join(placeholders, ","))
|
||||
queryBuilder.WriteString(")")
|
||||
|
||||
query := queryBuilder.String()
|
||||
|
||||
var wallets []*Wallets
|
||||
err := m.QueryRowNoCacheCtx(ctx, &wallets, query, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
walletMap := make(map[int64]*Wallets)
|
||||
for _, wallet := range wallets {
|
||||
walletMap[wallet.UserId] = wallet
|
||||
}
|
||||
|
||||
return walletMap, nil
|
||||
}
|
||||
|
||||
@@ -38,3 +38,8 @@ func (s *UserServer) GetEnterpriseAuthStatus(ctx context.Context, in *user.GetEn
|
||||
l := userlogic.NewGetEnterpriseAuthStatusLogic(ctx, s.svcCtx)
|
||||
return l.GetEnterpriseAuthStatus(in)
|
||||
}
|
||||
|
||||
func (s *UserServer) GetUserList(ctx context.Context, in *user.UserListRequest) (*user.UserListResponse, error) {
|
||||
l := userlogic.NewGetUserListLogic(ctx, s.svcCtx)
|
||||
return l.GetUserList(in)
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ type ServiceContext struct {
|
||||
UserConfigModel model.UserConfigModel
|
||||
SecretRpc sentinel.SecretClient
|
||||
ProductRpc sentinel.ProductClient
|
||||
UserProductRpc sentinel.UserProductClient
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
@@ -35,7 +36,7 @@ func NewServiceContext(c config.Config) *ServiceContext {
|
||||
// 初始化 SecretRpc 和 ProductRpc
|
||||
var secretRpc sentinel.SecretClient
|
||||
var productRpc sentinel.ProductClient
|
||||
|
||||
var userProductRpc sentinel.UserProductClient
|
||||
// 捕获RPC初始化时的错误,但不影响服务启动
|
||||
secretClient, err := zrpc.NewClient(c.SentinelRpc)
|
||||
if err != nil {
|
||||
@@ -49,6 +50,7 @@ func NewServiceContext(c config.Config) *ServiceContext {
|
||||
logx.Errorf("Failed to connect to ProductRpc: %v", err)
|
||||
} else {
|
||||
productRpc = sentinel.NewProductClient(productClient.Conn())
|
||||
userProductRpc = sentinel.NewUserProductClient(productClient.Conn())
|
||||
}
|
||||
|
||||
// 使用 MustNewRedis 来初始化 Redis 客户端
|
||||
@@ -66,5 +68,6 @@ func NewServiceContext(c config.Config) *ServiceContext {
|
||||
RechargeModel: model.NewRechargeModel(db, c.CacheRedis),
|
||||
SecretRpc: secretRpc, // 捕获连接错误后,继续运行
|
||||
ProductRpc: productRpc, // 捕获连接错误后,继续运行
|
||||
UserProductRpc: userProductRpc,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user