- limit mtps max addfee to 200.
This commit is contained in:
@@ -3,6 +3,7 @@ package mtps
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"math"
|
"math"
|
||||||
|
"time"
|
||||||
|
|
||||||
"git.rosy.net.cn/baseapi/platformapi/mtpsapi"
|
"git.rosy.net.cn/baseapi/platformapi/mtpsapi"
|
||||||
"git.rosy.net.cn/baseapi/utils"
|
"git.rosy.net.cn/baseapi/utils"
|
||||||
@@ -16,8 +17,13 @@ import (
|
|||||||
"github.com/astaxie/beego/orm"
|
"github.com/astaxie/beego/orm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
maxAddFee = 200 // 最大增加费用,单位为分,超过不发美团了
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrCanNotFindMTPSStore = errors.New("不能找到美团配送站点配置")
|
ErrCanNotFindMTPSStore = errors.New("不能找到美团配送站点配置")
|
||||||
|
ErrAddFeeExceeded = errors.New("美团配送超过基准价太多")
|
||||||
)
|
)
|
||||||
|
|
||||||
type WaybillController struct {
|
type WaybillController struct {
|
||||||
@@ -58,7 +64,7 @@ func (c *WaybillController) onWaybillMsg(msg *mtpsapi.CallbackOrderMsg) (retVal
|
|||||||
case mtpsapi.OrderStatusWaitingForSchedule:
|
case mtpsapi.OrderStatusWaitingForSchedule:
|
||||||
order.Status = model.WaybillStatusNew
|
order.Status = model.WaybillStatusNew
|
||||||
case mtpsapi.OrderStatusAccepted:
|
case mtpsapi.OrderStatusAccepted:
|
||||||
order.DesiredFee = c.calculateDeliveryFee(order)
|
order.DesiredFee, _ = c.calculateBillDeliveryFee(order)
|
||||||
order.Status = model.WaybillStatusAccepted
|
order.Status = model.WaybillStatusAccepted
|
||||||
case mtpsapi.OrderStatusPickedUp:
|
case mtpsapi.OrderStatusPickedUp:
|
||||||
order.Status = model.WaybillStatusDelivering
|
order.Status = model.WaybillStatusDelivering
|
||||||
@@ -87,14 +93,11 @@ func (c *WaybillController) callbackMsg2Waybill(msg *mtpsapi.CallbackOrderMsg) (
|
|||||||
return retVal
|
return retVal
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *WaybillController) calculateDeliveryFee(bill *model.Waybill) (retVal int64) {
|
func (c *WaybillController) calculateOrderDeliveryFee(order *model.GoodsOrder, billTime time.Time, db orm.Ormer) (delieveryFee, addFee int64) {
|
||||||
order, err := controller.OrderManager.LoadOrder(bill.VendorOrderID, bill.OrderVendorID)
|
|
||||||
if err != nil {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
var lists []orm.ParamsList
|
var lists []orm.ParamsList
|
||||||
db := orm.NewOrm()
|
if db == nil {
|
||||||
|
db = orm.NewOrm()
|
||||||
|
}
|
||||||
JxStoreID := jxutils.GetJxStoreIDFromOrder(order)
|
JxStoreID := jxutils.GetJxStoreIDFromOrder(order)
|
||||||
num, err := db.Raw(`
|
num, err := db.Raw(`
|
||||||
SELECT t2.price, t1.lng, t1.lat
|
SELECT t2.price, t1.lng, t1.lat
|
||||||
@@ -103,12 +106,11 @@ func (c *WaybillController) calculateDeliveryFee(bill *model.Waybill) (retVal in
|
|||||||
WHERE t1.storeid = ?
|
WHERE t1.storeid = ?
|
||||||
`, JxStoreID).ValuesList(&lists)
|
`, JxStoreID).ValuesList(&lists)
|
||||||
|
|
||||||
var delieveryFee int64
|
|
||||||
if err == nil && num == 1 {
|
if err == nil && num == 1 {
|
||||||
delieveryFee = utils.Str2Int64(lists[0][0].(string))
|
delieveryFee = utils.Str2Int64(lists[0][0].(string))
|
||||||
} else {
|
} else {
|
||||||
globals.SugarLogger.Warnf("calculateDeliveryFee can not calculate delivery fee for orderID:%s, num:%d, error:%v", order.VendorOrderID, num, err)
|
globals.SugarLogger.Warnf("calculateDeliveryFee can not calculate delivery fee for orderID:%s, num:%d, error:%v", order.VendorOrderID, num, err)
|
||||||
return 0
|
return 0, 0
|
||||||
}
|
}
|
||||||
|
|
||||||
lng := utils.Str2Float64(lists[0][1].(string))
|
lng := utils.Str2Float64(lists[0][1].(string))
|
||||||
@@ -118,33 +120,43 @@ func (c *WaybillController) calculateDeliveryFee(bill *model.Waybill) (retVal in
|
|||||||
distance := jxutils.EarthDistance(lat, lng, lat2, lng2) * 1.4
|
distance := jxutils.EarthDistance(lat, lng, lat2, lng2) * 1.4
|
||||||
if distance < 3 {
|
if distance < 3 {
|
||||||
} else if distance < 5 {
|
} else if distance < 5 {
|
||||||
delieveryFee += jxutils.StandardPrice2Int(math.Ceil(distance - 3))
|
addFee += jxutils.StandardPrice2Int(math.Ceil(distance - 3))
|
||||||
} else {
|
} else {
|
||||||
delieveryFee += jxutils.StandardPrice2Int(2 + 2*math.Ceil(distance-5))
|
addFee += jxutils.StandardPrice2Int(2 + 2*math.Ceil(distance-5))
|
||||||
}
|
}
|
||||||
|
|
||||||
if order.Weight < 5*1000 {
|
if order.Weight < 5*1000 {
|
||||||
} else if order.Weight < 10*1000 {
|
} else if order.Weight < 10*1000 {
|
||||||
delieveryFee += jxutils.StandardPrice2Int(0.5 * float64(order.Weight/1000-5))
|
addFee += jxutils.StandardPrice2Int(0.5 * float64(order.Weight/1000-5))
|
||||||
} else if order.Weight < 20*1000 {
|
} else if order.Weight < 20*1000 {
|
||||||
delieveryFee += jxutils.StandardPrice2Int(2.5 + 1*float64(order.Weight/1000-10))
|
addFee += jxutils.StandardPrice2Int(2.5 + 1*float64(order.Weight/1000-10))
|
||||||
} else {
|
} else {
|
||||||
delieveryFee += jxutils.StandardPrice2Int(2.5 + 10 + 2*float64(order.Weight/1000-20))
|
addFee += jxutils.StandardPrice2Int(2.5 + 10 + 2*float64(order.Weight/1000-20))
|
||||||
}
|
}
|
||||||
|
|
||||||
hour, min, sec := bill.StatusTime.Clock()
|
hour, min, sec := billTime.Clock()
|
||||||
totalSeconds := hour*3600 + min*60 + sec
|
totalSeconds := hour*3600 + min*60 + sec
|
||||||
if totalSeconds >= 11*3600+30*60 && totalSeconds <= 13*3600 { // 11:30 -- 13:00
|
if totalSeconds >= 11*3600+30*60 && totalSeconds <= 13*3600 { // 11:30 -- 13:00
|
||||||
delieveryFee += jxutils.StandardPrice2Int(3)
|
addFee += jxutils.StandardPrice2Int(3)
|
||||||
} else if totalSeconds >= 21*3600 || totalSeconds <= 6*3600 { // 21:00 -- 06:00
|
} else if totalSeconds >= 21*3600 || totalSeconds <= 6*3600 { // 21:00 -- 06:00
|
||||||
delieveryFee += jxutils.StandardPrice2Int(3)
|
addFee += jxutils.StandardPrice2Int(3)
|
||||||
}
|
}
|
||||||
return delieveryFee
|
return delieveryFee + addFee, addFee
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *WaybillController) calculateBillDeliveryFee(bill *model.Waybill) (delieveryFee, addFee int64) {
|
||||||
|
order, err := controller.OrderManager.LoadOrder(bill.VendorOrderID, bill.OrderVendorID)
|
||||||
|
if err != nil {
|
||||||
|
return 0, 0
|
||||||
|
}
|
||||||
|
return c.calculateOrderDeliveryFee(order, bill.StatusTime, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeliveryPlatformHandler
|
// DeliveryPlatformHandler
|
||||||
func (c *WaybillController) CreateWaybill(order *model.GoodsOrder) (err error) {
|
func (c *WaybillController) CreateWaybill(order *model.GoodsOrder) (err error) {
|
||||||
db := orm.NewOrm()
|
db := orm.NewOrm()
|
||||||
|
_, addFee := c.calculateOrderDeliveryFee(order, time.Now(), db)
|
||||||
|
if addFee <= maxAddFee {
|
||||||
// 忽略坐标转换错误,即使是转换出错,也只能当成转换成功来处理,底层会有错误日志输出
|
// 忽略坐标转换错误,即使是转换出错,也只能当成转换成功来处理,底层会有错误日志输出
|
||||||
lngFloat, latFloat, _ := jxutils.IntCoordinate2MarsStandard(order.ConsigneeLng, order.ConsigneeLat, order.CoordinateType)
|
lngFloat, latFloat, _ := jxutils.IntCoordinate2MarsStandard(order.ConsigneeLng, order.ConsigneeLat, order.CoordinateType)
|
||||||
billParams := &mtpsapi.CreateOrderByShopParam{
|
billParams := &mtpsapi.CreateOrderByShopParam{
|
||||||
@@ -189,6 +201,10 @@ func (c *WaybillController) CreateWaybill(order *model.GoodsOrder) (err error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
err = ErrAddFeeExceeded
|
||||||
|
globals.SugarLogger.Infof("CreateWaybill addFee exceeded too much, it's %d", addFee)
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user