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
|
||||
|
||||
}
|
||||
33
module/ec/order/internal/logic/common/get.go
Normal file
33
module/ec/order/internal/logic/common/get.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"bsm/full/module/ec/order/internal/models"
|
||||
pb "bsm/full/module/ec/order/pb"
|
||||
)
|
||||
|
||||
// GetOrderSummaryByIdentity 根据订单号获取订单详情
|
||||
func GetOrderSummaryByIdentity(identity string) (*pb.OrderSummaryItem, error) {
|
||||
var (
|
||||
err error
|
||||
summary = new(models.OrderSummary)
|
||||
)
|
||||
err = models.DBService.Preload("OrderDetails").Where("identity = ?", identity).First(&summary).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ReflectProtoOrderSummary(summary), nil
|
||||
}
|
||||
|
||||
func GetOrderSummaryByNo(no string) (*pb.OrderSummaryItem, error) {
|
||||
var (
|
||||
err error
|
||||
summary = new(models.OrderSummary)
|
||||
)
|
||||
err = models.DBService.Preload("OrderDetails").Where("order_no = ?", no).First(&summary).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ReflectProtoOrderSummary(summary), nil
|
||||
}
|
||||
14
module/ec/order/internal/logic/common/no.go
Normal file
14
module/ec/order/internal/logic/common/no.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 订单号:2位年份,月,日,时,分,秒,6位随机数共18位
|
||||
func CreateOrderNo() string {
|
||||
t := time.Now().Local()
|
||||
rand := fmt.Sprintf("%06v", rand.New(rand.NewSource(time.Now().UnixNano())).Int31n(1000000))
|
||||
return fmt.Sprintf("%d%d%d%d%d%d%s", t.Year()-2000, t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), rand)
|
||||
}
|
||||
100
module/ec/order/internal/logic/common/reflect.go
Normal file
100
module/ec/order/internal/logic/common/reflect.go
Normal file
@@ -0,0 +1,100 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"bsm/full/module/ec/order/internal/models"
|
||||
order "bsm/full/module/ec/order/pb"
|
||||
pb "bsm/full/module/ec/order/pb"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
)
|
||||
|
||||
// ReflectProtoOrderDetails 订单详情
|
||||
func ReflectProtoOrderDetails(v []models.OrderDetails) []*pb.OrderDetails {
|
||||
if nil == v {
|
||||
return nil
|
||||
}
|
||||
result := make([]*pb.OrderDetails, 0)
|
||||
for _, val := range v {
|
||||
result = append(result, &pb.OrderDetails{
|
||||
Id: int64(val.ID),
|
||||
ProductId: val.ProductID,
|
||||
SpecNo: val.SpecNo,
|
||||
SpecId: val.SpecID,
|
||||
Type: int64(val.Type),
|
||||
Title: val.Title,
|
||||
CoverImage: val.CoverImage,
|
||||
SalesPrice: val.SalesPrice,
|
||||
ProductArgs: val.ProductArgs,
|
||||
Number: int64(val.Number),
|
||||
UnitPrice: val.UnitPrice,
|
||||
ProductIdentity: val.ProductIdentity,
|
||||
GasTypes: int64(val.GasType),
|
||||
Spec: val.SpecTitle,
|
||||
TotalPrice: int64(val.Number) * val.UnitPrice,
|
||||
})
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func ReflectProtoOrderSummary(summary *models.OrderSummary) *pb.OrderSummaryItem {
|
||||
if summary == nil {
|
||||
return nil
|
||||
}
|
||||
item := pb.OrderSummaryItem{
|
||||
Id: int64(summary.ID),
|
||||
Identity: summary.Identity,
|
||||
OrderNo: summary.OrderNo,
|
||||
PartnerId: int64(summary.PartnerID),
|
||||
TotalPrice: summary.TotalPrice,
|
||||
TransPrice: summary.TransPrice,
|
||||
RefundPrice: summary.RefundPrice,
|
||||
LogisticsFee: summary.LogisticsFee,
|
||||
CouponAmount: summary.CouponAmount,
|
||||
Remark: summary.Remark,
|
||||
Status: summary.Status,
|
||||
LogisticsNumber: summary.LogisticsNumber,
|
||||
AddressIdentity: summary.AddressIdentity,
|
||||
County: summary.County,
|
||||
Province: summary.Province,
|
||||
City: summary.City,
|
||||
Area: summary.Area,
|
||||
Address: summary.Province + summary.City + summary.Area + summary.Address,
|
||||
Contact: summary.Contact,
|
||||
Phone: summary.Phone,
|
||||
DeliveryTime: summary.DeliveryTime,
|
||||
DeliveryIdentity: summary.DeliveryIdentity,
|
||||
PayType: int32(summary.PayType),
|
||||
PayAmount: summary.PayAmount,
|
||||
PayTradeNo: summary.PayTradeNo,
|
||||
PayTime: summary.PayTime.Format(vars.YYYY_MM_DD_HH_MM_SS),
|
||||
PayRemark: summary.PayRemark,
|
||||
Created: summary.CreatedAt.Format(vars.YYYY_MM_DD_HH_MM_SS),
|
||||
Updated: summary.UpdatedAt.Format(vars.YYYY_MM_DD_HH_MM_SS),
|
||||
Addr: summary.Address,
|
||||
CarIdentity: summary.CarIdentity,
|
||||
DeliveryAddress: summary.DeliveryAddress,
|
||||
Brand: summary.CarBrand,
|
||||
Version: summary.CarVersion,
|
||||
LicenseNumber: summary.LicenseNumber,
|
||||
MemberName: summary.MemberName,
|
||||
MemberPhone: summary.MemberPhone,
|
||||
Details: make([]*order.OrderDetails, 0),
|
||||
Approve: int32(summary.Approve),
|
||||
}
|
||||
if summary.OrderDetails != nil {
|
||||
item.Details = ReflectProtoOrderDetails(summary.OrderDetails)
|
||||
}
|
||||
if summary.PayTime.IsZero() {
|
||||
item.PayTime = ""
|
||||
}
|
||||
|
||||
return &item
|
||||
}
|
||||
|
||||
func ListModelToReply(list []*models.OrderSummary) []*pb.OrderSummaryItem {
|
||||
var data = make([]*order.OrderSummaryItem, 0)
|
||||
for _, summary := range list {
|
||||
data = append(data, ReflectProtoOrderSummary(summary))
|
||||
}
|
||||
return data
|
||||
}
|
||||
53
module/ec/order/internal/logic/coupon/by_status.go
Normal file
53
module/ec/order/internal/logic/coupon/by_status.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package coupon
|
||||
|
||||
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 ByStatus(ctx context.Context, in *pb.Status) (reply *pb.CouponListReply, err error) {
|
||||
// parse authorization meta.
|
||||
auth, err := service.ParseMetaCtx(ctx, &service.ParseOptions{RoleValue: "Mall_Admin"})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var (
|
||||
coupon = make([]*models.OrderCoupon, 0)
|
||||
result = make([]*pb.CouponItem, 0)
|
||||
)
|
||||
|
||||
err = models.DBService.Where("passport_id=?", auth.ID).Find(&coupon).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
for _, item := range coupon {
|
||||
result = append(result, ReflectProtoOrderCoupon(item))
|
||||
}
|
||||
|
||||
return &pb.CouponListReply{
|
||||
Data: result,
|
||||
}, nil
|
||||
|
||||
}
|
||||
func ReflectProtoOrderCoupon(v *models.OrderCoupon) *pb.CouponItem {
|
||||
if nil == v {
|
||||
return nil
|
||||
}
|
||||
result := &pb.CouponItem{
|
||||
Id: int64(v.ID),
|
||||
Identity: v.Identity,
|
||||
Title: v.Title,
|
||||
Intro: v.Intro,
|
||||
Amount: v.Intro,
|
||||
}
|
||||
return result
|
||||
}
|
||||
99
module/ec/order/internal/logic/mgt/order_approve.go
Normal file
99
module/ec/order/internal/logic/mgt/order_approve.go
Normal file
@@ -0,0 +1,99 @@
|
||||
package mgt
|
||||
|
||||
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"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// 订单审批
|
||||
func OrderApprove(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusReply, err error) {
|
||||
// 验证输入参数是否有效。
|
||||
if in.GetIdentity() == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
// 获取当前订单信息。
|
||||
var order models.OrderSummary
|
||||
if err := models.DBService.Preload("OrderDetails").Where("identity=?", in.Identity).First(&order).Error; err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
// 根据订单审批状态更新订单状态:状态:-2:未通过,0:默认 1:申请退款 2:申请退货 3:申请退款退货 4:申请通过
|
||||
switch order.Approve {
|
||||
// 申请退款
|
||||
case 1:
|
||||
if in.GetApprove() == 4 {
|
||||
err = models.DBService.Model(&models.OrderSummary{}).Where("identity=?", in.Identity).
|
||||
Updates(map[string]any{
|
||||
"approve": in.GetApprove(),
|
||||
"status": 7,
|
||||
}).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
}
|
||||
// 申请退货
|
||||
case 2:
|
||||
if in.GetApprove() == 4 {
|
||||
models.DBService.Transaction(func(tx *gorm.DB) error {
|
||||
err = tx.Model(&models.OrderSummary{}).Where("identity=?", in.Identity).
|
||||
Updates(map[string]any{
|
||||
"approve": in.GetApprove(),
|
||||
"status": 8,
|
||||
}).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return err
|
||||
}
|
||||
// 如果订单详情中的数量不为零,则更新产品规格的库存。
|
||||
for _, specs := range order.OrderDetails {
|
||||
if specs.Number != 0 {
|
||||
err = tx.Table("mall_product_spec").Where("product_identity = ?", specs.ProductIdentity).UpdateColumn("stock", gorm.Expr("stock + ?", specs.Number)).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
// 申请退款退货
|
||||
case 3:
|
||||
if in.GetApprove() == 4 {
|
||||
|
||||
err = models.DBService.Model(&models.OrderSummary{}).Where("identity=?", in.Identity).
|
||||
Updates(map[string]any{
|
||||
"approve": in.GetApprove(),
|
||||
"status": 6,
|
||||
}).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
}
|
||||
}
|
||||
// 审批未通过
|
||||
if in.GetApprove() == -2 {
|
||||
if err := models.DBService.Model(&models.OrderSummary{}).Where("identity=?", in.Identity).
|
||||
Update("approve", in.GetApprove()).Error; err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Code: 0,
|
||||
Message: "OK",
|
||||
Identity: in.GetIdentity(),
|
||||
Timeseq: time.Now().UnixNano(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
47
module/ec/order/internal/logic/mgt/order_cancel.go
Normal file
47
module/ec/order/internal/logic/mgt/order_cancel.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package mgt
|
||||
|
||||
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"
|
||||
)
|
||||
|
||||
// 取消订单
|
||||
func OrderCancel(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
auth, err := service.ParseMetaCtx(ctx, &service.ParseOptions{RoleValue: "Mall_Admin"})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if auth.Owner == nil {
|
||||
return nil, errcode.ErrPermissionDenied
|
||||
}
|
||||
|
||||
// valildate request id,identity.
|
||||
if in.Identity == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// 验证输入参数是否有效。
|
||||
if in.GetIdentity() == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
err = models.DBService.Model(&models.OrderSummary{}).Where("identity=?", in.Identity).Update("status", -1).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
return &pb.StatusReply{
|
||||
Code: 0,
|
||||
Message: "OK",
|
||||
Identity: in.GetIdentity(),
|
||||
Timeseq: time.Now().UnixNano(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
168
module/ec/order/internal/logic/mgt/order_create.go
Normal file
168
module/ec/order/internal/logic/mgt/order_create.go
Normal file
@@ -0,0 +1,168 @@
|
||||
package mgt
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/ec/order/internal/logic/common"
|
||||
"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 OrderCreate(ctx context.Context, in *pb.CreateOrderRequest) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
auth, err := service.ParseMetaCtx(ctx, &service.ParseOptions{RoleValue: "Mall_Admin"})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if auth.Owner == nil {
|
||||
return nil, errcode.ErrPermissionDenied
|
||||
}
|
||||
var (
|
||||
TotalPrice int64 = 0
|
||||
summaryIdentity string = utils.UUID()
|
||||
orderNo = common.CreateOrderNo()
|
||||
details = &models.OrderDetails{}
|
||||
summary = &models.OrderSummary{}
|
||||
specList = make([]models.OrderDetails, 0)
|
||||
)
|
||||
|
||||
// 验证输入参数是否有效。
|
||||
for _, spec := range in.Spec {
|
||||
if spec.GetProductIdentity() == "" || spec.GetNumber() == 0 {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
}
|
||||
if in.GetAddressIdentity() == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
ownerStore := auth.Owner.(map[string]any)
|
||||
storeId := uint(ownerStore["store_id"].(float64))
|
||||
storeIdentity := ownerStore["store_identity"].(string)
|
||||
|
||||
// 查询产品信息以确保其存在且状态为启用。
|
||||
for _, specs := range in.Spec {
|
||||
product := map[string]any{}
|
||||
err = models.DBService.Table("mall_product").Take(&product, "identity=?", specs.ProductIdentity).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
if product["status"].(int32) != 1 {
|
||||
return nil, errcode.ErrUnknown
|
||||
}
|
||||
// 查询产品规格信息。
|
||||
spec := map[string]any{}
|
||||
err = models.DBService.Table("mall_product_spec").Take(&spec, "product_identity=? ", specs.ProductIdentity).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
// 从产品信息中提取单价、ID等详情,并构建订单详情对象。
|
||||
productID := product["id"].(int64)
|
||||
unit_price := product["sales_price"].(int64)
|
||||
// 计算交易价格并构建订单摘要对象。
|
||||
itemTotal := int64(specs.Number) * unit_price
|
||||
TotalPrice += itemTotal
|
||||
details = &models.OrderDetails{
|
||||
SummaryIdentity: summaryIdentity,
|
||||
Type: 1,
|
||||
ProductID: productID,
|
||||
ProductIdentity: product["identity"].(string),
|
||||
OrderNo: orderNo,
|
||||
Title: product["title"].(string),
|
||||
CoverImage: product["cover_image"].(string),
|
||||
UnitPrice: unit_price,
|
||||
Number: specs.Number,
|
||||
ProductArgs: product["args"].(string),
|
||||
SpecID: spec["id"].(int64),
|
||||
SpecNo: spec["serial_number"].(string),
|
||||
SpecTitle: spec["title"].(string),
|
||||
SupplyId: product["supply_id"].(int64),
|
||||
GasType: product["gas_types"].(int32),
|
||||
}
|
||||
specList = append(specList, *details)
|
||||
}
|
||||
|
||||
// 查询地址信息以确保其存在。
|
||||
address := map[string]any{}
|
||||
err = models.DBService.Table("address_library").Take(&address, "identity=?", in.AddressIdentity).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
summary = &models.OrderSummary{
|
||||
StoreID: storeId,
|
||||
StoreIdentity: storeIdentity,
|
||||
OrderNo: orderNo,
|
||||
PartnerID: in.PartnerId,
|
||||
TransPrice: TotalPrice,
|
||||
TotalPrice: TotalPrice,
|
||||
Args: in.Args,
|
||||
County: address["country"].(string),
|
||||
Province: address["province"].(string),
|
||||
City: address["city"].(string),
|
||||
Area: address["area"].(string),
|
||||
Address: address["detail"].(string),
|
||||
Contact: address["contact"].(string),
|
||||
Phone: address["phone"].(string),
|
||||
AddressIdentity: address["identity"].(string),
|
||||
OrderDetails: specList,
|
||||
}
|
||||
summary.Identity = summaryIdentity
|
||||
summary.PassportID = auth.ID
|
||||
summary.PassportIdentity = auth.Identity
|
||||
summary.Status = 1
|
||||
|
||||
// 将订单摘要和订单详情记录到数据库。
|
||||
err = models.DBService.Transaction(func(tx *gorm.DB) error {
|
||||
// 创建订单摘要。
|
||||
if err := tx.Create(summary).Error; err != nil {
|
||||
printer.Error(err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
// 扣减库存
|
||||
for _, v := range summary.OrderDetails {
|
||||
if v.Number > 0 {
|
||||
result := tx.Table("mall_product_spec").
|
||||
Where("id = ? AND stock >= ?", v.SpecID, v.Number).
|
||||
UpdateColumn("stock", gorm.Expr("stock - ?", v.Number))
|
||||
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
|
||||
// 检查实际更新的行数
|
||||
if result.RowsAffected == 0 {
|
||||
return errors.New("库存不足或规格不存在")
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Code: 0,
|
||||
Message: "OK",
|
||||
Timeseq: time.Now().UnixNano(),
|
||||
Identity: summary.Identity,
|
||||
}, nil
|
||||
|
||||
}
|
||||
54
module/ec/order/internal/logic/mgt/order_get.go
Normal file
54
module/ec/order/internal/logic/mgt/order_get.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package mgt
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"bsm/full/module/ec/order/internal/logic/common"
|
||||
"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 OrderGet(ctx context.Context, in *pb.IdentRequest) (reply *pb.OrderGetReply, err error) {
|
||||
// parse authorization meta.
|
||||
RoleValve := &service.ParseOptions{RoleValue: "Mall_Admin"}
|
||||
if in.GetAgency() != "" {
|
||||
RoleValve = nil
|
||||
}
|
||||
auth, err := service.ParseMetaCtx(ctx, RoleValve)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if auth.Owner == nil {
|
||||
return nil, errcode.ErrPermissionDenied
|
||||
}
|
||||
|
||||
// valildate request id,identity.
|
||||
if in.Id == 0 && in.Identity == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
var storeIdentity string
|
||||
if in.GetAgency() != "" {
|
||||
storeIdentity = in.GetStoreIdentity()
|
||||
} else {
|
||||
ownerStore := auth.Owner.(map[string]any)
|
||||
storeIdentity = ownerStore["store_identity"].(string)
|
||||
}
|
||||
order := &models.OrderSummary{}
|
||||
err = models.DBService.Model(&models.OrderSummary{}).Where("store_identity = ? and identity=?", storeIdentity, in.Identity).Preload("OrderDetails").Find(&order).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, errcode.ErrRecordNotFound
|
||||
}
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
data := common.ReflectProtoOrderSummary(order)
|
||||
return &pb.OrderGetReply{Summary: data}, nil
|
||||
}
|
||||
90
module/ec/order/internal/logic/mgt/order_list_by_store.go
Normal file
90
module/ec/order/internal/logic/mgt/order_list_by_store.go
Normal file
@@ -0,0 +1,90 @@
|
||||
package mgt
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"bsm/full/module/ec/order/internal/logic/common"
|
||||
"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 OrderListByStore(ctx context.Context, in *pb.OrderListByStoreRequest) (reply *pb.OrderListByStoreReply, err error) {
|
||||
// parse authorization meta.
|
||||
RoleValve := &service.ParseOptions{RoleValue: "Mall_Admin"}
|
||||
if in.GetAgency() != "" {
|
||||
RoleValve = nil
|
||||
}
|
||||
auth, err := service.ParseMetaCtx(ctx, RoleValve)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if auth.Owner == nil {
|
||||
return nil, errcode.ErrPermissionDenied
|
||||
}
|
||||
|
||||
var (
|
||||
page = 1
|
||||
size = 50
|
||||
payType int32 = 0
|
||||
cnt int64 = 0
|
||||
storeIdentity string
|
||||
)
|
||||
|
||||
if in.GetPayType() != 0 {
|
||||
payType = in.PayType
|
||||
}
|
||||
|
||||
if in.GetPageNo() > 0 {
|
||||
page = int(in.PageNo)
|
||||
}
|
||||
if in.GetPageSize() > 0 {
|
||||
size = int(in.PageSize)
|
||||
}
|
||||
if in.GetAgency() != "" {
|
||||
storeIdentity = in.GetStoreIdentity()
|
||||
} else {
|
||||
ownerStore := auth.Owner.(map[string]any)
|
||||
storeIdentity = ownerStore["store_identity"].(string)
|
||||
}
|
||||
|
||||
orderList := make([]*models.OrderSummary, 0)
|
||||
tx := models.DBService.Model(&models.OrderSummary{}).Where("store_identity = ?", storeIdentity).Where("partner_id = ?", auth.ID).Preload("OrderDetails")
|
||||
// 根据状态查询订单
|
||||
if in.GetStatus() != 0 {
|
||||
tx = tx.Where("status = ?", in.GetStatus())
|
||||
}
|
||||
// 根据支付类型查询订单
|
||||
if payType != 0 {
|
||||
tx = tx.Where("pay_type = ?", payType)
|
||||
|
||||
}
|
||||
// 根据订单状态查询订单
|
||||
if len(in.GetOrderStatus()) != 0 {
|
||||
tx = tx.Where("status in ?", in.GetOrderStatus())
|
||||
}
|
||||
if in.GetKeyword() != "" {
|
||||
tx = tx.Where("order_no like ?", "%"+in.GetKeyword()+"%")
|
||||
}
|
||||
err = tx.Order("created_at desc").Count(&cnt).Limit(size).Offset((page - 1) * size).Find(&orderList).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, errcode.ErrRecordNotFound
|
||||
}
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
data := common.ListModelToReply(orderList)
|
||||
|
||||
return &pb.OrderListByStoreReply{
|
||||
Count: int32(cnt),
|
||||
Data: data,
|
||||
}, nil
|
||||
}
|
||||
34
module/ec/order/internal/logic/mgt/order_modify.go
Normal file
34
module/ec/order/internal/logic/mgt/order_modify.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package mgt
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
pb "bsm/full/module/ec/order/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
)
|
||||
|
||||
// 修改订单
|
||||
func OrderModify(ctx context.Context, in *pb.OrderSummaryItem) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
auth, err := service.ParseMetaCtx(ctx, &service.ParseOptions{RoleValue: "Mall_Admin"})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if auth.Owner == nil {
|
||||
return nil, errcode.ErrPermissionDenied
|
||||
}
|
||||
|
||||
// TODO: valid code
|
||||
|
||||
// TODO: add your logic code & delete this line.
|
||||
|
||||
return &pb.StatusReply{
|
||||
Code: 0,
|
||||
Message: "OK",
|
||||
Timeseq: time.Now().UnixNano(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
61
module/ec/order/internal/logic/mgt/order_returnable.go
Normal file
61
module/ec/order/internal/logic/mgt/order_returnable.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package mgt
|
||||
|
||||
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"
|
||||
)
|
||||
|
||||
// OrderReturnable 订单申请售后
|
||||
func OrderReturnable(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusReply, err error) {
|
||||
_, err = service.ParseMetaCtx(ctx, &service.ParseOptions{RoleValue: "Mall_Admin"})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 验证订单的approve审核状态:状态:-2:未通过,0:默认 1:申请退款 2:申请退货 3:申请退款退货 4:申请通过
|
||||
if in.GetApprove() != 1 && in.GetApprove() != 2 && in.GetApprove() != 3 {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
// 验证输入参数是否有效。
|
||||
if in.GetIdentity() == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// 获取当前订单信息
|
||||
order := models.OrderSummary{}
|
||||
if err := models.DBService.Preload("OrderDetails").Where("identity=?", in.Identity).First(&order).Error; err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
// 订单状态判断是否允许申请退款退货
|
||||
if order.Status == -1 || order.Status == 6 || order.Status == 7 || order.Status == 8 || order.Approve != 0 {
|
||||
return nil, errors.New("订单状态异常")
|
||||
}
|
||||
|
||||
// 订单状态更新
|
||||
summaryModel := models.OrderSummary{
|
||||
Approve: int8(in.GetApprove()),
|
||||
Reason: in.GetReason(),
|
||||
}
|
||||
err = models.DBService.Model(&models.OrderSummary{}).Where("identity = ?", in.Identity).Updates(&summaryModel).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Code: 0,
|
||||
Message: "OK",
|
||||
Identity: in.GetIdentity(),
|
||||
Reason: in.GetReason(),
|
||||
Timeseq: time.Now().UnixNano(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
60
module/ec/order/internal/logic/summary/cancel.go
Normal file
60
module/ec/order/internal/logic/summary/cancel.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package summary
|
||||
|
||||
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"
|
||||
)
|
||||
|
||||
// 取消订单
|
||||
func Cancel(ctx context.Context, in *pb.CancelRequest) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
_, err = service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var (
|
||||
summary = new(models.OrderSummary)
|
||||
)
|
||||
|
||||
err = models.DBService.Where("order_no = ?", in.OrderNo).First(&summary).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
if summary.Status == 1 {
|
||||
values := &models.OrderSummary{
|
||||
CouponIdentity: "",
|
||||
CouponAmount: 0.00,
|
||||
}
|
||||
values.Status = -1
|
||||
|
||||
err := models.DBService.Where("order_no = ?", in.OrderNo).Updates(values).Error
|
||||
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
err = models.DBService.Model(&models.OrderCoupon{}).Where("identity=?", summary.CouponIdentity).Update("status", 2).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Code: 0,
|
||||
Identity: summary.Identity,
|
||||
Message: "OK",
|
||||
Timeseq: time.Now().UnixNano(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
37
module/ec/order/internal/logic/summary/check.go
Normal file
37
module/ec/order/internal/logic/summary/check.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package summary
|
||||
|
||||
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"
|
||||
)
|
||||
|
||||
// 检测是否有未确认及付款的订单
|
||||
func Check(ctx context.Context, in *pb.Empty) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
auth, err := service.ParseMetaCtx(ctx, &service.ParseOptions{RoleValue: "Mall_Admin"})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var (
|
||||
summary = new(models.OrderSummary)
|
||||
)
|
||||
err = models.DBService.Where("passport_id = ?", auth.ID).First(&summary).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Code: 0,
|
||||
Identity: summary.OrderNo,
|
||||
Message: "OK",
|
||||
Timeseq: time.Now().UnixNano(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
84
module/ec/order/internal/logic/summary/confirm.go
Normal file
84
module/ec/order/internal/logic/summary/confirm.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package summary
|
||||
|
||||
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 Confirm(ctx context.Context, in *pb.ConfirmRequest) (reply *pb.ConfirmReply, err error) {
|
||||
// parse authorization meta.
|
||||
_, err = service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var (
|
||||
logisFee int64 = 0
|
||||
couponAmount int64 = 0
|
||||
summary = new(models.OrderSummary)
|
||||
coupon = new(models.OrderCoupon)
|
||||
)
|
||||
|
||||
err = models.DBService.Where("order_no = ?", in.OrderNo).First(&summary).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
if in.AddressIdentity != "" {
|
||||
/*
|
||||
err = models.DBService.Where("id=?", in.AddressId).First(&address).Error
|
||||
if err == nil {
|
||||
summary.Address = address.Detail
|
||||
summary.County = address.Country
|
||||
summary.Province = address.Province
|
||||
summary.City = address.City
|
||||
summary.Area = address.Area
|
||||
summary.Contact = address.Contact
|
||||
summary.Phone = address.Phone
|
||||
logisFee = GetLogisticsFee(summary.TotalPrice, address.Province)
|
||||
}
|
||||
*/
|
||||
}
|
||||
if logisFee > 0 {
|
||||
summary.TotalPrice = summary.TotalPrice + logisFee
|
||||
}
|
||||
|
||||
if in.CouponIdentity != "" {
|
||||
err = models.DBService.Where("identity=?", in.CouponIdentity).First(&coupon).Error
|
||||
if err == nil {
|
||||
if coupon.Status == 2 {
|
||||
models.DBService.Model(&models.OrderCoupon{}).Where("identity=?", in.CouponIdentity).Update("status", 3)
|
||||
couponAmount = coupon.Amount
|
||||
} else {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrUnavailable
|
||||
}
|
||||
} else {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrRecordNotFound
|
||||
}
|
||||
}
|
||||
|
||||
if couponAmount > 0 {
|
||||
summary.TotalPrice = summary.TotalPrice + couponAmount
|
||||
}
|
||||
|
||||
summary.Status = 1
|
||||
|
||||
err = models.DBService.Where("order_no = ?", in.OrderNo).Updates(summary).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.ConfirmReply{
|
||||
TotalPrice: summary.TotalPrice,
|
||||
}, nil
|
||||
}
|
||||
35
module/ec/order/internal/logic/summary/get.go
Normal file
35
module/ec/order/internal/logic/summary/get.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package summary
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"bsm/full/module/ec/order/internal/logic/common"
|
||||
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 Get(ctx context.Context, in *pb.IdentRequest) (reply *pb.SummaryGetReply, err error) {
|
||||
// parse authorization meta.
|
||||
_, err = service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// valildate request id,identity.
|
||||
if in.Identity == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
summary, err := common.GetOrderSummaryByIdentity(in.Identity)
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.SummaryGetReply{
|
||||
Summary: summary,
|
||||
}, nil
|
||||
}
|
||||
70
module/ec/order/internal/logic/summary/list.go
Normal file
70
module/ec/order/internal/logic/summary/list.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package summary
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"bsm/full/module/ec/order/internal/logic/common"
|
||||
"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 List(ctx context.Context, in *pb.SummaryListRequest) (reply *pb.SummaryListReply, err error) {
|
||||
// parse authorization meta.
|
||||
auth, err := service.ParseMetaCtx(ctx, &service.ParseOptions{RoleValue: "Mall_Admin"})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var (
|
||||
page = 1
|
||||
size = 50
|
||||
payType int32 = 0 // 支付方式
|
||||
orderStatus int32 = 0 // 订单状态
|
||||
cnt int64 = 0
|
||||
)
|
||||
|
||||
if in.PayType != 0 {
|
||||
payType = in.PayType
|
||||
}
|
||||
if in.OrderStatus != 0 {
|
||||
orderStatus = in.OrderStatus
|
||||
}
|
||||
if in.GetPageNo() > 0 {
|
||||
page = int(in.PageNo)
|
||||
}
|
||||
if in.GetPageSize() > 0 {
|
||||
size = int(in.PageSize)
|
||||
}
|
||||
|
||||
orderList := make([]*models.OrderSummary, 0)
|
||||
tx := models.DBService.Model(&models.OrderSummary{}).Preload("OrderDetails").Where("passport_identity = ?", auth.Identity)
|
||||
if payType != 0 {
|
||||
tx = tx.Where("pay_type = ?", payType)
|
||||
|
||||
}
|
||||
if orderStatus != 0 {
|
||||
tx = tx.Where("status = ?", orderStatus)
|
||||
}
|
||||
|
||||
err = tx.Order("created_at desc").Count(&cnt).Limit(size).Offset((page - 1) * size).Find(&orderList).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, errcode.ErrRecordNotFound
|
||||
}
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
data := common.ListModelToReply(orderList)
|
||||
|
||||
return &pb.SummaryListReply{
|
||||
Count: int32(cnt),
|
||||
Data: data,
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
package summary
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/ec/order/internal/logic/common"
|
||||
"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"
|
||||
)
|
||||
|
||||
// 快速创建一个店铺订单
|
||||
func QuickCreateByProduct(ctx context.Context, in *pb.QuickCreateByProductRequest) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
auth, err := service.ParseMetaCtx(ctx, &service.ParseOptions{RoleValue: "Mall_Admin"})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 验证输入参数是否有效。
|
||||
if in.GetProductIdentity() == "" || in.GetNumber() == 0 || in.GetStoreIdentity() == "" || in.GetAddressIdentity() == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// 查询商店信息以确保其存在且状态为启用。
|
||||
store := map[string]any{}
|
||||
err = models.DBService.Table("mall_store").Take(&store, "identity=?", in.StoreIdentity).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
if errors.Is(err, models.ErrNotFound) {
|
||||
return nil, errcode.ErrRecordNotFound
|
||||
}
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
if store["status"].(int16) != 1 {
|
||||
return nil, errcode.ErrUnknown
|
||||
}
|
||||
|
||||
// 查询产品信息以确保其存在且状态为启用。
|
||||
product := map[string]any{}
|
||||
var pi string = strings.ToLower(in.ProductIdentity)
|
||||
if pi == "gas_by_kg" || pi == "gas_by_bottle" {
|
||||
err = models.DBService.Table("mall_product").Take(&product, "store_identity=? and serial_id=?", in.StoreIdentity, pi).Error
|
||||
} else {
|
||||
err = models.DBService.Table("mall_product").Take(&product, "identity=?", in.ProductIdentity).Error
|
||||
}
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
if errors.Is(err, models.ErrNotFound) {
|
||||
return nil, errcode.ErrRecordNotFound
|
||||
}
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
if product["status"].(int32) != 1 {
|
||||
return nil, errcode.ErrUnknown
|
||||
}
|
||||
|
||||
// 查询产品规格信息
|
||||
spec := map[string]any{}
|
||||
err = models.DBService.Table("mall_product_spec").Take(&spec, "product_identity=?", in.ProductIdentity).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
if errors.Is(err, models.ErrNotFound) {
|
||||
return nil, errcode.ErrRecordNotFound
|
||||
}
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
// 查询地址信息以确保其存在。
|
||||
address := map[string]any{}
|
||||
err = models.DBService.Table("address_library").Take(&address, "identity=?", in.AddressIdentity).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
if errors.Is(err, models.ErrNotFound) {
|
||||
return nil, errcode.ErrRecordNotFound
|
||||
}
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
// // 查询配送员信息
|
||||
// member := map[string]any{}
|
||||
// err = models.DBService.Table("delivery_member").Take(&member, "identity=?", in.MemberIdentity).Error
|
||||
// if err != nil {
|
||||
// printer.Error(err.Error())
|
||||
// if errors.Is(err, models.ErrNotFound) {
|
||||
// return nil, errcode.ErrRecordNotFound
|
||||
// }
|
||||
// return nil, errcode.ErrDB
|
||||
// }
|
||||
|
||||
// 生成订单号。
|
||||
orderNo := common.CreateOrderNo()
|
||||
// 计算交易价格并构建订单摘要对象。
|
||||
TransPrice := (int64(in.Number) * spec["price"].(int64))
|
||||
summary := &models.OrderSummary{
|
||||
StoreID: uint(store["id"].(int64)),
|
||||
StoreIdentity: in.StoreIdentity,
|
||||
OrderNo: orderNo,
|
||||
PartnerID: in.PartnerId,
|
||||
TransPrice: TransPrice,
|
||||
TotalPrice: TransPrice,
|
||||
Args: in.Args,
|
||||
AddressIdentity: in.AddressIdentity,
|
||||
Province: address["province"].(string),
|
||||
City: address["city"].(string),
|
||||
Area: address["area"].(string),
|
||||
Address: address["detail"].(string),
|
||||
Contact: address["contact"].(string),
|
||||
Phone: address["phone"].(string),
|
||||
DeliveryTime: in.GetDeliveryTime(),
|
||||
// DeliveryIdentity: member["identity"].(string),
|
||||
DeliveryAddress: in.DeliveryAddress,
|
||||
}
|
||||
summary.Identity = utils.UUID()
|
||||
summary.PassportID = auth.ID
|
||||
summary.PassportIdentity = auth.Identity
|
||||
summary.Status = 1
|
||||
|
||||
// 从产品信息中提取单价、ID等详情,并构建订单详情对象。
|
||||
unit_price := product["sales_price"].(int64)
|
||||
productID := product["id"].(int64)
|
||||
details := &models.OrderDetails{
|
||||
Type: 1,
|
||||
ProductID: productID,
|
||||
ProductIdentity: product["identity"].(string),
|
||||
SpecID: spec["id"].(int64),
|
||||
OrderNo: orderNo,
|
||||
Title: product["title"].(string),
|
||||
CoverImage: product["cover_image"].(string),
|
||||
UnitPrice: unit_price,
|
||||
Number: in.Number,
|
||||
ProductArgs: product["args"].(string),
|
||||
SummaryIdentity: summary.Identity,
|
||||
SpecTitle: spec["title"].(string),
|
||||
SpecNo: spec["serial_number"].(string),
|
||||
GasType: product["gas_types"].(int32),
|
||||
SupplyId: product["supply_id"].(int64),
|
||||
}
|
||||
|
||||
// 将订单摘要和订单详情记录到数据库。
|
||||
err = models.DBService.Create(summary).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
err = models.DBService.Create(details).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Code: 0,
|
||||
Message: "OK",
|
||||
Timeseq: time.Now().UnixNano(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
38
module/ec/order/internal/logic/summary/simulate_pay.go
Normal file
38
module/ec/order/internal/logic/summary/simulate_pay.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package summary
|
||||
|
||||
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"
|
||||
)
|
||||
|
||||
// 模拟支付
|
||||
func SimulatePay(ctx context.Context, in *pb.SimulatePayRequest) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
_, err = service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = models.DBService.Model(&models.OrderSummary{}).Where("identity in ?", in.Identity).Updates(models.OrderSummary{Status: 2, PayTime: time.Now(), PayType: 4, PayRemark: "支付备注", PayTradeNo: "Pay12371937812897", PayAmount: 10000000}).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
if errors.Is(err, models.ErrNotFound) {
|
||||
return nil, errcode.ErrRecordNotFound
|
||||
}
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Code: 0,
|
||||
Message: "OK",
|
||||
Timeseq: time.Now().UnixNano(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
42
module/ec/order/internal/logic/summary/simulate_receiving.go
Normal file
42
module/ec/order/internal/logic/summary/simulate_receiving.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package summary
|
||||
|
||||
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"
|
||||
)
|
||||
|
||||
// 模拟收货
|
||||
func SimulateReceiving(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
_, err = service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// valildate request id,identity.
|
||||
if in.Id == 0 && in.Identity == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
err = models.DBService.Model(&models.OrderSummary{}).Where("identity = ?", in.Identity).Update("status", 4).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
if errors.Is(err, models.ErrNotFound) {
|
||||
return nil, errcode.ErrRecordNotFound
|
||||
}
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Code: 0,
|
||||
Message: "OK",
|
||||
Timeseq: time.Now().UnixNano(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
41
module/ec/order/internal/logic/summary/simulate_shipments.go
Normal file
41
module/ec/order/internal/logic/summary/simulate_shipments.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package summary
|
||||
|
||||
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"
|
||||
)
|
||||
|
||||
// 模拟发货
|
||||
func SimulateShipments(ctx context.Context, in *pb.SimulateShipmentsRequest) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
_, err = service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if in.GetMemberIdentity() == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
err = models.DBService.Where("identity = ?", in.Identity).Updates(&models.OrderSummary{Status: 3, DeliveryIdentity: in.MemberIdentity}).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
if errors.Is(err, models.ErrNotFound) {
|
||||
return nil, errcode.ErrRecordNotFound
|
||||
}
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
return &pb.StatusReply{
|
||||
Code: 0,
|
||||
Message: "OK",
|
||||
Timeseq: time.Now().UnixNano(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
245
module/ec/order/internal/logic/summary/submit.go
Normal file
245
module/ec/order/internal/logic/summary/submit.go
Normal file
@@ -0,0 +1,245 @@
|
||||
package summary
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/ec/order/internal/logic/common"
|
||||
"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/types"
|
||||
"git.apinb.com/bsm-sdk/core/utils"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// 将购物车的数据提交生成订单
|
||||
func Submit(ctx context.Context, in *pb.SubmitRequest) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
RoleValve := &service.ParseOptions{RoleValue: "Mall_Admin"}
|
||||
if in.GetAddress() != nil {
|
||||
RoleValve = nil
|
||||
}
|
||||
auth, err := service.ParseMetaCtx(ctx, RoleValve)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var (
|
||||
cart = make([]*models.OrderCart, 0)
|
||||
details = make([]*models.OrderDetails, 0)
|
||||
summary = make([]*models.OrderSummary, 0)
|
||||
keys = make([]string, 0)
|
||||
address = models.OrderAddress{}
|
||||
StoreId uint = 0
|
||||
)
|
||||
|
||||
// 获取购物车内数据信息
|
||||
err = models.DBService.Where("passport_identity = ?", auth.Identity).Find(&cart).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
if errors.Is(err, models.ErrNotFound) {
|
||||
return nil, errcode.ErrRecordNotFound
|
||||
}
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
// 获取地址信息
|
||||
if in.GetAddress() != nil {
|
||||
address = models.OrderAddress{
|
||||
Country: in.Address.Country,
|
||||
Province: in.Address.Province,
|
||||
City: in.Address.City,
|
||||
Detail: in.Address.Detail,
|
||||
Contact: in.Address.Contact,
|
||||
Phone: in.Address.Phone,
|
||||
Area: in.Address.Area,
|
||||
Email: in.Address.Email,
|
||||
ZipCode: in.Address.ZipCode,
|
||||
}
|
||||
} else {
|
||||
err = models.DBService.Table("address_library").Where("identity = ?", in.AddressIdentity).Find(&address).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
if errors.Is(err, models.ErrNotFound) {
|
||||
return nil, errcode.ErrRecordNotFound
|
||||
}
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
}
|
||||
|
||||
// 按店铺分组购物车商品
|
||||
storeGroups := make(map[string][]*models.OrderCart)
|
||||
for k, item := range cart {
|
||||
product := models.Product{}
|
||||
err = models.DBService.Select("store_identity").Table("mall_product").Where("id = ?", item.ProductID).First(&product).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
if errors.Is(err, models.ErrNotFound) {
|
||||
return nil, errcode.ErrRecordNotFound
|
||||
}
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
if k == 0 {
|
||||
StoreId = product.StoreId
|
||||
}
|
||||
storeGroups[product.StoreIdentity] = append(storeGroups[product.StoreIdentity], item)
|
||||
}
|
||||
|
||||
// 处理每个店铺
|
||||
for StoreIdentity, items := range storeGroups {
|
||||
var storeTotal int64 = 0
|
||||
var storeDetails []*models.OrderDetails
|
||||
|
||||
//订单号:2位年份,月,日,时,分,秒,6位随机数共18位
|
||||
summaryIdentity := utils.UUID()
|
||||
orderNo := common.CreateOrderNo()
|
||||
|
||||
// 处理店铺内所有商品
|
||||
for _, item := range items {
|
||||
product := models.Product{}
|
||||
spec := models.Spec{}
|
||||
|
||||
// 获取产品信息
|
||||
err = models.DBService.Select("title,cover_image,cost_price,supply_id,args,gas_types").Table("mall_product").
|
||||
Where("id = ?", item.ProductID).First(&product).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
if errors.Is(err, models.ErrNotFound) {
|
||||
return nil, errcode.ErrRecordNotFound
|
||||
}
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
// 获取规格信息
|
||||
err := models.DBService.Select("title,serial_number,price").Table("mall_product_spec").
|
||||
Where("id = ?", item.SpecID).First(&spec).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
if errors.Is(err, models.ErrNotFound) {
|
||||
return nil, errcode.ErrRecordNotFound
|
||||
}
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
// 计算商品总价
|
||||
itemTotal := int64(item.Number) * spec.Price
|
||||
storeTotal += itemTotal
|
||||
|
||||
// 添加到订单详情
|
||||
storeDetails = append(storeDetails, &models.OrderDetails{
|
||||
Type: 1,
|
||||
ProductID: item.ProductID,
|
||||
ProductIdentity: item.ProductIdentity,
|
||||
SpecID: item.SpecID,
|
||||
OrderNo: orderNo,
|
||||
Title: product.Title,
|
||||
CoverImage: product.CoverImage,
|
||||
UnitPrice: spec.Price,
|
||||
SalesPrice: spec.Price,
|
||||
Number: item.Number,
|
||||
ProductArgs: item.ProductArgs,
|
||||
SummaryIdentity: summaryIdentity,
|
||||
SpecTitle: spec.Title,
|
||||
SpecNo: spec.SerialNumber,
|
||||
GasType: int32(product.GasTypes),
|
||||
SupplyId: product.SupplyId,
|
||||
TotalPrice: int64(item.Number) * spec.Price,
|
||||
})
|
||||
}
|
||||
|
||||
// 创建店铺订单摘要
|
||||
summary = append(summary, &models.OrderSummary{
|
||||
OrderNo: orderNo,
|
||||
Std_Identity: types.Std_Identity{Identity: summaryIdentity},
|
||||
PartnerID: in.PartnerId,
|
||||
TransPrice: storeTotal,
|
||||
TotalPrice: storeTotal,
|
||||
StoreID: StoreId,
|
||||
StoreIdentity: StoreIdentity,
|
||||
LogisticsFee: 0,
|
||||
Remark: "",
|
||||
AddressIdentity: in.AddressIdentity,
|
||||
County: address.Country,
|
||||
Province: address.Province,
|
||||
City: address.City,
|
||||
Area: address.Area,
|
||||
Address: address.Detail,
|
||||
Contact: address.Contact,
|
||||
Phone: address.Phone,
|
||||
Std_Passport: types.Std_Passport{PassportID: auth.ID, PassportIdentity: auth.Identity},
|
||||
Status: 1,
|
||||
})
|
||||
|
||||
// 添加到总详情列表
|
||||
details = append(details, storeDetails...)
|
||||
keys = append(keys, StoreIdentity)
|
||||
}
|
||||
|
||||
err = models.DBService.Transaction(func(tx *gorm.DB) error {
|
||||
// 1. 创建订单摘要
|
||||
if err := tx.Create(summary).Error; err != nil {
|
||||
printer.Error(err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
// 2. 创建订单详情
|
||||
if err := tx.Model(&models.OrderDetails{}).CreateInBatches(details, len(details)).Error; err != nil {
|
||||
printer.Error(err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
// 3. 扣减库存
|
||||
for _, detail := range details {
|
||||
if detail.Number > 0 {
|
||||
var stock int32
|
||||
result := tx.Table("mall_product_spec").Select("stock").Where("id = ?", detail.SpecID).Scan(&stock)
|
||||
if result.Error != nil || stock < detail.Number {
|
||||
log.Printf("Insufficient stock or spec does not exist: %v", result.Error)
|
||||
return errors.New("库存不足或规格不存在")
|
||||
}
|
||||
updateErr := tx.Table("mall_product_spec").
|
||||
Where("id = ? AND stock >= ?", detail.SpecID, detail.Number).
|
||||
UpdateColumn("stock", gorm.Expr("stock - ?", detail.Number)).
|
||||
Error
|
||||
if updateErr != nil {
|
||||
printer.Error(updateErr.Error())
|
||||
return updateErr
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 清空购物车
|
||||
if err := tx.Where("passport_identity = ?", auth.Identity).
|
||||
Delete(&models.OrderCart{}).Error; err != nil {
|
||||
printer.Error(err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
// 返回第一个订单号作为标识
|
||||
firstOrderNo := ""
|
||||
if len(summary) > 0 {
|
||||
firstOrderNo = summary[0].OrderNo
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Code: 0,
|
||||
Identity: firstOrderNo,
|
||||
Message: strings.Join(keys, ","),
|
||||
Timeseq: time.Now().UnixNano(),
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user