animeic 2 年之前
父节点
当前提交
8b03d93833

+ 53 - 0
3dshow/api/address.go

@@ -0,0 +1,53 @@
+package api
+
+import (
+	"3dshow/db/model"
+	"3dshow/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("非法用户")
+			}
+			entity.UserId = userId
+			first, _ := repo.RepoCountDoc(apictx.CreateRepoCtx(), repo.CollectionAddress, repo.Map{"userId": userId})
+			if first > 0 {
+				entity.Defualt = 0
+			} else {
+				entity.Defualt = 1
+			}
+			return entity, nil
+		},
+		EmtyModel: func(c *gin.Context, apictx *ApiSession) interface{} {
+			return &model.Address{}
+		},
+		JWT: true,
+		SearchFilter: func(c *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")
+				return repo.Map{"userId": invalidId}
+			}
+
+			return repo.Map{"userId": userId}
+		},
+		SearchProject: []string{"area", "addr", "contact", "phone", "defualt", "createTime"},
+		DetailProject: []string{"area", "addr", "contact", "phone", "defualt", "createTime"},
+	})
+}

+ 142 - 0
3dshow/api/order.go

@@ -0,0 +1,142 @@
+package api
+
+import (
+	"3dshow/db/model"
+	"3dshow/db/repo"
+	"errors"
+	"time"
+
+	"github.com/gin-gonic/gin"
+	"go.mongodb.org/mongo-driver/bson/primitive"
+)
+
+// 产品管理
+func Order(r *GinRouter) {
+	r.POSTJWT("/order/create", OrderAdd)
+	r.GETJWT("/order/list", OrderList)
+	r.POSTJWT("/order/update", OrderUpdate)
+	r.GETJWT("/order/detail/:id", OrderDetail)
+	r.POSTJWT("/order/delete/:id", OrderDelete)
+}
+
+// 新增订单
+func OrderAdd(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	var form model.OrederAddReq
+	err := c.ShouldBindJSON(&form)
+	if err != nil {
+		return nil, errors.New("参数错误!")
+	}
+	ctx := apictx.CreateRepoCtx()
+	_userId := apictx.User.ID
+	userId, err := primitive.ObjectIDFromHex(_userId)
+	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(),
+			})
+
+		}
+	}
+
+	return true, err
+}
+
+// 订单列表
+func OrderList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	// customer
+	// supplier
+	page, size, query := UtilQueryPageSize(c)
+	_userId := apictx.User.ID
+	userId, err := primitive.ObjectIDFromHex(_userId)
+	if err != nil {
+		return nil, errors.New("非法用户")
+	}
+	// if apictx.User.Role == "customer"{
+	query["userId"] = userId
+	// }
+	return repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
+		CollectName: repo.CollectionOrder,
+		Page:        page,
+		Size:        size,
+		Query:       query,
+		// Project: []string{},
+		Sort: repo.Map{"createTime": -1},
+	})
+}
+
+// 个人页面数量展示
+func OrderCount(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	_userId := apictx.User.ID
+	userId, err := primitive.ObjectIDFromHex(_userId)
+	if err != nil {
+		return nil, errors.New("非法用户")
+	}
+	statusArray := []int{0, 1, 2, 3}
+	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)
+	}
+	return ret, nil
+}
+
+// 更新订单
+func OrderUpdate(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	var form model.Order
+	err := c.ShouldBindJSON(&form)
+	if err != nil {
+		return nil, errors.New("参数错误")
+	}
+	if form.Id.Hex() == "" {
+		return nil, errors.New("更新的产品id不能为空")
+	}
+	form.UpdateTime = time.Now()
+	return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionOrder, form.Id.Hex(), &form)
+}
+
+// 订单信息
+func OrderDetail(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	orderId := c.Param("id")
+	id, err := primitive.ObjectIDFromHex(orderId)
+	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 {
+		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)
+}

+ 153 - 0
3dshow/api/product.go

@@ -0,0 +1,153 @@
+package api
+
+import (
+	"3dshow/db/model"
+	"3dshow/db/repo"
+	"3dshow/log"
+	"errors"
+	"strconv"
+	"time"
+
+	"github.com/gin-gonic/gin"
+	"go.mongodb.org/mongo-driver/bson/primitive"
+)
+
+// 产品管理
+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.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()
+
+	result, err := repo.RepoAddDoc(ctx, repo.CollectionProduct, &form)
+	return result, err
+
+}
+
+// 产品列表
+func ProductList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	page, size, query := UtilQueryPageSize(c)
+	_sort := c.Query("sort")
+	// 1:升序  -1:降序
+	sort, _ := strconv.Atoi(_sort)
+	// 默认排序
+	onsaleTimeSort := repo.Map{"onsaleTime": -1}
+	if sort != 0 {
+		onsaleTimeSort = repo.Map{"onsaleTime": sort}
+	}
+	// 查询数据
+	if _, ok := query["supplyId"]; !ok {
+		return nil, errors.New("供应链id不能为空")
+	}
+	supplyId, err := primitive.ObjectIDFromHex(query["supplyId"].(string))
+	if err != nil {
+		return nil, errors.New("供应链id错误")
+	}
+
+	option := &repo.PageSearchOptions{
+		CollectName: repo.CollectionSupply,
+		Page:        page,
+		Size:        size,
+		Query:       query,
+		Project:     []string{},
+		Sort:        onsaleTimeSort,
+	}
+	pageResult, err := repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
+	if err != nil {
+		return nil, err
+	}
+	// 查询收藏状态
+	var collects []*model.Collect
+	_userId := apictx.User.ID
+	userId, err := primitive.ObjectIDFromHex(_userId)
+	if err != nil {
+		return nil, errors.New("非法用户")
+	}
+	option1 := &repo.PageSearchOptions{
+		CollectName: repo.CollectionCollect,
+		Query:       repo.Map{"userId": userId, "supplyId": supplyId},
+		Project:     []string{"productId"},
+	}
+	err1 := repo.RepoDocsSearch(apictx.CreateRepoCtx(), option1, &collects)
+
+	if len(pageResult.List) > 0 {
+		for _, v := range pageResult.List {
+			v["isCollect"] = false
+			if len(collects) > 0 && err1 != nil {
+				for _, col := range collects {
+					if v["_id"].(primitive.ObjectID) == col.ProductId { // productId唯一
+						v["isCollect"] = true
+						break
+					}
+				}
+			}
+		}
+	}
+	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不能为空")
+	}
+	form.UpdateTime = time.Now()
+	return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionProduct, form.Id.Hex(), &form)
+}
+
+// 产品信息
+func ProductDetail(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	productId := c.Param("id")
+	id, err := primitive.ObjectIDFromHex(productId)
+	if err != nil {
+		return nil, errors.New("非法id")
+	}
+	var product model.Product
+	option := &repo.DocSearchOptions{
+		CollectName: repo.CollectionProduct,
+		Query:       repo.Map{"_id": id},
+	}
+
+	found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &product)
+	if !found || err != nil {
+		log.Info(err)
+		return nil, errors.New("数据未找到")
+	}
+	// ??? 是否收藏 前端调用接口判断
+
+	return product, nil
+}
+
+// 删除产品
+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)
+}

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

