- refactor CalculateOrderDeliveryFee
- use QueryDeliverFee and AddOrderAfterQuery for dada
This commit is contained in:
104
business/partner/delivery/delivery.go
Normal file
104
business/partner/delivery/delivery.go
Normal file
@@ -0,0 +1,104 @@
|
||||
package delivery
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"math"
|
||||
"time"
|
||||
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"git.rosy.net.cn/jx-callback/business/jxutils"
|
||||
"git.rosy.net.cn/jx-callback/business/model"
|
||||
"git.rosy.net.cn/jx-callback/business/model/dao"
|
||||
"git.rosy.net.cn/jx-callback/business/partner"
|
||||
"git.rosy.net.cn/jx-callback/globals"
|
||||
"github.com/astaxie/beego/orm"
|
||||
)
|
||||
|
||||
const (
|
||||
warningDistance = 10 // 公里
|
||||
warningWeight = 50 * 1000 // 克
|
||||
)
|
||||
|
||||
var (
|
||||
ErrStoreNoCoordinate = errors.New("找不到门店的坐标信息")
|
||||
)
|
||||
|
||||
func CalculateOrderDeliveryFee(order *model.GoodsOrder, billTime time.Time, db orm.Ormer) (deliveryFee, addFee int64, err error) {
|
||||
globals.SugarLogger.Debugf("CalculateOrderDeliveryFee orderID:%s", order.VendorOrderID)
|
||||
if db == nil {
|
||||
db = orm.NewOrm()
|
||||
}
|
||||
jxStoreID := jxutils.GetSaleStoreIDFromOrder(order)
|
||||
var lng, lat float64
|
||||
priceInfo := &struct {
|
||||
Price int
|
||||
Lng int
|
||||
Lat int
|
||||
}{}
|
||||
db2 := dao.WrapDB(db)
|
||||
if err = dao.GetRow(db2, priceInfo, `
|
||||
SELECT t2.mtps_price price, t1.lng, t1.lat
|
||||
FROM store t1
|
||||
JOIN place t2 ON t1.city_code = t2.code
|
||||
WHERE t1.id = ? AND t1.deleted_at = ?
|
||||
`, jxStoreID, utils.DefaultTimeValue); err != nil {
|
||||
return 0, 0, err
|
||||
}
|
||||
lng = jxutils.IntCoordinate2Standard(priceInfo.Lng)
|
||||
lat = jxutils.IntCoordinate2Standard(priceInfo.Lat)
|
||||
deliveryFee = int64(priceInfo.Price)
|
||||
|
||||
if lng == 0 || lat == 0 {
|
||||
globals.SugarLogger.Warnf("calculateDeliveryFee can not calculate delivery fee for orderID:%s, because no coordinate info", order.VendorOrderID)
|
||||
return 0, 0, ErrStoreNoCoordinate
|
||||
}
|
||||
lng2, lat2, _ := jxutils.IntCoordinate2MarsStandard(order.ConsigneeLng, order.ConsigneeLat, order.CoordinateType)
|
||||
|
||||
var distanceAddFee, weightAddFee, timeAddFee int64
|
||||
distance := jxutils.EarthDistance(lat, lng, lat2, lng2) * 1.4
|
||||
if distance < 3 {
|
||||
} else if distance < 5 {
|
||||
distanceAddFee = jxutils.StandardPrice2Int(math.Ceil(distance - 3))
|
||||
} else {
|
||||
distanceAddFee = jxutils.StandardPrice2Int(2 + 2*math.Ceil(distance-5))
|
||||
if distance > warningDistance {
|
||||
globals.SugarLogger.Warnf("CalculateOrderDeliveryFee orderID:%s distance is %.3fkm", order.VendorOrderID, distance)
|
||||
}
|
||||
}
|
||||
globals.SugarLogger.Debugf("CalculateOrderDeliveryFee orderID:%s", order.VendorOrderID)
|
||||
|
||||
if order.Weight < 5*1000 {
|
||||
} else if order.Weight < 10*1000 {
|
||||
weightAddFee = jxutils.StandardPrice2Int(0.5 * float64(jxutils.IntWeight2Float(order.Weight)-5))
|
||||
} else if order.Weight < 20*1000 {
|
||||
weightAddFee = jxutils.StandardPrice2Int(2.5 + 1*float64(jxutils.IntWeight2Float(order.Weight)-10))
|
||||
} else {
|
||||
weightAddFee = jxutils.StandardPrice2Int(2.5 + 10 + 2*float64(jxutils.IntWeight2Float(order.Weight)-20))
|
||||
if order.Weight > warningWeight {
|
||||
globals.SugarLogger.Warnf("CalculateOrderDeliveryFee orderID:%s weight is %d", order.VendorOrderID, order.Weight)
|
||||
}
|
||||
}
|
||||
|
||||
hour, min, sec := billTime.Clock()
|
||||
totalSeconds := hour*3600 + min*60 + sec
|
||||
// 午高峰加价
|
||||
if totalSeconds >= 11*3600 && totalSeconds <= 14*3600 { // 11:00 -- 14:00
|
||||
timeAddFee = jxutils.StandardPrice2Int(3)
|
||||
}
|
||||
// 夜间加价
|
||||
// else if totalSeconds >= 21*3600 || totalSeconds <= 6*3600 { // 21:00 -- 06:00
|
||||
// timeAddFee = jxutils.StandardPrice2Int(3)
|
||||
// }
|
||||
addFee = distanceAddFee + weightAddFee + timeAddFee
|
||||
globals.SugarLogger.Debugf("CalculateOrderDeliveryFee orderID:%s, deliveryFee:%d addFee:%d, distance:%.3fkm distanceAddFee:%d, weight:%dg weightAddFee:%d, time:%d timeAddFee:%d", order.VendorOrderID, deliveryFee, addFee, distance, distanceAddFee, order.Weight, weightAddFee, totalSeconds, timeAddFee)
|
||||
return deliveryFee + addFee, addFee, nil
|
||||
}
|
||||
|
||||
func CalculateBillDeliveryFee(bill *model.Waybill) (deliveryFee, addFee int64) {
|
||||
order, err := partner.CurOrderManager.LoadOrder(bill.VendorOrderID, bill.OrderVendorID)
|
||||
if err != nil {
|
||||
return 0, 0
|
||||
}
|
||||
deliveryFee, addFee, _ = CalculateOrderDeliveryFee(order, bill.StatusTime, nil)
|
||||
return deliveryFee, addFee
|
||||
}
|
||||
Reference in New Issue
Block a user