product.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package api
  2. import (
  3. "3dshow-supplier/db/model"
  4. "3dshow-supplier/db/repo"
  5. "3dshow-supplier/log"
  6. "errors"
  7. "time"
  8. "github.com/gin-gonic/gin"
  9. "go.mongodb.org/mongo-driver/bson/primitive"
  10. )
  11. // 产品管理
  12. func Product(r *GinRouter) {
  13. // ??? supplier apis
  14. r.GETJWT("/product/list", ProductList)
  15. r.POSTJWT("/product/onOrOffShelves", OnOrOffShelves)
  16. r.GETJWT("/product/detail/:id", ProductDetail)
  17. r.POSTJWT("/product/delete/:id", ProductDelete)
  18. }
  19. // 上下架商品
  20. func OnOrOffShelves(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  21. // 根据id查询商品
  22. _id := c.Param("id")
  23. if len(_id) < 1 {
  24. return nil, errors.New("商品id不能为空")
  25. }
  26. productId, err := primitive.ObjectIDFromHex(_id)
  27. if err != nil {
  28. return nil, errors.New("非法id")
  29. }
  30. product := model.Product{}
  31. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  32. CollectName: repo.CollectionProduct,
  33. Query: repo.Map{"_id": productId},
  34. }, &product)
  35. if !found || err != nil {
  36. return nil, errors.New("该商品不存在")
  37. }
  38. _userId := apictx.User.ID
  39. userId, _ := primitive.ObjectIDFromHex(_userId)
  40. // 不是自己的商品
  41. if product.SupplyId != userId {
  42. return nil, errors.New("非法操作")
  43. }
  44. // 更新状态
  45. if product.Status == -1 {
  46. product.Status = 1
  47. product.OnsaleTime = time.Now()
  48. product.UpdateTime = time.Now()
  49. } else if product.Status == 1 {
  50. product.Status = -1
  51. product.UpdateTime = time.Now()
  52. }
  53. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionProduct, _id, &product)
  54. }
  55. // 产品列表
  56. func ProductList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  57. page, size, query := UtilQueryPageSize(c)
  58. // asc:升序1 desc:降序-1
  59. // 默认排序
  60. onsaleTimeSort := repo.Map{"onsaleTime": -1}
  61. if _, ok := query["sort"]; ok {
  62. if query["sort"].(string) == "asc" {
  63. onsaleTimeSort = repo.Map{"onsaleTime": 1}
  64. }
  65. delete(query, "sort")
  66. }
  67. // 供应商 查询自己的商品
  68. _userId := apictx.User.ID
  69. userId, _ := primitive.ObjectIDFromHex(_userId)
  70. query["supplyId"] = userId
  71. option := &repo.PageSearchOptions{
  72. CollectName: repo.CollectionProduct,
  73. Page: page,
  74. Size: size,
  75. Query: query,
  76. // Project: []string{},
  77. Sort: onsaleTimeSort,
  78. }
  79. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  80. }
  81. // 删除产品
  82. func ProductDelete(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  83. _id := c.Param("id")
  84. if len(_id) < 1 {
  85. return nil, errors.New("id不能为空")
  86. }
  87. productId, err := primitive.ObjectIDFromHex(_id)
  88. if err != nil {
  89. return nil, errors.New("非法id")
  90. }
  91. product := model.Product{}
  92. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  93. CollectName: repo.CollectionProduct,
  94. Query: repo.Map{"_id": productId},
  95. }, &product)
  96. if !found || err != nil {
  97. return nil, errors.New("该商品不存在")
  98. }
  99. _userId := apictx.User.ID
  100. userId, _ := primitive.ObjectIDFromHex(_userId)
  101. // 不是自己的商品
  102. if product.SupplyId != userId {
  103. return nil, errors.New("非法操作")
  104. }
  105. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionProduct, _id)
  106. }
  107. // 产品信息
  108. func ProductDetail(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  109. _id := c.Param("id")
  110. if len(_id) < 1 {
  111. return nil, errors.New("id不能为空")
  112. }
  113. productId, err := primitive.ObjectIDFromHex(_id)
  114. if err != nil {
  115. return nil, errors.New("非法id")
  116. }
  117. var product model.Product
  118. option := &repo.DocSearchOptions{
  119. CollectName: repo.CollectionProduct,
  120. Query: repo.Map{"_id": productId},
  121. }
  122. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &product)
  123. if !found || err != nil {
  124. log.Info(err)
  125. return nil, errors.New("数据未找到")
  126. }
  127. return product, nil
  128. }