Scenic.go 9.05 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"
	"strconv"
)

// @Title 返回景区基础信息
// @Description 返回景区基础信息
// @Accept  json
// @Produce  json
// @Param   id     5dfb03070a9ac17ac7a82054    string     true        "景区id"
// @Success 200 {object} tools.ResponseSeccess "Name名称;Describe介绍;OpenHours营业时间;Picture最上面图片;ShopAdPicture商城列表页图片;ItemScenicPicture项目场次照片;ActivityPicture活动照片;VideoList视频(VideoPicture=首桢图片);InvestigationUrl问券调查的url;RangeLocation景区范围(多个坐标点)ZoomForIOS地图的缩放大小(IOS);ZoomForAndroidMin地图的缩放大小(Android最小);ZoomForAndroidMax地图的缩放大小(Android最大);Rotation旋转角度;OpenTiles否开启地图切片;ColorTiles切片底色(#FFFFFF)"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /ScenicInfo? [get]
func ScenicInfo(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 Scenic *DB.SScenic
	objID, _ := primitive.ObjectIDFromHex(c.Query("id"))
	DB.CScenic.FindOne(tools.GetContext(), bson.M{"_id": objID}).Decode(&Scenic)

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

}

// @Title 更新景区基础信息
// @Description 更新景区基础信息
// @Accept  json
// @Produce  json
// @Param   id     5dfb03070a9ac17ac7a82054    string     true        "景区id"
// @Param   Token     wgergejfwe    string     true        "用户token"
// @Success 200 {object} tools.ResponseSeccess "Name名称;Describe介绍;OpenHours营业时间;Picture最上面图片;ShopAdPicture商城列表页图片;ItemScenicPicture项目场次照片;ActivityPicture活动照片;VideoList视频(VideoPicture=首桢图片);InvestigationUrl问券调查的url;"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /UpdateScenic? [post]
func UpdateScenic(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("id"), user)
	if err != nil {
		c.JSON(200, tools.ResponseError{
			401,
			"没有权限",
		})
		return
	}

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

	var ShopAdPicture []DB.SPicture
	json.Unmarshal([]byte(c.PostForm("ShopAdPicture")), &ShopAdPicture)

	var ItemScenicPicture []DB.SPicture
	json.Unmarshal([]byte(c.PostForm("ItemScenicPicture")), &ItemScenicPicture)

	var ActivityPicture []DB.SPicture
	json.Unmarshal([]byte(c.PostForm("ActivityPicture")), &ActivityPicture)

	var VideoList []DB.SVideo
	json.Unmarshal([]byte(c.PostForm("VideoList")), &VideoList)

	var id primitive.ObjectID
	if pid := c.PostForm("id"); pid == "null" {

		if user.UserType != "root" {
			c.JSON(200, tools.ResponseError{
				401,
				"没有权限",
			})
			return
		}
		id = primitive.NewObjectID()
		// 新景区,初始化
		initScenic(id.Hex())
	} else {
		id, _ = primitive.ObjectIDFromHex(pid)
	}

	upsert := true
	DB.CScenic.FindOneAndUpdate(tools.GetContext(),
		bson.M{"_id": id},
		bson.M{"$set": bson.M{
			"Name":              c.PostForm("Name"),
			"Describe":          c.PostForm("Describe"),
			"OpenHours":         c.PostForm("OpenHours"),
			"Mobile":            c.PostForm("Mobile"),
			"Address":           c.PostForm("Address"),
			"InvestigationUrl":  c.PostForm("InvestigationUrl"),
			"Picture":           Picture,
			"ShopAdPicture":     ShopAdPicture,
			"ItemScenicPicture": ItemScenicPicture,
			"ActivityPicture":   ActivityPicture,
			"VideoList":         VideoList,
		}}, &options.FindOneAndUpdateOptions{
			Upsert: &upsert,
		},
	)

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

