1、新增自定义用户私人价格2、优化api服务鉴权缓存3、新增管理员端对公充值

This commit is contained in:
2024-10-21 16:01:20 +08:00
parent 8896fd6b30
commit 2292d25d74
37 changed files with 1903 additions and 907 deletions

View File

@@ -34,7 +34,11 @@ func (l *CreateUserProductLogic) CreateUserProduct(in *sentinel.CreateUserProduc
if isExist {
return nil, errors.New("该产品已定阅读,无法重复订阅")
}
_, err = l.svcCtx.UserProductsModel.Insert(l.ctx, &model.UserProducts{UserId: in.UserId, ProductId: in.ProductId})
product, err := l.svcCtx.ProductsModel.FindOne(l.ctx, in.ProductId)
if err != nil {
return nil, err
}
_, err = l.svcCtx.UserProductsModel.Insert(l.ctx, &model.UserProducts{UserId: in.UserId, ProductId: in.ProductId, ProductPrice: product.ProductPrice})
if err != nil {
return nil, err
}

View File

@@ -0,0 +1,38 @@
package userproductlogic
import (
"context"
"tianyuan-api/apps/sentinel/internal/svc"
"tianyuan-api/apps/sentinel/sentinel"
"github.com/zeromicro/go-zero/core/logx"
)
type GetUserProductLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewGetUserProductLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserProductLogic {
return &GetUserProductLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *GetUserProductLogic) GetUserProduct(in *sentinel.UserProuctRequest) (*sentinel.UserProductResponse, error) {
userProduct, err := l.svcCtx.UserProductsModel.FindOneUserProduct(l.ctx, in.UserId, in.ProductId)
if err != nil {
return nil, err
}
return &sentinel.UserProductResponse{
Id: userProduct.Id,
UserId: userProduct.UserId,
ProductId: userProduct.ProductId,
ProductPrice: userProduct.ProductPrice,
}, nil
}

View File

@@ -25,7 +25,7 @@ func NewGetUserProductPageListLogic(ctx context.Context, svcCtx *svc.ServiceCont
}
}
func (l *GetUserProductPageListLogic) GetUserProductPageList(in *sentinel.UserProuctPageListRequest) (*sentinel.UserProductResponse, error) {
func (l *GetUserProductPageListLogic) GetUserProductPageList(in *sentinel.UserProuctPageListRequest) (*sentinel.UserProductPageListResponse, error) {
list, total, err := l.svcCtx.UserProductsModel.FindUserProductsList(l.ctx, in.UserId, in.Page, in.PageSize)
if err != nil {
return nil, err
@@ -45,7 +45,7 @@ func (l *GetUserProductPageListLogic) GetUserProductPageList(in *sentinel.UserPr
UpdatedAt: up.UpdatedAt.Format("2006-01-02 15:04:05"),
})
}
return &sentinel.UserProductResponse{
return &sentinel.UserProductPageListResponse{
Total: total,
UserProducts: userProducts,
}, nil

View File

@@ -61,7 +61,7 @@ func (m *defaultUserProductsModel) FindUserProductsList(ctx context.Context, use
p.product_code,
COALESCE(p.product_description, '') AS product_description,
p.product_group,
p.product_price,
up.product_price,
up.created_at,
up.updated_at
FROM user_products up
@@ -87,26 +87,11 @@ func (m *defaultUserProductsModel) FindUserProductsList(ctx context.Context, use
return userProducts, total, nil
}
func (m *customUserProductsModel) FindOneUserProduct(ctx context.Context, userId, productId int64) (*UserProducts, error) {
// 定义 Redis 缓存 Set 键
redisKey := fmt.Sprintf("user_products:%d", userId)
// 检查 Redis Set 中是否存在用户与产品的关联
isMember, err := m.rds.SismemberCtx(ctx, redisKey, productId)
if err == nil && isMember {
// 如果 Redis Set 中存在,返回空,因为不需要重复查询
return nil, nil
}
var userProduct UserProducts
query := fmt.Sprintf("SELECT %s FROM %s WHERE `user_id` = ? AND `product_id` = ? LIMIT 1", userProductsRows, m.table)
err = m.QueryRowNoCacheCtx(ctx, &userProduct, query, userId, productId)
err := m.QueryRowNoCacheCtx(ctx, &userProduct, query, userId, productId)
switch err {
case nil:
// 将用户产品的关联写入 Redis Set
_, err = m.rds.SaddCtx(ctx, redisKey, productId)
if err != nil {
return nil, err
}
return &userProduct, nil
case sqlc.ErrNotFound:
// 返回未找到的错误

View File

@@ -41,11 +41,12 @@ type (
}
UserProducts struct {
Id int64 `db:"id"` // 用户产品ID
UserId int64 `db:"user_id"` // 用户ID
ProductId int64 `db:"product_id"` // 产品ID
CreatedAt time.Time `db:"created_at"` // 创建时间
UpdatedAt time.Time `db:"updated_at"` // 更新时间
Id int64 `db:"id"` // 用户产品ID
UserId int64 `db:"user_id"` // 用户ID
ProductId int64 `db:"product_id"` // 产品ID
ProductPrice float64 `db:"product_price"` // 产品价格
CreatedAt time.Time `db:"created_at"` // 创建时间
UpdatedAt time.Time `db:"updated_at"` // 更新时间
}
)
@@ -85,8 +86,8 @@ func (m *defaultUserProductsModel) FindOne(ctx context.Context, id int64) (*User
func (m *defaultUserProductsModel) Insert(ctx context.Context, data *UserProducts) (sql.Result, error) {
userProductsIdKey := fmt.Sprintf("%s%v", cacheUserProductsIdPrefix, data.Id)
ret, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
query := fmt.Sprintf("insert into %s (%s) values (?, ?)", m.table, userProductsRowsExpectAutoSet)
return conn.ExecCtx(ctx, query, data.UserId, data.ProductId)
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?)", m.table, userProductsRowsExpectAutoSet)
return conn.ExecCtx(ctx, query, data.UserId, data.ProductId, data.ProductPrice)
}, userProductsIdKey)
return ret, err
}
@@ -95,7 +96,7 @@ func (m *defaultUserProductsModel) Update(ctx context.Context, data *UserProduct
userProductsIdKey := fmt.Sprintf("%s%v", cacheUserProductsIdPrefix, data.Id)
_, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, userProductsRowsWithPlaceHolder)
return conn.ExecCtx(ctx, query, data.UserId, data.ProductId, data.Id)
return conn.ExecCtx(ctx, query, data.UserId, data.ProductId, data.ProductPrice, data.Id)
}, userProductsIdKey)
return err
}

View File

@@ -29,11 +29,16 @@ func (s *UserProductServer) CreateUserProduct(ctx context.Context, in *sentinel.
return l.CreateUserProduct(in)
}
func (s *UserProductServer) GetUserProductPageList(ctx context.Context, in *sentinel.UserProuctPageListRequest) (*sentinel.UserProductResponse, error) {
func (s *UserProductServer) GetUserProductPageList(ctx context.Context, in *sentinel.UserProuctPageListRequest) (*sentinel.UserProductPageListResponse, error) {
l := userproductlogic.NewGetUserProductPageListLogic(ctx, s.svcCtx)
return l.GetUserProductPageList(in)
}
func (s *UserProductServer) GetUserProduct(ctx context.Context, in *sentinel.UserProuctRequest) (*sentinel.UserProductResponse, error) {
l := userproductlogic.NewGetUserProductLogic(ctx, s.svcCtx)
return l.GetUserProduct(in)
}
func (s *UserProductServer) MatchingUserIdProductCode(ctx context.Context, in *sentinel.MatchingUserIdProductCodeRequest) (*sentinel.MatchResponse, error) {
l := userproductlogic.NewMatchingUserIdProductCodeLogic(ctx, s.svcCtx)
return l.MatchingUserIdProductCode(in)