Forráskód Böngészése

refactor supplier apis

animeic 2 éve
szülő
commit
0eb8c95fa7

+ 0 - 90
3dshow-supplier/api/address.go

@@ -1,90 +0,0 @@
-package api
-
-import (
-	"3dshow-supplier/db/model"
-	"3dshow-supplier/db/repo"
-	"errors"
-	"time"
-
-	"github.com/gin-gonic/gin"
-	"go.mongodb.org/mongo-driver/bson/primitive"
-)
-
-func Address(r *GinRouter) {
-
-	CreateCRUD(r, "/address", &CRUDOption{
-		Collection: repo.CollectionAddress,
-		NewModel: func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
-			entity := &model.Address{}
-			c.ShouldBindJSON(entity)
-			entity.CreateTime = time.Now()
-			_userId := apictx.User.ID
-			userId, err := primitive.ObjectIDFromHex(_userId)
-			if err != nil {
-				return nil, errors.New("非法用户")
-			}
-			if entity.Addr == "" {
-				return nil, errors.New("地址不能为空")
-			}
-			if entity.Contact == "" {
-				return nil, errors.New("联系人不能为空")
-			}
-			entity.UserId = userId
-			// 默认设置第一条数据为默认数据
-			first, _ := repo.RepoCountDoc(apictx.CreateRepoCtx(), repo.CollectionAddress, repo.Map{"userId": userId})
-			if first > 0 {
-				entity.Defualt = -1
-			} else {
-				entity.Defualt = 1
-			}
-			return entity, nil
-		},
-		OnUpdate: func(_ *gin.Context, apictx *ApiSession, entity interface{}) {
-			address := entity.(*model.Address)
-			_userId := apictx.User.ID
-			userId, err := primitive.ObjectIDFromHex(_userId)
-			if err != nil {
-				invalidId, _ := primitive.ObjectIDFromHex("6369f4b028c4bf8b14f47a6b")
-				userId = invalidId
-			}
-			// 更改默认地址的流程
-			if address.Defualt == 1 {
-				// 将已有的默认地址更改为非默认
-				opt := repo.PageSearchOptions{
-					CollectName: repo.CollectionAddress,
-					Query:       repo.Map{"userId": userId, "defualt": 1},
-					Project:     []string{"_id"},
-				}
-				defs := make([]*model.Address, 0)
-				repo.RepoDocsSearch(apictx.CreateRepoCtx(), &opt, &defs)
-				if len(defs) > 0 {
-					for _, v := range defs {
-						// 设置为非默认
-						repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionAddress, v.Id.Hex(), &model.Address{Defualt: -1})
-					}
-				}
-
-			}
-
-		},
-		EmtyModel: func(c *gin.Context, apictx *ApiSession) interface{} {
-			return &model.Address{}
-		},
-		JWT: true,
-		SearchFilter: func(_ *gin.Context, apictx *ApiSession, query map[string]interface{}) map[string]interface{} {
-			_userId := apictx.User.ID
-			userId, err := primitive.ObjectIDFromHex(_userId)
-			if err != nil {
-				// 6369f4b028c4bf8b14f47a6b
-				invalidId, _ := primitive.ObjectIDFromHex("6369f4b028c4bf8b14f47a6b")
-				query["userId"] = invalidId
-				return query
-			}
-
-			query["userId"] = userId
-			return query
-		},
-		SearchProject: []string{"province", "city", "area", "addr", "contact", "phone", "defualt", "createTime"},
-		DetailProject: []string{"province", "city", "area", "addr", "contact", "phone", "defualt", "createTime"},
-	})
-}

+ 0 - 97
3dshow-supplier/api/collect.go

