sun-pc-linux před 4 měsíci
rodič
revize
bb2260ebc8

+ 8 - 8
src/api/ar.go

@@ -13,22 +13,22 @@ import (
 
 func CreateAr(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 
-	Ar := &model.Ar{}
-	err := c.ShouldBindJSON(&Ar)
+	ar := &model.Ar{}
+	err := c.ShouldBindJSON(&ar)
 	if err != nil {
 		log.Error(err)
 		return nil, err
 	}
 
 	defuealt := 0
-	Ar.Clicks = &defuealt
-	Ar.Sort = &defuealt
+	ar.Clicks = &defuealt
+	ar.Sort = getSortInt(apictx, repo.CollectionAr) + 1
 	// 正常状态
 	state := -1
-	Ar.State = &state
-	Ar.CreateTime = time.Now()
-	Ar.UpdateTime = time.Now()
-	return repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionAr, &Ar)
+	ar.State = &state
+	ar.CreateTime = time.Now()
+	ar.UpdateTime = time.Now()
+	return repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionAr, &ar)
 }
 
 func DeleteAr(c *gin.Context, apictx *ApiSession) (interface{}, error) {

+ 159 - 3
src/api/comm.go

@@ -8,6 +8,7 @@ import (
 
 	"github.com/gin-gonic/gin"
 	"go.mongodb.org/mongo-driver/bson"
+	"go.mongodb.org/mongo-driver/bson/primitive"
 )
 
 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{}) {
 	// query name查询
 	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查询
 	if _sort, ok := query["sort"]; ok {
 		// clicks 排序
@@ -68,9 +225,8 @@ func getQuery(query map[string]interface{}) (map[string]interface{}, map[string]
 			sortMap = repo.Map{"clicks": &_dclicks}
 		}
 		// sort int自定义排序
-		_dsort := 1
 		if _sort.(string) == "sort" {
-			sortMap = repo.Map{"sort": &_dsort}
+			sortMap = repo.Map{"sort": -1}
 
 		}
 		delete(query, "sort")

+ 8 - 8
src/api/image.go

@@ -13,22 +13,22 @@ import (
 
 func CreateImage(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 
-	Image := &model.Images{}
-	err := c.ShouldBindJSON(&Image)
+	image := &model.Images{}
+	err := c.ShouldBindJSON(&image)
 	if err != nil {
 		log.Error(err)
 		return nil, err
 	}
 	defuealt := 0
-	Image.Clicks = &defuealt
-	Image.Sort = &defuealt
+	image.Clicks = &defuealt
+	image.Sort = getSortInt(apictx, repo.CollectionImages) + 1
 
 	// 正常状态
 	state := -1
-	Image.State = &state
-	Image.CreateTime = time.Now()
-	Image.UpdateTime = time.Now()
-	return repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionImages, &Image)
+	image.State = &state
+	image.CreateTime = time.Now()
+	image.UpdateTime = time.Now()
+	return repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionImages, &image)
 }
 
 func DeleteImage(c *gin.Context, apictx *ApiSession) (interface{}, error) {

+ 8 - 9
src/api/relic.go

@@ -12,22 +12,21 @@ import (
 )
 
 func CreateRelic(c *gin.Context, apictx *ApiSession) (interface{}, error) {
-
-	Relic := &model.Relics{}
-	err := c.ShouldBindJSON(&Relic)
+	relic := &model.Relics{}
+	err := c.ShouldBindJSON(&relic)
 	if err != nil {
 		log.Error(err)
 		return nil, err
 	}
 	defuealt := 0
-	Relic.Clicks = &defuealt
-	Relic.Sort = &defuealt
+	relic.Clicks = &defuealt
+	relic.Sort = getSortInt(apictx, repo.CollectionRelics) + 1
 	// 正常状态
 	state := -1
-	Relic.State = &state
-	Relic.CreateTime = time.Now()
-	Relic.UpdateTime = time.Now()
-	return repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionRelics, &Relic)
+	relic.State = &state
+	relic.CreateTime = time.Now()
+	relic.UpdateTime = time.Now()
+	return repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionRelics, &relic)
 }
 
 func DeleteRelic(c *gin.Context, apictx *ApiSession) (interface{}, error) {

+ 4 - 1
src/api/router.go

@@ -96,8 +96,11 @@ func RegRouters(svc *Service) {
 	// 下架
 	moutai.POSTJWT("/admin/ar/off/:id", ArOff)
 
-	// 点击次数
+	// 排序操作
 	moutai.GET("/click", Clicks)
+	moutai.GET("/sort/top", SortTop)
+	moutai.GET("/sort/prev", SortPrev)
+	moutai.GET("/sort/next", SortNext)
 
 	// 获取token
 	moutai.GET("/getToken", getToken)

+ 8 - 9
src/api/video.go

@@ -12,23 +12,22 @@ import (
 )
 
 func CreateVideo(c *gin.Context, apictx *ApiSession) (interface{}, error) {
-
-	Video := &model.Videos{}
-	err := c.ShouldBindJSON(&Video)
+	video := &model.Videos{}
+	err := c.ShouldBindJSON(&video)
 	if err != nil {
 		log.Error(err)
 		return nil, err
 	}
 
 	defuealt := 0
-	Video.Clicks = &defuealt
-	Video.Sort = &defuealt
+	video.Clicks = &defuealt
+	video.Sort = getSortInt(apictx, repo.CollectionVideos) + 1
 	// 正常状态
 	state := -1
-	Video.State = &state
-	Video.CreateTime = time.Now()
-	Video.UpdateTime = time.Now()
-	return repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionVideos, &Video)
+	video.State = &state
+	video.CreateTime = time.Now()
+	video.UpdateTime = time.Now()
+	return repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionVideos, &video)
 }
 
 func DeleteVideo(c *gin.Context, apictx *ApiSession) (interface{}, error) {

+ 8 - 8
src/api/vr.go

@@ -13,22 +13,22 @@ import (
 
 func CreateVr(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 
-	Vr := &model.Vr{}
-	err := c.ShouldBindJSON(&Vr)
+	vr := &model.Vr{}
+	err := c.ShouldBindJSON(&vr)
 	if err != nil {
 		log.Error(err)
 		return nil, err
 	}
 
 	defuealt := 0
-	Vr.Clicks = &defuealt
-	Vr.Sort = &defuealt
+	vr.Clicks = &defuealt
+	vr.Sort = getSortInt(apictx, repo.CollectionVideos) + 1
 	// 正常状态
 	state := -1
-	Vr.State = &state
-	Vr.CreateTime = time.Now()
-	Vr.UpdateTime = time.Now()
-	return repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionVr, &Vr)
+	vr.State = &state
+	vr.CreateTime = time.Now()
+	vr.UpdateTime = time.Now()
+	return repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionVr, &vr)
 }
 
 func DeleteVr(c *gin.Context, apictx *ApiSession) (interface{}, error) {

+ 1 - 1
src/db/model/ar.go

@@ -15,7 +15,7 @@ type Ar struct {
 	AdminUrl string             `bson:"adminUrl,omitempty" json:"adminUrl,omitempty"`
 
 	Clicks *int `bson:"clicks,omitempty" json:"clicks,omitempty"`
-	Sort   *int `bson:"sort,omitempty" json:"sort,omitempty"`
+	Sort   int  `bson:"sort,omitempty" json:"sort,omitempty"`
 
 	CreateTime time.Time `bson:"createTime,omitempty" json:"createTime,omitempty"`
 	UpdateTime time.Time `bson:"updateTime,omitempty" json:"updateTime,omitempty"`

+ 1 - 1
src/db/model/image.go

@@ -13,7 +13,7 @@ type Images struct {
 	Name   string             `bson:"name,omitempty" json:"name,omitempty"`
 	Desc   string             `bson:"desc,omitempty" json:"desc,omitempty"`
 	Clicks *int               `bson:"clicks,omitempty" json:"clicks,omitempty"`
-	Sort   *int               `bson:"sort,omitempty" json:"sort,omitempty"`
+	Sort   int                `bson:"sort,omitempty" json:"sort,omitempty"`
 
 	CreateTime time.Time `bson:"createTime,omitempty" json:"createTime,omitempty"`
 	UpdateTime time.Time `bson:"updateTime,omitempty" json:"updateTime,omitempty"`

+ 1 - 1
src/db/model/relic.go

@@ -17,7 +17,7 @@ type Relics struct {
 	Document *Document          `bson:"document,omitempty" json:"document,omitempty"`
 
 	Clicks *int `bson:"clicks,omitempty" json:"clicks,omitempty"`
-	Sort   *int `bson:"sort,omitempty" json:"sort,omitempty"`
+	Sort   int  `bson:"sort,omitempty" json:"sort,omitempty"`
 
 	CreateTime time.Time `bson:"createTime,omitempty" json:"createTime,omitempty"`
 	UpdateTime time.Time `bson:"updateTime,omitempty" json:"updateTime,omitempty"`

+ 1 - 1
src/db/model/video.go

@@ -14,7 +14,7 @@ type Videos struct {
 	Name   string             `bson:"name,omitempty" json:"name,omitempty"`
 	Desc   string             `bson:"desc,omitempty" json:"desc,omitempty"`
 	Clicks *int               `bson:"clicks,omitempty" json:"clicks,omitempty"`
-	Sort   *int               `bson:"sort,omitempty" json:"sort,omitempty"`
+	Sort   int                `bson:"sort,omitempty" json:"sort,omitempty"`
 
 	CreateTime time.Time `bson:"createTime,omitempty" json:"createTime,omitempty"`
 	UpdateTime time.Time `bson:"updateTime,omitempty" json:"updateTime,omitempty"`

+ 1 - 1
src/db/model/vr.go

@@ -17,7 +17,7 @@ type Vr struct {
 
 	Desc   string `bson:"desc,omitempty" json:"desc,omitempty"`
 	Clicks *int   `bson:"clicks,omitempty" json:"clicks,omitempty"`
-	Sort   *int   `bson:"sort,omitempty" json:"sort,omitempty"`
+	Sort   int    `bson:"sort,omitempty" json:"sort,omitempty"`
 
 	CreateTime time.Time `bson:"createTime,omitempty" json:"createTime,omitempty"`
 	UpdateTime time.Time `bson:"updateTime,omitempty" json:"updateTime,omitempty"`