63 lines
1.6 KiB
Go
63 lines
1.6 KiB
Go
package cart
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"time"
|
|
|
|
"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"
|
|
"git.apinb.com/bsm-sdk/core/utils"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// 将商品增加至购物车
|
|
func Create(ctx context.Context, in *pb.CartAddRequest) (reply *pb.OrderStatusReply, err error) {
|
|
auth, err := service.ParseMetaCtx(ctx, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var cart *models.OrderCart
|
|
|
|
err = models.DBService.Where("cart_identity=? and product_identity=? and spec_id = ? ", in.CartIdentity, in.ProductIdentity, in.SpecId).First(&cart).Error
|
|
if err != nil {
|
|
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
printer.Error(err.Error())
|
|
return nil, errcode.ErrDB
|
|
}
|
|
}
|
|
|
|
var data = models.OrderCart{
|
|
CartIdentity: in.CartIdentity,
|
|
ProductID: in.ProductId,
|
|
ProductArgs: in.ProductArgs,
|
|
Number: in.Number,
|
|
ProductIdentity: in.ProductIdentity,
|
|
SpecID: in.SpecId,
|
|
}
|
|
data.PassportIdentity = auth.Identity
|
|
data.Identity = utils.UUID()
|
|
if cart.ID == 0 {
|
|
err = models.DBService.Create(&data).Error
|
|
} else {
|
|
data.Number += cart.Number
|
|
err = models.DBService.Where("cart_identity=? and product_identity=? and spec_id = ? ", in.CartIdentity, in.ProductIdentity, in.SpecId).Updates(&data).Error
|
|
}
|
|
|
|
if err != nil {
|
|
printer.Error(err.Error())
|
|
return nil, errcode.ErrDB
|
|
}
|
|
|
|
return &pb.OrderStatusReply{
|
|
Code: 0,
|
|
Identity: data.Identity,
|
|
Message: "OK",
|
|
Timeseq: time.Now().UnixNano(),
|
|
}, nil
|
|
|
|
}
|