animeic 2 years ago
parent
commit
9a2dcaf32c

+ 2 - 0
3dshow/3dshow.log

@@ -0,0 +1,2 @@
+{"level":"info","timestamp":"2022-11-09 12:15:34","message":"[<nil>]","service_name":"3dshow"}
+{"level":"info","timestamp":"2022-11-09 14:28:06","message":"[<nil>]","service_name":"3dshow"}

BIN
3dshow/__debug_bin


+ 4 - 1
3dshow/api/address.go

@@ -26,11 +26,14 @@ func Address(r *GinRouter) {
 			entity.UserId = userId
 			first, _ := repo.RepoCountDoc(apictx.CreateRepoCtx(), repo.CollectionAddress, repo.Map{"userId": userId})
 			if first > 0 {
-				entity.Defualt = 0
+				entity.Defualt = 2
 			} else {
 				entity.Defualt = 1
 			}
 			return entity, nil
+		},
+		OnUpdate: func(c *gin.Context, apictx *ApiSession, entity interface{}) {
+
 		},
 		EmtyModel: func(c *gin.Context, apictx *ApiSession) interface{} {
 			return &model.Address{}

+ 78 - 26
3dshow/api/order.go

@@ -17,11 +17,12 @@ func Order(r *GinRouter) {
 	r.POSTJWT("/order/update", OrderUpdate)
 	r.GETJWT("/order/detail/:id", OrderDetail)
 	r.POSTJWT("/order/delete/:id", OrderDelete)
+	r.GETJWT("/order/count", OrderCount)
 }
 
 // 新增订单
 func OrderAdd(c *gin.Context, apictx *ApiSession) (interface{}, error) {
-	var form model.OrederAddReq
+	var form model.Order
 	err := c.ShouldBindJSON(&form)
 	if err != nil {
 		return nil, errors.New("参数错误!")
@@ -32,26 +33,15 @@ func OrderAdd(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	if err != nil {
 		return nil, errors.New("非法用户")
 	}
-	// 批量添加商品订单
-	if len(form.Products) > 0 {
-		for _, v := range form.Products {
-			repo.RepoAddDoc(ctx, repo.CollectionOrder, &model.Order{
-				UserId:         userId,
-				SupplyId:       v.SupplyId,
-				Address:        form.Address,
-				Product:        v,
-				DeliveryMethod: form.DeliveryMethod,
-				Remark:         form.Remark,
-				ExpressNo:      form.ExpressNo,
-				Status:         0,
-				CreateTime:     time.Now(),
-				UpdateTime:     time.Now(),
-			})
-
-		}
+	for _, v := range form.Products {
+		v.Status = -1
 	}
+	form.UserId = userId
+	form.Status = -1
+	form.CreateTime = time.Now()
+	form.UpdateTime = time.Now()
+	return repo.RepoAddDoc(ctx, repo.CollectionOrder, &form)
 
-	return true, err
 }
 
 // 订单列表
@@ -73,7 +63,7 @@ func OrderList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 		Size:        size,
 		Query:       query,
 		// Project: []string{},
-		Sort: repo.Map{"createTime": -1},
+		// Sort: repo.Map{"createTime": -1},
 	})
 }
 
@@ -84,18 +74,20 @@ func OrderCount(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	if err != nil {
 		return nil, errors.New("非法用户")
 	}
-	statusArray := []int{0, 1, 2, 3}
-	var ret []int64
+	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)
+		// ret = append(ret, res)
+		resMap[v] = res
 	}
-	return ret, nil
+	return resMap, nil
 }
 
-// 更新订单
+// 更新订单 发货
 func OrderUpdate(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	var form model.Order
 	err := c.ShouldBindJSON(&form)
@@ -106,7 +98,67 @@ func OrderUpdate(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 		return nil, errors.New("更新的产品id不能为空")
 	}
 	form.UpdateTime = time.Now()
-	return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionOrder, form.Id.Hex(), &form)
+
+	result, err := repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionOrder, form.Id.Hex(), &form)
+	if err != nil {
+		return nil, err
+	}
+
+	// 更新订单状态
+	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)
+
+			}
+		}
+
+	}
+	return result, err
+
 }
 
 // 订单信息