@@ -1,97 +0,0 @@
-package api
-
-import (
-	"3dshow-supplier/db/model"
-	"3dshow-supplier/db/repo"
-	"errors"
-	"time"
-
-	"github.com/gin-gonic/gin"
-	"go.mongodb.org/mongo-driver/bson/primitive"
-)
-
-func Collect(r *GinRouter) {
-	CreateCRUD(r, "/collect", &CRUDOption{
-		Collection: repo.CollectionCollect,
-		NewModel: func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
-			entity := &model.Collect{}
-			err := c.ShouldBindJSON(entity)
-			if err != nil {
-				return nil, errors.New("参数错误")
-			}
-			if len(entity.SupplyId) < 12 {
-				return nil, errors.New("供应链id错误")
-			}
-			if len(entity.ProductId) < 12 {
-				return nil, errors.New("产品id错误")
-			}
-			entity.CreateTime = time.Now()
-			_userId := apictx.User.ID
-			userId, _ := primitive.ObjectIDFromHex(_userId)
-
-			// 同一用户对同一产品只能有一条收藏记录
-			// 联合唯一索引 db.collect.ensureIndex({userId:1,productId:1},{unique:true})
-			opt := &repo.DocSearchOptions{
-				CollectName: repo.CollectionCollect,
-				Query:       repo.Map{"userId": userId, "productId": entity.ProductId},
-			}
-			collect := &model.Collect{}
-			found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), opt, &collect)
-			if found {
-				return entity, errors.New("该物品已被收藏")
-			}
-
-			entity.UserId = userId
-			return entity, nil
-		},
-		EmtyModel: func(c *gin.Context, apictx *ApiSession) interface{} {
-			return &model.Collect{}
-		},
-		JWT: true,
-		SearchFilter: func(_ *gin.Context, apictx *ApiSession, query map[string]interface{}) map[string]interface{} {
-			_userId := apictx.User.ID
-			userId, err := primitive.ObjectIDFromHex(_userId)
-			if err != nil {
-
-				// 6369f4b028c4bf8b14f47a6b
-				invalidId, _ := primitive.ObjectIDFromHex("6369f4b028c4bf8b14f47a6b")
-				query["userId"] = invalidId
-				return query
-			}
-
-			query["userId"] = userId
-			return query
-		},
-		SearchPostProcess: func(page *repo.PageResult, _ *gin.Context, apictx *ApiSession, _ map[string]interface{}) (interface{}, error) {
-			// 查询组装数据
-			if len(page.List) > 0 {
-				for _, v := range page.List {
-					// 查询产品信息
-					productId := v["productId"].(primitive.ObjectID)
-					supplyId := v["supplyId"].(primitive.ObjectID)
-					product := model.Product{}
-					supply := model.Supply{}
-					repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
-						CollectName: repo.CollectionProduct,
-						Query:       repo.Map{"_id": productId},
-						Project:     []string{"name", "unit", "cover"},
-					}, &product)
-					// 供应商信息
-					repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
-						CollectName: repo.CollectionSupply,
-						Query:       repo.Map{"_id": supplyId},
-						Project:     []string{"name", "avatar", "contact"},
-					}, &supply)
-					v["productName"] = product.Name
-					v["productUnit"] = product.Unit
-					v["productCover"] = product.Cover
-					v["supplyName"] = supply.Name
-					v["supplyAvatar"] = supply.Avatar
-					v["supplyContact"] = supply.Contact
-
-				}
-			}
-			return page, nil
-		},
-	})
-}

+ 83 - 175
3dshow-supplier/api/order.go

@@ -4,8 +4,6 @@ import (
 	"3dshow-supplier/db/model"
 	"3dshow-supplier/db/repo"
 	"errors"
-	"fmt"
-	"time"
 
 	"github.com/gin-gonic/gin"
 	"go.mongodb.org/mongo-driver/bson/primitive"
@@ -13,224 +11,134 @@ import (
 
 // 产品管理
 func Order(r *GinRouter) {
-	r.POSTJWT("/order/createBatch", OrderAddBatch)
 	r.GETJWT("/order/list", OrderList)
-	r.POSTJWT("/order/update", OrderUpdate)
+	r.POST("/order/delivery", OrderDelivery)
 	r.GETJWT("/order/detail/:id", OrderDetail)
-	r.POSTJWT("/order/delete/:id", OrderDelete)
-	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 {
-		fmt.Println(err)
-		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,
-				})
-				// 加入购物车后 删除购物车对应商品
-				repo.RepoDeleteDoc(ctx, repo.CollectionShopCart, p.Id.Hex())
-
-			}
-		}
-		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 OrderList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
-	// customer
-	// supplier
 	page, size, query := UtilQueryPageSize(c)
 	_userId := apictx.User.ID
 	userId, _ := primitive.ObjectIDFromHex(_userId)
-
-	// if apictx.User.Role == "customer"{
-	query["userId"] = userId
-	// }
+	// 供应商的订单列表
+	query["supplyId"] = userId
 	result, err := repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
 		CollectName: repo.CollectionOrder,
 		Page:        page,
 		Size:        size,
 		Query:       query,
