fix version 1
This commit is contained in:
@@ -24,7 +24,7 @@ func Create(ctx context.Context, in *pb.CartAddRequest) (reply *pb.OrderStatusRe
|
||||
}
|
||||
var cart *models.OrderCart
|
||||
|
||||
err = impl.DBService.Where("cart_identity=? and product_identity=? and spec_id = ? ", in.CartIdentity, in.ProductIdentity, in.SpecId).First(&cart).Error
|
||||
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
|
||||
if err != nil {
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
printer.Error(err.Error())
|
||||
@@ -46,7 +46,7 @@ func Create(ctx context.Context, in *pb.CartAddRequest) (reply *pb.OrderStatusRe
|
||||
err = impl.DBService.Create(&data).Error
|
||||
} else {
|
||||
data.Number += cart.Number
|
||||
err = impl.DBService.Where("cart_identity=? and product_identity=? and spec_id = ? ", in.CartIdentity, in.ProductIdentity, in.SpecId).Updates(&data).Error
|
||||
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
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
|
||||
@@ -14,12 +14,12 @@ import (
|
||||
// 删除购物车中的商品
|
||||
func Delete(ctx context.Context, in *pb.CartDelRequest) (reply *pb.OrderStatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
_, err = service.ParseMetaCtx(ctx, nil)
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = impl.DBService.Where("id in ?", in.Id).Delete(&models.OrderCart{}).Error
|
||||
err = impl.DBService.Where("id in ? and passport_identity = ?", in.Id, auth.Identity).Delete(&models.OrderCart{}).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -27,8 +27,12 @@ func Fetch(ctx context.Context, in *pb.CartGetRequest) (reply *pb.CartGetReply,
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
}
|
||||
// 查询购物车数据
|
||||
err = impl.DBService.Where("cart_identity=? or passport_identity=?", in.GetCarIdentity(), auth.Identity).Find(&cart).Error
|
||||
// 查询购物车数据:仅返回属于当前登录者的条目,cart_identity 只作附加过滤。
|
||||
tx := impl.DBService.Where("passport_identity = ?", auth.Identity)
|
||||
if in.GetCarIdentity() != "" {
|
||||
tx = tx.Where("cart_identity = ?", in.GetCarIdentity())
|
||||
}
|
||||
err = tx.Find(&cart).Error
|
||||
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
@@ -39,29 +43,30 @@ func Fetch(ctx context.Context, in *pb.CartGetRequest) (reply *pb.CartGetReply,
|
||||
product := models.Product{}
|
||||
err := impl.DBService.Raw(`
|
||||
SELECT
|
||||
p.id, p.identity, p.title, p.cover_image, p.args, p.cost_price,
|
||||
p.id, p.identity, p.title, p.cover_image, p.args,
|
||||
( 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)
|
||||
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
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.SalesPrice,
|
||||
TotalPrice: int64(item.Number) * product.SalesPrice,
|
||||
Number: item.Number,
|
||||
Identity: item.Identity,
|
||||
}
|
||||
result = append(result, da)
|
||||
}
|
||||
|
||||
return &pb.CartGetReply{
|
||||
|
||||
@@ -16,14 +16,14 @@ import (
|
||||
// 修改购物车中的商品数量
|
||||
func Modify(ctx context.Context, in *pb.CartSetRequest) (reply *pb.OrderStatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
_, err = service.ParseMetaCtx(ctx, nil)
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(in.GetUpdates()) > 0 {
|
||||
err = impl.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
|
||||
err := tx.Table("order_cart").Where("id = ? and passport_identity = ?", update.Id, auth.Identity).Update("number", update.Number).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return err
|
||||
@@ -37,7 +37,7 @@ func Modify(ctx context.Context, in *pb.CartSetRequest) (reply *pb.OrderStatusRe
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
err = impl.DBService.Table("order_cart").Where("id = ?", in.Id).Update("number", in.Number).Error
|
||||
err = impl.DBService.Table("order_cart").Where("id = ? and passport_identity = ?", in.Id, auth.Identity).Update("number", in.Number).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
|
||||
@@ -10,21 +10,45 @@ import (
|
||||
|
||||
"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 OrderApprove(ctx context.Context, in *pb.OrderIdentRequest) (reply *pb.OrderStatusReply, 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
|
||||
}
|
||||
|
||||
// 验证输入参数是否有效。
|
||||
if in.GetIdentity() == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
// 审批动作只允许 4:申请通过,-2:未通过。
|
||||
if in.GetApprove() != 4 && in.GetApprove() != -2 {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
// 获取当前订单信息。
|
||||
var order models.OrderSummary
|
||||
if err := impl.DBService.Preload("OrderDetails").Where("identity=?", in.Identity).First(&order).Error; err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
// 只有处于"申请中"(1:申请退款 2:申请退货 3:申请退款退货)的订单才允许审批,拒绝非法状态跃迁。
|
||||
if order.Approve != 1 && order.Approve != 2 && order.Approve != 3 {
|
||||
return nil, errcode.ErrUnavailable
|
||||
}
|
||||
// 归属校验:管理员只能审批本店订单,避免通过直连 mall_product_spec 回补他店商品库存。
|
||||
if owner, ok := auth.Owner.(map[string]any); ok {
|
||||
if storeIdentity, ok := owner["store_identity"].(string); ok && storeIdentity != "" && storeIdentity != order.StoreIdentity {
|
||||
return nil, errcode.ErrPermissionDenied
|
||||
}
|
||||
}
|
||||
|
||||
// 根据订单审批状态更新订单状态:状态:-2:未通过,0:默认 1:申请退款 2:申请退货 3:申请退款退货 4:申请通过
|
||||
switch order.Approve {
|
||||
@@ -44,27 +68,30 @@ func OrderApprove(ctx context.Context, in *pb.OrderIdentRequest) (reply *pb.Orde
|
||||
// 申请退货
|
||||
case 2:
|
||||
if in.GetApprove() == 4 {
|
||||
impl.DBService.Transaction(func(tx *gorm.DB) error {
|
||||
err = tx.Model(&models.OrderSummary{}).Where("identity=?", in.Identity).
|
||||
// 事务错误需向上传递,避免回补库存失败却对外返回成功。
|
||||
err = impl.DBService.Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Model(&models.OrderSummary{}).Where("identity=?", in.Identity).
|
||||
Updates(map[string]any{
|
||||
"approve": in.GetApprove(),
|
||||
"status": 8,
|
||||
}).Error
|
||||
if err != nil {
|
||||
}).Error; 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 {
|
||||
if err := tx.Table("mall_product_spec").Where("id = ?", specs.SpecID).UpdateColumn("stock", gorm.Expr("stock + ?", specs.Number)).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
}
|
||||
// 申请退款退货
|
||||
case 3:
|
||||
|
||||
@@ -2,6 +2,7 @@ package mgt
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/ec/order/internal/impl"
|
||||
@@ -11,6 +12,7 @@ import (
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// 取消订单
|
||||
@@ -30,15 +32,54 @@ func OrderCancel(ctx context.Context, in *pb.OrderIdentRequest) (reply *pb.Order
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// 验证输入参数是否有效。
|
||||
if in.GetIdentity() == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
err = impl.DBService.Model(&models.OrderSummary{}).Where("identity=?", in.Identity).Update("status", -1).Error
|
||||
order := new(models.OrderSummary)
|
||||
err = impl.DBService.Where("identity = ?", in.Identity).First(order).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
if errors.Is(err, models.ErrNotFound) {
|
||||
return nil, errcode.ErrRecordNotFound
|
||||
}
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
// 状态机约束:只有"未支付"(status=1)的订单允许取消,其它状态拒绝,避免非法状态跃迁。
|
||||
if order.Status != 1 {
|
||||
return nil, errcode.ErrUnavailable
|
||||
}
|
||||
|
||||
details := make([]*models.OrderDetails, 0)
|
||||
err = impl.DBService.Transaction(func(tx *gorm.DB) error {
|
||||
// 以状态为条件更新,避免并发下重复取消导致库存重复回补。
|
||||
result := tx.Model(&models.OrderSummary{}).Where("identity = ? AND status = ?", in.Identity, 1).Update("status", -1)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
if result.RowsAffected == 0 {
|
||||
return errcode.ErrUnavailable
|
||||
}
|
||||
|
||||
// 按订单明细的规格回补库存,避免取消后库存丢失。
|
||||
if err := tx.Where("summary_identity = ?", order.Identity).Find(&details).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
for _, d := range details {
|
||||
if d.Number > 0 {
|
||||
if err := tx.Table("mall_product_spec").Where("id = ?", d.SpecID).
|
||||
UpdateColumn("stock", gorm.Expr("stock + ?", d.Number)).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
if errors.Is(err, errcode.ErrUnavailable) {
|
||||
return nil, errcode.ErrUnavailable
|
||||
}
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.OrderStatusReply{
|
||||
Code: 0,
|
||||
Message: "OK",
|
||||
|
||||
@@ -71,8 +71,9 @@ func OrderCreate(ctx context.Context, in *pb.CreateOrderRequest) (reply *pb.Orde
|
||||
}
|
||||
|
||||
// 从产品信息中提取单价、ID等详情,并构建订单详情对象。
|
||||
// 单价取实际的规格销售价 spec.price(商品的 sales_price 列从不被写入,取它会导致成交金额恒为 0)。
|
||||
productID := product["id"].(int64)
|
||||
unit_price := product["sales_price"].(int64)
|
||||
unit_price := spec["price"].(int64)
|
||||
// 计算交易价格并构建订单摘要对象。
|
||||
itemTotal := int64(specs.Number) * unit_price
|
||||
TotalPrice += itemTotal
|
||||
@@ -85,7 +86,9 @@ func OrderCreate(ctx context.Context, in *pb.CreateOrderRequest) (reply *pb.Orde
|
||||
Title: product["title"].(string),
|
||||
CoverImage: product["cover_image"].(string),
|
||||
UnitPrice: unit_price,
|
||||
SalesPrice: unit_price,
|
||||
Number: specs.Number,
|
||||
TotalPrice: itemTotal,
|
||||
ProductArgs: product["args"].(string),
|
||||
SpecID: spec["id"].(int64),
|
||||
SpecNo: spec["serial_number"].(string),
|
||||
@@ -96,11 +99,14 @@ func OrderCreate(ctx context.Context, in *pb.CreateOrderRequest) (reply *pb.Orde
|
||||
specList = append(specList, *details)
|
||||
}
|
||||
|
||||
// 查询地址信息以确保其存在。
|
||||
// 查询地址信息以确保其存在,并按归属过滤:只允许使用属于当前登录者的地址。
|
||||
address := map[string]any{}
|
||||
err = impl.DBService.Table("address_library").Take(&address, "identity=?", in.AddressIdentity).Error
|
||||
err = impl.DBService.Table("address_library").Take(&address, "identity=? and owner_identity=?", in.AddressIdentity, auth.Identity).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
if errors.Is(err, models.ErrNotFound) {
|
||||
return nil, errcode.ErrPermissionDenied
|
||||
}
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@ package mgt
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
pb "bsm/full/module/ec/order/pb"
|
||||
|
||||
@@ -22,14 +21,6 @@ func OrderModify(ctx context.Context, in *pb.OrderSummaryItem) (reply *pb.OrderS
|
||||
return nil, errcode.ErrPermissionDenied
|
||||
}
|
||||
|
||||
// TODO: valid code
|
||||
|
||||
// TODO: add your logic code & delete this line.
|
||||
|
||||
return &pb.OrderStatusReply{
|
||||
Code: 0,
|
||||
Message: "OK",
|
||||
Timeseq: time.Now().UnixNano(),
|
||||
}, nil
|
||||
|
||||
// 该方法尚未实现,显式返回未实现错误,避免调用方误判改单成功。
|
||||
return nil, errcode.ErrUnimplemented
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package summary
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/ec/order/internal/impl"
|
||||
@@ -11,6 +12,7 @@ import (
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// 取消订单
|
||||
@@ -31,25 +33,55 @@ func Cancel(ctx context.Context, in *pb.CancelRequest) (reply *pb.OrderStatusRep
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
if summary.Status == 1 {
|
||||
values := &models.OrderSummary{
|
||||
CouponIdentity: "",
|
||||
CouponAmount: 0.00,
|
||||
// 状态机约束:只有"未支付"(status=1)的订单允许取消,其它状态拒绝,避免非法状态跃迁。
|
||||
if summary.Status != 1 {
|
||||
return nil, errcode.ErrUnavailable
|
||||
}
|
||||
|
||||
details := make([]*models.OrderDetails, 0)
|
||||
err = impl.DBService.Transaction(func(tx *gorm.DB) error {
|
||||
// 1. 以状态为条件更新订单,避免并发下重复取消。
|
||||
result := tx.Model(&models.OrderSummary{}).Where("order_no = ? AND status = ?", in.OrderNo, 1).
|
||||
Updates(map[string]any{
|
||||
"status": -1,
|
||||
"coupon_identity": "",
|
||||
"coupon_amount": 0,
|
||||
})
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
values.Status = -1
|
||||
|
||||
err := impl.DBService.Where("order_no = ?", in.OrderNo).Updates(values).Error
|
||||
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
if result.RowsAffected == 0 {
|
||||
return errcode.ErrUnavailable
|
||||
}
|
||||
|
||||
err = impl.DBService.Model(&models.OrderCoupon{}).Where("identity=?", summary.CouponIdentity).Update("status", 2).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
// 2. 按订单明细的规格回补库存,避免取消后库存丢失。
|
||||
if err := tx.Where("summary_identity = ?", summary.Identity).Find(&details).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
for _, d := range details {
|
||||
if d.Number > 0 {
|
||||
if err := tx.Table("mall_product_spec").Where("id = ?", d.SpecID).
|
||||
UpdateColumn("stock", gorm.Expr("stock + ?", d.Number)).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 退回已使用的优惠券,使其重新可用。
|
||||
if summary.CouponIdentity != "" {
|
||||
if err := tx.Model(&models.OrderCoupon{}).Where("identity = ?", summary.CouponIdentity).
|
||||
Update("status", 2).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
if errors.Is(err, errcode.ErrUnavailable) {
|
||||
return nil, errcode.ErrUnavailable
|
||||
}
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.OrderStatusReply{
|
||||
|
||||
@@ -2,6 +2,7 @@ package summary
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/ec/order/internal/impl"
|
||||
"bsm/full/module/ec/order/internal/models"
|
||||
@@ -15,7 +16,7 @@ import (
|
||||
// 确认订单,物流,优惠卷等其它信息
|
||||
func Confirm(ctx context.Context, in *pb.ConfirmRequest) (reply *pb.ConfirmReply, err error) {
|
||||
// parse authorization meta.
|
||||
_, err = service.ParseMetaCtx(ctx, nil)
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -54,27 +55,56 @@ func Confirm(ctx context.Context, in *pb.ConfirmRequest) (reply *pb.ConfirmReply
|
||||
|
||||
if in.CouponIdentity != "" {
|
||||
err = impl.DBService.Where("identity=?", in.CouponIdentity).First(&coupon).Error
|
||||
if err == nil {
|
||||
if coupon.Status == 2 {
|
||||
impl.DBService.Model(&models.OrderCoupon{}).Where("identity=?", in.CouponIdentity).Update("status", 3)
|
||||
couponAmount = coupon.Amount
|
||||
} else {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrUnavailable
|
||||
}
|
||||
} else {
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrRecordNotFound
|
||||
}
|
||||
// 校验优惠券归属:passport_id 或 passport_identity 命中调用者才算本人所有。
|
||||
if coupon.PassportIdentity != auth.Identity && coupon.PassportID != auth.ID {
|
||||
return nil, errcode.ErrPermissionDenied
|
||||
}
|
||||
// 校验使用状态:仅可用的优惠券(status=2)可使用。
|
||||
if coupon.Status != 2 {
|
||||
return nil, errcode.ErrUnavailable
|
||||
}
|
||||
// 校验有效期:started/expired 为字符串,格式可解析时强制校验,无法解析时跳过。
|
||||
now := time.Now()
|
||||
if started, ok := parseCouponTime(coupon.Started); ok && now.Before(started) {
|
||||
return nil, errcode.ErrUnavailable
|
||||
}
|
||||
if expired, ok := parseCouponTime(coupon.Expired); ok && now.After(expired) {
|
||||
return nil, errcode.ErrUnavailable
|
||||
}
|
||||
// 核销优惠券:带状态条件更新并检查影响行数,避免重复使用同一张券。
|
||||
result := impl.DBService.Model(&models.OrderCoupon{}).Where("identity = ? AND status = 2", in.CouponIdentity).Update("status", 3)
|
||||
if result.Error != nil {
|
||||
printer.Error(result.Error.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
if result.RowsAffected == 0 {
|
||||
return nil, errcode.ErrUnavailable
|
||||
}
|
||||
couponAmount = coupon.Amount
|
||||
}
|
||||
|
||||
if couponAmount > 0 {
|
||||
summary.TotalPrice = summary.TotalPrice + couponAmount
|
||||
// 优惠券为减免项,抵扣后总额不得为负:抵扣额不超过订单金额。
|
||||
if couponAmount >= summary.TotalPrice {
|
||||
summary.TotalPrice = 0
|
||||
} else {
|
||||
summary.TotalPrice = summary.TotalPrice - couponAmount
|
||||
}
|
||||
// 回写所用券与其抵扣额,供取消订单时退券使用。
|
||||
summary.CouponIdentity = in.CouponIdentity
|
||||
summary.CouponAmount = couponAmount
|
||||
}
|
||||
|
||||
summary.Status = 1
|
||||
|
||||
err = impl.DBService.Where("order_no = ?", in.OrderNo).Updates(summary).Error
|
||||
// 显式指定需落库的列,保证总额抵扣为 0 时也能写入。
|
||||
err = impl.DBService.Where("order_no = ?", in.OrderNo).
|
||||
Select("total_price", "coupon_identity", "coupon_amount", "status").
|
||||
Updates(summary).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
@@ -84,3 +114,17 @@ func Confirm(ctx context.Context, in *pb.ConfirmRequest) (reply *pb.ConfirmReply
|
||||
TotalPrice: summary.TotalPrice,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// parseCouponTime 解析优惠券 started/expired 时间字段,兼容"2006-01-02 15:04:05"与"2006-01-02"两种格式;
|
||||
// 无法解析时返回 ok=false,由调用方跳过该项校验。
|
||||
func parseCouponTime(v string) (time.Time, bool) {
|
||||
if v == "" {
|
||||
return time.Time{}, false
|
||||
}
|
||||
for _, layout := range []string{"2006-01-02 15:04:05", "2006-01-02"} {
|
||||
if t, err := time.ParseInLocation(layout, v, time.Local); err == nil {
|
||||
return t, true
|
||||
}
|
||||
}
|
||||
return time.Time{}, false
|
||||
}
|
||||
|
||||
@@ -2,8 +2,11 @@ package summary
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"bsm/full/module/ec/order/internal/impl"
|
||||
"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"
|
||||
@@ -14,7 +17,7 @@ import (
|
||||
// 获取一个订单的详情数据
|
||||
func Get(ctx context.Context, in *pb.OrderIdentRequest) (reply *pb.SummaryGetReply, err error) {
|
||||
// parse authorization meta.
|
||||
_, err = service.ParseMetaCtx(ctx, nil)
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -24,13 +27,30 @@ func Get(ctx context.Context, in *pb.OrderIdentRequest) (reply *pb.SummaryGetRep
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
summary, err := common.GetOrderSummaryByIdentity(in.Identity)
|
||||
order := new(models.OrderSummary)
|
||||
err = impl.DBService.Preload("OrderDetails").Where("identity = ?", in.Identity).First(&order).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
if errors.Is(err, models.ErrNotFound) {
|
||||
return nil, errcode.ErrRecordNotFound
|
||||
}
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
// 归属校验:订单只能由下单人本人或订单所属店铺的管理员/员工查看,避免越权读取他人订单与收货信息。
|
||||
if order.PassportIdentity != auth.Identity {
|
||||
ownerStoreIdentity := ""
|
||||
if owner, ok := auth.Owner.(map[string]any); ok {
|
||||
if v, ok := owner["store_identity"].(string); ok {
|
||||
ownerStoreIdentity = v
|
||||
}
|
||||
}
|
||||
if ownerStoreIdentity == "" || ownerStoreIdentity != order.StoreIdentity {
|
||||
return nil, errcode.ErrPermissionDenied
|
||||
}
|
||||
}
|
||||
|
||||
return &pb.SummaryGetReply{
|
||||
Summary: summary,
|
||||
Summary: common.ReflectProtoOrderSummary(order),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -74,13 +74,13 @@ func QuickCreateByProduct(ctx context.Context, in *pb.QuickCreateByProductReques
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
// 查询地址信息以确保其存在。
|
||||
// 查询地址信息以确保其存在,并按归属过滤:只允许使用属于当前登录者的地址。
|
||||
address := map[string]any{}
|
||||
err = impl.DBService.Table("address_library").Take(&address, "identity=?", in.AddressIdentity).Error
|
||||
err = impl.DBService.Table("address_library").Take(&address, "identity=? and owner_identity=?", in.AddressIdentity, auth.Identity).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
if errors.Is(err, models.ErrNotFound) {
|
||||
return nil, errcode.ErrRecordNotFound
|
||||
return nil, errcode.ErrPermissionDenied
|
||||
}
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
@@ -125,7 +125,8 @@ func QuickCreateByProduct(ctx context.Context, in *pb.QuickCreateByProductReques
|
||||
summary.Status = 1
|
||||
|
||||
// 从产品信息中提取单价、ID等详情,并构建订单详情对象。
|
||||
unit_price := product["sales_price"].(int64)
|
||||
// 单价取实际的规格销售价 spec.price(商品的 sales_price 列从不被写入,取它会导致成交金额恒为 0)。
|
||||
unit_price := spec["price"].(int64)
|
||||
productID := product["id"].(int64)
|
||||
details := &models.OrderDetails{
|
||||
Type: 1,
|
||||
@@ -136,7 +137,9 @@ func QuickCreateByProduct(ctx context.Context, in *pb.QuickCreateByProductReques
|
||||
Title: product["title"].(string),
|
||||
CoverImage: product["cover_image"].(string),
|
||||
UnitPrice: unit_price,
|
||||
SalesPrice: unit_price,
|
||||
Number: in.Number,
|
||||
TotalPrice: int64(in.Number) * unit_price,
|
||||
ProductArgs: product["args"].(string),
|
||||
SummaryIdentity: summary.Identity,
|
||||
SpecTitle: spec["title"].(string),
|
||||
|
||||
@@ -9,9 +9,11 @@ import (
|
||||
"bsm/full/module/ec/order/internal/models"
|
||||
pb "bsm/full/module/ec/order/pb"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/env"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// 模拟支付
|
||||
@@ -22,7 +24,20 @@ func SimulatePay(ctx context.Context, in *pb.SimulatePayRequest) (reply *pb.Orde
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = impl.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
|
||||
// 模拟接口仅允许在 dev 模式使用,其它模式默认关闭,避免生产环境被用于伪造支付、推进订单状态。
|
||||
if env.Runtime == nil || env.Runtime.Mode != "dev" {
|
||||
return nil, errcode.ErrPermissionDenied
|
||||
}
|
||||
|
||||
// 支付金额取订单实际应付金额,交易号与备注从环境变量读取,避免硬编码的固定金额导致账实不符。
|
||||
err = impl.DBService.Model(&models.OrderSummary{}).Where("identity in ?", in.Identity).Updates(map[string]any{
|
||||
"status": 2,
|
||||
"pay_time": time.Now(),
|
||||
"pay_type": 4,
|
||||
"pay_trade_no": env.GetEnvDefault("BSM_SimulatePayTradeNo", "SIMULATE"),
|
||||
"pay_remark": env.GetEnvDefault("BSM_SimulatePayRemark", "模拟支付"),
|
||||
"pay_amount": gorm.Expr("trans_price"),
|
||||
}).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
if errors.Is(err, models.ErrNotFound) {
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"bsm/full/module/ec/order/internal/models"
|
||||
pb "bsm/full/module/ec/order/pb"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/env"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
@@ -21,6 +22,11 @@ func SimulateReceiving(ctx context.Context, in *pb.OrderIdentRequest) (reply *pb
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 模拟接口仅允许在 dev 模式使用,其它模式默认关闭,避免生产环境被用于伪造收货、推进订单状态。
|
||||
if env.Runtime == nil || env.Runtime.Mode != "dev" {
|
||||
return nil, errcode.ErrPermissionDenied
|
||||
}
|
||||
// valildate request id,identity.
|
||||
if in.Id == 0 && in.Identity == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"bsm/full/module/ec/order/internal/models"
|
||||
pb "bsm/full/module/ec/order/pb"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/env"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
@@ -22,6 +23,11 @@ func SimulateShipments(ctx context.Context, in *pb.SimulateShipmentsRequest) (re
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 模拟接口仅允许在 dev 模式使用,其它模式默认关闭,避免生产环境被用于伪造发货、推进订单状态。
|
||||
if env.Runtime == nil || env.Runtime.Mode != "dev" {
|
||||
return nil, errcode.ErrPermissionDenied
|
||||
}
|
||||
|
||||
if in.GetMemberIdentity() == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package summary
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -33,12 +33,11 @@ func Submit(ctx context.Context, in *pb.SubmitRequest) (reply *pb.OrderStatusRep
|
||||
}
|
||||
|
||||
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
|
||||
cart = make([]*models.OrderCart, 0)
|
||||
details = make([]*models.OrderDetails, 0)
|
||||
summary = make([]*models.OrderSummary, 0)
|
||||
keys = make([]string, 0)
|
||||
address = models.OrderAddress{}
|
||||
)
|
||||
|
||||
// 获取购物车内数据信息
|
||||
@@ -51,6 +50,26 @@ func Submit(ctx context.Context, in *pb.SubmitRequest) (reply *pb.OrderStatusRep
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
// 只处理请求中选中的购物车条目;未指定 id 时按整车处理,兼容旧调用方。
|
||||
if ids := in.GetId(); len(ids) > 0 {
|
||||
wanted := make(map[uint]struct{}, len(ids))
|
||||
for _, id := range ids {
|
||||
if v, e := strconv.ParseUint(id, 10, 64); e == nil {
|
||||
wanted[uint(v)] = struct{}{}
|
||||
}
|
||||
}
|
||||
selected := make([]*models.OrderCart, 0, len(ids))
|
||||
for _, item := range cart {
|
||||
if _, ok := wanted[item.ID]; ok {
|
||||
selected = append(selected, item)
|
||||
}
|
||||
}
|
||||
cart = selected
|
||||
}
|
||||
if len(cart) == 0 {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// 获取地址信息
|
||||
if in.GetAddress() != nil {
|
||||
address = models.OrderAddress{
|
||||
@@ -65,21 +84,25 @@ func Submit(ctx context.Context, in *pb.SubmitRequest) (reply *pb.OrderStatusRep
|
||||
ZipCode: in.Address.ZipCode,
|
||||
}
|
||||
} else {
|
||||
err = impl.DBService.Table("address_library").Where("identity = ?", in.AddressIdentity).Find(&address).Error
|
||||
// 地址库按归属过滤:仅允许使用属于当前登录者的收货地址,避免盗用他人地址下单。
|
||||
err = impl.DBService.Table("address_library").
|
||||
Where("identity = ? and owner_identity = ?", in.AddressIdentity, auth.Identity).
|
||||
Take(&address).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
if errors.Is(err, models.ErrNotFound) {
|
||||
return nil, errcode.ErrRecordNotFound
|
||||
return nil, errcode.ErrPermissionDenied
|
||||
}
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
}
|
||||
|
||||
// 按店铺分组购物车商品
|
||||
// 按店铺分组购物车商品,并记录每个店铺对应的 store_id。
|
||||
storeGroups := make(map[string][]*models.OrderCart)
|
||||
for k, item := range cart {
|
||||
storeIDs := make(map[string]uint)
|
||||
for _, item := range cart {
|
||||
product := models.Product{}
|
||||
err = impl.DBService.Select("store_identity").Table("mall_product").Where("id = ?", item.ProductID).First(&product).Error
|
||||
err = impl.DBService.Select("store_id,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) {
|
||||
@@ -88,9 +111,7 @@ func Submit(ctx context.Context, in *pb.SubmitRequest) (reply *pb.OrderStatusRep
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
if k == 0 {
|
||||
StoreId = product.StoreId
|
||||
}
|
||||
storeIDs[product.StoreIdentity] = product.StoreId
|
||||
storeGroups[product.StoreIdentity] = append(storeGroups[product.StoreIdentity], item)
|
||||
}
|
||||
|
||||
@@ -163,7 +184,7 @@ func Submit(ctx context.Context, in *pb.SubmitRequest) (reply *pb.OrderStatusRep
|
||||
PartnerID: in.PartnerId,
|
||||
TransPrice: storeTotal,
|
||||
TotalPrice: storeTotal,
|
||||
StoreID: StoreId,
|
||||
StoreID: storeIDs[StoreIdentity],
|
||||
StoreIdentity: StoreIdentity,
|
||||
LogisticsFee: 0,
|
||||
Remark: "",
|
||||
@@ -197,28 +218,29 @@ func Submit(ctx context.Context, in *pb.SubmitRequest) (reply *pb.OrderStatusRep
|
||||
return err
|
||||
}
|
||||
|
||||
// 3. 扣减库存
|
||||
// 3. 扣减库存:按规格做原子条件更新,影响行数为 0 即视为库存不足并回滚整个下单事务,避免超卖。
|
||||
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").
|
||||
result := 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
|
||||
UpdateColumn("stock", gorm.Expr("stock - ?", detail.Number))
|
||||
if result.Error != nil {
|
||||
printer.Error(result.Error.Error())
|
||||
return result.Error
|
||||
}
|
||||
if result.RowsAffected == 0 {
|
||||
printer.Error("库存不足或规格不存在")
|
||||
return errors.New("库存不足或规格不存在")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 清空购物车
|
||||
if err := tx.Where("passport_identity = ?", auth.Identity).
|
||||
// 4. 只删除本次已下单的购物车条目,保留用户未结算的其它条目。
|
||||
cartIDs := make([]uint, 0, len(cart))
|
||||
for _, item := range cart {
|
||||
cartIDs = append(cartIDs, item.ID)
|
||||
}
|
||||
if err := tx.Where("id in ?", cartIDs).
|
||||
Delete(&models.OrderCart{}).Error; err != nil {
|
||||
printer.Error(err.Error())
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user