collect.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package api
  2. import (
  3. "3dshow/db/model"
  4. "3dshow/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. c.ShouldBindJSON(entity)
  16. entity.CreateTime = time.Now()
  17. _userId := apictx.User.ID
  18. userId, err := primitive.ObjectIDFromHex(_userId)
  19. if err != nil {
  20. return nil, errors.New("非法用户")
  21. }
  22. entity.UserId = userId
  23. return entity, nil
  24. },
  25. EmtyModel: func(c *gin.Context, apictx *ApiSession) interface{} {
  26. return &model.Collect{}
  27. },
  28. JWT: true,
  29. SearchFilter: func(c *gin.Context, apictx *ApiSession, query map[string]interface{}) map[string]interface{} {
  30. _userId := apictx.User.ID
  31. userId, err := primitive.ObjectIDFromHex(_userId)
  32. if err != nil {
  33. // 6369f4b028c4bf8b14f47a6b
  34. invalidId, _ := primitive.ObjectIDFromHex("6369f4b028c4bf8b14f47a6b")
  35. return repo.Map{"userId": invalidId}
  36. }
  37. return repo.Map{"userId": userId}
  38. },
  39. SearchPostProcess: func(page *repo.PageResult, c *gin.Context, apictx *ApiSession, query map[string]interface{}) (interface{}, error) {
  40. // 查询组装数据
  41. if len(page.List) > 0 {
  42. for _, v := range page.List {
  43. // 查询产品信息
  44. productId := v["productId"].(primitive.ObjectID)
  45. supplyId := v["supplyId"].(primitive.ObjectID)
  46. product := model.Product{}
  47. supply := model.Supply{}
  48. repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  49. CollectName: repo.CollectionProduct,
  50. Query: repo.Map{"_id": productId},
  51. Project: []string{"name", "unit", "cover"},
  52. }, &product)
  53. repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  54. CollectName: repo.CollectionSupply,
  55. Query: repo.Map{"_id": supplyId},
  56. Project: []string{"name"},
  57. }, &supply)
  58. v["productName"] = product.Name
  59. v["productUnit"] = product.Unit
  60. v["productCover"] = product.Cover
  61. v["supplyName"] = supply.Name
  62. }
  63. }
  64. return page, nil
  65. },
  66. })
  67. }