- 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

@@ -8,10 +8,8 @@ import (
"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
"git.rosy.net.cn/jx-callback/business/model"
"git.rosy.net.cn/jx-callback/business/model/dao"
"git.rosy.net.cn/jx-callback/business/model/legacymodel2"
"git.rosy.net.cn/jx-callback/business/partner"
"git.rosy.net.cn/jx-callback/globals"
"github.com/astaxie/beego/orm"
)
func (c *BaseScheduler) CreateWaybillOnProviders(ctx *jxcontext.Context, vendorOrderID string, vendorID int, userName string) (bills []*model.Waybill, err error) {
@@ -41,7 +39,7 @@ func (c *BaseScheduler) SelfDeliveredAndUpdateStatus(ctx *jxcontext.Context, ven
globals.SugarLogger.Infof("SelfDeliveredAndUpdateStatus orderID:%s userName:%s", vendorOrderID, userName)
order, err := partner.CurOrderManager.LoadOrder(vendorOrderID, vendorID)
if err == nil {
if getStoreDeliveryType(order) == scheduler.StoreDeliveryTypeByStore {
if GetStoreDeliveryType(order, nil) == scheduler.StoreDeliveryTypeByStore {
err = c.SelfDeliverDelievered(order, userName)
} else {
err = c.Swtich2SelfDelivered(order, userName)
@@ -75,28 +73,21 @@ func (c *BaseScheduler) PickupGoodsAndUpdateStatus(ctx *jxcontext.Context, vendo
return err
}
func getStoreDeliveryType(order *model.GoodsOrder) (deliveryType int) {
func GetStoreDeliveryType(order *model.GoodsOrder, storeMap *model.StoreMap) (deliveryType int) {
jxStoreID := jxutils.GetSaleStoreIDFromOrder(order)
if globals.OrderUseNewTable || jxStoreID == globals.DebugStoreID {
deliveryType = scheduler.StoreDeliveryTypeByPlatform // 缺省值
if storeMap, _ := dao.GetStoreMapByStoreID(nil, jxStoreID, order.VendorID); storeMap != nil {
deliveryType = int(storeMap.DeliveryType)
}
return deliveryType
if storeMap == nil {
storeMap, _ = dao.FakeGetStoreMapByStoreID(nil, jxStoreID, order.VendorID)
}
storefeature := &legacymodel2.Jxstorefeature{
Id: jxStoreID,
}
if storefeature.Id != 0 {
db := orm.NewOrm()
err := db.Read(storefeature, "Id")
if err == nil {
if order.VendorID == model.VendorIDJD {
return int(storefeature.JdDeliveryType)
deliveryType = scheduler.StoreDeliveryTypeByPlatform // 缺省值
if storeMap != nil {
deliveryType = int(storeMap.DeliveryType)
// 微盟订单,要确认有绑达达,才能是非自送的
if order.VendorID == model.VendorIDWSC && deliveryType != scheduler.StoreDeliveryTypeByStore {
if courierMapList, _ := dao.GetOpenedStoreCouriersByStoreID(nil, jxStoreID, model.VendorIDDada); len(courierMapList) > 0 {
deliveryType = scheduler.StoreDeliveryTypeByStore
}
return int(storefeature.ElmDeliveryType)
}
globals.SugarLogger.Infof("getStoreDeliveryType read storefeature failed with error:%v", err)
}
return scheduler.StoreDeliveryTypeByPlatform
return deliveryType
}

View File

@@ -110,23 +110,26 @@ func (s *WatchOrderInfo) updateOrderStoreFeature(order *model.GoodsOrder) (err e
if globals.OrderUseNewTable || jxStoreID == globals.DebugStoreID {
if jxStoreID > 0 {
db := dao.GetDB()
storeMap, err2 := dao.GetStoreMapByStoreID(db, jxStoreID, order.VendorID)
storeMap, err2 := dao.FakeGetStoreMapByStoreID(db, jxStoreID, order.VendorID)
if err = err2; err != nil {
return err
}
s.autoPickupTimeoutMinute = int(storeMap.AutoPickup)
s.storeDeliveryType = int(storeMap.DeliveryType)
s.storeDeliveryType = basesch.GetStoreDeliveryType(order, storeMap)
if s.storeDeliveryType == scheduler.StoreDeliveryTypeByStore {
order.DeliveryFlag |= model.OrderDeliveryFlagMaskPurcahseDisabled
}
isNeedSchedule := s.storeDeliveryType == scheduler.StoreDeliveryTypeByStore || storeMap.DeliveryCompetition != 0
if isNeedSchedule {
vendorList, err2 := dao.GetStoreCouriersByStoreID(db, jxStoreID, -1)
vendorList, err2 := dao.GetOpenedStoreCouriersByStoreID(db, jxStoreID, -1)
if err = err2; err != nil {
return err
}
for _, v := range vendorList {
s.supported3rdCarriers = append(s.supported3rdCarriers, v.VendorID)
// 达达作为微商城的自有配送,不参与竞争配送
if !(order.VendorID == model.VendorIDWSC && v.VendorID == model.VendorIDDada) {
s.supported3rdCarriers = append(s.supported3rdCarriers, v.VendorID)
}
}
if len(s.supported3rdCarriers) == 0 {
isNeedSchedule = false

View File

@@ -9,7 +9,7 @@ import (
const (
StoreDeliveryTypeCrowdSourcing = 0 //缺省,平台众包配送,可转自送
StoreDeliveryTypeByPlatform = 1 //平台专送
StoreDeliveryTypeByStore = 2 //完全门店自送
StoreDeliveryTypeByStore = 2 //完全门店自送,这个表示的意思是平台的门店属性(就是购物平台不负责配送),而不是真正是否是老板自己送
)
const (

View File

@@ -157,7 +157,29 @@ func GetStoreMapByStoreID(db *DaoDB, storeID, vendorID int) (storeMap *model.Sto
return storeMap, nil
}
func GetStoreCouriersByStoreID(db *DaoDB, storeID, vendorID int) (storeMaps []*model.StoreMap, err error) {
func FakeGetStoreMapByStoreID(db *DaoDB, storeID, vendorID int) (storeMap *model.StoreMap, err error) {
vendorID2 := vendorID
if vendorID == model.VendorIDWSC {
vendorID2 = model.VendorIDJD // 微商城的属性以京东属性为准(以免再绑定)
}
if storeMap, err = GetStoreMapByStoreID(db, storeID, vendorID2); vendorID == model.VendorIDWSC && IsNoRowsError(err) {
err = nil
storeMap = &model.StoreMap{
StoreID: storeID,
VendorID: vendorID2,
Status: model.StoreStatusOpened,
PricePercentage: 100,
AutoPickup: 1,
DeliveryType: model.StoreDeliveryTypeByStore,
// DeliveryFee
DeliveryCompetition: 1,
IsSync: 1,
}
}
return storeMap, err
}
func GetOpenedStoreCouriersByStoreID(db *DaoDB, storeID, vendorID int) (storeMaps []*model.StoreCourierMap, err error) {
if db == nil {
db = GetDB()
}

View File

@@ -54,7 +54,7 @@ func GetPossibleStoresByPlaceName(db *DaoDB, cityName, provinceName string) (sto
FROM store t1
JOIN place t2 ON t2.code = t1.city_code AND t2.name = ?
LEFT JOIN store_map t5 ON t1.id = t5.store_id AND t5.vendor_id = ? AND t5.deleted_at = ?
WHERE t1.status = ?
WHERE t1.status = ? AND (SELECT COUNT(*) FROM store_map t10 WHERE t10.store_id = t1.id AND t10.deleted_at = '1970-01-01 00:00:00' AND t10.status <> ?) > 0
`,
`
SELECT t1.*, t5.vendor_store_id
@@ -62,13 +62,13 @@ func GetPossibleStoresByPlaceName(db *DaoDB, cityName, provinceName string) (sto
JOIN place t2 ON t2.code = t1.city_code
JOIN place t3 ON t3.code = t2.parent_code AND t3.name = ?
LEFT JOIN store_map t5 ON t1.id = t5.store_id AND t5.vendor_id = ? AND t5.deleted_at = ?
WHERE t1.status = ?
WHERE t1.status = ? AND (SELECT COUNT(*) FROM store_map t10 WHERE t10.store_id = t1.id AND t10.deleted_at = '1970-01-01 00:00:00' AND t10.status <> ?) > 0
`,
`
SELECT t1.*, t5.vendor_store_id
FROM store t1
LEFT JOIN store_map t5 ON t1.id = t5.store_id AND t5.vendor_id = ? AND t5.deleted_at = ?
WHERE t1.status = ?
WHERE t1.status = ? AND (SELECT COUNT(*) FROM store_map t10 WHERE t10.store_id = t1.id AND t10.deleted_at = '1970-01-01 00:00:00' AND t10.status <> ?) > 0
`,
}
sqlParamsList := [][]interface{}{
@@ -77,17 +77,20 @@ func GetPossibleStoresByPlaceName(db *DaoDB, cityName, provinceName string) (sto
model.VendorIDWSC,
utils.DefaultTimeValue,
model.StoreStatusOpened,
model.StoreStatusDisabled,
},
[]interface{}{
provinceName,
model.VendorIDWSC,
utils.DefaultTimeValue,
model.StoreStatusOpened,
model.StoreStatusDisabled,
},
[]interface{}{
model.VendorIDWSC,
utils.DefaultTimeValue,
model.StoreStatusOpened,
model.StoreStatusDisabled,
},
}
for k := range sqlList {

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
}