service-test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package api
  2. import (
  3. "assetcenter/bus"
  4. "assetcenter/db/repo"
  5. "fmt"
  6. "time"
  7. "github.com/gin-gonic/gin"
  8. "go.mongodb.org/mongo-driver/bson"
  9. "infish.cn/comm"
  10. )
  11. func TestRouter(r *GinRouter) {
  12. r.GET("/test/print", Printa)
  13. r.GET("/test/prinb", Printb)
  14. // r.GET("/test/print", Printa)
  15. r.GET("/test/cate", CateParse)
  16. }
  17. func CateParse(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  18. // userId:6742fcd3446577969d26d97
  19. // userId, _ := primitive.ObjectIDFromHex("6742fcd3446577969d26d97")
  20. dbName := "sku3d-tree"
  21. query := map[string]interface{}{
  22. // "categories": []interface{}{"1735781110196"},
  23. "categories": []interface{}{"61dcf7784b133be3f60fd6e0"},
  24. // "userId": userId,
  25. }
  26. dbConf := &comm.AssetDbConf{}
  27. if query["categories"] != nil {
  28. enity := &comm.Database{}
  29. ok, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  30. CollectName: repo.CollectionDatabase,
  31. Query: repo.Map{"name": dbName},
  32. Project: []string{"categories"},
  33. }, enity)
  34. if !ok {
  35. return nil, fmt.Errorf("没有查询到数据库:%s", dbName)
  36. }
  37. dbConf.Db = enity
  38. }
  39. bus.ParseCategories(query, apictx.CreateRepoCtx(), dbConf)
  40. return repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  41. Db: dbName,
  42. CollectName: "company_shoe_user",
  43. Query: query,
  44. Page: 1,
  45. Size: 100,
  46. Sort: bson.M{"createTime": -1},
  47. })
  48. }
  49. func Printa(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  50. // _, bset := GetIncrementTimes("day", "1h")
  51. // fmt.Println(bset)
  52. query := make(map[string]interface{})
  53. b, _ := time.ParseDuration(fmt.Sprintf("%dh", -(24 * 10)))
  54. now := time.Now()
  55. end := now.Add(b)
  56. fmt.Println(end)
  57. query["createTime"] = bson.M{"$lte": end}
  58. parm := &repo.PageSearchOptions{
  59. CollectName: "database",
  60. Project: []string{"_id", "name", "createTime"},
  61. Sort: bson.M{"createTime": -1},
  62. }
  63. parm.Query = query
  64. var assetCount []comm.Database
  65. err := repo.RepoDocsSearch(apictx.CreateRepoCtx(), parm, &assetCount)
  66. if err != nil {
  67. return nil, err
  68. }
  69. return assetCount[0], nil
  70. }
  71. func Printb(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  72. query := make(map[string]interface{})
  73. b, _ := time.ParseDuration(fmt.Sprintf("%dh", -(24 * 50)))
  74. now := time.Now()
  75. end := now.Add(b)
  76. fmt.Println(end)
  77. query["createTime"] = bson.M{"$lte": end}
  78. parm := &repo.DocSearchOptions{
  79. CollectName: "database",
  80. Project: []string{"_id", "name", "createTime"},
  81. Sort: bson.M{"createTime": -1}, // 降序 小于等于createTime中取时间最近的 第一条
  82. }
  83. parm.Query = query
  84. var assetCount comm.Database
  85. _, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), parm, &assetCount)
  86. if err != nil {
  87. return nil, err
  88. }
  89. return assetCount, nil
  90. }