- limit mtps max addfee to 200.

This commit is contained in:
gazebo
2018-07-30 17:45:38 +08:00
parent a7d63caf08
commit 0615758d9a

View File

@@ -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,76 +120,90 @@ 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)
lngFloat, latFloat, _ := jxutils.IntCoordinate2MarsStandard(order.ConsigneeLng, order.ConsigneeLat, order.CoordinateType) if addFee <= maxAddFee {
billParams := &mtpsapi.CreateOrderByShopParam{ // 忽略坐标转换错误,即使是转换出错,也只能当成转换成功来处理,底层会有错误日志输出
OrderID: jxutils.ComposeUniversalOrderID(order.VendorOrderID, order.VendorID), lngFloat, latFloat, _ := jxutils.IntCoordinate2MarsStandard(order.ConsigneeLng, order.ConsigneeLat, order.CoordinateType)
DeliveryServiceCode: mtpsapi.DeliveryServiceCodeRapid, billParams := &mtpsapi.CreateOrderByShopParam{
ReceiverName: order.ConsigneeName, OrderID: jxutils.ComposeUniversalOrderID(order.VendorOrderID, order.VendorID),
ReceiverAddress: order.ConsigneeAddress, DeliveryServiceCode: mtpsapi.DeliveryServiceCodeRapid,
ReceiverPhone: order.ConsigneeMobile, ReceiverName: order.ConsigneeName,
CoordinateType: model.CoordinateTypeMars, ReceiverAddress: order.ConsigneeAddress,
ReceiverLng: jxutils.StandardCoordinate2Int(lngFloat), ReceiverPhone: order.ConsigneeMobile,
ReceiverLat: jxutils.StandardCoordinate2Int(latFloat), CoordinateType: model.CoordinateTypeMars,
GoodsValue: jxutils.IntPrice2Standard(order.SalePrice), // todo 超价处理 ReceiverLng: jxutils.StandardCoordinate2Int(lngFloat),
GoodsWeight: float64(order.Weight) / 1000, ReceiverLat: jxutils.StandardCoordinate2Int(latFloat),
// ExpectedDeliveryTime: order.ExpectedDeliveredTime.Unix(), GoodsValue: jxutils.IntPrice2Standard(order.SalePrice), // todo 超价处理
OrderType: mtpsapi.OrderTypeASAP, GoodsWeight: float64(order.Weight) / 1000,
} // ExpectedDeliveryTime: order.ExpectedDeliveredTime.Unix(),
if billParams.DeliveryID, err = c.getDeliveryID(order, db); err == nil { OrderType: mtpsapi.OrderTypeASAP,
if billParams.ShopID, err = c.getMTPSShopID(order, db); err == nil { }
globals.SugarLogger.Debug(billParams.ShopID) if billParams.DeliveryID, err = c.getDeliveryID(order, db); err == nil {
goods := &mtpsapi.GoodsDetail{ if billParams.ShopID, err = c.getMTPSShopID(order, db); err == nil {
Goods: []*mtpsapi.GoodsItem{}, globals.SugarLogger.Debug(billParams.ShopID)
} goods := &mtpsapi.GoodsDetail{
goodItemMap := map[string]*mtpsapi.GoodsItem{} Goods: []*mtpsapi.GoodsItem{},
for _, sku := range order.Skus {
goodItem := &mtpsapi.GoodsItem{
GoodCount: sku.Count,
GoodPrice: jxutils.IntPrice2Standard(sku.SalePrice),
} }
goodItem.GoodName, goodItem.GoodUnit = jxutils.SplitSkuName(sku.SkuName) goodItemMap := map[string]*mtpsapi.GoodsItem{}
// 好像SKU名不能重复否则会报错尝试处理一下 for _, sku := range order.Skus {
if item, ok := goodItemMap[goodItem.GoodName]; !ok { goodItem := &mtpsapi.GoodsItem{
goods.Goods = append(goods.Goods, goodItem) GoodCount: sku.Count,
goodItemMap[goodItem.GoodName] = goodItem GoodPrice: jxutils.IntPrice2Standard(sku.SalePrice),
} else { }
item.GoodCount += goodItem.GoodCount goodItem.GoodName, goodItem.GoodUnit = jxutils.SplitSkuName(sku.SkuName)
// 好像SKU名不能重复否则会报错尝试处理一下
if item, ok := goodItemMap[goodItem.GoodName]; !ok {
goods.Goods = append(goods.Goods, goodItem)
goodItemMap[goodItem.GoodName] = goodItem
} else {
item.GoodCount += goodItem.GoodCount
}
}
addParams := utils.Params2Map("note", order.BuyerComment, "goods_detail", string(utils.MustMarshal(goods)))
_, err = api.MtpsAPI.CreateOrderByShop(billParams, addParams)
if err != nil {
globals.SugarLogger.Debugf("CreateWaybill, orderID:%s, billParams:%v, addParams:%v", order.VendorOrderID, billParams, addParams)
} }
}
addParams := utils.Params2Map("note", order.BuyerComment, "goods_detail", string(utils.MustMarshal(goods)))
_, err = api.MtpsAPI.CreateOrderByShop(billParams, addParams)
if err != nil {
globals.SugarLogger.Debugf("CreateWaybill, orderID:%s, billParams:%v, addParams:%v", order.VendorOrderID, billParams, addParams)
} }
} }
} else {
err = ErrAddFeeExceeded
globals.SugarLogger.Infof("CreateWaybill addFee exceeded too much, it's %d", addFee)
} }
return err return err
} }