refactor: reorganize modules and add Linux build tooling
This commit is contained in:
62
module/ec/order/internal/logic/cart/create.go
Normal file
62
module/ec/order/internal/logic/cart/create.go
Normal file
@@ -0,0 +1,62 @@
|
||||
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.StatusReply, 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.StatusReply{
|
||||
Code: 0,
|
||||
Identity: data.Identity,
|
||||
Message: "OK",
|
||||
Timeseq: time.Now().UnixNano(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
31
module/ec/order/internal/logic/cart/delete.go
Normal file
31
module/ec/order/internal/logic/cart/delete.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package cart
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/ec/order/internal/models"
|
||||
pb "bsm/full/module/ec/order/pb"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
)
|
||||
|
||||
// 删除购物车中的商品
|
||||
func Delete(ctx context.Context, in *pb.CartDelRequest) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
_, err = service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = models.DBService.Where("id in ?", in.Id).Delete(&models.OrderCart{}).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Code: 0,
|
||||
Message: "OK",
|
||||
Timeseq: time.Now().UnixNano(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
69
module/ec/order/internal/logic/cart/fetch.go
Normal file
69
module/ec/order/internal/logic/cart/fetch.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package cart
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"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 = models.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 := models.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
|
||||
}
|
||||
52
module/ec/order/internal/logic/cart/modify.go
Normal file
52
module/ec/order/internal/logic/cart/modify.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package cart
|
||||
|
||||
import (
|
||||
"context"
|
||||
"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"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// 修改购物车中的商品数量
|
||||
func Modify(ctx context.Context, in *pb.CartSetRequest) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
_, err = service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(in.GetUpdates()) > 0 {
|
||||
err = models.DBService.Transaction(func(tx *gorm.DB) error {
|
||||
for _, update := range in.Updates {
|
||||
err := tx.Table("order_cart").Where("id = ?", update.Id).Update("number", update.Number).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
} else {
|
||||
if in.GetNumber() == 0 {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
err = models.DBService.Table("order_cart").Where("id = ?", in.Id).Update("number", in.Number).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Code: 0,
|
||||
Message: "OK",
|
||||
Timeseq: time.Now().UnixNano(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user