|
|
package order_preview
|
|
|
|
|
|
import (
|
|
|
"github.com/gin-gonic/gin"
|
|
|
"github.com/shopspring/decimal"
|
|
|
"log"
|
|
|
"recook/internal/back"
|
|
|
"recook/internal/dbc"
|
|
|
"recook/internal/model/coupon"
|
|
|
"recook/internal/model/order_preview"
|
|
|
"recook/tools"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
type QueryCouponAvailableRequest struct {
|
|
|
UserID uint `json:"userId"`
|
|
|
PreOrderID uint `json:"preOrderId"`
|
|
|
}
|
|
|
|
|
|
type QueryResponse struct {
|
|
|
Total int `json:"total"`
|
|
|
List couponList `json:"list"`
|
|
|
}
|
|
|
|
|
|
type couponList struct {
|
|
|
// 当前订单可用的优惠券, 就是告诉c端页面可以选择
|
|
|
Available []Available `json:"available"`
|
|
|
// 当前订单[不]可用的优惠券, 就是告诉c端页面可以选择
|
|
|
NotAvailable []coupon.ReceiverMid `json:"notAvailable"`
|
|
|
}
|
|
|
type Available struct {
|
|
|
coupon.ReceiverMid
|
|
|
Selected bool `json:"select"`
|
|
|
}
|
|
|
|
|
|
// 查询针对当前订单,返回可用的优惠券/所有优惠券
|
|
|
func QueryCouponAvailable(c *gin.Context) {
|
|
|
defer func() {
|
|
|
if err := recover(); err != nil {
|
|
|
log.Println("服务器出错:", err)
|
|
|
back.Fail(c, "服务器出错")
|
|
|
return
|
|
|
}
|
|
|
}()
|
|
|
var p QueryCouponAvailableRequest
|
|
|
if err := tools.ParseParams(&p, c); err != nil {
|
|
|
back.Fail(c, err.Error())
|
|
|
return
|
|
|
}
|
|
|
|
|
|
// 查找当前用户的所有"可用"的优惠券
|
|
|
var userReceiverMids []coupon.ReceiverMid
|
|
|
dbc.DB.Order("id desc").Find(&userReceiverMids, "user_id = ? AND status = 0 AND end_time > ?", p.UserID, time.Now())
|
|
|
// 初始化返回参数
|
|
|
var qr = QueryResponse{
|
|
|
Total: len(userReceiverMids),
|
|
|
List: couponList{},
|
|
|
}
|
|
|
if len(userReceiverMids) < 1 {
|
|
|
back.Suc(c, "没有优惠券可用", &qr)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
var orderDetail order_preview.Information
|
|
|
if err := dbc.DB.Find(&orderDetail, p.PreOrderID).Error; err != nil {
|
|
|
back.Fail(c, "没有此订单")
|
|
|
return
|
|
|
}
|
|
|
// 查找preOrder信息: 订单现在所选的
|
|
|
var orderCoupons []struct {
|
|
|
CouponID uint `gorm:"column:coupon_id"`
|
|
|
BrandID uint `gorm:"column:brand_id"`
|
|
|
}
|
|
|
dbc.DB.Model(&coupon.ReceiverMid{}).Select("coupon_id").Where("order_id = ?", p.PreOrderID).Scan(&orderCoupons)
|
|
|
|
|
|
// 判断订单货物详情
|
|
|
var orderGoods []order_preview.GoodsDetail
|
|
|
dbc.DB.Find(&orderGoods, p.PreOrderID)
|
|
|
// 品牌id =》 总价的 一个map
|
|
|
var brandMap = make(map[uint]decimal.Decimal)
|
|
|
for _, orderGood := range orderGoods {
|
|
|
brandMap[orderGood.BrandID] = orderGood.GoodsAmount
|
|
|
}
|
|
|
|
|
|
//判断两个信息, 是不是同一个牌子,是否达到可抵扣的起点
|
|
|
// 标记当前能用,和当前选择的
|
|
|
var availableList = make([]Available, 0, 0)
|
|
|
var notAvailableList = make([]coupon.ReceiverMid, 0, 0)
|
|
|
// only one can be selected
|
|
|
var selectIt = true
|
|
|
for _, userReceiverMid := range userReceiverMids {
|
|
|
// 这里的orderCoupons 按照继峰说法,其实只有一个
|
|
|
for _, orderCoupon := range orderCoupons {
|
|
|
// 1、当前已选
|
|
|
if selectIt || orderCoupon.CouponID == userReceiverMid.CouponID {
|
|
|
availableList = append(availableList, Available{
|
|
|
ReceiverMid: userReceiverMid,
|
|
|
Selected: true,
|
|
|
})
|
|
|
// 只有一张优惠券,选中后,短路,下次不再进入这个循环
|
|
|
selectIt = false
|
|
|
goto LabelDone
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 2、判断是否是可以被当前订单使用
|
|
|
if v, ok := brandMap[userReceiverMid.BrandID]; (userReceiverMid.BrandID == 0 && orderDetail.GoodsTotalAmount.GreaterThan(decimal.NewFromInt(int64(userReceiverMid.Threshold)))) ||
|
|
|
(ok && v.GreaterThan(decimal.NewFromInt(int64(userReceiverMid.Threshold)))) {
|
|
|
// 品牌是否不受限制,
|
|
|
// 或者是否在order的商品中,且领的券没有门槛金额,或者门槛金额小于订单的该品牌货物金额
|
|
|
availableList = append(availableList, Available{
|
|
|
ReceiverMid: userReceiverMid,
|
|
|
Selected: false,
|
|
|
})
|
|
|
continue
|
|
|
}
|
|
|
// 3、无法被使用的扔进 notAvaiList
|
|
|
notAvailableList = append(notAvailableList, userReceiverMid)
|
|
|
|
|
|
LabelDone:
|
|
|
}
|
|
|
|
|
|
// 拼接数据 返回数据
|
|
|
qr.List = couponList{
|
|
|
availableList,
|
|
|
notAvailableList,
|
|
|
}
|
|
|
|
|
|
back.Suc(c, "操作成功", qr)
|
|
|
return
|
|
|
}
|