-		// Project: []string{},
-		Sort: repo.Map{"createTime": -1},
+		Project:     []string{"createTime", "userId", "supplyId", "products"},
+		Sort:        repo.Map{"createTime": -1},
 	})
 
 	if len(result.List) > 0 {
 		for _, v := range result.List {
-			supplyId := v["supplyId"].(primitive.ObjectID)
-			supply := model.Supply{}
+			orderCount := 0
+			notShipCount := 0
+
+			// 客户信息
+			customUserId := v["userId"].(primitive.ObjectID)
+			customerUser := model.UserSmaple{}
 			repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
-				CollectName: repo.CollectionSupply,
-				Query:       repo.Map{"_id": supplyId},
-			}, &supply)
-			v["supplyName"] = supply.Name
+				Db:          "customer-user",
+				CollectName: "users",
+				Query:       repo.Map{"_id": customUserId},
+			}, &customerUser)
+			v["customer"] = customerUser
+
+			// 统计下单数和未发货数
+			orderCount = len(v["products"].([]*model.OrderProduct))
+			if orderCount > 0 {
+				for _, p := range v["products"].([]*model.OrderProduct) {
+					if p.Status == -1 {
+						notShipCount++
+					}
+				}
+			}
+			v["orderCount"] = orderCount
+			v["notShipCount"] = notShipCount
 		}
 	}
 
 	return result, err
 }
 
-// 个人页面数量展示
-func OrderCount(_ *gin.Context, apictx *ApiSession) (interface{}, error) {
-	_userId := apictx.User.ID
-	userId, err := primitive.ObjectIDFromHex(_userId)
-	if err != nil {
-		return nil, errors.New("非法用户")
-	}
-	statusArray := []int{-1, 1, 2, 3}
-	resMap := make(map[int]int64)
-	// var ret []int64
-	ctx := apictx.CreateRepoCtx()
-	for _, v := range statusArray {
-		query := repo.Map{"userId": userId, "status": v}
-		res, _ := repo.RepoCountDoc(ctx, repo.CollectionOrder, query)
-		// ret = append(ret, res)
-		resMap[v] = res
-	}
-	return resMap, nil
+// 发货
+type OrderDeliveryReq struct {
+	Id        primitive.ObjectID `json:"id"`
+	ProductId primitive.ObjectID `json:"productId"`
+	ExpressNo string             `json:"expressNo"`
 }
 
-// 更新订单 发货
-func OrderUpdate(c *gin.Context, apictx *ApiSession) (interface{}, error) {
-	var form model.Order
-	err := c.ShouldBindJSON(&form)
+func OrderDelivery(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	var form OrderDeliveryReq
+	err := c.ShouldBind(&form)
 	if err != nil {
 		return nil, errors.New("参数错误")
 	}
-	if form.Id.Hex() == "" {
-		return nil, errors.New("更新的产品id不能为空")
+	// 验证订单是不是自己的
+	_userId := apictx.User.ID
+	userId, _ := primitive.ObjectIDFromHex(_userId)
+	order := model.Order{}
+	found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
+		CollectName: repo.CollectionOrder,
+		Query:       repo.Map{"_id": form.Id},
+	}, &order)
+	if !found || err != nil {
+		return nil, errors.New("该订单未找到")
 	}
-	form.UpdateTime = time.Now()
-
-	result, err := repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionOrder, form.Id.Hex(), &form)
+	if userId != order.SupplyId {
+		return nil, errors.New("非法操作")
+	}
+	if len(order.Products) < 1 {
+		return nil, errors.New("无效订单")
+	}
+	// 发货
+	result, err := repo.RepoDocArrayOneUpdate(apictx.CreateRepoCtx(), &repo.ArrayOneUpdateOption{
+		CollectName: repo.CollectionOrder,
+		Id:          form.Id.Hex(),
+		Query:       repo.Map{"products.id": form.ProductId},
+		Set:         repo.Map{"status": 1, "expressNo": form.ExpressNo},
+	})
 	if err != nil {
-		return nil, err
+		return nil, errors.New("发货商品异常")
 	}
 
-	// 更新订单状态
+	// 更新订单状态 已发货
 	if result.ModifiedCount > 0 {
-		// 查询订单
-		curOder := model.Order{}
-		found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
-			CollectName: repo.CollectionOrder,
-			Query:       repo.Map{"_id": form.Id},
-			Project:     []string{"products"},
-		}, &curOder)
-
-		if found {
-			if len(curOder.Products) > 0 {
-				statusMap := make(map[int]struct{})
-				for _, v := range curOder.Products {
-					statusMap[v.Status] = struct{}{}
-				}
-				// {"-1",1,2}
-				// {-1,1}
-				// {-1,2}
-				// {1,2}
-				// {-1}
-				// {1}
-				// {3}
-				status := curOder.Status
-				if len(statusMap) == 1 {
-					if _, ok := statusMap[-1]; ok {
-						status = -1
-					}
-					if _, ok := statusMap[1]; ok {
-						status = 1
-					}
-					if _, ok := statusMap[2]; ok {
-						status = 2
-					}
-				}
-				if len(statusMap) == 2 {
-					if _, ok := statusMap[-1]; ok {
-						status = -1
-					} else if _, ok := statusMap[1]; ok {
-						status = 1
-					}
-				}
-				if len(statusMap) == 3 {
-					status = -1
-				}
-				var statusOrder model.Order
-				statusOrder.Status = status
-				repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionOrder, form.Id.Hex(), &statusOrder)
-
-			}
-		}
-
+		repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionOrder, form.Id.Hex(), &model.Order{Status: 1})
 	}
