2026-08-09 10:43:45 +08:00
package cart
import (
"context"
"errors"
"time"
2026-08-10 11:35:48 +08:00
"bsm/full/module/ec/order/internal/impl"
2026-08-09 12:41:08 +08:00
"bsm/full/module/ec/order/internal/models"
pb "bsm/full/module/ec/order/pb"
2026-08-10 11:35:48 +08:00
2026-08-09 10:43:45 +08:00
"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"
)
// 将商品增加至购物车
2026-08-09 20:14:57 +08:00
func Create ( ctx context . Context , in * pb . CartAddRequest ) ( reply * pb . OrderStatusReply , err error ) {
2026-08-09 10:43:45 +08:00
auth , err := service . ParseMetaCtx ( ctx , nil )
if err != nil {
return nil , err
}
var cart * models . OrderCart
2026-09-22 21:15:34 +08:00
err = impl . DBService . Where ( "passport_identity=? and cart_identity=? and product_identity=? and spec_id = ? " , auth . Identity , in . CartIdentity , in . ProductIdentity , in . SpecId ) . First ( & cart ) . Error
2026-08-09 10:43:45 +08:00
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 {
2026-08-10 11:35:48 +08:00
err = impl . DBService . Create ( & data ) . Error
2026-08-09 10:43:45 +08:00
} else {
data . Number += cart . Number
2026-09-22 21:15:34 +08:00
err = impl . DBService . Where ( "passport_identity=? and cart_identity=? and product_identity=? and spec_id = ? " , auth . Identity , in . CartIdentity , in . ProductIdentity , in . SpecId ) . Updates ( & data ) . Error
2026-08-09 10:43:45 +08:00
}
if err != nil {
printer . Error ( err . Error ( ) )
return nil , errcode . ErrDB
}
2026-08-09 20:14:57 +08:00
return & pb . OrderStatusReply {
2026-08-09 10:43:45 +08:00
Code : 0 ,
Identity : data . Identity ,
Message : "OK" ,
Timeseq : time . Now ( ) . UnixNano ( ) ,
} , nil
}