Files
full/module/ec/order/internal/logic/cart/fetch.go
2026-09-22 21:15:34 +08:00

77 lines
2.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package cart
import (
"context"
"bsm/full/module/ec/order/internal/impl"
"bsm/full/module/ec/order/internal/models"
pb "bsm/full/module/ec/order/pb"
"git.apinb.com/bsm-sdk/core/errcode"
"git.apinb.com/bsm-sdk/core/printer"
"git.apinb.com/bsm-sdk/core/service"
)
// 获取购物车的商品数据
func Fetch(ctx context.Context, in *pb.CartGetRequest) (reply *pb.CartGetReply, err error) {
auth, err := service.ParseMetaCtx(ctx, nil)
if err != nil {
return nil, err
}
var (
result = make([]*pb.CartItem, 0)
cart = make([]*models.OrderCart, 0)
)
if in.GetPassport() == "" {
if in.GetCarIdentity() == "" && in.GetPassportIdentity() == "" {
return nil, errcode.ErrInvalidArgument
}
}
// 查询购物车数据仅返回属于当前登录者的条目cart_identity 只作附加过滤。
tx := impl.DBService.Where("passport_identity = ?", auth.Identity)
if in.GetCarIdentity() != "" {
tx = tx.Where("cart_identity = ?", in.GetCarIdentity())
}
err = tx.Find(&cart).Error
if err != nil {
printer.Error(err.Error())
return nil, errcode.ErrDB
}
for _, item := range cart {
product := models.Product{}
err := impl.DBService.Raw(`
SELECT
p.id, p.identity, p.title, p.cover_image, p.args,
( SELECT MIN(mps.price)
FROM product_spec ps
JOIN mall_product_spec mps ON ps.spec_id = mps.id
WHERE ps.product_identity = p.identity
) as sales_price FROM mall_product p WHERE p.identity = ?`, item.ProductIdentity).Scan(&product).Error
if err != nil {
printer.Error(err.Error())
return nil, errcode.ErrDB
}
da := &pb.CartItem{
Id: int64(item.ID),
ProductId: product.Id,
Title: product.Title,
ProductIdentity: product.Identity,
CoverImage: product.CoverImage,
SalesPrice: product.SalesPrice,
ProductArgs: item.ProductArgs,
UnitPrice: product.SalesPrice,
TotalPrice: int64(item.Number) * product.SalesPrice,
Number: item.Number,
Identity: item.Identity,
}
result = append(result, da)
}
return &pb.CartGetReply{
Data: result,
CarIdentity: in.GetCarIdentity(),
}, nil
}