collect.go 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package api
  2. import (
  3. "3dshow-supplier/db/model"
  4. "3dshow-supplier/db/repo"
  5. "errors"
  6. "time"
  7. "github.com/gin-gonic/gin"
  8. "go.mongodb.org/mongo-driver/bson/primitive"
  9. )
  10. func Collect(r *GinRouter) {
  11. CreateCRUD(r, "/collect", &CRUDOption{
  12. Collection: repo.CollectionCollect,
  13. NewModel: func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  14. entity := &model.Collect{}
  15. err := c.ShouldBindJSON(entity)
  16. if err != nil {
  17. return nil, errors.New("参数错误")
  18. }
  19. if len(entity.SupplyId) < 12 {
  20. return nil, errors.New("供应链id错误")
  21. }
  22. if len(entity.ProductId) < 12 {
  23. return nil, errors.New("产品id错误")
  24. }
  25. entity.CreateTime = time.Now()
  26. _userId := apictx.User.ID
  27. userId, _ := primitive.ObjectIDFromHex(_userId)
  28. // 同一用户对同一产品只能有一条收藏记录
  29. // 联合唯一索引 db.collect.ensureIndex({userId:1,productId:1},{unique:true})
  30. opt := &repo.DocSearchOptions{
  31. CollectName: repo.CollectionCollect,
  32. Query: repo.Map{"userId": userId, "productId": entity.ProductId},
  33. }
  34. collect := &model.Collect{}
  35. found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), opt, &collect)
  36. if found {
  37. return entity, errors.New("该物品已被收藏")
  38. }
  39. entity.UserId = userId
  40. return entity, nil
  41. },
  42. EmtyModel: func(c *gin.Context, apictx *ApiSession) interface{} {
  43. return &model.Collect{}
  44. },
  45. JWT: true,
  46. SearchFilter: func(_ *gin.Context, apictx *ApiSession, query map[string]interface{}) map[string]interface{} {
  47. _userId := apictx.User.ID
  48. userId, err := primitive.ObjectIDFromHex(_userId)
  49. if err != nil {
  50. // 6369f4b028c4bf8b14f47a6b
  51. invalidId, _ := primitive.ObjectIDFromHex("6369f4b028c4bf8b14f47a6b")
  52. query["userId"] = invalidId
  53. return query
  54. }
  55. query["userId"] = userId
  56. return query
  57. },
  58. SearchPostProcess: func(page *repo.PageResult, _ *gin.Context, apictx *ApiSession, _ map[string]interface{}) (interface{}, error) {
  59. // 查询组装数据
  60. if len(page.List) > 0 {
  61. for _, v := range page.List {
  62. // 查询产品信息
  63. productId := v["productId"].(primitive.ObjectID)
  64. supplyId := v["supplyId"].(primitive.ObjectID)
  65. product := model.Product{}
  66. supply := model.Supply{}
  67. repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  68. CollectName: repo.CollectionProduct,
  69. Query: repo.Map{"_id": productId},
  70. Project: []string{"name", "unit", "cover"},
  71. }, &product)
  72. // 供应商信息
  73. repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  74. CollectName: repo.CollectionSupply,
  75. Query: repo.Map{"_id": supplyId},
  76. Project: []string{"name", "avatar", "contact"},
  77. }, &supply)
  78. v["productName"] = product.Name
  79. v["productUnit"] = product.Unit
  80. v["productCover"] = product.Cover
  81. v["supplyName"] = supply.Name
  82. v["supplyAvatar"] = supply.Avatar
  83. v["supplyContact"] = supply.Contact
  84. }
  85. }
  86. return page, nil
  87. },
  88. })
  89. }