// @Title 更新景区基础信息-高级
// @Description 更新景区基础信息-高级
// @Accept  json
// @Produce  json
// @Param   id     5dfb03070a9ac17ac7a82054    string     true        "景区id"
// @Param   Token     wgergejfwe    string     true        "用户token"
// @Success 200 {object} tools.ResponseSeccess "RangeLocation景区范围(多个坐标点)ZoomForIOS地图的缩放大小(IOS);ZoomForAndroidMin地图的缩放大小(Android最小);ZoomForAndroidMax地图的缩放大小(Android最大);Rotation旋转角度;OpenTiles否开启地图切片;ColorTiles切片底色(#FFFFFF)"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /UpdateScenicSenior? [post]
func UpdateScenicSenior(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("id"), user)
	if err != nil {
		c.JSON(200, tools.ResponseError{
			401,
			"没有权限",
		})
		return
	}

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

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

	var ZoomForIOS DB.SRange
	json.Unmarshal([]byte(c.PostForm("ZoomForIOS")), &ZoomForIOS)

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

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

	var id primitive.ObjectID
	if pid := c.PostForm("id"); pid == "null" {

		if user.UserType != "root" {
			c.JSON(200, tools.ResponseError{
				401,
				"没有权限",
			})
			return
		}
		id = primitive.NewObjectID()
		// 新景区,初始化
		initScenic(id.Hex())
	} else {
		id, _ = primitive.ObjectIDFromHex(pid)
	}

	Rotation, err := strconv.ParseFloat(c.PostForm("Rotation"), 64)
	OpenTiles, err := strconv.ParseBool(c.PostForm("OpenTiles"))
	Display, _ := strconv.ParseBool(c.PostForm("Display"))
	upsert := true
	DB.CScenic.FindOneAndUpdate(tools.GetContext(),
		bson.M{"_id": id},
		bson.M{"$set": bson.M{
			"Location":          Location,
			"RangeLocation":     RangeLocation,
			"ZoomForIOS":        ZoomForIOS,
			"ZoomForAndroidMin": ZoomForAndroidMin,
			"ZoomForAndroidMax": ZoomForAndroidMax,
			"Rotation":          Rotation,
			"OpenTiles":         OpenTiles,
			"ColorTiles":        c.PostForm("ColorTiles"),
			"Display":           Display,
			"Remove":            false,
		}}, &options.FindOneAndUpdateOptions{
			Upsert: &upsert,
		},
	)

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

func initScenic(id string) {

	var dba []interface{}
	dba = append(dba, DB.STag{
		id,
		"type",
		"服务设施",
	})
	dba = append(dba, DB.STag{
		id,
		"type",
		"普通",
	})
	dba = append(dba, DB.STag{
		id,
		"type",
		"演出",
	})
	dba = append(dba, DB.STag{
		id,
		"type",
		"购物",
	})
	dba = append(dba, DB.STag{
		id,
		"type",
		"餐饮",
	})
	dba = append(dba, DB.STag{
		id,
		"type",
		"游乐设施",
	})

	dba = append(dba, DB.STag{
		id,
		"age",
		"儿童",
	})
	dba = append(dba, DB.STag{
		id,
		"age",
		"成人",
	})
	dba = append(dba, DB.STag{
		id,
		"age",
		"青少年",
	})

	dba = append(dba, DB.STag{
		id,
		"food",
		"海鲜",
	})
	dba = append(dba, DB.STag{
		id,
		"food",
		"团餐",
	})
	dba = append(dba, DB.STag{
		id,
		"food",
		"烧烤",
	})
	dba = append(dba, DB.STag{
		id,
		"food",
		"小吃",
	})

	dba = append(dba, DB.STag{
		id,
		"recommend",
		"必玩",
	})

	dba = append(dba, DB.STag{
		id,
		"shop",
		"纪念品",
	})
	dba = append(dba, DB.STag{
		id,
		"shop",
		"收藏品",
	})
	dba = append(dba, DB.STag{
		id,
		"shop",
		"玩具",
	})

	dba = append(dba, DB.STag{
		id,
		"thrilling",
		"刺激",
	})
	dba = append(dba, DB.STag{
		id,
		"thrilling",
		"放松",
	})

	DB.CTags.InsertMany(tools.GetContext(), dba[1:])
}

// @Title 所有景区基础信息
// @Description 所有景区基础信息
// @Accept  json
// @Produce  json
// @Param   Display     true    string     true        "true=显示隐藏数据"
// @Success 200 {object} tools.ResponseSeccess ""
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /AllScenic? [get]
func AllScenic(c *gin.Context) {
	c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
	c.Header("Access-Control-Allow-Credentials", "true")

	_select := bson.M{"Display": true}
	if c.Query("Display") == "all"{
		_select = bson.M{}
	}

	var Scenic []DB.SScenic
	cur, err := DB.CScenic.Find(tools.GetContext(), _select)
	defer cur.Close(tools.GetContext())
	if err == nil {
		for cur.Next(tools.GetContext()) {
			var e DB.SScenic
			cur.Decode(&e)
			Scenic = append(Scenic, e)
		}
	}

	if Scenic == nil {
		Scenic = []DB.SScenic{}
	}

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

}