-
Notifications
You must be signed in to change notification settings - Fork 120
/
custormerOrder.go
107 lines (92 loc) · 2.97 KB
/
custormerOrder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package api
import (
"net/http"
"github.com/Allenxuxu/mogutouERP/middleware"
"github.com/Allenxuxu/mogutouERP/models"
"github.com/Allenxuxu/mogutouERP/utils/response"
"github.com/gin-gonic/gin"
)
// CreateCustormerOrder 创建新的客户订单
func CreateCustormerOrder(c *gin.Context) {
userName := c.GetString(middleware.RequestUserNameKey)
var data struct {
Remarks string `json:"remarks"`
Amount float32 `json:"amount" binding:"required"`
Name string `json:"name" binding:"required"`
Tel string `json:"tel" binding:"required"`
DeliveryAddress string `json:"deliveryAddress" binding:"required"`
DeliveryTime string `json:"deliveryTime" binding:"required"`
Deposit float32 `json:"deposit" binding:"required"`
Goods []struct {
ID string `json:"id" binding:"required"`
Number uint `json:"number" binding:"required"`
}
}
err := c.ShouldBindJSON(&data)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, response.Error{Error: err.Error()})
return
}
if len(data.Goods) == 0 {
c.AbortWithStatusJSON(http.StatusBadRequest, response.Error{Error: "没有商品"})
return
}
goods := make([]models.CustormerGoods, len(data.Goods))
for i, v := range data.Goods {
goods[i].GoodsID = v.ID
goods[i].Number = v.Number
}
order := models.CustormerOrder{
Operator: userName,
Remarks: data.Remarks,
Amount: data.Amount,
Name: data.Name,
Tel: data.Tel,
DeliveryAddress: data.DeliveryAddress,
DeliveryTime: data.DeliveryTime,
Deposit: data.Deposit,
}
orderInfo, err := models.CreateCustormerOrder(&order, goods)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, response.Error{Error: err.Error()})
return
}
c.JSON(http.StatusCreated, orderInfo)
}
// DeleteCustormerOrder 删除订单
func DeleteCustormerOrder(c *gin.Context) {
orderID := c.Param("id")
err := models.DeleteCustormerOrder(orderID)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, response.Error{Error: err.Error()})
return
}
c.JSON(http.StatusNoContent, gin.H{})
}
// ListCustormerOrders 列举所有订单
func ListCustormerOrders(c *gin.Context) {
orders, err := models.ListCustormerOrders()
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, response.Error{Error: err.Error()})
return
}
c.JSON(http.StatusOK, orders)
}
// ConfirmCustormerOrder 确认订单
func ConfirmCustormerOrder(c *gin.Context) {
orderID := c.Param("id")
var data struct {
Freight float32 `json:"freight" binding:"required"`
}
err := c.ShouldBindJSON(&data)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, response.Error{Error: err.Error()})
return
}
orderInfo, err := models.ConfirmCustormerOrder(orderID, data.Freight)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, response.Error{Error: err.Error()})
return
}
c.JSON(http.StatusCreated, orderInfo)
}