|
@@ -14,12 +14,68 @@ import (
|
|
// 产品管理
|
|
// 产品管理
|
|
func Product(r *GinRouter) {
|
|
func Product(r *GinRouter) {
|
|
// ??? supplier apis
|
|
// ??? supplier apis
|
|
|
|
+ r.POSTJWT("/product/create", ProductAdd)
|
|
|
|
+ r.POSTJWT("/product/update", ProductUpdate)
|
|
r.GETJWT("/product/list", ProductList)
|
|
r.GETJWT("/product/list", ProductList)
|
|
r.POSTJWT("/product/onOrOffShelves", OnOrOffShelves)
|
|
r.POSTJWT("/product/onOrOffShelves", OnOrOffShelves)
|
|
r.GETJWT("/product/detail/:id", ProductDetail)
|
|
r.GETJWT("/product/detail/:id", ProductDetail)
|
|
r.POSTJWT("/product/delete/:id", ProductDelete)
|
|
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) {
|
|
func OnOrOffShelves(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
// 根据id查询商品
|
|
// 根据id查询商品
|