OperatorLog.go 1.68 KB
package Api

import (
	"github.com/aarongao/tools"
	"github.com/gin-gonic/gin"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo/options"
	"letu/DB"
	"math"
	"strconv"
)

// @Title 查询所有管理员日志
// @Description 查询所有用户行为
// @Accept  json
// @Produce  json
// @Param   ScenicId     5dfb03070a9ac17ac7a82054    string     true        "景区id"
// @Param   Page     1    int     true        "当前第几页"
// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"total":1,"currpage":1,"totalpages":1,"prepage":20,"result":}"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /AllOperatorLog? [get]
func AllOperatorLog(c *gin.Context) {
	c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
	c.Header("Access-Control-Allow-Credentials", "true")

	total, _ := DB.COperatorLog.CountDocuments(tools.GetContext(), bson.M{"ScenicId": c.Query("ScenicId")})
	limit, _ := strconv.ParseInt(c.Query("Limit"), 10, 64)
	if limit == 0 {
		limit = 50
	}
	currPage, _ := strconv.ParseInt(c.Query("Page"), 10, 64)
	if currPage == 0 {
		currPage = 1
	}

	skip := (currPage - 1) * limit

	var aOperatorLog []DB.SOperatorLog
	cur, err := DB.COperatorLog.Find(tools.GetContext(), bson.M{"ScenicId": c.Query("ScenicId")}, &options.FindOptions{Limit: &limit, Skip: &skip, Sort: bson.M{"_id": -1}})
	defer cur.Close(tools.GetContext())
	if err == nil {
		for cur.Next(tools.GetContext()) {
			var e DB.SOperatorLog
			cur.Decode(&e)
			aOperatorLog = append(aOperatorLog, e)
		}
	}

	c.JSON(200, tools.Page{
		0,
		total,
		currPage,
		int64(math.Ceil(float64(total) / float64(limit))),
		limit,
		aOperatorLog,
	})

}