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.
118 lines
3.4 KiB
118 lines
3.4 KiB
package newertehui
|
|
|
|
import (
|
|
"recook/internal/back"
|
|
"recook/internal/dbc"
|
|
"recook/internal/model/coupon"
|
|
"recook/internal/model/goods"
|
|
"recook/internal/model/newertehui"
|
|
"recook/internal/model/promotion"
|
|
"recook/tools"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/shopspring/decimal"
|
|
)
|
|
|
|
type tehuiResp struct {
|
|
Name string `json:"name"`
|
|
CouponID uint `json:"couponId"`
|
|
Cash uint `json:"cash"`
|
|
Explanation string `json:"explanation"`
|
|
Status uint `json:"status"`
|
|
GoodsList []goodsDetail `json:"goodsList"`
|
|
}
|
|
|
|
type goodsDetail struct {
|
|
GoodsID uint `json:"goodsId"` //
|
|
GoodsName string `json:"goodsName"` //
|
|
MainPhotoURL string `json:"mainPhotoUrl"`
|
|
Label string `json:"label"`
|
|
Price string `json:"price"`
|
|
OriginPrice string `json:"originPrice"`
|
|
}
|
|
|
|
type allPrice struct {
|
|
OriginalPrice decimal.Decimal `gorm:"column:original_price" json:"originalPrice"` // 重新计算折扣价
|
|
DiscountPrice decimal.Decimal `gorm:"column:discount_price" json:"discountPrice"`
|
|
}
|
|
|
|
type currentParams struct {
|
|
UserID uint `json:"userId"`
|
|
IsSale bool `json:"is_sale"`
|
|
}
|
|
|
|
func QueryCurrent(c *gin.Context) {
|
|
var p currentParams
|
|
if err := tools.ParseParams(&p, c); err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
|
|
var info newertehui.Info
|
|
dbc.DB.Last(&info)
|
|
|
|
if info.ID == 0 {
|
|
back.Fail(c, "还没有设置新人优惠")
|
|
return
|
|
}
|
|
|
|
var cou coupon.Information
|
|
dbc.DB.First(&cou, info.CouponID)
|
|
|
|
var status uint
|
|
dbc.DB.Table((&coupon.ReceiverMid{}).TableName()).Where("coupon_id = ? AND user_id = ?", info.CouponID, p.UserID).Count(&status)
|
|
|
|
var tehuiGoodsList []newertehui.Goods
|
|
dbc.DB.Find(&tehuiGoodsList, "tehui_id = ?", info.ID)
|
|
|
|
list := make([]goodsDetail, 0)
|
|
for _, v := range tehuiGoodsList {
|
|
var goodsInfo goods.Information
|
|
dbc.DB.First(&goodsInfo, v.GoodsId)
|
|
|
|
var photo goods.MainPhoto
|
|
dbc.DB.First(&photo, "goods_id = ? AND is_master = 1", goodsInfo.ID)
|
|
|
|
var minAllPrice allPrice
|
|
selectMinPriceStr := "MIN(original_price) AS original_price, " +
|
|
"MIN(discount_price) AS discount_price "
|
|
dbc.DB.Table((&goods.Sku{}).TableName()).Select(selectMinPriceStr).First(&minAllPrice, "goods_id = ?", goodsInfo.ID)
|
|
|
|
var promotionGoods promotion.Goods
|
|
dbc.DB.Table((&promotion.Goods{}).TableName()).First(&(promotionGoods), "goods_id = ? AND start_time <= ? AND end_time > ?", goodsInfo.ID, time.Now(), time.Now())
|
|
if promotionGoods.ID == 0 {
|
|
dbc.DB.Table((&promotion.Goods{}).TableName()).First(&(promotionGoods), "goods_id = ? AND start_time > ?", goodsInfo.ID, time.Now())
|
|
}
|
|
|
|
price := minAllPrice.DiscountPrice
|
|
if promotionGoods.ID > 0 {
|
|
var minPromSku promotion.Sku
|
|
dbc.DB.Select("MIN(discount_price) AS discount_price, MIN(commission) AS commission").First(&minPromSku, "promotion_goods_id = ?", promotionGoods.ID)
|
|
|
|
price = minPromSku.DiscountPrice
|
|
}
|
|
|
|
if price.GreaterThanOrEqual(decimal.NewFromInt(int64(cou.Cash))) {
|
|
price = price.Sub(decimal.NewFromInt(int64(cou.Cash)))
|
|
}
|
|
|
|
list = append(list, goodsDetail{
|
|
GoodsID: goodsInfo.ID,
|
|
GoodsName: goodsInfo.GoodsName,
|
|
MainPhotoURL: photo.URL,
|
|
Label: "新人特惠",
|
|
Price: price.String(),
|
|
OriginPrice: minAllPrice.OriginalPrice.String(),
|
|
})
|
|
}
|
|
back.Suc(c, "", tehuiResp{
|
|
Name: cou.Name,
|
|
CouponID: cou.ID,
|
|
Cash: cou.Cash,
|
|
Explanation: cou.Explanation,
|
|
Status: status,
|
|
GoodsList: list,
|
|
})
|
|
}
|