Преглед на файлове

add product add and update

animeic преди 2 години
родител
ревизия
1e0ff6ab36
променени са 3 файла, в които са добавени 76 реда и са изтрити 4 реда
  1. 18 3
      3dshow-supplier/api/order.go
  2. 56 0
      3dshow-supplier/api/product.go
  3. 2 1
      3dshow-supplier/db/model/order.go

+ 18 - 3
3dshow-supplier/api/order.go

@@ -12,7 +12,7 @@ import (
 // 产品管理
 func Order(r *GinRouter) {
 	r.GETJWT("/order/list", OrderList)
-	r.POST("/order/delivery", OrderDelivery)
+	r.POSTJWT("/order/delivery", OrderDelivery)
 	r.GETJWT("/order/detail/:id", OrderDetail)
 }
 
@@ -105,9 +105,24 @@ func OrderDelivery(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 		return nil, errors.New("发货商品异常")
 	}
 
-	// 更新订单状态 已发货
 	if result.ModifiedCount > 0 {
-		repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionOrder, form.Id.Hex(), &model.Order{Status: 1})
+		// 查询其中产品 判断订单状态
+		// 该订单产品有多少种状态 在此时订单只有-1,1两种状态
+		statusMap := make(map[int]struct{})
+		for _, v := range order.Products {
+			statusMap[v.Status] = struct{}{}
+		}
+		status := order.Status
+		// 只有一种状态时为已发货
+		if len(statusMap) == 1 {
+			status = 1
+		}
+		// 有两种状态时为待发货
+		if len(statusMap) == 2 {
+			status = -1
+		}
+		// 发货成功该订单不可取消
+		repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionOrder, form.Id.Hex(), &model.Order{Status: status, IsCancel: -1})
 	}
 
 	return result, nil

+ 56 - 0
3dshow-supplier/api/product.go

@@ -14,12 +14,68 @@ import (
 // 产品管理
 func Product(r *GinRouter) {
 	// ??? supplier apis
+	r.POSTJWT("/product/create", ProductAdd)
+	r.POSTJWT("/product/update", ProductUpdate)
 	r.GETJWT("/product/list", ProductList)
 	r.POSTJWT("/product/onOrOffShelves", OnOrOffShelves)
 	r.GETJWT("/product/detail/:id", ProductDetail)
 	r.POSTJWT("/product/delete/:id", ProductDelete)
 }
 
+// 新增产品
+func ProductAdd(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	var form model.Product
+	err := c.ShouldBindJSON(&form)
+	if err != nil {
+		return nil, errors.New("参数错误!")
+	}
+	ctx := apictx.CreateRepoCtx()
+	if form.SupplyId.Hex() == "" {
+		return nil, errors.New("供应链id不能为空")
+	}
+	if form.Name == "" {
+		return nil, errors.New("产品名不能为空")
+	}
+	form.CreateTime = time.Now()
+	// 状态默认为下架
+	if form.Status == 0 {
+		form.Status = -1
+	}
+
+	result, err := repo.RepoAddDoc(ctx, repo.CollectionProduct, &form)
+	return result, err
+
+}
+
+// 更新产品/编辑、下架
+func ProductUpdate(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	var form model.Product
+	err := c.ShouldBindJSON(&form)
+	if err != nil {
+		return nil, errors.New("参数错误")
+	}
+	if form.Id.Hex() == "" {
+		return nil, errors.New("产品id不能为空")
+	}
+	product := model.Product{}
+	found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
+		CollectName: repo.CollectionProduct,
+		Query:       repo.Map{"_id": form.Id},
+	}, &product)
+	if !found || err != nil {
+		return nil, errors.New("该商品不存在")
+	}
+
+	_userId := apictx.User.ID
+	userId, _ := primitive.ObjectIDFromHex(_userId)
+	// 不是自己的商品
+	if product.SupplyId != userId {
+		return nil, errors.New("非法操作")
+	}
+	form.UpdateTime = time.Now()
+	return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionProduct, form.Id.Hex(), &form)
+}
+
 // 上下架商品
 func OnOrOffShelves(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	// 根据id查询商品

+ 2 - 1
3dshow-supplier/db/model/order.go

@@ -15,7 +15,8 @@ type Order struct {
 	Products       []*OrderProduct    `bson:"products,omitempty" json:"products"`
 	DeliveryMethod string             `bson:"deliveryMethod,omitempty" json:"deliveryMethod"`
 	Remark         string             `bson:"remark,omitempty" json:"remark"`
-	Status         int                `bson:"status,omitempty" json:"status"` // -1:待发货 1:已发货 2:已完成 3:取消订单
+	Status         int                `bson:"status,omitempty" json:"status"`     // -1:待发货 1:已发货 2:已完成 3:取消订单
+	IsCancel       int                `bson:"isCancel,omitempty" json:"isCancel"` // -1:不可取消 1:可取消
 	CreateTime     time.Time          `bson:"createTime,omitempty" json:"createTime"`
 	UpdateTime     time.Time          `bson:"updateTime,omitempty" json:"updateTime"`
 }