package api import ( "box-cost/db/model" "box-cost/db/repo" "box-cost/log" "errors" "fmt" "time" "github.com/gin-gonic/gin" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" ) // 供应商价格管理 func SupplierPrice(r *GinRouter) { // 创建供应商价格 r.POST("/supplierPrice", CreateSupplierPrice) // 获取供应商价格详情 r.GET("/supplierPrice/:id", GetSupplierPrice) // 获取供应商价格列表 r.GET("/supplierPrices", GetSupplierPrices) // 更新供应商价格 r.POST("/supplierPrice/update", UpdateSupplierPrice) // 删除供应商价格 r.POST("/supplierPrice/delete/:id", DelSupplierPrice) } // 创建供应商价格 func CreateSupplierPrice(c *gin.Context, apictx *ApiSession) (interface{}, error) { var supplierprice model.SupplierPrice err := c.ShouldBindJSON(&supplierprice) if err != nil { fmt.Println(err) return nil, errors.New("参数错误!") } ctx := apictx.CreateRepoCtx() if supplierprice.SupplierId.Hex() == "" { return nil, errors.New("供应商id为空") } if supplierprice.ProductId.Hex() == "" { return nil, errors.New("供应产品id为空") } if supplierprice.PriceStrategy == nil { return nil, errors.New("供应商价格策略为空") } supplierprice.CreateTime = time.Now() supplierprice.UpdateTime = time.Now() result, err := repo.RepoAddDoc(ctx, repo.CollectionSupplierPrice, &supplierprice) return result, err } // 获取供应商价格信息 func GetSupplierPrice(c *gin.Context, apictx *ApiSession) (interface{}, error) { supplierpriceId := c.Param("id") id, err := primitive.ObjectIDFromHex(supplierpriceId) if err != nil { return nil, errors.New("非法id") } var supplierprice model.SupplierPrice option := &repo.DocSearchOptions{ CollectName: repo.CollectionSupplierPrice, Query: repo.Map{"_id": id}, } found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &supplierprice) if !found || err != nil { log.Info(err) return nil, errors.New("数据未找到") } return supplierprice, nil } // 获取供应商价格列表 func GetSupplierPrices(c *gin.Context, apictx *ApiSession) (interface{}, error) { page, size, query := UtilQueryPageSize(c) option := &repo.PageSearchOptions{ CollectName: repo.CollectionSupplierPrice, Query: query, Page: page, Size: size, Sort: bson.M{"createTime": -1}, } return repo.RepoPageSearch(apictx.CreateRepoCtx(), option) } // 更新供应商价格 func UpdateSupplierPrice(c *gin.Context, apictx *ApiSession) (interface{}, error) { var supplierprice model.SupplierPrice err := c.ShouldBindJSON(&supplierprice) if err != nil { return nil, errors.New("参数错误") } if supplierprice.Id.Hex() == "" { return nil, errors.New("id的为空") } supplierprice.UpdateTime = time.Now() return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionSupplierPrice, supplierprice.Id.Hex(), &supplierprice) } // 删除供应商价格 func DelSupplierPrice(c *gin.Context, apictx *ApiSession) (interface{}, error) { supplierpriceId := c.Param("id") if supplierpriceId == "" { return nil, errors.New("id为空") } return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionSupplierPrice, supplierpriceId) }