Files
full/module/ec/order/internal/logic/cart/fetch.go

77 lines
2.2 KiB
Go
Raw Normal View History

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
}
}
2026-09-22 21:15:34 +08:00
// 查询购物车数据仅返回属于当前登录者的条目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
2026-09-22 21:15:34 +08:00
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
2026-09-22 21:15:34 +08:00
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,
}
2026-09-22 21:15:34 +08:00
result = append(result, da)
}
return &pb.CartGetReply{
Data: result,
CarIdentity: in.GetCarIdentity(),
}, nil
}