Line.go 3.88 KB
package Api

import (
	"encoding/json"
	"github.com/aarongao/tools"
	"github.com/gin-gonic/gin"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"go.mongodb.org/mongo-driver/mongo/options"
	"letu/DB"
	"letu/Lib/Auth"
)

// @Title 查询线路信息
// @Description 查询线路信息
// @Accept  json
// @Produce  json
// @Param   id     5dfb03070a9ac17ac7a82054    string     true        "id"
// @Success 200 {object} tools.ResponseSeccess "Name名称;SubName副标题;PlayDuration游玩时长;Suitable适合人群;Location线路点坐标;Annotations需要点亮的设施id;Distance距离"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /LineInfo? [get]
func LineInfo(c *gin.Context) {
	c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
	c.Header("Access-Control-Allow-Credentials", "true")

	if c.Query("id") == "" {
		c.JSON(200, tools.ResponseError{
			1,
			"空",
		})
		return
	}

	var SLine DB.SLine
	objID, _ := primitive.ObjectIDFromHex(c.Query("id"))
	DB.CLine.FindOne(tools.GetContext(), bson.M{"_id": objID}).Decode(&SLine)

	c.JSON(200, tools.ResponseSeccess{
		0,
		SLine,
	})

}

// @Title 查询所有线路
// @Description 查询所有线路
// @Accept  json
// @Produce  json
// @Success 200 {object} tools.ResponseSeccess "Name名称;SubName副标题;PlayDuration游玩时长;Suitable适合人群;Location线路点坐标;Annotations需要点亮的设施id;Distance距离"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /AllLine? [get]
func AllLine(c *gin.Context) {
	c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
	c.Header("Access-Control-Allow-Credentials", "true")

	var ScenicId string
	if  ScenicId = c.Query("ScenicId");ScenicId == ""{
		ScenicId = "5e0d504e24e03431008b4567" // 乐岛
	}

	var aLine []DB.SLine
	cur, err := DB.CLine.Find(tools.GetContext(), bson.M{"ScenicId": ScenicId})
	defer cur.Close(tools.GetContext())
	if err == nil {
		for cur.Next(tools.GetContext()) {
			var e DB.SLine
			cur.Decode(&e)
			aLine = append(aLine, e)
		}
	}

	c.JSON(200, aLine)

}

// @Title 更新线路
// @Description 更新线路
// @Accept  json
// @Produce  json
// @Param   id     5dfb03070a9ac17ac7a82054    string     true        "线路id"
// @Param   ScenicId     5dfb03070a9ac17ac7a82054    string     true        "景区id"
// @Param   Token     wgergejfwe    string     true        "用户token"
// @Success 200 {object} tools.ResponseSeccess ""
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /UpdateLine? [post]
func UpdateLine(c *gin.Context) {

	c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
	c.Header("Access-Control-Allow-Credentials", "true")

	_user, _ := c.Get("UserInfo")
	user := _user.(*DB.SMember)
	err := Auth.CheckScenicAuth(c.PostForm("ScenicId"), user)
	if err != nil {
		c.JSON(200, tools.ResponseError{
			401,
			"没有权限",
		})
		return
	}


	var Location []DB.SLocation
	json.Unmarshal([]byte(c.PostForm("Location")), &Location)

	var Annotations []string
	json.Unmarshal([]byte(c.PostForm("Annotations")), &Annotations)

	var id primitive.ObjectID
	if pid := c.PostForm("id"); pid == "null" {
		id = primitive.NewObjectID()
	} else {
		id, _ = primitive.ObjectIDFromHex(pid)
	}

	upsert := true
	DB.CLine.FindOneAndUpdate(tools.GetContext(),
		bson.M{"_id": id},
		bson.M{"$set": bson.M{
			"Name":         c.PostForm("Name"),
			"SubName":      c.PostForm("SubName"),
			"PlayDuration": c.PostForm("PlayDuration"),
			"Suitable":     c.PostForm("Suitable"),
			"Content":      c.PostForm("Content"),
			"Distance":     c.PostForm("Distance"),
			"Annotations":  Annotations,
			"Location":     Location,
		}}, &options.FindOneAndUpdateOptions{
			Upsert: &upsert,
		},
	)

	c.JSON(200, tools.ResponseSeccess{
		0,
		"ok",
	})

}