package api import ( "assetcenter/bus" "assetcenter/db/repo" "fmt" "time" "github.com/gin-gonic/gin" "go.mongodb.org/mongo-driver/bson" "infish.cn/comm" ) func TestRouter(r *GinRouter) { r.GET("/test/print", Printa) r.GET("/test/prinb", Printb) // r.GET("/test/print", Printa) r.GET("/test/cate", CateParse) } func CateParse(c *gin.Context, apictx *ApiSession) (interface{}, error) { // userId:6742fcd3446577969d26d97 // userId, _ := primitive.ObjectIDFromHex("6742fcd3446577969d26d97") dbName := "sku3d-tree" query := map[string]interface{}{ // "categories": []interface{}{"1735781110196"}, "categories": []interface{}{"61dcf7784b133be3f60fd6e0"}, // "userId": userId, } dbConf := &comm.AssetDbConf{} if query["categories"] != nil { enity := &comm.Database{} ok, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{ CollectName: repo.CollectionDatabase, Query: repo.Map{"name": dbName}, Project: []string{"categories"}, }, enity) if !ok { return nil, fmt.Errorf("没有查询到数据库:%s", dbName) } dbConf.Db = enity } bus.ParseCategories(query, apictx.CreateRepoCtx(), dbConf) return repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{ Db: dbName, CollectName: "company_shoe_user", Query: query, Page: 1, Size: 100, Sort: bson.M{"createTime": -1}, }) } func Printa(c *gin.Context, apictx *ApiSession) (interface{}, error) { // _, bset := GetIncrementTimes("day", "1h") // fmt.Println(bset) query := make(map[string]interface{}) b, _ := time.ParseDuration(fmt.Sprintf("%dh", -(24 * 10))) now := time.Now() end := now.Add(b) fmt.Println(end) query["createTime"] = bson.M{"$lte": end} parm := &repo.PageSearchOptions{ CollectName: "database", Project: []string{"_id", "name", "createTime"}, Sort: bson.M{"createTime": -1}, } parm.Query = query var assetCount []comm.Database err := repo.RepoDocsSearch(apictx.CreateRepoCtx(), parm, &assetCount) if err != nil { return nil, err } return assetCount[0], nil } func Printb(c *gin.Context, apictx *ApiSession) (interface{}, error) { query := make(map[string]interface{}) b, _ := time.ParseDuration(fmt.Sprintf("%dh", -(24 * 50))) now := time.Now() end := now.Add(b) fmt.Println(end) query["createTime"] = bson.M{"$lte": end} parm := &repo.DocSearchOptions{ CollectName: "database", Project: []string{"_id", "name", "createTime"}, Sort: bson.M{"createTime": -1}, // 降序 小于等于createTime中取时间最近的 第一条 } parm.Query = query var assetCount comm.Database _, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), parm, &assetCount) if err != nil { return nil, err } return assetCount, nil }