123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- 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)
- }
|