fix:购物车下单

This commit is contained in:
momo 2023-09-20 18:04:33 +08:00
parent 4d3f1512ac
commit 137919a4d7
7 changed files with 61 additions and 52 deletions

View File

@ -53,7 +53,7 @@ type PayInfo struct {
Metadata map[string]interface{} `json:"metadata"` // 额外参数
PayAmount AmountInfo `json:"pay_amount"` // 金额明细
PayMethod string `json:"pay_method"` // 交易方式
PayTime time.Time `json:"pay_time"` // 支付时间
PayTime **time.Time `json:"pay_time"` // 支付时间
Status PayStatus `json:"status"` // 当前状态
StatusLink []PayStatus `json:"status_link"` // 状态链路
TradeNo string `json:"trade_no"` // 支付交易号
@ -86,35 +86,35 @@ type PayStatus struct {
// 订单信息
type OrderInfo struct {
Ctime time.Time `json:"ctime"` // 创建日期
Ctime *time.Time `json:"ctime"` // 创建日期
DeliveryMethod int64 `json:"delivery_method"` // 物流类型
Metadata map[string]interface{} `json:"metadata"` // 额外参数
OrderSn string `json:"order_sn"` // 订单编号
Status OrderStatus `json:"status"` // 当前状态
StatusLink []OrderStatus `json:"status_link"` // 状态链路
Utime time.Time `json:"utime"` // 更新时间
Utime *time.Time `json:"utime"` // 更新时间
}
// 订单状态--用户
type OrderStatus struct {
Children []*OrderStatus `json:"children"` // 子状态,管理人员的处理状态, 用户不可见
Ctime time.Time `json:"ctime"` // 创建时间
ExpectedTime time.Time `json:"expected_time"` // 预计时间
Ctime *time.Time `json:"ctime"` // 创建时间
ExpectedTime *time.Time `json:"expected_time"` // 预计时间
Metadata map[string]interface{} `json:"metadata"` // 额外参数
StatusCode constants.OrderStatusCode `json:"status_code"` // 状态编码
StatusTitle string `json:"status_title"` // 状态名称
Utime time.Time `json:"utime"` // 更新时间
Utime *time.Time `json:"utime"` // 更新时间
}
// 订单商品
type OrderProduct struct {
TotalPrice AmountInfo `json:"amount"` // 商品总价
ExpectedDeliveryTime time.Time `json:"expected_delivery_time"` // 预计到货时间
ExpectedDeliveryTime *time.Time `json:"expected_delivery_time"` // 预计到货时间
PurchaseQuantity int64 `json:"purchase_quantity"` // 购买数量
ProductID int64 `json:"product_id"` // 商品ID
ProductName string `json:"product_name"` // 商品名称
ItemPrice AmountInfo `json:"product_price"` // 商品单价
ProductSnapshot map[string]interface{} `json:"product_snapshot"` // 商品快照
ProductSnapshot interface{} `json:"product_snapshot"` // 商品快照
ShoppingCartSnapshot *FsShoppingCart `json:"shopping_cart_snapshot"` // 购物车快照
ProductCover string `json:"product_cover"` // 商品封面
ProductCoverMetadata map[string]interface{} `json:"product_cover_metadata"` // 商品封面

View File

@ -4,6 +4,10 @@ import (
"context"
)
func (m *FsShoppingCartModel) TableName() string {
return m.name
}
// 关联查询
type RelaFsShoppingCart struct {
FsShoppingCart
@ -37,6 +41,7 @@ type ModelInfo struct {
type FittingInfo struct {
FittingJson string `json:"fitting_json"` //配件设计json数据
FittingName string `json:"fitting_name"` //配件名称
}
type TemplateInfo struct {
TemplateJson string `json:"template_json"` //模板设计json数据

View File

@ -36,7 +36,7 @@ func NewCreateOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Creat
func (l *CreateOrderLogic) CreateOrder(req *types.CreateOrderReq, userinfo *auth.UserInfo) (resp *basic.Response) {
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
// userinfo 传入值时, 一定不为null
if userinfo.IsUser() {
if !userinfo.IsUser() {
// 如果是,返回未授权的错误码
return resp.SetStatus(basic.CodeUnAuth)
}
@ -54,7 +54,9 @@ func (l *CreateOrderLogic) CreateOrder(req *types.CreateOrderReq, userinfo *auth
return resp.SetStatus(&res.ErrorCode)
}
return resp.SetStatus(basic.CodeOK)
return resp.SetStatus(basic.CodeOK, map[string]interface{}{
"order_sn": res.OrderSn,
})
}
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理

View File

@ -0,0 +1,7 @@
package main
import "testing"
func TestMain(t *testing.T) {
main()
}

View File

@ -11,6 +11,7 @@ import (
"time"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/zeromicro/go-zero/core/logx"
"gorm.io/gorm"
)
@ -65,9 +66,10 @@ func (d *defaultOrder) Create(ctx context.Context, in *CreateReq) (res *CreateRe
err = d.MysqlConn.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
// 查询购物车
var shoppingCartList []*gmodel.RelaFsShoppingCart
resShoppingCartFind := tx.Preload("ShoppingCartProduct", func(dbPreload *gorm.DB) *gorm.DB {
return dbPreload.Table(gmodel.NewFsProductModel(tx).TableName()).Preload("CoverResource")
}).Preload("ShoppingCartProductPriceList").
resShoppingCartFind := tx.Table(gmodel.NewFsShoppingCartModel(tx).TableName()).
Preload("ShoppingCartProduct", func(dbPreload *gorm.DB) *gorm.DB {
return dbPreload.Table(gmodel.NewFsProductModel(tx).TableName()).Preload("CoverResource")
}).Preload("ShoppingCartProductPriceList").
Preload("ShoppingCartProductModel3dList").
Preload("ShoppingCartProductModel3dFitting").
Where("id IN ?", in.CartIds).
@ -150,7 +152,7 @@ func (d *defaultOrder) Create(ctx context.Context, in *CreateReq) (res *CreateRe
} else {
var isProductPrice bool
for _, shoppingCartProductPriceInfo := range shoppingCart.ShoppingCartProductPriceList {
if shoppingCart.SizeId == shoppingCartProductPriceInfo.SizeId {
if *shoppingCart.SizeId == *shoppingCartProductPriceInfo.SizeId {
shoppingCartProductPrice = shoppingCartProductPriceInfo
isProductPrice = true
break
@ -172,7 +174,7 @@ func (d *defaultOrder) Create(ctx context.Context, in *CreateReq) (res *CreateRe
} else {
var isProductModel bool
for _, shoppingCartProductModel3dInfo := range shoppingCart.ShoppingCartProductModel3dList {
if shoppingCart.SizeId == shoppingCartProductModel3dInfo.SizeId {
if *shoppingCart.SizeId == *shoppingCartProductModel3dInfo.SizeId {
shoppingCartProductModel3d = shoppingCartProductModel3dInfo
isProductModel = true
break
@ -216,8 +218,6 @@ func (d *defaultOrder) Create(ctx context.Context, in *CreateReq) (res *CreateRe
orderProductTotal = orderProductTotal + productTotalPrice
// 订单商品
var productSnapshot = make(map[string]interface{}, 1)
productSnapshot["product_snapshot"] = shoppingCart.ShoppingCartProduct
var productCoverMetadata map[string]interface{}
if shoppingCart.ShoppingCartProduct.CoverResource != nil && shoppingCart.ShoppingCartProduct.CoverResource.Metadata != nil {
json.Unmarshal(*shoppingCart.ShoppingCartProduct.CoverResource.Metadata, &productCoverMetadata)
@ -230,7 +230,7 @@ func (d *defaultOrder) Create(ctx context.Context, in *CreateReq) (res *CreateRe
CurrentCurrency: in.CurrentCurrency,
OriginalCurrency: in.OriginalCurrency,
}),
ExpectedDeliveryTime: in.ExpectedDeliveryTime,
ExpectedDeliveryTime: &in.ExpectedDeliveryTime,
PurchaseQuantity: *shoppingCart.PurchaseQuantity,
ProductID: *shoppingCart.ProductId,
ProductCover: *shoppingCart.ShoppingCartProduct.Cover,
@ -243,7 +243,7 @@ func (d *defaultOrder) Create(ctx context.Context, in *CreateReq) (res *CreateRe
CurrentCurrency: in.CurrentCurrency,
OriginalCurrency: in.OriginalCurrency,
}),
ProductSnapshot: productSnapshot,
ProductSnapshot: shoppingCart.ShoppingCartProduct,
ShoppingCartSnapshot: &shoppingCart.FsShoppingCart,
ProductSn: *shoppingCart.ShoppingCartProduct.Sn,
DiyInformation: &shoppingCartSnapshot.UserDiyInformation,
@ -322,15 +322,15 @@ func (d *defaultOrder) Create(ctx context.Context, in *CreateReq) (res *CreateRe
// 订单状态--当前
var status = gmodel.OrderStatus{
Ctime: nowTime,
Utime: nowTime,
Ctime: &nowTime,
Utime: &nowTime,
StatusCode: constants.ORDERSTATUSUNPAIDDEPOSIT,
StatusTitle: constants.OrderStatusMessage[constants.ORDERSTATUSUNPAIDDEPOSIT],
}
// 订单状态--链路
var statusLink = order.GenerateOrderStatusLink(in.DeliveryMethod, nowTime, in.ExpectedDeliveryTime)
var orderInfo = gmodel.OrderInfo{
Ctime: nowTime,
Ctime: &nowTime,
DeliveryMethod: in.DeliveryMethod,
OrderSn: orderSn,
Status: status,
@ -345,14 +345,19 @@ func (d *defaultOrder) Create(ctx context.Context, in *CreateReq) (res *CreateRe
PayStatus: payStatus,
}
// 数据库操作
var order = gmodel.NewFsOrder{
orderDetailByte, err := json.Marshal(orderDetail)
if err != nil {
return err
}
var order = gmodel.FsOrder{
UserId: &in.UserId,
DeliveryMethod: &in.DeliveryMethod,
OrderSn: &orderSn,
Status: (*int64)(&status.StatusCode),
PayStatus: (*int64)(&payStatus),
Ctime: &nowTime,
Metadata: &orderDetail,
Metadata: &orderDetailByte,
}
result := tx.Create(&order)
if result.Error != nil {
@ -361,6 +366,12 @@ func (d *defaultOrder) Create(ctx context.Context, in *CreateReq) (res *CreateRe
return nil
})
if err != nil {
logx.Errorf("create order failed, err: %v", err)
if errorCode.Code == 0 {
errorCode.Code = basic.CodeApiErr.Code
errorCode.Message = basic.CodeApiErr.Message
}
return &CreateRes{
OrderSn: orderSn,
ErrorCode: errorCode,

View File

@ -100,12 +100,12 @@ var (
CodeLogoCombineErr = &StatusResponse{5115, "logo combine fail"} // 合图失败
CodeLogoCombineNoFoundErr = &StatusResponse{5116, "template record not found"} // 模版不存在
CodeErrOrder = &StatusResponse{5300, "ocreate order failed"} // 订单错误
CodeErrOrderCreatShoppingCartEmpty = &StatusResponse{5301, "ocreate order failed, shopping cart is empty"} // 订单创建失败,购物车为空
CodeErrOrderCreatShoppingCartNotMatched = &StatusResponse{5302, "ocreate order failed, shopping cart not matched"} // 订单创建失败,购物车不相符
CodeErrOrderCreatProductAbsent = &StatusResponse{5303, "ocreate order failed, product is absent"} // 订单创建失败,商品不存在
CodeErrOrderCreatProductPriceAbsent = &StatusResponse{5304, "ocreate order failed, price of product is absent"} // 订单创建失败,商品价格不存在
CodeErrOrderCreatProductAccessoryAbsent = &StatusResponse{5305, "ocreate order failed, accessory of product is absent"} // 订单创建失败,商品配件不存在
CodeErrOrder = &StatusResponse{5300, "create order failed"} // 订单错误
CodeErrOrderCreatShoppingCartEmpty = &StatusResponse{5301, "create order failed, shopping cart is empty"} // 订单创建失败,购物车为空
CodeErrOrderCreatShoppingCartNotMatched = &StatusResponse{5302, "create order failed, shopping cart not matched"} // 订单创建失败,购物车不相符
CodeErrOrderCreatProductAbsent = &StatusResponse{5303, "create order failed, product is absent"} // 订单创建失败,商品不存在
CodeErrOrderCreatProductPriceAbsent = &StatusResponse{5304, "create order failed, price of product is absent"} // 订单创建失败,商品价格不存在
CodeErrOrderCreatProductAccessoryAbsent = &StatusResponse{5305, "create order failed, accessory of product is absent"} // 订单创建失败,商品配件不存在
)
type Response struct {

View File

@ -4,8 +4,6 @@ import (
"fmt"
"fusenapi/constants"
"fusenapi/model/gmodel"
"math/rand"
"strconv"
"time"
)
@ -82,23 +80,9 @@ func GetAmountInfo(req GetAmountInfoReq) gmodel.AmountInfo {
}
func GenerateOrderNumber(deliveryMethod int, userID int) string {
// 获取当前时间
now := time.Now()
// 生成年月日时分秒的字符串
year := strconv.Itoa(now.Year())
month := strconv.Itoa(int(now.Month()))
day := strconv.Itoa(now.Day())
hour := strconv.Itoa(now.Hour())
minute := strconv.Itoa(now.Minute())
second := strconv.Itoa(now.Second())
// 生成2位随机数
rand.Seed(time.Now().UnixNano())
randomNum := fmt.Sprintf("%02d", rand.Intn(100))
// 拼接订单号
orderNumber := year + month + day + hour + minute + second + randomNum + strconv.Itoa(userID) + strconv.Itoa(deliveryMethod)
t := time.Now()
orderNumber := fmt.Sprintf("%d%02d%02d%d", t.Year(), t.Month(), t.Day(), t.Nanosecond())
fmt.Println(orderNumber)
return orderNumber
}
@ -118,9 +102,9 @@ func GenerateOrderStatusLink(deliveryMethod int64, noTime time.Time, expectedTim
StatusTitle: constants.OrderStatusMessage[v],
})
}
list[0].Ctime = noTime
list[0].Utime = noTime
list[len(list)-1].ExpectedTime = expectedTime
list[0].Ctime = &noTime
list[0].Utime = &noTime
list[len(list)-1].ExpectedTime = &expectedTime
return list
}