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.

106 lines
2.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package activity
import (
"recook/internal/back"
"recook/internal/dbc"
"recook/internal/model/activity"
"recook/internal/model/manage"
"recook/tools"
"github.com/gin-gonic/gin"
)
type subGoodsInfo struct {
GoodsId int64 `json:"goodsId" validator:"required"`
GoodsName string `json:"goodsName"`
PhotoUrl string `json:"photoUrl" validator:"required"`
}
type createActivityParam struct {
Area int64 `json:"area" validate:"oneof=0 1 2 3 4"` // 区域 ABC三个区域
BarFontColor string `json:"barFontColor" validator:"required"`
BarBackgroundColor string `json:"barBackgroundColor" validator:"required"`
Name string `json:"name" validator:"required"`
LogoUrl string `json:"logoUrl" validator:"required"`
TopUrl string `json:"topUrl" validator:"required"`
GoodsList []subGoodsInfo `json:"goodsList" validator:"required"`
Status uint `json:"status"` //新增模块选项 传1或2 1单个2并列
IsSale bool `json:"is_sale"`
}
func CreateActivity(c *gin.Context) {
var p createActivityParam
err := tools.Params(&p, c)
if err != nil {
back.Fail(c, err.Error())
return
}
if p.Status != 1 && p.Status != 2 && p.Status != 10 {
back.Fail(c, "样式为1或2")
return
}
if len(p.GoodsList) == 0 {
back.Fail(c, "活动至少包含3款商品")
return
}
for _, v := range p.GoodsList {
if v.GoodsId == 0 {
back.Fail(c, "商品id未传入")
return
}
if len(v.PhotoUrl) == 0 {
back.Fail(c, "商品图片未传入")
return
}
if len(v.GoodsName) == 0 {
back.Fail(c, "商品名称未传入")
return
}
}
var userId = c.MustGet("Manager_ID").(uint)
var user manage.UserInfo
user.ID = userId
if err = dbc.DB.Find(&user).Error; err != nil {
back.Fail(c, "数据错误:"+err.Error())
return
}
tx := dbc.DB.Begin()
{
info := &activity.Info{
Name: p.Name,
BarFontColor: p.BarFontColor,
BarBackgroundColor: p.BarBackgroundColor,
LogoUrl: p.LogoUrl,
TopUrl: p.TopUrl,
Type: p.Area,
IsActive: 1, // 默认添加的都是激活的活动2020-03-12
UserID: user.ID,
UserName: user.Name,
Status: p.Status,
IsSale: p.IsSale,
}
if err = tx.Create(info).Error; err != nil {
back.Fail(c, err.Error())
return
}
for _, v := range p.GoodsList {
g := &activity.Goods{
ActivityId: info.ID,
GoodsId: v.GoodsId,
GoodsName: v.GoodsName,
PhotoUrl: v.PhotoUrl,
}
if err = tx.Create(g).Error; err != nil {
back.Fail(c, err.Error())
return
}
}
}
tx.Commit()
back.Suc(c, "操作成功", nil)
}