Item.go 4.44 KB
package Api

import (
	"encoding/json"
	"github.com/aarongao/tools"
	"github.com/gin-gonic/gin"
	"gopkg.in/mgo.v2/bson"
	"letu/DB"
)

// @Title 查询设备信息
// @Description 查询设备信息
// @Accept  json
// @Produce  json
// @Param   id     5dfb03070a9ac17ac7a82054    string     true        "设备id"
// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":{"Name":"名称","Describe":"介绍","OpenHours":"开放时间","Mobile":"电话","Address":"地址","SLocation":{"Latitude":0,"Longitude":0},"Picture":["照片1","照片2"]}}"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /ItemInfo? [get]
func ItemInfo(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 SItem *DB.SItem
	DB.CItem.Find(bson.M{"_id": bson.ObjectIdHex(c.Query("id"))}).One(&SItem)

	if SItem == nil {

		c.JSON(200, tools.ResponseError{
			1,
			"空",
		})
	} else {

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

// @Title 查询所有游玩项目
// @Description 查询所有游玩项目
// @Accept  json
// @Produce  json
// @Success 200 {object} tools.ResponseSeccess "Tags所属标签,标签有分类;LimitHeight限高;PlayDuration游玩时长;SceneTime场次时间;Picture照片;Voice音频;AverageConsumption平均消费;Menu菜单"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /AllItems? [get]
func AllItems(c *gin.Context) {
	c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
	c.Header("Access-Control-Allow-Credentials", "true")

	var aItems []DB.SItem
	DB.CItem.Find(bson.M{}).All(&aItems)

	c.JSON(200, aItems)

}

// @Title 更新设施
// @Description 更新设施
// @Accept  json
// @Produce  json
// @Success 200 {object} tools.ResponseSeccess "Tags所属标签,标签有分类;LimitHeight限高;PlayDuration游玩时长;SceneTime场次时间;Picture照片;Voice音频;AverageConsumption平均消费;Menu菜单"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /UpdateItem? [post]
func UpdateItem(c *gin.Context) {
	c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
	c.Header("Access-Control-Allow-Credentials", "true")

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

	var Tags []DB.STag
	json.Unmarshal([]byte(c.PostForm("Tags")), &Tags)

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

	var id bson.ObjectId
	if pid := c.PostForm("id"); pid == "null" {
		id = bson.NewObjectId()
	} else {
		id = bson.ObjectIdHex(pid)
	}

	DB.CItem.UpsertId(
		id,
		bson.M{"$set": bson.M{
			"Name":               c.PostForm("Name"),
			"SubName":            c.PostForm("SubName"),
			"Location":           Location,
			"Icon":               c.PostForm("Icon"),
			"LimitHeight":        c.PostForm("LimitHeight"),
			"PlayDuration":       c.PostForm("PlayDuration"),
			"SceneTime":          c.PostForm("SceneTime"),
			"Picture":            Picture,
			"Voice":              c.PostForm("Voice"),
			"Tel":                c.PostForm("Tel"),
			"AverageConsumption": c.PostForm("AverageConsumption"),
			"Menu":               c.PostForm("Menu"),
			"Tags":               Tags,
		}},
	)

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

}

type ItemTime struct {
	Id   string `json:"id"`
	Time string `json:"time"`
}


// @Title 更新等待时间
// @Description 更新等待时间
// @Accept  json
// @Produce  json
// @Param   item     [{"id":"5df864740a9ac17ac7a7feb8","time":"20"},{"id":"5df8660924e03417008b4567","time":"33"}]    string     true        "设备列表"
// @Success 200 {object} tools.ResponseSeccess "{errcode: 0, result: "ok"}"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /UpdateItemTime? [post]
func UpdateItemTime(c *gin.Context) {
	c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
	c.Header("Access-Control-Allow-Credentials", "true")

	var ItemTime []ItemTime
	json.Unmarshal([]byte(c.PostForm("items")), &ItemTime)

	for _, v := range ItemTime {

		DB.CItem.Update(
			bson.M{"_id": bson.ObjectIdHex(v.Id)},
			bson.M{"$set": bson.M{
				"Time": v.Time,
			}},
		)
	}

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

}