|
@@ -8,6 +8,7 @@ import (
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gin-gonic/gin"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
|
+ "go.mongodb.org/mongo-driver/bson/primitive"
|
|
)
|
|
)
|
|
|
|
|
|
var clickMap = map[string]struct{}{
|
|
var clickMap = map[string]struct{}{
|
|
@@ -33,6 +34,162 @@ func Clicks(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// sort自增
|
|
|
|
+// 查询最大sort值
|
|
|
|
+func getSortInt(apictx *ApiSession, collection string) int {
|
|
|
|
+ out, err := repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
|
|
|
|
+ CollectName: collection,
|
|
|
|
+ Page: 1,
|
|
|
|
+ Size: 2,
|
|
|
|
+ // 降序,越大的在前面
|
|
|
|
+ Sort: repo.Map{"Sort": -1},
|
|
|
|
+ })
|
|
|
|
+ if err != nil {
|
|
|
|
+ return 0
|
|
|
|
+ }
|
|
|
|
+ if len(out.List) < 1 {
|
|
|
|
+ return 0
|
|
|
|
+ }
|
|
|
|
+ sort := out.List[0]["sort"].(int)
|
|
|
|
+ fmt.Println("getSortInt: ", sort)
|
|
|
|
+ return 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: 2,
|
|
|
|
+ // 降序,越大的在前面
|
|
|
|
+ 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"].(int) + 1
|
|
|
|
+ _, err := repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), ClickType, id, bson.M{"sort": 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"].(int)
|
|
|
|
+ // 获取当前数据的上一条数据的sort
|
|
|
|
+ out, err := repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
|
|
|
|
+ CollectName: ClickType,
|
|
|
|
+ Query: repo.Map{"sort": repo.Map{"$gt": currentSort}},
|
|
|
|
+ Page: 1,
|
|
|
|
+ Size: 2,
|
|
|
|
+ // 升序,越小的在前面
|
|
|
|
+ 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"].(int)
|
|
|
|
+ prevId := out.List[0]["sort"].(primitive.ObjectID)
|
|
|
|
+ // 交换id
|
|
|
|
+ _, err = repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), ClickType, id, bson.M{"sort": prevSort})
|
|
|
|
+ if err != nil {
|
|
|
|
+ return false, err
|
|
|
|
+ }
|
|
|
|
+ _, err = repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), ClickType, prevId.Hex(), bson.M{"sort": 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"].(int)
|
|
|
|
+ // 获取当前数据的上一条数据的sort
|
|
|
|
+ out, err := repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
|
|
|
|
+ CollectName: ClickType,
|
|
|
|
+ Query: repo.Map{"sort": repo.Map{"$lt": currentSort}},
|
|
|
|
+ Page: 1,
|
|
|
|
+ Size: 2,
|
|
|
|
+ // 降序,越大的在前面
|
|
|
|
+ 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"].(int)
|
|
|
|
+ prevId := out.List[0]["sort"].(primitive.ObjectID)
|
|
|
|
+ // 交换id
|
|
|
|
+ _, err = repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), ClickType, id, bson.M{"sort": prevSort})
|
|
|
|
+ if err != nil {
|
|
|
|
+ return false, err
|
|
|
|
+ }
|
|
|
|
+ _, err = repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), ClickType, prevId.Hex(), bson.M{"sort": 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{}) {
|
|
func getQuery(query map[string]interface{}) (map[string]interface{}, map[string]interface{}) {
|
|
// query name查询
|
|
// query name查询
|
|
if _name, ok := query["name"]; ok {
|
|
if _name, ok := query["name"]; ok {
|
|
@@ -59,7 +216,7 @@ func getQuery(query map[string]interface{}) (map[string]interface{}, map[string]
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- sortMap := repo.Map{"createTime": -1}
|
|
|
|
|
|
+ sortMap := repo.Map{}
|
|
// query sort查询
|
|
// query sort查询
|
|
if _sort, ok := query["sort"]; ok {
|
|
if _sort, ok := query["sort"]; ok {
|
|
// clicks 排序
|
|
// clicks 排序
|
|
@@ -68,9 +225,8 @@ func getQuery(query map[string]interface{}) (map[string]interface{}, map[string]
|
|
sortMap = repo.Map{"clicks": &_dclicks}
|
|
sortMap = repo.Map{"clicks": &_dclicks}
|
|
}
|
|
}
|
|
// sort int自定义排序
|
|
// sort int自定义排序
|
|
- _dsort := 1
|
|
|
|
if _sort.(string) == "sort" {
|
|
if _sort.(string) == "sort" {
|
|
- sortMap = repo.Map{"sort": &_dsort}
|
|
|
|
|
|
+ sortMap = repo.Map{"sort": -1}
|
|
|
|
|
|
}
|
|
}
|
|
delete(query, "sort")
|
|
delete(query, "sort")
|