You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
2.9 KiB

package diamond_show
import (
"github.com/gin-gonic/gin"
"recook/internal/back"
"recook/internal/dbc"
"recook/internal/model/activity"
"recook/internal/model/diamond_show"
"recook/tools"
"strings"
)
type createParam struct {
URL string `json:"url"`
GoodsID uint `json:"goodsId" validate:"numeric"`
ActivityID uint `json:"activityId" validate:"numeric"`
ActivityOrGoods string `json:"activityOrGoods" validate:"required"` // 传递 activity or goods 大小写随意
Color string `json:"color"`
BackgroundColor string `json:"BackgroundColor" validate:"required"`
}
/*钻展位置的图片为活动图 活动图片重新设置 不允许直接使用活动图 因为有些活动图的尺寸是正方形*/
func CreateDiamondShow(c *gin.Context) {
var p createParam
err := tools.Params(&p, c)
if err != nil {
back.Fail(c, err.Error())
return
}
if p.GoodsID <= 0 && p.ActivityID <= 0 {
back.Fail(c, "非法的参数")
return
}
var activityOrGoods = strings.ToLower(p.ActivityOrGoods)
if activityOrGoods != "goods" && activityOrGoods != "activity" {
back.Fail(c, "非法的参数")
return
}
// 检查个数
var count uint
dbc.DB.Table((&diamond_show.Information{}).TableName()).Where("is_active=?", 1).Count(&count)
if count > 10 {
back.Fail(c, "最多只能有十个钻展商品")
return
}
// 验证颜色
if len(p.Color) > 0 && (len(p.Color) != 7 || !strings.HasPrefix(p.Color, "#")) {
back.Fail(c, "非法的颜色参数")
return
}
var one diamond_show.Information
if activityOrGoods == "goods" && p.GoodsID > 0 {
// 重复性检查
var show diamond_show.Information
dbc.DB.Where("is_active=1").First(&show, "good_id = ?", p.GoodsID)
if show.ID > 0 {
back.Fail(c, "该商品已在钻展中")
return
}
// 检查商品是否存在
//var good = activity.Goods{}
//dbc.DB.Where("id=?", p.GoodsID).Find(&good)
//if good.ID <= 0 {
// http.Fail(c, "商品不存在,无法添加")
// return
//}
one = diamond_show.Information{
GoodsID: p.GoodsID,
URL: p.URL,
IsActive: 1,
Color: p.BackgroundColor,
}
dbc.DB.Create(&one)
} else if activityOrGoods == "activity" && p.ActivityID > 0 {
// 重复性检查
var show diamond_show.Information
dbc.DB.Where("is_active=1").First(&show, "activity_id = ?", p.ActivityID)
if show.ID > 0 {
back.Fail(c, "该活动已在钻展中")
return
}
// 检查活动是否存在
var activityData = activity.Info{}
dbc.DB.Where("id=?", p.ActivityID).Find(&activityData)
if activityData.ID <= 0 {
back.Fail(c, "活动不存在,无法添加")
return
}
one = diamond_show.Information{
GoodsID: 0,
ActiveID: p.ActivityID,
URL: p.URL,
IsActive: 1,
Color: p.BackgroundColor,
}
if err = dbc.DB.Create(&one).Error; err != nil {
back.Fail(c, "添加出错:"+err.Error())
return
}
} else {
back.Fail(c, "非法的参数")
return
}
back.Suc(c, "", &one)
return
}