@@ -16,6 +16,9 @@ func RegRouters(svc *Service) {
 	// 供应链管理
 	Supply(_3dshow)
 
+	// 产品管理
+	Product(_3dshow)
+
 }
 
 func Logger() gin.HandlerFunc {

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

@@ -2,6 +2,7 @@ package api
 
 import (
 	"3dshow/db/model"
+	"3dshow/db/repo"
 	"time"
 
 	"github.com/gin-gonic/gin"
@@ -10,7 +11,7 @@ import (
 func Supply(r *GinRouter) {
 
 	CreateCRUD(r, "/supply", &CRUDOption{
-		Collection: "supply",
+		Collection: repo.CollectionSupply,
 		NewModel: func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 			entity := &model.Supply{}
 			c.ShouldBindJSON(entity)
@@ -20,7 +21,8 @@ func Supply(r *GinRouter) {
 		EmtyModel: func(c *gin.Context, apictx *ApiSession) interface{} {
 			return &model.Supply{}
 		},
-		JWT:           true,
+		JWT:           false,
 		SearchProject: []string{"name", "createTime"},
+		DetailProject: []string{"name", "createTime"},
 	})
 }

+ 8 - 2
3dshow/api/test_print.go

@@ -1,7 +1,13 @@
 package api
 
-import "github.com/gin-gonic/gin"
+import (
+	"strconv"
+
+	"github.com/gin-gonic/gin"
+)
 
 func Printr(c *gin.Context, apictx *ApiSession) (interface{}, error) {
-	return "aaa", nil
+	// _sort := "a"
+	sort, _ := strconv.Atoi("-1")
+	return sort, nil
 }

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

@@ -35,7 +35,7 @@ func (db *MongoDB) GetOrCreateDatabase(name string) *mongo.Database {
 }
 
 func NewMongoDB(bus *comm.NatsBus) *MongoDB {
-	inst, err := bus.NewMongoDBFromConfig("3dshow-mongo")
+	inst, err := bus.NewMongoDBFromConfigDev("3dshow-mongo")
 	if err != nil {
 		panic(err)
 	}

+ 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    uint8              `bson:"defualt,omitempty" json:"defualt"` // 是否默认 0:否 1:是
+	Defualt    int                `bson:"defualt,omitempty" json:"defualt"` // 是否默认 0:否 1:是
 	CreateTime time.Time          `bson:"createTime,omitempty" json:"createTime"`
 	UpdateTime time.Time          `bson:"updateTime,omitempty" json:"updateTime"`
 }

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

@@ -10,6 +10,7 @@ import (
 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"`

+ 23 - 6
3dshow/db/model/order.go

@@ -10,20 +10,37 @@ import (
 type Order 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"`
 	Address        *Address           `bson:"address,omitempty" json:"address"`
 	Product        *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:已发货
+	Status         int                `bson:"status,omitempty" json:"status"`       // 0:待发货 1:已发货 2:已收货 3:已完成
 	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"`
-	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"` // 封面图
+	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"`
 }

+ 1 - 1
3dshow/db/redis.go

@@ -6,7 +6,7 @@ import (
 )
 
 func NewRedisClient(bus *comm.NatsBus) *redis.Client {
-	client, err := bus.NewRedisFromConfig("3dshow-redis")
+	client, err := bus.NewRedisFromConfigDev("3dshow-redis")
 	if err != nil {
 		return nil
 	}

+ 5 - 8
3dshow/db/repo/repo.go

@@ -18,14 +18,11 @@ type RepoSession struct {
 }
 
 const (
-	CollectionMaterial      = "material"
-	CollectionCraft         = "craft"
-	CollectionProcess       = "process"
-	CollectionSupplier      = "supplier"
-	CollectionSupplierPrice = "supplier-price"
-	CollectionPack          = "pack"
-	CollectionProductPlan   = "product-plan"
-	CollectionBill          = "bill"
+	CollectionSupply  = "supply"
+	CollectionCollect = "collect"
+	CollectionProduct = "product"
+	CollectionAddress = "address"
+	CollectionOrder   = "order"
 )
 
 type Map map[string]interface{}