优惠券相关逻辑

This commit is contained in:
yan.y 2024-08-14 15:45:17 +08:00
parent 3cd4e4b335
commit 39aa34f6c2
3 changed files with 72 additions and 33 deletions

View File

@ -145,7 +145,12 @@ func (p DefParty) orderCreate() web_iris.Party {
}
}
//订单总金额
var orderTotalAmount = 0
//可以参与会员折扣的金额
var needDiscountAmount = 0
//不可参与会员折扣的金额
var notDiscountAmount = 0
orderLock.Lock()
orderId := NextId.Generate().String()
orderMain := models.OrderMain{
@ -166,20 +171,20 @@ func (p DefParty) orderCreate() web_iris.Party {
coupons := CouponsMap[value.Cid]
subOrderId := NextId.Generate().String()
var totalAmount = 0
var discountTotalAmount = 0
var projectionServiceTime = 0
var goodsName string
for _, value := range value.GoodsIds {
goods := GoodsMap[value]
totalAmount = totalAmount + goods.Price
orderDetail := models.OrderDetail{SubOrderId: subOrderId, GoodsId: value, Amount: goods.Price}
//设置订单详情折扣
if coupons.Id > 0 && coupons.GoodsSubType == goods.GoodsSubType {
//单个订单折扣
discountTotalAmount = discountTotalAmount + int(utils.RoundToOneDecimalPlace(float64(goods.Price)*(float64(coupons.Discount)/100.0))*10)
} else {
discountTotalAmount = discountTotalAmount + goods.Price
orderDetail.Cid = coupons.Id
orderDetail.Discount = coupons.Discount
orderDetail.DiscountAmount = int(utils.RoundToOneDecimalPlace(float64(orderDetail.Amount)*(float64(coupons.Discount)/100.0)) * 10)
}
orderSubDetailList = append(orderSubDetailList, models.OrderDetail{SubOrderId: subOrderId, Cid: coupons.Id, Discount: coupons.Discount, GoodsId: value, Amount: goods.Price})
orderSubDetailList = append(orderSubDetailList, orderDetail)
if goods.Time != "/" && len(goods.Time) > 0 {
goodsTime, _ := strconv.Atoi(goods.Time)
projectionServiceTime += goodsTime
@ -192,15 +197,29 @@ func (p DefParty) orderCreate() web_iris.Party {
userPetInfo := GetUserPet(headerBaseInfo.Uid, value.PetId)
var petInfo = "宠物名称:" + userPetInfo.PetInfo.NickName + "</br>品种:" + userPetInfo.PetBaseInfo.Assortment + "</br>服务项目:" + goodsName
projectionServiceTimeAll += projectionServiceTime
for _, orderDetail := range orderSubDetailList {
if orderDetail.Cid > 0 {
notDiscountAmount = notDiscountAmount + orderDetail.DiscountAmount
} else {
needDiscountAmount = needDiscountAmount + orderDetail.Amount
}
}
orderSub := models.OrderSub{
OrderId: subOrderId,
MainOrderId: orderMain.OrderId,
OrderStatus: 1,
PetId: value.PetId,
PayType: 1,
Discount: 100,
TotalAmount: totalAmount,
PayAmount: totalAmount,
OrderId: subOrderId,
MainOrderId: orderMain.OrderId,
OrderStatus: 1,
PetId: value.PetId,
PayType: 1,
Discount: 100,
TotalAmount: totalAmount,
PayAmount: totalAmount,
//设置商品折扣金额用于后续打折计算
GoodsDiscountAmount: notDiscountAmount,
//设置商品非折扣金额
GoodsOriginAmount: needDiscountAmount,
//宠物信息
PetInfo: petInfo,
Status: 1,
ProjectionServiceTime: projectionServiceTime,
@ -217,22 +236,33 @@ func (p DefParty) orderCreate() web_iris.Party {
orderMain.ProjectionServiceTime = projectionServiceTimeAll
var userInfo *models.User
database.Instance().Model(&models.User{}).Where("id = ?", headerBaseInfo.Uid).Find(&userInfo)
var discountAmount = orderTotalAmount
//计算参与折扣计算的金额
var discountAmount = 0
var orderGoodsDiscountAmount = 0
//参与计算折扣的金额
var inDiscountAmount = 0
for _, orderSub := range orderSubList {
inDiscountAmount = inDiscountAmount + orderSub.GoodsOriginAmount
orderGoodsDiscountAmount = orderGoodsDiscountAmount + orderSub.GoodsDiscountAmount
}
//用户折扣排除使用优惠券的商品总金额
if userInfo.Discount > 0 {
discount := float64(userInfo.Discount)
discountAmount = int(utils.RoundToOneDecimalPlace(float64(orderTotalAmount)*(discount/100.0)) * 10)
discountAmount = int(utils.RoundToOneDecimalPlace(float64(inDiscountAmount)*(discount/100.0)) * 10)
} else {
discountAmount = int(utils.RoundToOneDecimalPlace(float64(orderTotalAmount) * 10))
discountAmount = int(utils.RoundToOneDecimalPlace(float64(inDiscountAmount) * 10))
}
orderMain.TotalAmount = orderTotalAmount
tx := database.Instance().Begin()
var db4 *gorm.DB
if userInfo.Amount >= discountAmount {
//用户需要扣除总金额
if userInfo.Amount >= discountAmount+orderGoodsDiscountAmount {
var currAmount = userInfo.Amount
orderMain.PayStatus = 1
orderMain.PayTotalAmount = discountAmount
userInfo.Amount = userInfo.Amount - discountAmount
orderMain.PayTotalAmount = discountAmount + orderGoodsDiscountAmount
userInfo.Amount = userInfo.Amount - discountAmount - orderGoodsDiscountAmount
updateValues := map[string]interface{}{
"Amount": userInfo.Amount,
}
@ -246,26 +276,25 @@ func (p DefParty) orderCreate() web_iris.Party {
UserId: userInfo.Id,
Type: 2,
OriginAmount: currAmount,
ProAmount: discountAmount,
ProAmount: discountAmount + orderGoodsDiscountAmount,
CurrAmount: userInfo.Amount,
OrderId: orderId,
ProScene: "下单",
Status: 1,
}
tx.Model(&models.UserAmountRecord{}).Create(&userAmountRecord)
zap_server.ZAPLOG.Info("会员金额扣除", zap.Any("用户ID", userInfo.Id), zap.Any("订单号", orderMain.OrderId), zap.Any("当前余额", currAmount), zap.Any("扣除余额", discountAmount), zap.Any("剩余余额", userInfo.Amount), zap.Any("折扣", userInfo.Discount))
discount := float64(userInfo.Discount)
zap_server.ZAPLOG.Info("会员金额扣除", zap.Any("用户ID", userInfo.Id), zap.Any("订单号", orderMain.OrderId), zap.Any("当前余额", currAmount), zap.Any("扣除余额", discountAmount+orderGoodsDiscountAmount), zap.Any("剩余余额", userInfo.Amount), zap.Any("折扣", userInfo.Discount))
for index, _ := range orderSubList {
orderSubList[index].Discount = userInfo.Discount
//orderSubList[index].PayAmount = int32(math.Round(float64(orderSubList[index].TotalAmount) * (discount / 100.0)))
orderSubList[index].PayAmount = int(utils.RoundToOneDecimalPlace(float64(orderSubList[index].TotalAmount)*(discount/100.0)) * 10)
orderSubList[index].PayAmount = discountAmount + orderGoodsDiscountAmount
}
} else {
userAmountRecord := models.UserAmountRecord{
UserId: userInfo.Id,
Type: 3,
OriginAmount: userInfo.Amount,
ProAmount: discountAmount,
ProAmount: discountAmount + orderGoodsDiscountAmount,
CurrAmount: userInfo.Amount,
OrderId: orderId,
ProScene: "下单",
@ -320,9 +349,11 @@ func GetOrderDetail(orderId string) OrderDetail {
Uid: orderMain.Uid,
UserAmount: float64(userInfo.Amount) / 10.0,
}
if orderMain.PayStatus == 1 {
orderDetail.DiscountAmount = strconv.FormatFloat(float64(orderMain.PayTotalAmount)/10.0, 'f', 1, 64)
}
var discountTotalAmount = 0
var subOrderList []SubOrder
var orderSubList []models.OrderSub
database.Instance().Model(&models.OrderSub{}).Where("main_order_id = ? ", orderMain.OrderId).Find(&orderSubList)
@ -343,6 +374,7 @@ func GetOrderDetail(orderId string) OrderDetail {
Discount: float64(orderSub.Discount),
Goods: goods,
}
discountTotalAmount = discountTotalAmount + orderSub.GoodsOriginAmount*10 + orderSub.GoodsDiscountAmount
if orderSub.PayType == 3 {
x := float64(orderSub.PayAmount) / 10
orderSubR1.PayAmount = strconv.FormatFloat(x, 'f', 1, 64)
@ -359,6 +391,9 @@ func GetOrderDetail(orderId string) OrderDetail {
}
subOrderList = append(subOrderList, orderSubR1)
}
if discountTotalAmount > 0 {
orderDetail.DiscountAmount = strconv.Itoa(discountTotalAmount / 10.0)
}
var carOrder models.CarOrder
database.Instance().Model(&models.CarOrder{}).Where("order_id = ?", orderMain.OrderId).Find(&carOrder)
if carOrder.Id > 0 {

View File

@ -67,6 +67,7 @@ func (p DefParty) serviceAddOrEdit() web_iris.Party {
Addr: serviceAddOrEditRequest.Addr,
Longitude: serviceAddOrEditRequest.Longitude,
Latitude: serviceAddOrEditRequest.Latitude,
Status: 1,
}
if len(serviceAddOrEditRequest.Longitude) == 0 || len(serviceAddOrEditRequest.Latitude) == 0 {
addrGeocoding := utils.GetAddrGeocoding(utils.BmapGeoCodingRequest{Address: serviceAddOrEditRequest.Addr})

View File

@ -154,10 +154,12 @@ type OrderSub struct {
PetId int64 `gorm:"index" json:"petId"` //宠物ID
PayType int `gorm:"not null" json:"payType"` //支付方式 1线下 2线上 3会员余额
Discount int `json:"discount"` //折扣
TotalAmount int `gorm:"not null" json:"totalAmount"` //总金额
TotalAmount int `gorm:"not null" json:"totalAmount"` //总金额 - 原价
ProjectionServiceTime int `json:"projectionServiceTime"` //服务预估时长
PayStatus int `gorm:"default:0" json:"payStatus"` //支付状态 0未支付 1已支付
PayAmount int `json:"payAmount"` //实际支付金额
PayAmount int `json:"payAmount"` //实际支付金额 - 折扣金额
GoodsDiscountAmount int `json:"-"` //商品折扣金额
GoodsOriginAmount int `json:"-"` //商品原价 除去折扣价的金额
PayTime time.Time `gorm:"type:timestamp;" json:"payTime"` //支付时间
PayRemark string `json:"payRemark"` //支付备注
PetInfo string `json:"petInfo"` //宠物快照信息
@ -168,12 +170,13 @@ type OrderSub struct {
// OrderDetail 订单明细
type OrderDetail struct {
Id int64 `gorm:"primaryKey;autoIncrement" json:"id"` //id
Cid int `json:"cid"` //子订单ID
Discount int `json:"discount"` //折扣
SubOrderId string `gorm:"index;not null" json:"subOrderId"` //子订单ID
GoodsId int64 `gorm:"index;not null" json:"goodsId"` //商品ID
Amount int `gorm:"not null" json:"amount"` //价格
Id int64 `gorm:"primaryKey;autoIncrement" json:"id"` //id
Cid int `json:"cid"` //子订单ID
Discount int `json:"discount"` //折扣
SubOrderId string `gorm:"index;not null" json:"subOrderId"` //子订单ID
GoodsId int64 `gorm:"index;not null" json:"goodsId"` //商品ID
Amount int `gorm:"not null" json:"amount"` //价格
DiscountAmount int `json:"-"` //折扣价格
}
// OrderServiceRecord 订单服务记录