- refactor CalculateOrderDeliveryFee

- use QueryDeliverFee and AddOrderAfterQuery for dada
This commit is contained in:
gazebo
2019-01-24 14:34:49 +08:00
parent 3bceb5b6b0
commit 16a3261bd1
3 changed files with 147 additions and 130 deletions

View File

@@ -3,7 +3,6 @@ package mtps
import (
"errors"
"fmt"
"math"
"time"
"git.rosy.net.cn/baseapi/platformapi/mtpsapi"
@@ -13,6 +12,7 @@ import (
"git.rosy.net.cn/jx-callback/business/model/dao"
"git.rosy.net.cn/jx-callback/business/model/legacymodel"
"git.rosy.net.cn/jx-callback/business/partner"
"git.rosy.net.cn/jx-callback/business/partner/delivery"
"git.rosy.net.cn/jx-callback/globals"
"git.rosy.net.cn/jx-callback/globals/api"
"github.com/astaxie/beego"
@@ -27,7 +27,6 @@ const (
var (
ErrCanNotFindMTPSStore = errors.New("不能找到美团配送站点配置")
ErrStoreNoPriceInfo = errors.New("找不到门店的美团配送价格信息")
ErrStoreNoCoordinate = errors.New("找不到门店的坐标信息")
)
var (
@@ -85,7 +84,7 @@ func (c *DeliveryHandler) onWaybillMsg(msg *mtpsapi.CallbackOrderMsg) (retVal *m
case mtpsapi.OrderStatusWaitingForSchedule:
order.Status = model.WaybillStatusNew
case mtpsapi.OrderStatusAccepted:
order.DesiredFee, _ = c.calculateBillDeliveryFee(order)
order.DesiredFee, _ = delivery.CalculateBillDeliveryFee(order)
order.Status = model.WaybillStatusAccepted
case mtpsapi.OrderStatusPickedUp:
order.Status = model.WaybillStatusDelivering
@@ -115,97 +114,10 @@ func (c *DeliveryHandler) callbackMsg2Waybill(msg *mtpsapi.CallbackOrderMsg) (re
return retVal
}
func (c *DeliveryHandler) calculateOrderDeliveryFee(order *model.GoodsOrder, billTime time.Time, db orm.Ormer) (deliveryFee, addFee int64, err error) {
if db == nil {
db = orm.NewOrm()
}
jxStoreID := jxutils.GetSaleStoreIDFromOrder(order)
var lng, lat float64
if globals.OrderUseNewTable || jxStoreID == globals.DebugStoreID {
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)
} else {
var lists []orm.ParamsList
num, err := db.Raw(`
SELECT t2.price, t1.lng, t1.lat
FROM jxstore t1
JOIN mtpsdeliveryprice t2 ON t2.citycode = t1.area
WHERE t1.storeid = ?
`, jxStoreID).ValuesList(&lists)
if err != nil || num == 0 {
globals.SugarLogger.Warnf("calculateDeliveryFee can not calculate delivery fee for orderID:%s, num:%d, error:%v", order.VendorOrderID, num, err)
if err != nil {
return 0, 0, err
}
return 0, 0, ErrStoreNoPriceInfo
}
lng = utils.Str2Float64(utils.Interface2String(lists[0][1]))
lat = utils.Str2Float64(utils.Interface2String(lists[0][2]))
deliveryFee = utils.Str2Int64(lists[0][0].(string))
}
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)
distance := jxutils.EarthDistance(lat, lng, lat2, lng2) * 1.4
if distance < 3 {
} else if distance < 5 {
addFee += jxutils.StandardPrice2Int(math.Ceil(distance - 3))
} else {
addFee += jxutils.StandardPrice2Int(2 + 2*math.Ceil(distance-5))
}
if order.Weight < 5*1000 {
} else if order.Weight < 10*1000 {
addFee += jxutils.StandardPrice2Int(0.5 * float64(jxutils.IntWeight2Float(order.Weight)-5))
} else if order.Weight < 20*1000 {
addFee += jxutils.StandardPrice2Int(2.5 + 1*float64(jxutils.IntWeight2Float(order.Weight)-10))
} else {
addFee += jxutils.StandardPrice2Int(2.5 + 10 + 2*float64(jxutils.IntWeight2Float(order.Weight)-20))
}
hour, min, sec := billTime.Clock()
totalSeconds := hour*3600 + min*60 + sec
if totalSeconds >= 11*3600 && totalSeconds <= 14*3600 { // 11:00 -- 14:00
addFee += jxutils.StandardPrice2Int(3)
}
// else if totalSeconds >= 21*3600 || totalSeconds <= 6*3600 { // 21:00 -- 06:00
// addFee += jxutils.StandardPrice2Int(3)
// }
return deliveryFee + addFee, addFee, nil
}
func (c *DeliveryHandler) calculateBillDeliveryFee(bill *model.Waybill) (deliveryFee, addFee int64) {
order, err := partner.CurOrderManager.LoadOrder(bill.VendorOrderID, bill.OrderVendorID)
if err != nil {
return 0, 0
}
deliveryFee, addFee, _ = c.calculateOrderDeliveryFee(order, bill.StatusTime, nil)
return deliveryFee, addFee
}
// IDeliveryPlatformHandler
func (c *DeliveryHandler) CreateWaybill(order *model.GoodsOrder, policy func(deliveryFee, addFee int64) error) (bill *model.Waybill, err error) {
db := orm.NewOrm()
deliveryFee, addFee, err := c.calculateOrderDeliveryFee(order, time.Now(), db)
deliveryFee, addFee, err := delivery.CalculateOrderDeliveryFee(order, time.Now(), db)
if err == nil {
if policy != nil {
err = policy(deliveryFee, addFee)
@@ -300,43 +212,19 @@ func (c *DeliveryHandler) getDeliveryID(order *model.GoodsOrder, db orm.Ormer) (
func (c *DeliveryHandler) getMTPSShopID(order *model.GoodsOrder, db orm.Ormer) (retVal string, err error) {
saleStoreID := jxutils.GetSaleStoreIDFromOrder(order)
if globals.OrderUseNewTable || saleStoreID == globals.DebugStoreID {
db2 := dao.WrapDB(db)
storeCourierList, err2 := dao.GetOpenedStoreCouriersByStoreID(db2, saleStoreID, model.VendorIDMTPS)
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
db2 := dao.WrapDB(db)
storeCourierList, err2 := dao.GetOpenedStoreCouriersByStoreID(db2, saleStoreID, model.VendorIDMTPS)
if err = err2; err != nil && err != orm.ErrNoRows {
return "", err
}
sql := "SELECT zs_store_id FROM jx_to_zs_store_map WHERE jx_store_id = ?"
var lists []orm.ParamsList
num, err := db.Raw(sql, saleStoreID).ValuesList(&lists)
if err == nil && num == 1 {
retVal = lists[0][0].(string)
if beego.BConfig.RunMode == "dev" {
retVal = "test_0001"
}
} else {
globals.SugarLogger.Infof("getMTPSShopID can not find mtps store info for orderID:%s, store:%d, num:%d, error:%v", order.VendorOrderID, saleStoreID, num, err)
if err == nil {
err = ErrCanNotFindMTPSStore
}
tmpLog := &legacymodel.TempLog{
VendorOrderID: order.VendorOrderID,
RefVendorOrderID: order.VendorOrderID,
Msg: fmt.Sprintf("getMTPSShopID can not find mtps store info for orderID:%s, store:%d, num:%d, error:%v", order.VendorOrderID, saleStoreID, num, err),
}
db.Insert(tmpLog)
if len(storeCourierList) == 0 {
return "", partner.ErrStoreHaveNoCourier
}
return retVal, err
retVal = storeCourierList[0].VendorStoreID
if beego.BConfig.RunMode == "dev" {
retVal = "test_0001"
}
return retVal, nil
}
func limitOrderWeight(weight int) int {