From 2c523cffe5f7f4439afb4590fa735a292b592945 Mon Sep 17 00:00:00 2001 From: howell <2827207845@qq.com> Date: Tue, 15 Mar 2022 14:08:35 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E7=B2=BE=E5=93=81=E7=89=B9=E6=8E=A8?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0is=5Fsale?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/api/manage/promotion/create.go | 44 ++++++++++++------------- internal/api/manage/promotion/query.go | 33 ++++++++++++++++--- internal/model/promotion/information.go | 1 + 3 files changed, 51 insertions(+), 27 deletions(-) diff --git a/internal/api/manage/promotion/create.go b/internal/api/manage/promotion/create.go index 582f9b0..f29e5b9 100755 --- a/internal/api/manage/promotion/create.go +++ b/internal/api/manage/promotion/create.go @@ -20,6 +20,7 @@ type createPromotionParam struct { StartDate string `json:"startDate" validate:"required" time_format:"2006-01-02"` EndDate string `json:"endDate" validate:"required" time_format:"2006-01-02"` TimeItems []timeItem `json:"timeItems" validate:"required"` + IsSale bool `json:"is_sale"` } type timeItem struct { @@ -62,8 +63,6 @@ func CreatePromotion(c *gin.Context) { back.Fail(c, "日期无法解析") return } - startTime := time.Date(startDate.Year(), startDate.Month(), startDate.Day(), 0, 0, 0, 0, time.Local) - endDate, err := time.Parse("2006-01-02", p.EndDate) if err != nil { back.Fail(c, "日期无法解析") @@ -84,29 +83,29 @@ func CreatePromotion(c *gin.Context) { } endTime := time.Date(endDate.Year(), endDate.Month(), endDate.Day(), 23, 59, 59, 0, time.Local) - // 先检测 - { - var promotionGoodsList []promotion.Goods - dbc.DB.Select("id, goods_id").Find(&promotionGoodsList, "start_time < ? AND end_time > ?", endTime, startTime) + // // 先检测 + // { + // var promotionGoodsList []promotion.Goods + // dbc.DB.Select("id, goods_id").Find(&promotionGoodsList, "start_time < ? AND end_time > ?", endTime, startTime) - ids := map[uint]uint{} - for _, v := range promotionGoodsList { - ids[v.GoodsID] = 1 - } + // ids := map[uint]uint{} + // for _, v := range promotionGoodsList { + // ids[v.GoodsID] = 1 + // } - for _, v := range p.TimeItems { - for _, v2 := range v.GoodsList { - if _, ok := ids[v2.GoodsID]; ok { - back.Fail(c, "商品活动冲突") - return - } - } + // for _, v := range p.TimeItems { + // for _, v2 := range v.GoodsList { + // if _, ok := ids[v2.GoodsID]; ok { + // back.Fail(c, "商品活动冲突") + // return + // } + // } - if len(v.ActivitiesList) < 1 { - continue - } - } - } + // if len(v.ActivitiesList) < 1 { + // continue + // } + // } + // } //for _, v := range p.TimeItems { // for _, g := range v.GoodsList { @@ -136,6 +135,7 @@ func CreatePromotion(c *gin.Context) { Name: p.Name, StartDate: formatime.NewDateFrom(startDate), EndDate: formatime.NewDateFrom(endDate), + IsSale: p.IsSale, } if err := tx.Create(&info).Error; err != nil { back.Err(c, err.Error()) diff --git a/internal/api/manage/promotion/query.go b/internal/api/manage/promotion/query.go index 2e49342..d844d7a 100755 --- a/internal/api/manage/promotion/query.go +++ b/internal/api/manage/promotion/query.go @@ -15,25 +15,47 @@ import ( ) type goodsListParam struct { - Date string `json:"date" validate:"required" time_format:"2006-01-02"` + Date string `json:"date" validate:"required" time_format:"2006-01-02"` + IsSale bool `json:"is_sale"` +} + +type PromotionReq struct { + IsSale bool `json:"is_sale"` } func QueryWaitingList(c *gin.Context) { + var p PromotionReq + err := tools.Params(&p, c) + if err != nil { + back.Fail(c, err.Error()) + return + } var l []promotion.Information - dbc.DB.Find(&l, "start_date > ? ", time.Now().Format("2006-01-02")) + dbc.DB.Where("is_sale = ?", p.IsSale).Find(&l, "start_date > ? ", time.Now().Format("2006-01-02")) back.Suc(c, "操作成功", l) } func QueryActiveList(c *gin.Context) { + var p PromotionReq + err := tools.Params(&p, c) + if err != nil { + back.Fail(c, err.Error()) + return + } var l []promotion.Information format := time.Now().Format("2006-01-02") - dbc.DB.Find(&l, "start_date <= ? AND end_date >= ?", format, format) + dbc.DB.Where("is_sale = ?", p.IsSale).Find(&l, "start_date <= ? AND end_date >= ?", format, format) back.Suc(c, "操作成功", l) } func QueryExpiredList(c *gin.Context) { + var p PromotionReq + err := tools.Params(&p, c) + if err != nil { + back.Fail(c, err.Error()) + } var l []promotion.Information - dbc.DB.Find(&l, "end_date < ?", time.Now().Format("2006-01-02")) + dbc.DB.Where("is_sale = ?", p.IsSale).Find(&l, "end_date < ?", time.Now().Format("2006-01-02")) back.Suc(c, "操作成功", l) } @@ -52,7 +74,8 @@ func QueryCheckPromotionDate(c *gin.Context) { } var count uint - dbc.DB.Table((&promotion.Information{}).TableName()).Where("start_date = ?", startDate.Format("2006-01-02")).Count(&count) + dbc.DB.Table((&promotion.Information{}).TableName()).Where("is_sale = ?", p.IsSale). + Where("start_date = ?", startDate.Format("2006-01-02")).Count(&count) if count > 0 { back.Fail(c, "该日期已创建过活动,请更改日期") return diff --git a/internal/model/promotion/information.go b/internal/model/promotion/information.go index cba2352..2cc78e3 100755 --- a/internal/model/promotion/information.go +++ b/internal/model/promotion/information.go @@ -10,6 +10,7 @@ type Information struct { StartDate formatime.Date `gorm:"column:start_date" json:"startDate"` EndDate formatime.Date `gorm:"column:end_date" json:"endDate"` CreatedAt formatime.Second `gorm:"column:created_at" json:"-"` + IsSale bool `json:"-" gorm:"column:is_sale"` } // TableName sets the insert table name for this struct type