-	return result, err
 
+	return result, nil
 }
 
 // 订单信息
 func OrderDetail(c *gin.Context, apictx *ApiSession) (interface{}, error) {
-	orderId := c.Param("id")
-	id, err := primitive.ObjectIDFromHex(orderId)
+	_id := c.Param("id")
+	if len(_id) < 1 {
+		return nil, errors.New("id不能为空")
+	}
+	orderId, err := primitive.ObjectIDFromHex(_id)
 	if err != nil {
 		return nil, errors.New("非法id")
 	}
-	var order model.Order
-	option := &repo.DocSearchOptions{
-		CollectName: repo.CollectionOrder,
-		Query:       repo.Map{"_id": id},
-	}
 
-	found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &order)
-	if !found || err != nil {
+	ok, result := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
+		CollectName: repo.CollectionOrder,
+		Query:       repo.Map{"_id": orderId},
+	})
+	if !ok {
 		return nil, errors.New("数据未找到")
 	}
-
-	return order, nil
-}
-
-// 删除订单
-func OrderDelete(c *gin.Context, apictx *ApiSession) (interface{}, error) {
-	orderId := c.Param("id")
-	_, err := primitive.ObjectIDFromHex(orderId)
-	if err != nil {
-		return nil, errors.New("非法id")
-	}
-
-	return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionOrder, orderId)
+	// 客户信息
+	customer := model.UserSmaple{}
+	repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
+		Db:          "customer-user",
+		CollectName: "users",
+		Query:       repo.Map{"_id": result["userId"].(primitive.ObjectID)},
+	}, &customer)
+	result["customer"] = customer
+
+	return result, nil
 }

+ 72 - 116
3dshow-supplier/api/product.go

