liwei 2 years ago
parent
commit
a420e8c880
4 changed files with 138 additions and 6 deletions
  1. 5 6
      boxcost/api/supplier-price.go
  2. 126 0
      boxcost/api/supplier.go
  3. 4 0
      boxcost/db/model/pack.go
  4. 3 0
      boxcost/db/repo/repo.go

+ 5 - 6
boxcost/api/supplier-price.go

@@ -29,9 +29,9 @@ func SupplierPrice(r *GinRouter) {
 	// r.POST("/supplier/mat/delete/:id", DelSupplierPrice)
 
 	//材料供应
-	SupplierMatTable := "supplier-mats"
+
 	CreateCRUD(r, "/supplier/mat", &CRUDOption{
-		Collection: SupplierMatTable,
+		Collection: repo.CollectionSupplierMatprice,
 		NewModel: func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 			entity := &model.SupplierPrice{}
 			c.ShouldBindJSON(entity)
@@ -41,7 +41,7 @@ func SupplierPrice(r *GinRouter) {
 
 			curr := &model.SupplierPrice{}
 			ok, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
-				CollectName: SupplierMatTable,
+				CollectName: repo.CollectionSupplierMatprice,
 				Query:       repo.Map{"productId": entity.ProductId, "supplierId": entity.SupplierId},
 				Project:     []string{"_id"},
 			}, curr)
@@ -90,9 +90,8 @@ func SupplierPrice(r *GinRouter) {
 		SearchProject: []string{"price", "supplierId", "productId", "updateTime", "_id"},
 	})
 
-	SupplierCraftTable := "supplier-crafts"
 	CreateCRUD(r, "/supplier/craft", &CRUDOption{
-		Collection: SupplierCraftTable,
+		Collection: repo.CollectionSupplierCraftprice,
 		NewModel: func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 			entity := &model.SupplierPrice{}
 			c.ShouldBindJSON(entity)
@@ -103,7 +102,7 @@ func SupplierPrice(r *GinRouter) {
 
 			curr := &model.SupplierPrice{}
 			ok, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
-				CollectName: SupplierCraftTable,
+				CollectName: repo.CollectionSupplierCraftprice,
 				Query:       repo.Map{"productId": entity.ProductId, "supplierId": entity.SupplierId},
 				Project:     []string{"_id"},
 			}, curr)

+ 126 - 0
boxcost/api/supplier.go

@@ -11,6 +11,7 @@ import (
 	"github.com/gin-gonic/gin"
 	"go.mongodb.org/mongo-driver/bson"
 	"go.mongodb.org/mongo-driver/bson/primitive"
+	"go.mongodb.org/mongo-driver/mongo/options"
 )
 
 // 供应商管理
@@ -30,6 +31,9 @@ func Supplier(r *GinRouter) {
 
 	// 删除供应商
 	r.POST("/supplier/delete/:id", DelSupplier)
+
+	// 获取供应商列表
+	r.GET("/plan/supplier/list", GetPlanSuppliers)
 }
 
 // 创建供应商
@@ -98,6 +102,128 @@ func GetSuppliers(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
 }
 
+func GetPlanSuppliers(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	page, size, query := UtilQueryPageSize(c)
+	//category =>根据内容查询 供应对应内容的供应商
+	emtyPage := &repo.PageResult{
+		Total: 0,
+		Size:  size,
+		Page:  page,
+		List:  []map[string]interface{}{},
+	}
+	listOut := []map[string]interface{}{}
+
+	if query["matId"] != nil {
+		matId := query["matId"].(string)
+		if len(matId) < 1 {
+			return nil, fmt.Errorf("matId(string)为空")
+		}
+		id, _ := primitive.ObjectIDFromHex(matId)
+		ok, list := repo.RepoSeachDocsMap(apictx.CreateRepoCtx(), &repo.DocsSearchOptions{
+			CollectName: repo.CollectionSupplierMatprice,
+			Query:       repo.Map{"productId": id},
+			Project:     []string{"supplierId"},
+		})
+		if !ok {
+			return emtyPage, nil
+		}
+
+		listOut = list
+	} else {
+
+		if query["craftId"] == nil {
+			return nil, fmt.Errorf("参数错误 craftId 或matId不能为空")
+		}
+
+		cratf := &model.Craft{}
+
+		ok, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
+			Query:       repo.Map{"_id": query["craftId"].(string)},
+			CollectName: repo.CollectionCraft,
+		}, cratf)
+
+		if !ok {
+			return nil, fmt.Errorf("没有对应的工艺信息")
+		}
+
+		//查询工艺分类
+		pipleLine := []bson.M{
+			{
+				"$lookup": bson.M{
+					"from":         repo.CollectionCraft,
+					"localField":   "productId",
+					"foreignField": "_id",
+					"as":           "craft_docs",
+				},
+			},
+			{
+				"$match": bson.M{
+					"craft_docs.0.category": cratf.Category,
+				},
+			},
+			{
+				"$project": bson.M{
+					"craft_docs": 0,
+					"createTime": 0,
+					"price":      0,
+					"productId":  0,
+					"updateTime": 0,
+				},
+			},
+		}
+		ctx := apictx.CreateRepoCtx()
+
+		colls := ctx.Client.GetCollection(repo.CollectionSupplierCraftprice)
+
+		findoptions := &options.AggregateOptions{}
+
+		cur, err := colls.Aggregate(ctx.Ctx, pipleLine, findoptions)
+		if err != nil {
+			return nil, err
+		}
+
+		defer cur.Close(ctx.Ctx)
+
+		err = cur.All(ctx.Ctx, &listOut)
+
+		if err != nil {
+			return nil, err
+		}
+		if len(listOut) < 1 {
+			return emtyPage, nil
+		}
+	}
+
+	//获取供应商列表
+	suppliers := []primitive.ObjectID{}
+	suppliersMap := map[string]bool{}
+	for _, item := range listOut {
+		if item["supplierId"] != nil {
+
+			id, ok := item["supplierId"].(primitive.ObjectID)
+			if !ok {
+				continue
+			}
+			if !suppliersMap[id.Hex()] {
+				suppliers = append(suppliers, id)
+				suppliersMap[id.Hex()] = true
+			}
+		}
+	}
+	if len(suppliers) < 1 {
+		return emtyPage, nil
+	}
+
+	option := &repo.PageSearchOptions{
+		CollectName: repo.CollectionSupplier,
+		Query:       repo.Map{"_id": bson.M{"$in": suppliers}},
+		Page:        page,
+		Size:        size,
+		Sort:        bson.M{"createTime": -1},
+	}
+	return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
+}
+
 // 更新供应商
 func UpdateSupplier(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	var supplier model.Supplier

+ 4 - 0
boxcost/db/model/pack.go

@@ -77,6 +77,8 @@ type PackComponentMat struct {
 	BatchSizeHeight int `bson:"batchSizeHeight,omitempty" json:"batchSizeHeight"` //平板尺寸
 
 	BillId string `bson:"billId,omitempty" json:"billId"`
+
+	Remark string `bson:"remark,omitempty" json:"remark"` //备注
 }
 
 type PackComponentMatCraft struct {
@@ -98,4 +100,6 @@ type PackComponentMatCraft struct {
 	BatchSizeHeight int `bson:"batchSizeHeight,omitempty" json:"batchSizeHeight"` //平板尺寸
 
 	BillId string `bson:"billId,omitempty" json:"billId"`
+
+	Remark string `bson:"remark,omitempty" json:"remark"` //备注
 }

+ 3 - 0
boxcost/db/repo/repo.go

@@ -27,6 +27,9 @@ const (
 	CollectionProductPlan   = "product-plan"
 	CollectionBillPurchase  = "bill-purchase"
 	CollectionBillProduce   = "bill-produce"
+
+	CollectionSupplierMatprice   = "supplier-mats"
+	CollectionSupplierCraftprice = "supplier-crafts"
 )
 
 type Map map[string]interface{}