72 lines
2.0 KiB
Go
72 lines
2.0 KiB
Go
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
|
|
}
|
|
}
|
|
// 查询购物车数据
|
|
err = impl.DBService.Where("cart_identity=? or passport_identity=?", in.GetCarIdentity(), auth.Identity).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, p.cost_price,
|
|
( 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 {
|
|
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.CostPrice,
|
|
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
|
|
}
|