@@ -5,7 +5,6 @@ import (
 	"3dshow-supplier/db/repo"
 	"3dshow-supplier/log"
 	"errors"
-	"fmt"
 	"time"
 
 	"github.com/gin-gonic/gin"
@@ -14,42 +13,54 @@ import (
 
 // 产品管理
 func Product(r *GinRouter) {
-	r.POSTJWT("/product/create", ProductAdd)
+	// ??? supplier apis
 	r.GETJWT("/product/list", ProductList)
-	r.POSTJWT("/product/update", ProductUpdate)
+	r.POSTJWT("/product/onOrOffShelves", OnOrOffShelves)
 	r.GETJWT("/product/detail/:id", ProductDetail)
-	r.GET("/product/share/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 {
-		fmt.Println(err)
-		return nil, errors.New("参数错误!")
-	}
-	ctx := apictx.CreateRepoCtx()
-	if form.SupplyId.Hex() == "" {
-		return nil, errors.New("供应链id不能为空")
+// 上下架商品
+func OnOrOffShelves(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	// 根据id查询商品
+	_id := c.Param("id")
+	if len(_id) < 1 {
+		return nil, errors.New("商品id不能为空")
 	}
-	if form.Name == "" {
-		return nil, errors.New("产品名不能为空")
+
+	productId, err := primitive.ObjectIDFromHex(_id)
+	if err != nil {
+		return nil, errors.New("非法id")
 	}
-	form.CreateTime = time.Now()
-	// 状态默认为下架
-	if form.Status == 0 {
-		form.Status = -1
+	product := model.Product{}
+	found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
+		CollectName: repo.CollectionProduct,
+		Query:       repo.Map{"_id": productId},
+	}, &product)
+	if !found || err != nil {
+		return nil, errors.New("该商品不存在")
 	}
 
-	result, err := repo.RepoAddDoc(ctx, repo.CollectionProduct, &form)
-	return result, err
-
+	_userId := apictx.User.ID
+	userId, _ := primitive.ObjectIDFromHex(_userId)
+
+	// 不是自己的商品
+	if product.SupplyId != userId {
+		return nil, errors.New("非法操作")
+	}
+	// 更新状态
+	if product.Status == -1 {
+		product.Status = 1
+		product.OnsaleTime = time.Now()
+		product.UpdateTime = time.Now()
+	} else if product.Status == 1 {
+		product.Status = -1
+		product.UpdateTime = time.Now()
+	}
+	return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionProduct, _id, &product)
 }
 
 // 产品列表
-// ??? 上架才能展示 前端传 status:1
 func ProductList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	page, size, query := UtilQueryPageSize(c)
 	// asc:升序1  desc:降序-1
@@ -59,22 +70,13 @@ func ProductList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 		if query["sort"].(string) == "asc" {
 			onsaleTimeSort = repo.Map{"onsaleTime": 1}
 		}
-		// if sort.(string) == "desc"{
-		// 	onsaleTimeSort = repo.Map{"onsaleTime": -1}
-		// }
-
-	}
-	delete(query, "sort")
-	// 查询数据
-	if _, ok := query["supplyId"]; !ok {
-		return nil, errors.New("供应链id不能为空")
+		delete(query, "sort")
 	}
-	supplyId, err := primitive.ObjectIDFromHex(query["supplyId"].(string))
-	if err != nil {
-		return nil, errors.New("供应链id错误")
-	}
-	query["supplyId"] = supplyId
 
+	// 供应商 查询自己的商品
+	_userId := apictx.User.ID
+	userId, _ := primitive.ObjectIDFromHex(_userId)
+	query["supplyId"] = userId
 	option := &repo.PageSearchOptions{
 		CollectName: repo.CollectionProduct,
 		Page:        page,
@@ -83,73 +85,53 @@ func ProductList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 		// Project:     []string{},
 		Sort: onsaleTimeSort,
 	}
-	pageResult, err := repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
-	if err != nil {
-		return nil, err
+	return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
+}
+
+// 删除产品
+func ProductDelete(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	_id := c.Param("id")
+	if len(_id) < 1 {
+		return nil, errors.New("id不能为空")
 	}
-	// 查询收藏状态
-	var collects []*model.Collect
-	_userId := apictx.User.ID
-	userId, err := primitive.ObjectIDFromHex(_userId)
+	productId, err := primitive.ObjectIDFromHex(_id)
 	if err != nil {
-		return nil, errors.New("非法用户")
-	}
-	option1 := &repo.PageSearchOptions{
-		CollectName: repo.CollectionCollect,
-		Query:       repo.Map{"userId": userId, "supplyId": supplyId},
-		Project:     []string{"_id", "productId"},
+		return nil, errors.New("非法id")
 	}
-	err1 := repo.RepoDocsSearch(apictx.CreateRepoCtx(), option1, &collects)
-
-	if len(pageResult.List) > 0 {
-		for _, v := range pageResult.List {
-			v["isCollect"] = false
-			v["collectId"] = ""
-			if len(collects) > 0 && err1 == nil {
-				for _, col := range collects {
-					if v["_id"].(primitive.ObjectID) == col.ProductId { // productId唯一
-						v["isCollect"] = true
-						v["collectId"] = col.Id.Hex()
-						break
-					}
-				}
-			}
-		}
+	product := model.Product{}
+	found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
+		CollectName: repo.CollectionProduct,
+		Query:       repo.Map{"_id": productId},
+	}, &product)
+	if !found || err != nil {
+		return nil, errors.New("该商品不存在")
 	}
-	return pageResult, 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不能为空")
+	_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)
-}
 
