product.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package api
  2. import (
  3. "3dshow/db/model"
  4. "3dshow/db/repo"
  5. "3dshow/log"
  6. "errors"
  7. "strconv"
  8. "time"
  9. "github.com/gin-gonic/gin"
  10. "go.mongodb.org/mongo-driver/bson/primitive"
  11. )
  12. // 产品管理
  13. func Product(r *GinRouter) {
  14. r.POSTJWT("/product/create", ProductAdd)
  15. r.GETJWT("/product/list", ProductList)
  16. r.POSTJWT("/product/update", ProductUpdate)
  17. r.GET("/product/detail/:id", ProductDetail)
  18. r.POSTJWT("/product/delete/:id", ProductDelete)
  19. }
  20. // 新增产品
  21. func ProductAdd(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  22. var form model.Product
  23. err := c.ShouldBindJSON(&form)
  24. if err != nil {
  25. return nil, errors.New("参数错误!")
  26. }
  27. ctx := apictx.CreateRepoCtx()
  28. if form.SupplyId.Hex() == "" {
  29. return nil, errors.New("供应链id不能为空")
  30. }
  31. if form.Name == "" {
  32. return nil, errors.New("产品名不能为空")
  33. }
  34. form.CreateTime = time.Now()
  35. result, err := repo.RepoAddDoc(ctx, repo.CollectionProduct, &form)
  36. return result, err
  37. }
  38. // 产品列表
  39. func ProductList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  40. page, size, query := UtilQueryPageSize(c)
  41. _sort := c.Query("sort")
  42. // 1:升序 -1:降序
  43. sort, _ := strconv.Atoi(_sort)
  44. // 默认排序
  45. onsaleTimeSort := repo.Map{"onsaleTime": -1}
  46. if sort != 0 {
  47. onsaleTimeSort = repo.Map{"onsaleTime": sort}
  48. }
  49. // 查询数据
  50. if _, ok := query["supplyId"]; !ok {
  51. return nil, errors.New("供应链id不能为空")
  52. }
  53. supplyId, err := primitive.ObjectIDFromHex(query["supplyId"].(string))
  54. if err != nil {
  55. return nil, errors.New("供应链id错误")
  56. }
  57. option := &repo.PageSearchOptions{
  58. CollectName: repo.CollectionSupply,
  59. Page: page,
  60. Size: size,
  61. Query: query,
  62. Project: []string{},
  63. Sort: onsaleTimeSort,
  64. }
  65. pageResult, err := repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  66. if err != nil {
  67. return nil, err
  68. }
  69. // 查询收藏状态
  70. var collects []*model.Collect
  71. _userId := apictx.User.ID
  72. userId, err := primitive.ObjectIDFromHex(_userId)
  73. if err != nil {
  74. return nil, errors.New("非法用户")
  75. }
  76. option1 := &repo.PageSearchOptions{
  77. CollectName: repo.CollectionCollect,
  78. Query: repo.Map{"userId": userId, "supplyId": supplyId},
  79. Project: []string{"productId"},
  80. }
  81. err1 := repo.RepoDocsSearch(apictx.CreateRepoCtx(), option1, &collects)
  82. if len(pageResult.List) > 0 {
  83. for _, v := range pageResult.List {
  84. v["isCollect"] = false
  85. if len(collects) > 0 && err1 != nil {
  86. for _, col := range collects {
  87. if v["_id"].(primitive.ObjectID) == col.ProductId { // productId唯一
  88. v["isCollect"] = true
  89. break
  90. }
  91. }
  92. }
  93. }
  94. }
  95. return pageResult, err
  96. }
  97. // 更新产品
  98. func ProductUpdate(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  99. var form model.Product
  100. err := c.ShouldBindJSON(&form)
  101. if err != nil {
  102. return nil, errors.New("参数错误")
  103. }
  104. if form.Id.Hex() == "" {
  105. return nil, errors.New("更新的产品id不能为空")
  106. }
  107. form.UpdateTime = time.Now()
  108. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionProduct, form.Id.Hex(), &form)
  109. }
  110. // 产品信息
  111. func ProductDetail(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  112. productId := c.Param("id")
  113. id, err := primitive.ObjectIDFromHex(productId)
  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": id},
  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. // ??? 是否收藏 前端调用接口判断
  128. return product, nil
  129. }
  130. // 删除产品
  131. func ProductDelete(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  132. productId := c.Param("id")
  133. _, err := primitive.ObjectIDFromHex(productId)
  134. if err != nil {
  135. return nil, errors.New("非法id")
  136. }
  137. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionProduct, productId)
  138. }