Tag.go 6.1 KB
package Api

import (
	"github.com/aarongao/tools"
	"github.com/gin-gonic/gin"
	"go.mongodb.org/mongo-driver/bson"
	"letu/DB"
	"letu/Lib/Auth"
	"letu/Lib/LeYouTu"
	"time"
)

// @Title 标签列表
// @Description 标签 - 所有标签
// @Accept  json
// @Produce  json
// @Param   ScenicId     5dfb03070a9ac17ac7a82054    string     true        "景区id"
// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":[{"Type":"menu","Name":"服务设施"},{"Type":"normal","Name":"不错"},{"Type":"thrilling","Name":"刺激"},{"Type":"recommend","Name":"必玩"}]}"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /AllTag? [get]
func AllTag(c *gin.Context) {
	c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
	c.Header("Access-Control-Allow-Credentials", "true")

	ScenicId, err := LeYouTu.GetScenicId(c)
	if err != nil {

		c.JSON(200, tools.ResponseError{
			1,
			"景区id不能为空",
		})
		return
	}

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



	if Stags == nil {
		Stags = []DB.STag{}
	}

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

// @Title 标签列表
// @Description 标签 - 按照标签分组查看所有标签
// @Accept  json
// @Produce  json
// @Param   ScenicId     5dfb03070a9ac17ac7a82054    string     true        "景区id"
// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":[{"Tags":["儿童","成人","青少年"],"Type":"age"}]}"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /AllTagGroup? [get]
func AllTagGroup(c *gin.Context) {
	c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
	c.Header("Access-Control-Allow-Credentials", "true")

	ScenicId, err := LeYouTu.GetScenicId(c)
	if err != nil {
		c.JSON(200, tools.ResponseError{
			1,
			"景区id不能为空",
		})
		return
	}

	// cache
	cache := DB.Redis.Get("Tags_" + ScenicId)
	if cache != nil {
		c.JSON(200, tools.ResponseSeccess{
			0,
			cache,
		})
		println("读取缓存Tags", cache)
		return
	}

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

	if Stags == nil {
		Stags = []DB.STag{}
	}

	Group := make(map[string][]string)
	// 去重
	for _, v := range Stags {
		Group[v.Type] = append(Group[v.Type], v.Name)
	}

	// 转为数组
	aGroup := []Tag{}
	for k, v := range Group {
		aGroup = append(aGroup, Tag{k, v})
	}

	DB.Redis.Set("Tags_"+ScenicId, aGroup, time.Second*3600*24)
	c.JSON(200, tools.ResponseSeccess{
		0,
		aGroup,
	})

}



// @Title 标签
// @Description 标签 - 增加标签
// @Accept  json
// @Produce  json
// @Param   ScenicId     5dfb03070a9ac17ac7a82054    string     true        "景区id"
// @Param   Token     wgergejfwe    string     true        "用户token"
// @Param   TagName     广场    string     true        "标签名称"
// @Param   TagGroup     location    string     true        "群组名称"
// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":"ok"}"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /Tag/Create? [post]
func CreateTag(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
	}

	if c.PostForm("TagName") == "" {
		c.JSON(200, tools.ResponseError{
			1,
			"标签名称不能为空",
		})
		return
	}


	if c.PostForm("TagGroup") == "" {
		c.JSON(200, tools.ResponseError{
			1,
			"群组名称不能为空",
		})
		return
	}


	if c.PostForm("TagGroup") == "type" {
		c.JSON(200, tools.ResponseError{
			1,
			"此类型只能加不能加",
		})
		return
	}

	DB.CTags.InsertOne(tools.GetContext(),DB.STag{
		c.PostForm("ScenicId"),
		c.PostForm("TagGroup"),
		c.PostForm("TagName"),
	})

	DB.Redis.Delete("Tags_" + c.PostForm("ScenicId"))
	println("清楚缓存Tags")

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




// @Title 标签
// @Description 标签 - 删除标签
// @Accept  json
// @Produce  json
// @Param   ScenicId     5dfb03070a9ac17ac7a82054    string     true        "景区id"
// @Param   Token     wgergejfwe    string     true        "用户token"
// @Param   TagName     广场    string     true        "标签名称"
// @Param   TagGroup     location    string     true        "群组名称"
// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":"ok"}"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /Tag/Remove? [post]
func RemoveTag(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
	}

	if c.PostForm("TagName") == "" {
		c.JSON(200, tools.ResponseError{
			1,
			"标签名称不能为空",
		})
		return
	}


	if c.PostForm("TagGroup") == "" {
		c.JSON(200, tools.ResponseError{
			1,
			"群组名称不能为空",
		})
		return
	}


	if c.PostForm("TagGroup") == "type" {
		c.JSON(200, tools.ResponseError{
			1,
			"此类型只能加不能删",
		})
		return
	}

	DB.CTags.DeleteOne(tools.GetContext(), bson.M{"ScenicId": c.PostForm("ScenicId"),"Name":c.PostForm("TagName"),"Type":c.PostForm("TagGroup")})

	DB.Redis.Delete("Tags_" + c.PostForm("ScenicId"))
	println("清楚缓存Tags")

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


type Tag struct {
	Type string
	Tags []string
}