supplier-price.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package api
  2. import (
  3. "box-cost/db/model"
  4. "box-cost/db/repo"
  5. "box-cost/log"
  6. "errors"
  7. "fmt"
  8. "time"
  9. "github.com/gin-gonic/gin"
  10. "go.mongodb.org/mongo-driver/bson"
  11. "go.mongodb.org/mongo-driver/bson/primitive"
  12. )
  13. // 供应商价格管理
  14. func SupplierPrice(r *GinRouter) {
  15. // 创建供应商价格
  16. r.POST("/supplierPrice", CreateSupplierPrice)
  17. // 获取供应商价格详情
  18. r.GET("/supplierPrice/:id", GetSupplierPrice)
  19. // 获取供应商价格列表
  20. r.GET("/supplierPrices", GetSupplierPrices)
  21. // 更新供应商价格
  22. r.POST("/supplierPrice/update", UpdateSupplierPrice)
  23. // 删除供应商价格
  24. r.POST("/supplierPrice/delete/:id", DelSupplierPrice)
  25. }
  26. // 创建供应商价格
  27. func CreateSupplierPrice(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  28. var supplierprice model.SupplierPrice
  29. err := c.ShouldBindJSON(&supplierprice)
  30. if err != nil {
  31. fmt.Println(err)
  32. return nil, errors.New("参数错误!")
  33. }
  34. ctx := apictx.CreateRepoCtx()
  35. if supplierprice.SupplierId.Hex() == "" {
  36. return nil, errors.New("供应商id为空")
  37. }
  38. if supplierprice.ProductId.Hex() == "" {
  39. return nil, errors.New("供应产品id为空")
  40. }
  41. if supplierprice.PriceStrategy == nil {
  42. return nil, errors.New("供应商价格策略为空")
  43. }
  44. supplierprice.CreateTime = time.Now()
  45. supplierprice.UpdateTime = time.Now()
  46. result, err := repo.RepoAddDoc(ctx, repo.CollectionSupplierPrice, &supplierprice)
  47. return result, err
  48. }
  49. // 获取供应商价格信息
  50. func GetSupplierPrice(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  51. supplierpriceId := c.Param("id")
  52. id, err := primitive.ObjectIDFromHex(supplierpriceId)
  53. if err != nil {
  54. return nil, errors.New("非法id")
  55. }
  56. var supplierprice model.SupplierPrice
  57. option := &repo.DocSearchOptions{
  58. CollectName: repo.CollectionSupplierPrice,
  59. Query: repo.Map{"_id": id},
  60. }
  61. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &supplierprice)
  62. if !found || err != nil {
  63. log.Info(err)
  64. return nil, errors.New("数据未找到")
  65. }
  66. return supplierprice, nil
  67. }
  68. // 获取供应商价格列表
  69. func GetSupplierPrices(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  70. page, size, query := UtilQueryPageSize(c)
  71. option := &repo.PageSearchOptions{
  72. CollectName: repo.CollectionSupplierPrice,
  73. Query: query,
  74. Page: page,
  75. Size: size,
  76. Sort: bson.M{"createTime": -1},
  77. }
  78. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  79. }
  80. // 更新供应商价格
  81. func UpdateSupplierPrice(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  82. var supplierprice model.SupplierPrice
  83. err := c.ShouldBindJSON(&supplierprice)
  84. if err != nil {
  85. return nil, errors.New("参数错误")
  86. }
  87. if supplierprice.Id.Hex() == "" {
  88. return nil, errors.New("id的为空")
  89. }
  90. supplierprice.UpdateTime = time.Now()
  91. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionSupplierPrice, supplierprice.Id.Hex(), &supplierprice)
  92. }
  93. // 删除供应商价格
  94. func DelSupplierPrice(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  95. supplierpriceId := c.Param("id")
  96. if supplierpriceId == "" {
  97. return nil, errors.New("id为空")
  98. }
  99. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionSupplierPrice, supplierpriceId)
  100. }