-// 产品信息
-type ProudctDetailRes struct {
-	model.Product
-	IsCollect bool   `json:"isCollect"`
-	CollectId string `json:"collectId"`
+	return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionProduct, _id)
 }
 
+// 产品信息
 func ProductDetail(c *gin.Context, apictx *ApiSession) (interface{}, error) {
-	productId := c.Param("id")
-	id, err := primitive.ObjectIDFromHex(productId)
+	_id := c.Param("id")
+	if len(_id) < 1 {
+		return nil, errors.New("id不能为空")
+	}
+	productId, err := primitive.ObjectIDFromHex(_id)
 	if err != nil {
 		return nil, errors.New("非法id")
 	}
 	var product model.Product
 	option := &repo.DocSearchOptions{
 		CollectName: repo.CollectionProduct,
-		Query:       repo.Map{"_id": id},
+		Query:       repo.Map{"_id": productId},
 	}
 
 	found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &product)
@@ -157,32 +139,6 @@ func ProductDetail(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 		log.Info(err)
 		return nil, errors.New("数据未找到")
 	}
-	// 是否收藏
-	if apictx.User != nil {
-		_userId := apictx.User.ID
-		userId, err := primitive.ObjectIDFromHex(_userId)
-		if err != nil {
-			return nil, errors.New("非法用户")
-		}
-		var collect model.Collect
-		found, _ = repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
-			CollectName: repo.CollectionCollect,
-			Query:       repo.Map{"userId": userId, "productId": id},
-		}, &collect)
-		return ProudctDetailRes{Product: product, IsCollect: found, CollectId: collect.Id.Hex()}, nil
-	}
 
 	return product, nil
 }
-
-// 删除产品
-// ???权限控制 admin 和 供应商本人
-func ProductDelete(c *gin.Context, apictx *ApiSession) (interface{}, error) {
-	productId := c.Param("id")
-	_, err := primitive.ObjectIDFromHex(productId)
-	if err != nil {
-		return nil, errors.New("非法id")
-	}
-
-	return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionProduct, productId)
-}

+ 0 - 13
3dshow-supplier/api/router.go

