123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- 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
- }
|