Shop.go 3.85 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 "Price=价格;ShopName=店铺名称;KvPhoto用于列表页的图片;TopPhoto详情页最上面的轮播图;Images详情页下面的产品详细图"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /CommodityInfo? [get]
func CommodityInfo(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 SCommodity DB.SCommodity
	objID, _ := primitive.ObjectIDFromHex(c.Query("id"))
	DB.CCommodity.FindOne(tools.GetContext(), bson.M{"_id": objID}).Decode(&SCommodity)

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

}

// @Title 查询所有商品
// @Description 查询所有商品
// @Accept  json
// @Produce  json
// @Param   ScenicId     5dfb03070a9ac17ac7a82054    string     true        "景区id"
// @Success 200 {object} tools.ResponseSeccess "Price=价格;ShopName=店铺名称;KvPhoto用于列表页的图片;TopPhoto详情页最上面的轮播图;Images详情页下面的产品详细图"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /AllCommodity? [get]
func AllCommodity(c *gin.Context) {
	c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
	c.Header("Access-Control-Allow-Credentials", "true")

	var aCommoditys = []DB.SCommodity{}
	cur, err := DB.CCommodity.Find(tools.GetContext(), bson.M{"ScenicId": c.Query("ScenicId")})
	defer cur.Close(tools.GetContext())
	if err == nil {
		for cur.Next(tools.GetContext()) {
			var e DB.SCommodity
			cur.Decode(&e)
			aCommoditys = append(aCommoditys, e)
		}
	}

	c.JSON(200, aCommoditys)

}

// @Title 更新商品
// @Description 更新商品
// @Accept  json
// @Produce  json
// @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 /UpdateCommodity? [post]
func UpdateCommodity(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 Picture []string
	json.Unmarshal([]byte(c.PostForm("Images")), &Picture)

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

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

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

	upsert := true
	DB.CCommodity.FindOneAndUpdate(tools.GetContext(),
		bson.M{"_id": id},
		bson.M{"$set": bson.M{
			"Name":     c.PostForm("Name"),
			"Price":    c.PostForm("Price"),
			"ShopName": c.PostForm("ShopName"),
			"KvPhoto":  c.PostForm("KvPhoto"),
			"TopPhoto": TopPhoto,
			"ItemId":   c.PostForm("ItemId"),
			"Images":   Picture,
		}}, &options.FindOneAndUpdateOptions{
			Upsert: &upsert,
		},
	)

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

}