@@ -13,24 +13,11 @@ func RegRouters(svc *Service) {
 	_3dshow.group.Use(Logger())
 	_3dshow.GET("/printr", Printr)
 
-	// 供应链管理
-	Supply(_3dshow)
-
 	// 产品管理
 	Product(_3dshow)
-
-	// 地址管理
-	Address(_3dshow)
-
-	// 购物车
-	ShopCart(_3dshow)
-
 	// 订单管理
 	Order(_3dshow)
 
-	// 收藏
-	Collect(_3dshow)
-
 }
 
 func Logger() gin.HandlerFunc {

+ 0 - 140
3dshow-supplier/api/shopCart.go

@@ -1,140 +0,0 @@
-package api
-
-import (
-	"3dshow-supplier/db/model"
-	"3dshow-supplier/db/repo"
-	"context"
-	"errors"
-	"fmt"
-	"time"
-
-	"github.com/gin-gonic/gin"
-	"go.mongodb.org/mongo-driver/bson"
-	"go.mongodb.org/mongo-driver/bson/primitive"
-)
-
-func ShopCart(r *GinRouter) {
-	r.POSTJWT("/shopCart/deleteBatch", ShopCartDeleteBatch)
-	r.GETJWT("/shopCart/groupList", shopCartGroupList)
-
-	CreateCRUD(r, "/shopCart", &CRUDOption{
-		Collection: repo.CollectionShopCart,
-		NewModel: func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
-			entity := &model.ShopCart{}
-			err := c.ShouldBindJSON(entity)
-			if err != nil {
-				return nil, errors.New("参数错误")
-			}
-			if len(entity.SupplyId) < 12 {
-				return nil, errors.New("供应链id错误")
-			}
-			if len(entity.ProductId) < 12 {
-				return nil, errors.New("商品id错误")
-			}
-			entity.CreateTime = time.Now()
-			_userId := apictx.User.ID
-			userId, _ := primitive.ObjectIDFromHex(_userId)
-			entity.UserId = userId
-
-			return entity, nil
-		},
-		EmtyModel: func(c *gin.Context, apictx *ApiSession) interface{} {
-			return &model.ShopCart{}
-		},
-		JWT: true,
-		SearchFilter: func(_ *gin.Context, apictx *ApiSession, query map[string]interface{}) map[string]interface{} {
-			_userId := apictx.User.ID
-			userId, _ := primitive.ObjectIDFromHex(_userId)
-			query["userId"] = userId
-			return query
-		},
-		OnUpdate: func(_ *gin.Context, _ *ApiSession, entity interface{}) {
-			ety := entity.(*model.ShopCart)
-			ety.UpdateTime = time.Now()
-		},
-		SearchPostProcess: func(page *repo.PageResult, _ *gin.Context, apictx *ApiSession, _ map[string]interface{}) (interface{}, error) {
-			// 查询组装数据
-			if len(page.List) > 0 {
-				for _, v := range page.List {
-					// 查询产品信息
-					supplyId := v["supplyId"].(primitive.ObjectID)
-					supply := model.Supply{}
-					// 供应商信息
-					repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
-						CollectName: repo.CollectionSupply,
-						Query:       repo.Map{"_id": supplyId},
-						Project:     []string{"name", "avatar", "contact"},
-					}, &supply)
-					v["supplyName"] = supply.Name
-					v["supplyAvatar"] = supply.Avatar
-					v["supplyContact"] = supply.Contact
-
-				}
-			}
-			return page, nil
-		},
-	})
-}
-
-// 批量删除
-type ShopCartDeleteBatchReq struct {
-	Ids []string `json:"ids"`
-}
-
-func ShopCartDeleteBatch(c *gin.Context, apictx *ApiSession) (interface{}, error) {
-	var form ShopCartDeleteBatchReq
-	err := c.ShouldBindJSON(&form)
-	if err != nil {
-		fmt.Println(form)
-		return nil, errors.New("参数错误")
-	}
-	if len(form.Ids) > 0 {
-		// 批量删除
-		for _, v := range form.Ids {
-			// id 错误
-			_, err := primitive.ObjectIDFromHex(v)
-			if err != nil {
-				continue
-			}
-			// 删除
-			repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionShopCart, v)
-
-		}
-	}
-
-	return true, nil
-}
-
-// 供应链分组列表 数据库分组
-func shopCartGroupList(_ *gin.Context, apictx *ApiSession) (interface{}, error) {
-	userId, _ := primitive.ObjectIDFromHex(apictx.User.ID)
-	mongo := apictx.Svc.Mongo
-	cond := []interface{}{
-		bson.M{"$match": bson.M{"userId": userId}},
-		bson.M{"$group": bson.M{"_id": "$supplyId", "products": bson.M{"$push": "$$ROOT"}}},
-		bson.M{"$project": bson.M{"_id": 0, "supplyId": "$_id", "products": 1}},
-	}
-	tmp, err := mongo.GetCollection(repo.CollectionShopCart).Aggregate(context.Background(), cond)
-	if err != nil {
-		return nil, err
-	}
-	defer tmp.Close(context.Background())
-	list := make([]map[string]interface{}, 0)
-	tmp.All(context.Background(), &list)
-	if len(list) > 0 {
-		for _, item := range list {
-			supply := model.Supply{}
-			supplyId := item["supplyId"].(primitive.ObjectID)
-			// 供应商信息
-			repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
-				CollectName: repo.CollectionSupply,
-				Query:       repo.Map{"_id": supplyId},
-			}, &supply)
-			item["supply"] = supply
-			delete(item, "supplyId")
-
-		}
-	}
-
-	return list, nil
-}

+ 0 - 28
3dshow-supplier/api/supply.go

@@ -1,28 +0,0 @@
-package api
-
-import (
-	"3dshow-supplier/db/model"
-	"3dshow-supplier/db/repo"
-	"time"
-
-	"github.com/gin-gonic/gin"
-)
-
-func Supply(r *GinRouter) {
-
-	CreateCRUD(r, "/supply", &CRUDOption{
-		Collection: repo.CollectionSupply,
-		NewModel: func(c *gin.Context, _ *ApiSession) (interface{}, error) {
-			entity := &model.Supply{}
-			c.ShouldBindJSON(entity)
-			entity.CreateTime = time.Now()
-			return entity, nil
-		},
-		EmtyModel: func(c *gin.Context, apictx *ApiSession) interface{} {
-			return &model.Supply{}
-		},
-		JWT:           true,
-		SearchProject: []string{"name", "avatar", "contact", "createTime"},
-		DetailProject: []string{"name", "avatar", "contact", "createTime"},
-	})
-}

