Notice.go 7.62 KB
package Api

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

// @Title 查询公告
// @Description 查询公告-单条
// @Accept  json
// @Produce  json
// @Param   id     5dfb03070a9ac17ac7a82054    string     true        "id"
// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":{"Id":"5e82d2539561231535f72958","Title":"公告2","Url":"http://www.google.cn","CreateTime":1585631827,"UpdateTime":0,"Expiry":1585670400,"ExpiryString":"今天"}}"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /Notice/Info? [get]
func NoticeInfo(c *gin.Context) {
	c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
	c.Header("Access-Control-Allow-Credentials", "true")

	var aNotice *DB.SNotice
	objId, _ := primitive.ObjectIDFromHex(c.Query("id"))
	DB.CNotice.FindOne(tools.GetContext(), bson.M{"_id": objId}).Decode(&aNotice)

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

// @Title 查询公告
// @Description 查询公告-列表
// @Accept  json
// @Produce  json
// @Param   ScenicId     wgergejfwe    string     true        "景区id"
// @Param   ExpiryState     all    string     false        "是否有效,默认只显示有效数据,==all所有数据"
// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":[{"Id":"5e82c27f41914b0fdcac489f","Title":"公告","Url":"http://www.google.cn","CreateTime":1585627775,"UpdateTime":0,"Expiry":1585670400,"ExpiryString":"今天"}...]}"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /Notice/List? [get]
func NoticeList(c *gin.Context) {
	c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
	c.Header("Access-Control-Allow-Credentials", "true")

	var aNotice []*DB.SNotice

	var _select primitive.M
	if c.Query("ExpiryState") == "all"{
		_select = bson.M{"ScenicId":c.Query("ScenicId")}
	}else{
		_select = bson.M{"Expiry": bson.M{"$gte": time.Now().Unix()},"ScenicId":c.Query("ScenicId")}
	}


	cur, err := DB.CNotice.Find(tools.GetContext(), _select)//
	defer cur.Close(tools.GetContext())
	if err == nil {
		for cur.Next(tools.GetContext()) {
			var e *DB.SNotice
			cur.Decode(&e)
			aNotice = append(aNotice, e)
		}
	}

	if aNotice == nil {
		aNotice = []*DB.SNotice{}
	}
	c.JSON(200, tools.ResponseSeccess{
		0,
		aNotice,
	})
}

// @Title 创建公告
// @Description 创建公告
// @Accept  json
// @Produce  json
// @Param   ScenicId     wgergejfwe    string     true        "景区id"
// @Param   Title     营业时间公告    string     true        "公告名称"
// @Param   Url     http://abc.com    string     true        "公告链接"
// @Param   ExpiryString     http://abc.com    string     true        "有效期(15分钟|1小时|今天|今年)"
// @Param   Token     wgergejfwe    string     true        "用户token"
// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":"ok"}"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /Notice/Create? [post]
func CreateNotice(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 Expiry = getExp(c.PostForm("ExpiryString"))

	NowTime := time.Now()
	objectID := primitive.NewObjectID()
	Notice := &DB.SNotice{
		&objectID,
		c.PostForm("ScenicId"),
		c.PostForm("Title"),
		c.PostForm("Url"),
		NowTime.Unix(),
		0,
		Expiry,
		c.PostForm("ExpiryString"),
	}

	_, err = govalidator.ValidateStruct(Notice);
	if err != nil {
		c.JSON(200, tools.ResponseError{
			1,
			err.Error(),
		})
		return
	}

	_, err = DB.CNotice.InsertOne(tools.GetContext(), Notice)
	if err != nil {
		c.JSON(200, tools.ResponseError{
			1,
			err.Error(),
		})
		return
	}

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

// @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 "{"errcode":0,"result":"ok"}"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /Notice/Remove? [post]
func RemoveNotice(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
	}

	_, err = primitive.ObjectIDFromHex(c.PostForm("id"))
	if err != nil {
		c.JSON(200, tools.ResponseError{
			1,
			"id不正确",
		})
		return
	}

	objID, _ := primitive.ObjectIDFromHex(c.PostForm("id"))
	DB.CNotice.DeleteOne(tools.GetContext(), bson.M{"_id": objID})

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

}

// @Title 修改公告
// @Description 修改公告
// @Accept  json
// @Produce  json
// @Param   id     5dfb03070a9ac17ac7a82054    string     true        "公告id"
// @Param   ScenicId     wgergejfwe    string     true        "景区id"
// @Param   Title     营业时间公告    string     true        "公告名称"
// @Param   Url     http://abc.com    string     true        "公告链接"
// @Param   ExpiryString     http://abc.com    string     true        "有效期(15分钟|1小时|今天|今年)"
// @Param   Token     wgergejfwe    string     true        "用户token"
// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":"ok"}"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /Notice/Modify? [post]
func ModifyNotice(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 Expiry = getExp(c.PostForm("ExpiryString"))

	NowTime := time.Now()
	objectID, _ := primitive.ObjectIDFromHex(c.PostForm("id"))

	Notice := &DB.SNotice{
		&objectID,
		c.PostForm("ScenicId"),
		c.PostForm("Title"),
		c.PostForm("Url"),
		0,
		NowTime.Unix(),
		Expiry,
		c.PostForm("ExpiryString"),
	}
	_, err = govalidator.ValidateStruct(Notice);
	if err != nil {
		c.JSON(200, tools.ResponseError{
			1,
			err.Error(),
		})
		return
	}

	_, err = DB.CNotice.UpdateOne(tools.GetContext(),
		bson.M{"_id": objectID},
		bson.M{"$set": Notice},
	)

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

}

func getExp(ExpiryString string) int64 {

	var Expiry int64
	NowTime := time.Now()
	if ExpiryString == "15分钟" {
		Expiry = NowTime.Add(time.Minute * 15).Unix()
	}
	if ExpiryString == "1小时" {
		Expiry = NowTime.Add(time.Minute * 60).Unix()
	}
	if ExpiryString == "今天" {
		md := NowTime.Add(time.Hour * 24)
		Expiry = time.Date(md.Year(), md.Month(), md.Day(), 0, 0, 0, 0, NowTime.Location()).Unix()
	}
	if ExpiryString == "今年" {
		Expiry = time.Date(NowTime.Year()+1, 1, 1, 0, 0, 0, 0, NowTime.Location()).Unix()
	}
	return Expiry
}