1、新增自定义用户私人价格2、优化api服务鉴权缓存3、新增管理员端对公充值
This commit is contained in:
@@ -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:
|
||||
// 返回未找到的错误
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user