+ 45 - 13
3dshow/api/product.go

@@ -5,7 +5,7 @@ import (
 	"3dshow/db/repo"
 	"3dshow/log"
 	"errors"
-	"strconv"
+	"fmt"
 	"time"
 
 	"github.com/gin-gonic/gin"
@@ -17,7 +17,8 @@ func Product(r *GinRouter) {
 	r.POSTJWT("/product/create", ProductAdd)
 	r.GETJWT("/product/list", ProductList)
 	r.POSTJWT("/product/update", ProductUpdate)
-	r.GET("/product/detail/:id", ProductDetail)
+	r.GETJWT("/product/detail/:id", ProductDetail)
+	r.GET("/product/share/detail/:id", ProductDetail)
 	r.POSTJWT("/product/delete/:id", ProductDelete)
 }
 
@@ -26,6 +27,7 @@ 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()
@@ -36,6 +38,10 @@ func ProductAdd(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 		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
@@ -43,16 +49,22 @@ func ProductAdd(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 }
 
 // 产品列表
+// ??? 上架才能展示 前端传 status:1
 func ProductList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	page, size, query := UtilQueryPageSize(c)
-	_sort := c.Query("sort")
-	// 1:升序  -1:降序
-	sort, _ := strconv.Atoi(_sort)
+	// asc:升序1  desc:降序-1
 	// 默认排序
 	onsaleTimeSort := repo.Map{"onsaleTime": -1}
-	if sort != 0 {
-		onsaleTimeSort = repo.Map{"onsaleTime": sort}
+	if _, ok := query["sort"]; ok {
+		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不能为空")
@@ -61,14 +73,15 @@ func ProductList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	if err != nil {
 		return nil, errors.New("供应链id错误")
 	}
+	query["supplyId"] = supplyId
 
 	option := &repo.PageSearchOptions{
-		CollectName: repo.CollectionSupply,
+		CollectName: repo.CollectionProduct,
 		Page:        page,
 		Size:        size,
 		Query:       query,
-		Project:     []string{},
-		Sort:        onsaleTimeSort,
+		// Project:     []string{},
+		Sort: onsaleTimeSort,
 	}
 	pageResult, err := repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
 	if err != nil {
@@ -91,7 +104,7 @@ func ProductList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	if len(pageResult.List) > 0 {
 		for _, v := range pageResult.List {
 			v["isCollect"] = false
-			if len(collects) > 0 && err1 != nil {
+			if len(collects) > 0 && err1 == nil {
 				for _, col := range collects {
 					if v["_id"].(primitive.ObjectID) == col.ProductId { // productId唯一
 						v["isCollect"] = true
@@ -104,7 +117,7 @@ func ProductList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	return pageResult, err
 }
 
-// 更新产品
+// 更新产品/编辑、下架
 func ProductUpdate(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	var form model.Product
 	err := c.ShouldBindJSON(&form)
@@ -119,6 +132,11 @@ func ProductUpdate(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 }
 
 // 产品信息
+type ProudctDetailRes struct {
+	model.Product
+	IsCollect bool `json:"isCollect"`
+}
+
 func ProductDetail(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	productId := c.Param("id")
 	id, err := primitive.ObjectIDFromHex(productId)
@@ -136,12 +154,26 @@ 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},
+		}, &collect)
+		return ProudctDetailRes{Product: product, IsCollect: found}, nil
+	}
 
 	return product, nil
 }
 
 // 删除产品
+// ???权限控制 admin 和 供应商本人
 func ProductDelete(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	productId := c.Param("id")
 	_, err := primitive.ObjectIDFromHex(productId)

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

@@ -19,6 +19,9 @@ func RegRouters(svc *Service) {
 	// 产品管理
 	Product(_3dshow)
 
+	// 地址管理
+	Address(_3dshow)
+
 	// 订单管理
 	Order(_3dshow)
 

+ 3 - 3
3dshow/api/supply.go

@@ -21,8 +21,8 @@ func Supply(r *GinRouter) {
 		EmtyModel: func(c *gin.Context, apictx *ApiSession) interface{} {
 			return &model.Supply{}
 		},
-		JWT:           false,
-		SearchProject: []string{"name", "createTime"},
-		DetailProject: []string{"name", "createTime"},
+		JWT:           true,
+		SearchProject: []string{"name", "avatar", "createTime"},
+		DetailProject: []string{"name", "avatar", "createTime"},
 	})
 }

+ 36 - 3
3dshow/api/test_print.go

@@ -1,13 +1,46 @@
 package api
 
 import (
-	"strconv"
+	"3dshow/db/model"
+	"3dshow/db/repo"
+	"fmt"
 
 	"github.com/gin-gonic/gin"
 )
 
 func Printr(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	// _sort := "a"
-	sort, _ := strconv.Atoi("-1")
-	return sort, nil
+	statusMap := make(map[int]struct{})
+	// statusMap[-1] = struct{}{}
+	statusMap[1] = struct{}{}
+	statusMap[2] = struct{}{}
+	status := 0
+	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
+	}
+	fmt.Println(status)
+	var statusOrder model.Order
+	statusOrder.Status = status
+	return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionOrder, "636b5b4a0bab456f8a558962", &statusOrder)
+	// return repo.RepoUpdateSetDocProps(apictx.CreateRepoCtx(), repo.CollectionOrder, "636b5b4a0bab456f8a558962", repo.Map{"status": status})
+	// sort, _ := strconv.Atoi("-1")
+	// return status, nil
 }

+ 1 - 0
3dshow/api/tmp.md

@@ -0,0 +1 @@
+eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NjgwNjE1MjIsImlkIjoiNjMwNWMzZGRkNWQ3OGRiY2I3MGI3ZjcwIiwia2V5Ijoic2t1M2QtdXNlciIsIm5hbWUiOiIxMzQwODU0NzgyMyIsIm9yaWdfaWF0IjoxNjY3OTc1MTIyLCJwYXJlbnQiOiI2MzA1YzNkZGQ1ZDc4ZGJjYjcwYjdmNzAiLCJwaG9uZSI6IjEzNDA4NTQ3ODIzIiwicm9sZSI6IiIsInN0YXRlIjoxLCJ1c2VyVHlwZSI6Mn0.HEnotz-KJ5wz1uReAL3EuP5HpFbYmFrxMuYf2pDh78w

+ 2 - 1
3dshow/app.yaml

@@ -20,6 +20,7 @@ debug:
   UserRole: string
 
 nats:
-  url: nats://124.71.139.24:14301
+  # url: nats://124.71.139.24:14301
+  url: nats://127.0.0.1:14301
   maxReconnect: 1000
   reconnDelaySecond: 5

+ 1 - 1
3dshow/db/model/address.go

@@ -14,7 +14,7 @@ type Address struct {
 	Addr       string             `bson:"addr,omitempty" json:"addr"`       // 详细地址
 	Contact    string             `bson:"contact,omitempty" json:"contact"` // 联系人
 	Phone      string             `bson:"phone,omitempty" json:"phone"`     // 联系人电话
-	Defualt    int                `bson:"defualt,omitempty" json:"defualt"` // 是否默认 0:否 1:是
+	Defualt    int                `bson:"defualt,omitempty" json:"defualt"` // 是否默认 2:否 1:是
 	CreateTime time.Time          `bson:"createTime,omitempty" json:"createTime"`
 	UpdateTime time.Time          `bson:"updateTime,omitempty" json:"updateTime"`
 }

+ 11 - 24
3dshow/db/model/order.go

@@ -12,35 +12,22 @@ type Order struct {
 	UserId         primitive.ObjectID `bson:"userId,omitempty" json:"userId"`
 	SupplyId       primitive.ObjectID `bson:"supplyId,omitempty" json:"supplyId"`
 	Address        *Address           `bson:"address,omitempty" json:"address"`
-	Product        *OrderProduct      `bson:"product,omitempty" json:"product"`
+	Products       []*OrderProduct    `bson:"products,omitempty" json:"products"`
 	DeliveryMethod string             `bson:"deliveryMethod,omitempty" json:"deliveryMethod"`
 	Remark         string             `bson:"remark,omitempty" json:"remark"`
-	ExpressNo      string             `bson:"expressNo,omitempty" json:"expressNo"` // 快递单号,商家发货时需要输入
-	Status         int                `bson:"status,omitempty" json:"status"`       // 0:待发货 1:已发货 2:已收货 3:已完成
+	Status         int                `bson:"status,omitempty" json:"status"` // -1:待发货 1:已发货 2:已完成
 	CreateTime     time.Time          `bson:"createTime,omitempty" json:"createTime"`
 	UpdateTime     time.Time          `bson:"updateTime,omitempty" json:"updateTime"`
 }
 
 type OrderProduct struct {
-	Id       primitive.ObjectID `bson:"id,omitempty" json:"id"`             // 对应产品id
-	SupplyId primitive.ObjectID `bson:"supplyId,omitempty" json:"supplyId"` // 供应链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"` // 封面图
-}
-
-// 可能一次性提交多个产品
-type OrederAddReq struct {
-	Id             primitive.ObjectID `bson:"_id,omitempty" json:"_id"`
-	UserId         primitive.ObjectID `bson:"userId,omitempty" json:"userId"`
-	Address        *Address           `bson:"address,omitempty" json:"address"`
-	Products       []*OrderProduct    `bson:"product,omitempty" json:"product"`
-	DeliveryMethod string             `bson:"deliveryMethod,omitempty" json:"deliveryMethod"`
-	Remark         string             `bson:"remark,omitempty" json:"remark"`
-	ExpressNo      string             `bson:"expressNo,omitempty" json:"expressNo"` // 快递单号,商家发货时需要输入
-	Status         int                `bson:"status,omitempty" json:"status"`       // 0:代发货 1:已发货
-	CreateTime     time.Time          `bson:"createTime,omitempty" json:"createTime"`
-	UpdateTime     time.Time          `bson:"updateTime,omitempty" json:"updateTime"`
+	Id        primitive.ObjectID `bson:"id,omitempty" json:"id"`             // 对应产品id
+	SupplyId  primitive.ObjectID `bson:"supplyId,omitempty" json:"supplyId"` // 供应链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"`         // 封面图
+	ExpressNo string             `bson:"expressNo,omitempty" json:"expressNo"` // 快递单号,商家发货时需要输入
+	Status    int                `bson:"status,omitempty" json:"status"`       // -1:待发货 1:已发货
 }

+ 1 - 1
3dshow/db/model/product.go

@@ -19,7 +19,7 @@ type Product struct {
 	Color      []string           `bson:"color,omitempty" json:"color"`
 	Size       []int              `bson:"size,omitempty" json:"size"`                 // 尺寸
 	Thumbnail  []string           `bson:"thumbnail,omitempty" json:"thumbnail"`       // 缩略图集合
-	Status     int                `bson:"status,omitempty" json:"status"`             // 0:下架 1:上架
+	Status     int                `bson:"status,omitempty" json:"status"`             // -1:下架 1:上架
 	OnsaleTime time.Time          `bson:"onsaleTime,omitempty" json:"onsaleTimeTime"` // 上架时间
 	CreateTime time.Time          `bson:"createTime,omitempty" json:"createTime"`
 	UpdateTime time.Time          `bson:"updateTime,omitempty" json:"updateTime"`

+ 1 - 0
3dshow/db/model/supply.go

@@ -10,6 +10,7 @@ import (
 type Supply struct {
 	Id         primitive.ObjectID `bson:"_id,omitempty" json:"_id"`
 	Name       string             `bson:"name,omitempty" json:"name"`
+	Avatar     string             `bson:"avatar,omitempty" json:"avatar"`
 	CreateTime time.Time          `bson:"createTime,omitempty" json:"createTime"`
 	UpdateTime time.Time          `bson:"updateTime,omitempty" json:"updateTime"`
 }