- refactor order deliveryType

This commit is contained in:
gazebo
2019-01-23 18:31:34 +08:00
parent fb894c94f8
commit 3a0f47790f
7 changed files with 131 additions and 68 deletions

View File

@@ -12,11 +12,13 @@ import (
"git.rosy.net.cn/jx-callback/business/partner"
"git.rosy.net.cn/jx-callback/globals"
"git.rosy.net.cn/jx-callback/globals/api"
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
)
const (
maxCargoPrice = 63.99 // 单位为,达达最大价格,超过这个价格配送费会增加
maxOrderPrice = 6399 // 单位为,达达最大价格,超过这个价格配送费会增加
maxOrderWeight = 5000 // 5公斤
)
var (
@@ -97,55 +99,54 @@ func (c *DeliveryHandler) callbackMsg2Waybill(msg *dadaapi.CallbackMsg) (retVal
// IDeliveryPlatformHandler
func (c *DeliveryHandler) CreateWaybill(order *model.GoodsOrder, policy func(deliveryFee, addFee int64) error) (bill *model.Waybill, err error) {
billParams := &dadaapi.OperateOrderRequiredParams{
ShopNo: utils.Int2Str(order.StoreID), // 当前达达的门店号与京西是一样的
// ShopNo: utils.Int2Str(order.StoreID), // 当前达达的门店号与京西是一样的
OriginID: jxutils.ComposeUniversalOrderID(order.VendorOrderID, order.VendorID),
CargoPrice: jxutils.IntPrice2Standard(order.ActualPayPrice),
CargoPrice: jxutils.IntPrice2Standard(limitOrderPrice(order.ActualPayPrice)),
IsPrepay: 0,
ReceiverName: utils.FilterMb4(order.ConsigneeName),
ReceiverAddress: utils.FilterMb4(order.ConsigneeAddress),
ReceiverPhone: order.ConsigneeMobile,
}
if billParams.CargoPrice > maxCargoPrice {
billParams.CargoPrice = maxCargoPrice
}
db := orm.NewOrm()
if billParams.CityCode, err = c.getDataCityCodeFromOrder(order, db); err == nil {
billParams.ReceiverLng, billParams.ReceiverLat, _ = jxutils.IntCoordinate2MarsStandard(order.ConsigneeLng, order.ConsigneeLat, order.CoordinateType)
addParams := map[string]interface{}{
"info": utils.FilterMb4(order.BuyerComment),
// "origin_mark": model.VendorChineseNames[order.VendorID],
"origin_mark_no": fmt.Sprintf("%d", order.OrderSeq),
"cargo_type": 13,
"cargo_weight": jxutils.IntWeight2Float(order.Weight),
"cargo_num": order.GoodsCount,
}
if billParams.ShopNo, err = c.getDadaShopID(order, db); err == nil {
if billParams.CityCode, err = c.getDataCityCodeFromOrder(order, db); err == nil {
billParams.ReceiverLng, billParams.ReceiverLat, _ = jxutils.IntCoordinate2MarsStandard(order.ConsigneeLng, order.ConsigneeLat, order.CoordinateType)
addParams := map[string]interface{}{
"info": utils.FilterMb4(order.BuyerComment),
// "origin_mark": model.VendorChineseNames[order.VendorID],
"origin_mark_no": fmt.Sprintf("%d", order.OrderSeq),
"cargo_type": 13,
"cargo_weight": jxutils.IntWeight2Float(limitOrderWeight(order.Weight)),
"cargo_num": order.GoodsCount,
}
// 达达要求第二次创建运单,调用函数不同。所以查找两天内有无相同订单号的运单
var lists []orm.ParamsList
num, err2 := db.Raw(`
// 达达要求第二次创建运单,调用函数不同。所以查找两天内有无相同订单号的运单
var lists []orm.ParamsList
num, err2 := db.Raw(`
SELECT vendor_waybill_id
FROM waybill
WHERE waybill_created_at > DATE_ADD(NOW(), interval -2 day)
AND vendor_order_id = ?
AND waybill_vendor_id = ?
`, jxutils.ComposeUniversalOrderID(order.VendorOrderID, order.VendorID), model.VendorIDDada).ValuesList(&lists)
var result *dadaapi.CreateOrderResponse
if err2 == nil && num > 0 {
globals.SugarLogger.Debugf("CreateWaybill orderID:%s num=%d use ReaddOrder", order.VendorOrderID, num)
result, err = api.DadaAPI.ReaddOrder(billParams, addParams)
} else {
if err2 != nil {
globals.SugarLogger.Warnf("CreateWaybill orderID:%s error:%v", order.VendorOrderID, err2)
var result *dadaapi.CreateOrderResponse
if err2 == nil && num > 0 {
globals.SugarLogger.Debugf("CreateWaybill orderID:%s num=%d use ReaddOrder", order.VendorOrderID, num)
result, err = api.DadaAPI.ReaddOrder(billParams, addParams)
} else {
if err2 != nil {
globals.SugarLogger.Warnf("CreateWaybill orderID:%s error:%v", order.VendorOrderID, err2)
}
result, err = api.DadaAPI.AddOrder(billParams, addParams)
}
result, err = api.DadaAPI.AddOrder(billParams, addParams)
}
if err == nil && result != nil {
bill = &model.Waybill{
VendorOrderID: order.VendorOrderID,
OrderVendorID: order.VendorID,
WaybillVendorID: model.VendorIDDada,
DesiredFee: jxutils.StandardPrice2Int(result.DeliverFee),
ActualFee: jxutils.StandardPrice2Int(result.Fee),
if err == nil && result != nil {
bill = &model.Waybill{
VendorOrderID: order.VendorOrderID,
OrderVendorID: order.VendorID,
WaybillVendorID: model.VendorIDDada,
DesiredFee: jxutils.StandardPrice2Int(result.DeliverFee),
ActualFee: jxutils.StandardPrice2Int(result.Fee),
}
}
}
}
@@ -227,3 +228,34 @@ func (c *DeliveryHandler) getDataCityCodeFromOrder(order *model.GoodsOrder, db o
}
return retVal, err
}
func (c *DeliveryHandler) getDadaShopID(order *model.GoodsOrder, db orm.Ormer) (retVal string, err error) {
saleStoreID := jxutils.GetSaleStoreIDFromOrder(order)
db2 := dao.WrapDB(db)
storeCourierList, err2 := dao.GetOpenedStoreCouriersByStoreID(db2, saleStoreID, model.VendorIDDada)
if err = err2; err != nil && err != orm.ErrNoRows {
return "", err
}
if len(storeCourierList) == 0 {
return "", partner.ErrStoreHaveNoCourier
}
retVal = storeCourierList[0].VendorStoreID
if beego.BConfig.RunMode == "dev" {
retVal = "test_0001"
}
return retVal, nil
}
func limitOrderPrice(price int64) int64 {
if price > maxOrderPrice {
return maxOrderPrice
}
return price
}
func limitOrderWeight(weight int) int {
if weight > maxOrderWeight {
return maxOrderWeight
}
return weight
}

View File

@@ -19,6 +19,11 @@ import (
"github.com/astaxie/beego/orm"
)
const (
// maxOrderPrice = 6399 // 单位为分,达达最大价格,超过这个价格配送费会增加
maxOrderWeight = 5000 // 5公斤
)
var (
ErrCanNotFindMTPSStore = errors.New("不能找到美团配送站点配置")
ErrStoreNoPriceInfo = errors.New("找不到门店的美团配送价格信息")
@@ -218,7 +223,7 @@ func (c *DeliveryHandler) CreateWaybill(order *model.GoodsOrder, policy func(del
ReceiverLng: jxutils.StandardCoordinate2Int(lngFloat),
ReceiverLat: jxutils.StandardCoordinate2Int(latFloat),
GoodsValue: jxutils.IntPrice2Standard(order.ActualPayPrice), // todo 超价处理
GoodsWeight: float64(jxutils.IntWeight2Float(order.Weight)),
GoodsWeight: float64(jxutils.IntWeight2Float(limitOrderWeight(order.Weight))),
// ExpectedDeliveryTime: order.ExpectedDeliveredTime.Unix(),
OrderType: mtpsapi.OrderTypeASAP,
}
@@ -297,7 +302,7 @@ func (c *DeliveryHandler) getMTPSShopID(order *model.GoodsOrder, db orm.Ormer) (
saleStoreID := jxutils.GetSaleStoreIDFromOrder(order)
if globals.OrderUseNewTable || saleStoreID == globals.DebugStoreID {
db2 := dao.WrapDB(db)
storeCourierList, err2 := dao.GetStoreCouriersByStoreID(db2, saleStoreID, model.VendorIDMTPS)
storeCourierList, err2 := dao.GetOpenedStoreCouriersByStoreID(db2, saleStoreID, model.VendorIDMTPS)
if err = err2; err != nil && err != orm.ErrNoRows {
return "", err
}
@@ -333,3 +338,10 @@ func (c *DeliveryHandler) getMTPSShopID(order *model.GoodsOrder, db orm.Ormer) (
}
return retVal, err
}
func limitOrderWeight(weight int) int {
if weight > maxOrderWeight {
return maxOrderWeight
}
return weight
}