Home  >  Article  >  Backend Development  >  How to use Go language to develop the order cancellation function of the ordering system

How to use Go language to develop the order cancellation function of the ordering system

WBOY
WBOYOriginal
2023-11-02 08:51:441288browse

How to use Go language to develop the order cancellation function of the ordering system

As the takeout market continues to expand, the demand for ordering systems has increased dramatically. Among them, the order system is an essential part of the ordering system. The order system includes many functions, such as ordering, payment, delivery, etc. However, in actual development, we often encounter situations where users cancel orders for various reasons. So, how to use Go language to develop the order cancellation function of the ordering system?

In this article, we will implement the order cancellation function through the following steps:

  1. Design database model

In the order system, we An order form needs to be designed. In this table, we need to include various information about the order, such as order number, user ID, merchant ID, order status, order amount, etc. In addition, we also need to design an order cancellation table to store canceled order information. When canceling an order, we need to store the canceled order information in the cancellation table and modify the original order status to "Cancelled".

The following is the data model of the order table and order cancellation table:

//Order information table
type Order struct {

OrderID    string    `json:"order_id"`
UserID     string    `json:"user_id"`
MerchantID string    `json:"merchant_id"`
Amount     float32   `json:"amount"`
Status     int       `json:"status"`          //订单状态:1:未支付  2:已支付  3:已完成  4:已取消
CreatedAt  time.Time `json:"created_at"`
UpdatedAt  time.Time `json:"updated_at"`

}
//Order Cancellation table
type OrderCancel struct {

OrderID   string    `json:"order_id"`
UserID    string    `json:"user_id"`
Reason    string    `json:"reason"`         //取消原因
CreatedAt time.Time `json:"created_at"`

}

  1. Implements the API interface for order cancellation

When implementing the API interface for order cancellation, We need to first check whether the status of the order is "Unpaid" or "Paid". If the order status is "Completed" or "Cancelled", cancellation is not possible. If the order status is "Unpaid" or "Paid", you can cancel it. Cancellation of an order needs to be completed in two steps. First, we need to modify the order status to "Cancelled" and then store the canceled order information into the order cancellation table.

The following is the API interface code for order cancellation:

func CancelOrder(c *gin.Context) {

orderID := c.Param("order_id")
reason := c.PostForm("reason")

//查询订单状态
orderDetail, err := db.GetOrderDetail(orderID)
if err != nil {
    c.JSON(http.StatusBadRequest, gin.H{"error": "订单不存在"})
    return
}
if orderDetail.Status != 1 && orderDetail.Status != 2 {
    c.JSON(http.StatusBadRequest, gin.H{"error": "订单状态无法取消"})
    return
}

//取消订单操作
tx := db.GetDB().Begin()
//将订单状态修改为取消
err = tx.Model(&db.Order{}).Where("order_id = ?", orderID).Update("status", 4).Error
if err != nil {
    tx.Rollback()
    c.JSON(http.StatusInternalServerError, gin.H{"error": "取消订单失败"})
    return
}
//将取消订单信息存储到订单取消表中
orderCancel := &db.OrderCancel{
    OrderID: orderID,
    UserID:  orderDetail.UserID,
    Reason:  reason,
}
err = tx.Create(orderCancel).Error
if err != nil {
    tx.Rollback()
    c.JSON(http.StatusInternalServerError, gin.H{"error": "取消订单失败"})
    return
}
tx.Commit()
c.JSON(http.StatusOK, gin.H{"message": "订单取消成功"})

}

  1. Verify cancellation Order function

In order to verify whether the order cancellation function is implemented, we can create a simple front-end page for order cancellation. On this page, we need to enter the order number and cancellation reason, and then click the "Cancel Order" button. If the order is canceled successfully, the message "Order Cancelled Successfully" will be displayed. If the order status cannot be canceled or the order cancellation fails, an appropriate error message will be displayed.

The following is a sample code for the front-end page:


<title>取消订单</title>


<form action="/cancel_order" method="POST">
    <p>订单号:<input type="text" name="order_id"></p>
    <p>取消原因:<input type="text" name="reason"></p>
    <p><input type="submit" value="取消订单"></p>
</form>


These are the specific steps to use Go language to develop the order cancellation function of the ordering system. Through the above steps, we can complete a simple order system, including various functions such as order submission, payment, completion and cancellation.

The above is the detailed content of How to use Go language to develop the order cancellation function of the ordering system. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn