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.

184 lines
4.5 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 removeGoodsParams struct {
ID int64 `json:"id"`
}
func RemoveActivityGoods(c *gin.Context) {
var p removeGoodsParams
err := tools.Params(&p, c)
if err != nil {
back.Fail(c, err.Error())
return
}
err = dbc.DB.Delete(activity.Goods{}, "id=?", p.ID).Error
if err != nil {
back.Err(c, err.Error())
return
}
back.Suc(c, "", nil)
}
// 编辑 更新活动
type updateActivityParam struct {
ID int64 `json:"id" validate:"numeric,required"` // 需要更新的活动id
Area int64 `json:"area" validate:"oneof=0 1 2 3"` // 区域 ABC三个区域
BarFontColor string `json:"barFontColor" validate:"required"`
BarBackgroundColor string `json:"barBackgroundColor" validate:"required"`
Name string `json:"name" validate:"required"`
LogoUrl string `json:"logoUrl" validate:"required"`
TopUrl string `json:"topUrl" validate:"required"`
GoodsList []subGoodsInfo `json:"goodsList" validate:"required"`
Status uint `json:"status"`
}
func UpdateActivity(c *gin.Context) {
var p updateActivityParam
err := tools.Params(&p, c)
if err != nil {
back.Fail(c, err.Error())
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 actInfo = activity.Info{}
dbc.DB.Where("id=?", p.ID).Find(&actInfo)
if actInfo.Name == "" {
back.Fail(c, "活动不存在")
return
}
// 找出原先的ID
var activities []activity.Goods
dbc.DB.Table("recook_activity_goods").Select("id").Where("activity_id=?", p.ID).Find(&activities)
var ids = make(map[int64]bool, len(activities))
for _, val := range activities {
ids[val.ID] = true
}
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{
ID: p.ID,
Name: p.Name,
BarFontColor: p.BarFontColor,
BarBackgroundColor: p.BarBackgroundColor,
LogoUrl: p.LogoUrl,
TopUrl: p.TopUrl,
Type: p.Area,
IsActive: 1,
UserID: user.ID,
UserName: user.Name,
Status: p.Status,
IsSale: actInfo.IsSale,
}
if err = tx.Save(&info).Error; err != nil {
back.Fail(c, err.Error())
return
}
// 不改变表结构, 将原来的id重置为负数
dbc.DB.Table("recook_activity_goods").Where("activity_id=?", p.ID).Update("activity_id", -p.ID)
// 重新添加 商品
for _, v := range p.GoodsList {
// 判断是否能在ids中找到 找不到即新商品,更新;找不到,老商品,不更新
if _, ok := ids[v.GoodsId]; !ok {
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
}
ids[v.GoodsId] = false
}
var delIds = make([]int64, 1)
// ids中为true的为本次更新需要删除的
for id, b := range ids {
if b == true {
delIds = append(delIds, id)
}
}
if len(delIds) > 1 || delIds[0] > 0 {
dbc.DB.Table("recook_activity_goods").Where("id in (?)", delIds).Update("activity_id", -p.ID)
}
}
}
tx.Commit()
back.Suc(c, "操作成功", nil)
}
// 删除活动
type deleteActivityParam struct {
ID int64 `json:"id" validate:"required,numeric"`
}
func DeleteActivity(c *gin.Context) {
var p deleteActivityParam
err := tools.Params(&p, c)
if err != nil {
back.Fail(c, err.Error())
return
}
if p.ID <= 0 {
back.Fail(c, "错误的参数")
return
}
var actInfo = activity.Info{}
dbc.DB.Where("id=?", p.ID).Find(&actInfo)
if actInfo.Name == "" {
back.Fail(c, "活动不存在")
return
}
// 删除活动
dbc.DB.Table("recook_activity_info").Where("id=?", p.ID).Update("is_active", 0)
back.Suc(c, "操作成功", nil)
}