package api import ( "errors" "fmt" "moutai/db/repo" "time" "github.com/gin-gonic/gin" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" ) var clickMap = map[string]struct{}{ repo.CollectionImages: {}, repo.CollectionVideos: {}, repo.CollectionRelics: {}, repo.CollectionVr: {}, repo.CollectionAr: {}, } func Clicks(c *gin.Context, apictx *ApiSession) (interface{}, error) { ClickType := c.Query("type") id := c.Query("id") if _, ok := clickMap[ClickType]; ok { _, err := repo.RepoUpdateSetDocProps(apictx.CreateRepoCtx(), ClickType, id, bson.M{"$inc": bson.M{"clicks": 1}}) if err != nil { return false, err } return true, nil } return false, errors.New("参数错误!") } // sort自增 // 查询最大sort值 func getSortInt(apictx *ApiSession, collection string) int { out, err := repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{ CollectName: collection, Page: 1, Size: 1, // 降序,越大的在前面 Sort: repo.Map{"sort": -1}, }) if err != nil { return 0 } if len(out.List) < 1 { return 0 } sort := out.List[0]["sort"].(int32) fmt.Println("getSortInt: ", sort) return int(sort) } // 排序置顶 func SortTop(c *gin.Context, apictx *ApiSession) (interface{}, error) { // 根据id,type查询获取数据 ClickType := c.Query("type") id := c.Query("id") if _, ok := clickMap[ClickType]; ok { out, err := repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{ CollectName: ClickType, Page: 1, Size: 1, // 降序,越大的在前面 Sort: repo.Map{"sort": -1}, }) // 出错false if err != nil { return false, err } // 第一次插入没有数据 if len(out.List) < 1 { return true, nil } // 有数据 // 如果是自己 if id == out.List[0]["_id"].(primitive.ObjectID).Hex() { return true, nil } else { //如果不是自己 // 更新sort值为: 最大sort的值+1 sort := out.List[0]["sort"].(int32) + 1 _, err := repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), ClickType, id, bson.M{"sort": int(sort)}) if err != nil { return false, err } return true, nil } } return false, errors.New("参数错误") } // sort 上移 func SortPrev(c *gin.Context, apictx *ApiSession) (interface{}, error) { // 根据id,type查询获取数据 ClickType := c.Query("type") id := c.Query("id") if _, ok := clickMap[ClickType]; ok { // 获取当前数据的sort _, result := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{ CollectName: ClickType, Query: repo.Map{"_id": id}, }) currentSort := result["sort"].(int32) currentType := "" if v, ok := result["type"]; ok { currentType = v.(string) } query := repo.Map{"sort": repo.Map{"$gt": int(currentSort)}} if len(currentType) > 0 { query["type"] = currentType } // 获取当前数据的上一条数据的sort out, err := repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{ CollectName: ClickType, Query: query, Page: 1, Size: 1, // 升序,越小的在前面 Sort: repo.Map{"sort": 1}, }) // 出错false if err != nil { return false, err } // 没有数据 if len(out.List) < 1 { return true, nil } // 有数据 prevSort := out.List[0]["sort"].(int32) prevId := out.List[0]["_id"].(primitive.ObjectID) // 交换id _, err = repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), ClickType, id, bson.M{"sort": int(prevSort)}) if err != nil { return false, err } _, err = repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), ClickType, prevId.Hex(), bson.M{"sort": int(currentSort)}) if err != nil { return false, err } return true, nil } return false, errors.New("参数错误") } // sort 下移 func SortNext(c *gin.Context, apictx *ApiSession) (interface{}, error) { // 根据id,type查询获取数据 ClickType := c.Query("type") id := c.Query("id") if _, ok := clickMap[ClickType]; ok { // 获取当前数据的sort _, result := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{ CollectName: ClickType, Query: repo.Map{"_id": id}, }) currentSort := result["sort"].(int32) currentType := "" if v, ok := result["type"]; ok { currentType = v.(string) } query := repo.Map{"sort": repo.Map{"$lt": int(currentSort)}} if len(currentType) > 0 { query["type"] = currentType } // 获取当前数据的上一条数据的sort out, err := repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{ CollectName: ClickType, Query: query, Page: 1, Size: 1, // 降序,越大的在前面 Sort: repo.Map{"sort": -1}, }) // 出错false if err != nil { return false, err } // 没有数据 if len(out.List) < 1 { return true, nil } // 有数据 nextSort := out.List[0]["sort"].(int32) nextId := out.List[0]["_id"].(primitive.ObjectID) // 交换id _, err = repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), ClickType, id, bson.M{"sort": int(nextSort)}) if err != nil { return false, err } _, err = repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), ClickType, nextId.Hex(), bson.M{"sort": int(currentSort)}) if err != nil { return false, err } return true, nil } return false, errors.New("参数错误") } func getQuery(query map[string]interface{}) (map[string]interface{}, map[string]interface{}) { if _cid, ok := query["cid"]; ok { delete(query, "cid") cid, _ := primitive.ObjectIDFromHex(_cid.(string)) if !cid.IsZero() { query["cid"] = cid } } // query name查询 if _name, ok := query["name"]; ok { name := _name.(string) delete(query, "name") if len(name) > 0 { query["name"] = bson.M{"$regex": name, "$options": "$i"} } } // query uploadTime查询 if _uploadTime, ok := query["uploadTime"]; ok { uploadTime := _uploadTime.(string) delete(query, "uploadTime") if len(uploadTime) > 0 { uploadTime, err := time.Parse("2006-01-02", uploadTime) if err == nil { startTime := time.Date(uploadTime.Year(), uploadTime.Month(), uploadTime.Day(), 0, 0, 0, 0, uploadTime.Location()) endTime := time.Date(uploadTime.Year(), uploadTime.Month(), uploadTime.Day(), 23, 59, 59, 0, uploadTime.Location()) query["createTime"] = bson.M{ "$gte": startTime, "$lte": endTime, } } } } sortMap := repo.Map{} // query sort查询 if _sort, ok := query["sort"]; ok { // clicks 排序 _dclicks := -1 if _sort.(string) == "clicks" { sortMap = repo.Map{"clicks": &_dclicks} } // sort int自定义排序 if _sort.(string) == "sort" { sortMap = repo.Map{"sort": -1} } delete(query, "sort") } fmt.Printf("查询条件:\nquery:%#v\nsortMap: %#v\n", query, sortMap) return query, sortMap }