|
|
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 updateCouponRequest struct {
|
|
|
UserID uint `json:"userId" validate:"numeric"`
|
|
|
OrderID uint `json:"orderId" validate:"numeric"`
|
|
|
CouponID []uint `json:"couponId" validate:"numeric"`
|
|
|
}
|
|
|
|
|
|
// 修改订单的优惠券
|
|
|
func UpdateCoupon(c *gin.Context) {
|
|
|
defer func() {
|
|
|
if err := recover(); err != nil {
|
|
|
log.Println("服务器出错:", err)
|
|
|
back.Fail(c, "服务器出错")
|
|
|
return
|
|
|
}
|
|
|
}()
|
|
|
|
|
|
var p updateCouponRequest
|
|
|
if err := tools.ParseParams(&p, c); err != nil {
|
|
|
back.Fail(c, err.Error())
|
|
|
return
|
|
|
}
|
|
|
|
|
|
// todo 这里可能以后有个逻辑需要考虑,就是以后优惠券多了之后,是否一个订单可以用多个通用券(品牌券)
|
|
|
// 如果需只能用一张同一类的券,这里需要限制,做判断
|
|
|
|
|
|
// 产品说这里只有一张优惠券可以使用
|
|
|
// 提前加判断,如果以后取消,删除判断代码即可
|
|
|
if len(p.CouponID) > 1 {
|
|
|
back.Fail(c, "一次只能选择一张优惠券")
|
|
|
return
|
|
|
}
|
|
|
|
|
|
// 修改优惠券后,要修改订单,重新计算价格
|
|
|
|
|
|
//查找订单
|
|
|
var previewOrderInfo order_preview.Information
|
|
|
previewOrderInfo.UserID = p.UserID
|
|
|
err := dbc.DB.First(&previewOrderInfo, p.OrderID).Error
|
|
|
if err != nil {
|
|
|
back.Err(c, err.Error())
|
|
|
return
|
|
|
}
|
|
|
|
|
|
// 查找优惠券
|
|
|
var couponInfos []coupon.ReceiverMid
|
|
|
err = dbc.DB.Where(" start_time <= ? AND end_time > ?", time.Now(), time.Now()).Find(&couponInfos, p.CouponID).Error
|
|
|
if err != nil {
|
|
|
back.Err(c, err.Error())
|
|
|
return
|
|
|
}
|
|
|
|
|
|
// 计算减免金额, 暂时只有一张优惠券, 其实可以不用for, 不过以后要是取消限制,这里不需要改, zhi
|
|
|
var universeCoupon []coupon.ReceiverMid // 通用券
|
|
|
universeCouponReducePrice := decimal.NewFromFloat(0.0) // 通用优惠券抵扣的价格
|
|
|
var brandCoupon []coupon.ReceiverMid // 品牌券
|
|
|
brandCouponReducePrice := decimal.NewFromFloat(0.0) // 品牌优惠券抵扣的价格
|
|
|
for _, couponInfo := range couponInfos {
|
|
|
if couponInfo.BrandID > 0 {
|
|
|
brandCoupon = append(brandCoupon, couponInfo)
|
|
|
brandCouponReducePrice.Add(decimal.NewFromInt(int64(couponInfo.Cash)))
|
|
|
} else if couponInfo.BrandID == 0 {
|
|
|
universeCoupon = append(universeCoupon, couponInfo)
|
|
|
universeCouponReducePrice.Add(decimal.NewFromInt(int64(couponInfo.Cash)))
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 实际支付的金额 = (老)实际支付 + (老)优惠券减免 - (新)优惠券减免
|
|
|
actualAmount := previewOrderInfo.ActualTotalAmount.Add(previewOrderInfo.BrandCouponTotalAmount).
|
|
|
Add(previewOrderInfo.UniverseCouponTotalAmount).Sub(brandCouponReducePrice).Sub(universeCouponReducePrice)
|
|
|
|
|
|
// 修改订单信息, 实际支付,品牌优惠券,通用优惠券
|
|
|
previewOrderInfo.ActualTotalAmount = actualAmount
|
|
|
previewOrderInfo.BrandCouponTotalAmount = brandCouponReducePrice
|
|
|
previewOrderInfo.UniverseCouponTotalAmount = universeCouponReducePrice
|
|
|
tx := dbc.DB.Begin()
|
|
|
{
|
|
|
if err := dbc.DB.Save(&previewOrderInfo).Error; err != nil {
|
|
|
tx.Rollback()
|
|
|
back.Fail(c, "更新失败")
|
|
|
log.Println("更新失败1:", err)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
// 还要更新修改 recook_order_preview_coupon_detail, 先删除原来的,再添加现在的
|
|
|
dbc.DB.Where("order_id = ?", p.OrderID).Delete(order_preview.CouponDetail{})
|
|
|
|
|
|
if err = UpdatePrevCouponInfo(p.OrderID, couponInfos); err != nil {
|
|
|
tx.Rollback()
|
|
|
back.Fail(c, "更新失败")
|
|
|
log.Println("更新失败2:", err)
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
tx.Commit()
|
|
|
back.Suc(c, "更新成功", queryPreviewOrderGroup(p.OrderID))
|
|
|
return
|
|
|
}
|
|
|
|
|
|
func UpdatePrevCouponInfo(orderId uint, couponInfos []coupon.ReceiverMid) error {
|
|
|
for _, couponInfo := range couponInfos {
|
|
|
var newPrevCoupon order_preview.CouponDetail
|
|
|
newPrevCoupon.OrderID = orderId
|
|
|
newPrevCoupon.CouponID = couponInfo.CouponID
|
|
|
newPrevCoupon.BrandID = couponInfo.BrandID
|
|
|
newPrevCoupon.CouponName = couponInfo.Name
|
|
|
newPrevCoupon.PersonalCouponID = couponInfo.ID
|
|
|
newPrevCoupon.Scope = couponInfo.Scope
|
|
|
newPrevCoupon.DeductedAmount = decimal.NewFromInt(int64(couponInfo.Cash))
|
|
|
newPrevCoupon.EndTime = couponInfo.EndTime
|
|
|
if err := dbc.DB.Save(&newPrevCoupon).Error; err != nil {
|
|
|
return err
|
|
|
}
|
|
|
}
|
|
|
return nil
|
|
|
}
|