animeic 2 жил өмнө
parent
commit
8f607c9c8c

+ 50 - 1
3dshow/api/order.go

@@ -13,6 +13,7 @@ import (
 // 产品管理
 func Order(r *GinRouter) {
 	r.POSTJWT("/order/create", OrderAdd)
+	r.POSTJWT("/order/createBatch", OrderAddBatch)
 	r.GETJWT("/order/list", OrderList)
 	r.POSTJWT("/order/update", OrderUpdate)
 	r.GETJWT("/order/detail/:id", OrderDetail)
@@ -20,7 +21,55 @@ func Order(r *GinRouter) {
 	r.GETJWT("/order/count", OrderCount)
 }
 
-// 新增订单
+// 批量新增订单
+func OrderAddBatch(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	var orders []*model.OrderAddReq
+	err := c.ShouldBindJSON(&orders)
+	if err != nil {
+		return nil, errors.New("参数错误!")
+	}
+	ctx := apictx.CreateRepoCtx()
+	_userId := apictx.User.ID
+	userId, _ := primitive.ObjectIDFromHex(_userId)
+
+	if len(orders) < 1 {
+		return nil, errors.New("购物车为空")
+	}
+
+	for _, v := range orders {
+		order := &model.Order{}
+		order.UserId = userId
+		order.SupplyId = v.Supply.Id
+		order.Address = v.Address
+		// order.Products
+		// 组装商品和设置默认状态
+		if len(v.Products) > 0 {
+			for _, p := range v.Products {
+				order.Products = append(order.Products, &model.OrderProduct{
+					Id:        p.ProductId,
+					SupplyId:  p.SupplyId,
+					Name:      p.Name,
+					Size:      p.Size,
+					Color:     p.Color,
+					Unit:      p.Unit,
+					Cover:     p.Cover,
+					ExpressNo: "",
+					Status:    -1,
+				})
+
+			}
+		}
+		order.DeliveryMethod = v.DeliveryMethod
+		order.Remark = v.Remark
+		order.Status = -1
+		order.CreateTime = time.Now()
+		repo.RepoAddDoc(ctx, repo.CollectionOrder, order)
+	}
+
+	return true, nil
+
+}
+
 func OrderAdd(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	var form model.Order
 	err := c.ShouldBindJSON(&form)

+ 8 - 0
3dshow/db/model/order.go

@@ -31,3 +31,11 @@ type OrderProduct struct {
 	ExpressNo string             `bson:"expressNo,omitempty" json:"expressNo"` // 快递单号,商家发货时需要输入
 	Status    int                `bson:"status,omitempty" json:"status"`       // -1:待发货 1:已发货
 }
+
+type OrderAddReq struct {
+	Products       []*ShopCart `bson:"products,omitempty" json:"products"`
+	Supply         *Supply     `bson:"supply,omitempty" json:"supply"`
+	Address        *Address    `bson:"address,omitempty" json:"address"`
+	DeliveryMethod string      `bson:"deliveryMethod,omitempty" json:"deliveryMethod"`
+	Remark         string      `bson:"remark,omitempty" json:"remark"`
+}

+ 11 - 14
3dshow/utils/uitls.go

@@ -1,19 +1,16 @@
 package utils
 
-import (
-	"math/rand"
-	"strings"
-	"time"
-)
+import "reflect"
 
-func RandString() string {
-	rand.Seed(time.Now().UnixNano())
-	chars := []rune("abcdefghijklmnopqrstuvwxyz" +
-		"0123456789")
-	length := 24
-	var b strings.Builder
-	for i := 0; i < length; i++ {
-		b.WriteRune(chars[rand.Intn(len(chars))])
+// 把value的值按字段分配给 binding
+func StructAssign(binding interface{}, value interface{}) {
+	bVal := reflect.ValueOf(binding).Elem()
+	vVal := reflect.ValueOf(value).Elem()
+	vTypeOfT := vVal.Type()
+	for i := 0; i < vVal.NumField(); i++ {
+		name := vTypeOfT.Field(i).Name
+		if ok := bVal.FieldByName(name).IsValid(); ok {
+			bVal.FieldByName(name).Set(reflect.ValueOf(vVal.Field(i).Interface()))
+		}
 	}
-	return b.String()
 }