+ 2 - 2
3dshow-supplier/app.yaml

@@ -3,8 +3,8 @@ name: supplier
 version: 1.0.0
 jwt: 
   timeoutHour: 24
-  realm: 3dshow-supply-user
-  key: 3dshow-supply-user secret
+  realm: spu3d
+  key: spu3d secret
   
 log:
   fileName: 3dshow-supplier.log

+ 0 - 17
3dshow-supplier/db/model/collect.go

@@ -1,17 +0,0 @@
-package model
-
-import (
-	"time"
-
-	"go.mongodb.org/mongo-driver/bson/primitive"
-)
-
-// 收藏
-type Collect struct {
-	Id         primitive.ObjectID `bson:"_id,omitempty" json:"_id"`
-	UserId     primitive.ObjectID `bson:"userId,omitempty" json:"userId"`
-	SupplyId   primitive.ObjectID `bson:"supplyId,omitempty" json:"supplyId"`
-	ProductId  primitive.ObjectID `bson:"productId,omitempty" json:"productId"`
-	CreateTime time.Time          `bson:"createTime,omitempty" json:"createTime"`
-	UpdateTime time.Time          `bson:"updateTime,omitempty" json:"updateTime"`
-}

+ 5 - 7
3dshow-supplier/db/model/order.go

@@ -15,7 +15,7 @@ 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:已完成
+	Status         int                `bson:"status,omitempty" json:"status"` // -1:待发货 1:已发货 2:已完成 3:取消订单
 	CreateTime     time.Time          `bson:"createTime,omitempty" json:"createTime"`
 	UpdateTime     time.Time          `bson:"updateTime,omitempty" json:"updateTime"`
 }
@@ -32,10 +32,8 @@ type OrderProduct struct {
 	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"`
+type OrderCustomerUser struct {
+	Id     primitive.ObjectID `bson:"_id,omitempty" json:"_id"`       // id
+	Avatar primitive.ObjectID `bson:"avatar,omitempty" json:"avatar"` // 头像
+	Name   string             `bson:"name,omitempty" json:"name"`     // 客户名字
 }

+ 0 - 22
3dshow-supplier/db/model/shopCart.go

@@ -1,22 +0,0 @@
-package model
-
-import (
-	"time"
-
-	"go.mongodb.org/mongo-driver/bson/primitive"
-)
-
-// 购物车
-type ShopCart struct {
-	Id         primitive.ObjectID `bson:"_id,omitempty" json:"_id"` // id
-	UserId     primitive.ObjectID `bson:"userId,omitempty" json:"userId"`
-	SupplyId   primitive.ObjectID `bson:"supplyId,omitempty" json:"supplyId"`   // 供应链id
-	ProductId  primitive.ObjectID `bson:"productId,omitempty" json:"productId"` // 购物车商品id
-	Name       string             `bson:"name,omitempty" json:"name"`
-	Size       int                `bson:"size,omitempty" json:"size"`   // 选定的尺寸
-	Color      string             `bson:"color,omitempty" json:"color"` // 选定的颜色
-	Unit       string             `bson:"unit,omitempty" json:"unit"`   // 型号
-	Cover      string             `bson:"cover,omitempty" json:"cover"` // 封面图
-	CreateTime time.Time          `bson:"createTime,omitempty" json:"createTime"`
-	UpdateTime time.Time          `bson:"updateTime,omitempty" json:"updateTime"`
-}

+ 3 - 3
3dshow-supplier/db/model/supply.go → 3dshow-supplier/db/model/user.go

@@ -6,12 +6,12 @@ import (
 	"go.mongodb.org/mongo-driver/bson/primitive"
 )
 
-// 供应链
-type Supply struct {
+// user简单信息
+type UserSmaple struct {
 	Id         primitive.ObjectID `bson:"_id,omitempty" json:"_id"`
 	Name       string             `bson:"name,omitempty" json:"name"`
 	Avatar     string             `bson:"avatar,omitempty" json:"avatar"`
-	Contact    string             `bson:"contact,omitempty" json:"contact"`
+	Email      string             `bson:"email,omitempty" json:"email"`
 	CreateTime time.Time          `bson:"createTime,omitempty" json:"createTime"`
 	UpdateTime time.Time          `bson:"updateTime,omitempty" json:"updateTime"`
 }