- refactor file structure.
This commit is contained in:
175
business/partner/delivery/dada/waybill.go
Normal file
175
business/partner/delivery/dada/waybill.go
Normal file
@@ -0,0 +1,175 @@
|
||||
package dada
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"git.rosy.net.cn/baseapi/platformapi/dadaapi"
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"git.rosy.net.cn/jx-callback/business/jxcallback/scheduler"
|
||||
"git.rosy.net.cn/jx-callback/business/jxutils"
|
||||
"git.rosy.net.cn/jx-callback/business/model"
|
||||
"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/orm"
|
||||
)
|
||||
|
||||
const (
|
||||
maxCargoPrice = 63.99 // 单位为元,达达最大价格,超过这个价格配送费会增加
|
||||
)
|
||||
|
||||
var (
|
||||
ErrCanNotFindDadaCityCode = errors.New("不能找到美团配送站点配置")
|
||||
)
|
||||
|
||||
type WaybillController struct {
|
||||
}
|
||||
|
||||
func init() {
|
||||
scheduler.CurrentScheduler.RegisterDeliveryPlatform(model.VendorIDDada, new(WaybillController), true)
|
||||
}
|
||||
|
||||
func (c *WaybillController) OnWaybillMsg(msg *dadaapi.CallbackMsg) (retVal *dadaapi.CallbackResponse) {
|
||||
jxutils.CallMsgHandler(func() {
|
||||
retVal = c.onWaybillMsg(msg)
|
||||
}, msg.OrderID)
|
||||
return retVal
|
||||
}
|
||||
|
||||
func (c *WaybillController) onWaybillMsg(msg *dadaapi.CallbackMsg) (retVal *dadaapi.CallbackResponse) {
|
||||
order := c.callbackMsg2Waybill(msg)
|
||||
switch msg.OrderStatus {
|
||||
case dadaapi.OrderStatusWaitingForAccept:
|
||||
order.Status = model.WaybillStatusNew
|
||||
case dadaapi.OrderStatusAccepted:
|
||||
if result, err := api.DadaAPI.QueryOrderInfo(msg.OrderID); err == nil {
|
||||
order.DesiredFee = jxutils.StandardPrice2Int(utils.Interface2FloatWithDefault(result["deliveryFee"], 0.0))
|
||||
}
|
||||
order.Status = model.WaybillStatusAccepted
|
||||
case dadaapi.OrderStatusDelivering:
|
||||
order.Status = model.WaybillStatusDelivering
|
||||
case dadaapi.OrderStatusFinished:
|
||||
order.Status = model.WaybillStatusDelivered
|
||||
case dadaapi.OrderStatusCanceled:
|
||||
order.Status = model.WaybillStatusCanceled
|
||||
case dadaapi.OrderStatusExpired, dadaapi.OrderStatusAddOrderFailed:
|
||||
order.Status = model.WaybillStatusFailed
|
||||
default:
|
||||
order.Status = model.WaybillStatusUnknown
|
||||
}
|
||||
return dadaapi.Err2CallbackResponse(partner.CurOrderManager.OnWaybillStatusChanged(order), utils.Int2Str(order.Status))
|
||||
}
|
||||
|
||||
func (c *WaybillController) callbackMsg2Waybill(msg *dadaapi.CallbackMsg) (retVal *model.Waybill) {
|
||||
retVal = &model.Waybill{
|
||||
VendorWaybillID: msg.ClientID,
|
||||
WaybillVendorID: model.VendorIDDada,
|
||||
CourierName: msg.DmName,
|
||||
CourierMobile: msg.DmMobile,
|
||||
VendorStatus: utils.Int2Str(msg.OrderStatus),
|
||||
Remark: msg.CancelReason,
|
||||
// StatusTime: utils.Timestamp2Time(int64(msg.UpdateTime)),
|
||||
}
|
||||
// dada太扯了,不同消息过来的时间格式不一样
|
||||
updateTime := int64(msg.UpdateTime)
|
||||
if updateTime > 2511789475 {
|
||||
updateTime = updateTime / 1000
|
||||
}
|
||||
retVal.StatusTime = utils.Timestamp2Time(updateTime)
|
||||
retVal.VendorOrderID, retVal.OrderVendorID = jxutils.SplitUniversalOrderID(msg.OrderID)
|
||||
return retVal
|
||||
}
|
||||
|
||||
// IDeliveryPlatformHandler
|
||||
func (c *WaybillController) CreateWaybill(order *model.GoodsOrder) (err error) {
|
||||
billParams := &dadaapi.OperateOrderRequiredParams{
|
||||
ShopNo: utils.Int2Str(order.StoreID), // 当前达达的门店号与京西是一样的
|
||||
OriginID: jxutils.ComposeUniversalOrderID(order.VendorOrderID, order.VendorID),
|
||||
CargoPrice: jxutils.IntPrice2Standard(order.ActualPayPrice),
|
||||
IsPrepay: 0,
|
||||
ReceiverName: order.ConsigneeName,
|
||||
ReceiverAddress: 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": order.BuyerComment,
|
||||
// "origin_mark": model.VendorNames[order.VendorID],
|
||||
"origin_mark_no": fmt.Sprintf("%d", order.OrderSeq),
|
||||
}
|
||||
|
||||
// 达达要求第二次创建运单,调用函数不同。所以查找两天内有无相同订单号的运单
|
||||
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)
|
||||
if err2 == nil && num > 0 {
|
||||
globals.SugarLogger.Debugf("CreateWaybill orderID:%s num=%d use ReaddOrder", order.VendorOrderID, num)
|
||||
_, err = api.DadaAPI.ReaddOrder(billParams, addParams)
|
||||
} else {
|
||||
if err2 != nil {
|
||||
globals.SugarLogger.Warnf("CreateWaybill orderID:%s error:%v", order.VendorOrderID, err2)
|
||||
}
|
||||
_, err = api.DadaAPI.AddOrder(billParams, addParams)
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *WaybillController) CancelWaybill(bill *model.Waybill) (err error) {
|
||||
reasonID := dadaapi.ReasonIDOther
|
||||
reasonMsg := "send not in time"
|
||||
if bill.Status < model.WaybillStatusAccepted {
|
||||
reasonID = dadaapi.ReasonIDNobodyAccept
|
||||
reasonMsg = "ReasonIDNobodyAccept"
|
||||
} else if bill.Status < model.WaybillStatusCourierArrived {
|
||||
reasonID = dadaapi.ReasonIDNobodyPickup
|
||||
reasonMsg = "ReasonIDNobodyPickup"
|
||||
}
|
||||
_, err = api.DadaAPI.CancelOrder(bill.VendorOrderID, reasonID, reasonMsg)
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *WaybillController) getDataCityCodeFromOrder(order *model.GoodsOrder, db orm.Ormer) (retVal string, err error) {
|
||||
var sql string
|
||||
if order.VendorID == model.VendorIDJD {
|
||||
sql = `
|
||||
SELECT t2.tel_code
|
||||
FROM jxstoremap t0
|
||||
JOIN jxstore t1 ON t0.jxstoreid = t1.storeid
|
||||
JOIN city t2 ON t1.area = t2.citycode
|
||||
WHERE t0.jdstoreid = ?
|
||||
`
|
||||
} else if order.VendorID == model.VendorIDELM {
|
||||
sql = `
|
||||
SELECT t2.tel_code
|
||||
FROM jx_to_elm_store_map t0
|
||||
JOIN jxstore t1 ON t0.jx_store_id = t1.storeid
|
||||
JOIN city t2 ON t1.area = t2.citycode
|
||||
WHERE t0.elm_store_id = ?
|
||||
`
|
||||
} else {
|
||||
panic(fmt.Sprintf("wrong vendorid:%d", order.VendorID))
|
||||
}
|
||||
var lists []orm.ParamsList
|
||||
num, err := db.Raw(sql, utils.Str2Int64(order.VendorStoreID)).ValuesList(&lists)
|
||||
if err == nil && num == 1 {
|
||||
retVal = lists[0][0].(string)
|
||||
} else {
|
||||
globals.SugarLogger.Errorf("GetDataCityCodeFromOrder can not find store info for vendorID:%d, store:%s, num:%d, error:%v", order.VendorID, order.VendorStoreID, num, err)
|
||||
if err == nil {
|
||||
err = ErrCanNotFindDadaCityCode
|
||||
}
|
||||
}
|
||||
return retVal, err
|
||||
}
|
||||
45
business/partner/delivery/dada/waybill_test.go
Normal file
45
business/partner/delivery/dada/waybill_test.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package dada
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.rosy.net.cn/jx-callback/business/jxcallback/orderman"
|
||||
"git.rosy.net.cn/jx-callback/business/model"
|
||||
"git.rosy.net.cn/jx-callback/globals"
|
||||
"git.rosy.net.cn/jx-callback/globals/api"
|
||||
"git.rosy.net.cn/jx-callback/globals/db"
|
||||
"github.com/astaxie/beego"
|
||||
)
|
||||
|
||||
func init() {
|
||||
beego.InitBeegoBeforeTest("/Users/xujianhua/go/src/git.rosy.net.cn/jx-callback/conf/app.conf")
|
||||
beego.BConfig.RunMode = "dev" // InitBeegoBeforeTest会将runmode设置为test
|
||||
|
||||
globals.Init()
|
||||
db.Init()
|
||||
api.Init()
|
||||
}
|
||||
|
||||
func TestCreateWaybill(t *testing.T) {
|
||||
orderID := "817540316000041"
|
||||
if order, err := orderman.CurOrderManager.LoadOrder(orderID, model.VendorIDJD); err == nil {
|
||||
// globals.SugarLogger.Debug(order)
|
||||
c := new(WaybillController)
|
||||
if err = c.CreateWaybill(order); err == nil {
|
||||
time.Sleep(1 * time.Second)
|
||||
bill := &model.Waybill{
|
||||
VendorOrderID: orderID,
|
||||
WaybillVendorID: model.VendorIDDada,
|
||||
}
|
||||
err = c.CancelWaybill(bill)
|
||||
if err != nil {
|
||||
t.Fatal(err.Error())
|
||||
}
|
||||
} else {
|
||||
t.Fatal(err.Error())
|
||||
}
|
||||
} else {
|
||||
t.Fatal(err.Error())
|
||||
}
|
||||
}
|
||||
283
business/partner/delivery/mtps/waybill.go
Normal file
283
business/partner/delivery/mtps/waybill.go
Normal file
@@ -0,0 +1,283 @@
|
||||
package mtps
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"time"
|
||||
|
||||
"git.rosy.net.cn/baseapi/platformapi/mtpsapi"
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"git.rosy.net.cn/jx-callback/business/jxcallback/scheduler"
|
||||
"git.rosy.net.cn/jx-callback/business/jxutils"
|
||||
"git.rosy.net.cn/jx-callback/business/legacymodel"
|
||||
"git.rosy.net.cn/jx-callback/business/model"
|
||||
"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 (
|
||||
maxAddFee = 200 // 最大增加费用,单位为分,超过不发美团了
|
||||
)
|
||||
|
||||
var (
|
||||
ErrCanNotFindMTPSStore = errors.New("不能找到美团配送站点配置")
|
||||
ErrAddFeeExceeded = errors.New("美团配送超过基准价太多")
|
||||
ErrStoreNoPriceInfo = errors.New("找不到门店的美团配送价格信息")
|
||||
ErrStoreNoCoordinate = errors.New("找不到门店的坐标信息")
|
||||
)
|
||||
|
||||
type WaybillController struct {
|
||||
}
|
||||
|
||||
func init() {
|
||||
scheduler.CurrentScheduler.RegisterDeliveryPlatform(model.VendorIDMTPS, new(WaybillController), true)
|
||||
}
|
||||
|
||||
func (c *WaybillController) OnWaybillMsg(msg *mtpsapi.CallbackOrderMsg) (retVal *mtpsapi.CallbackResponse) {
|
||||
jxutils.CallMsgHandler(func() {
|
||||
retVal = c.onWaybillMsg(msg)
|
||||
}, msg.OrderID)
|
||||
return retVal
|
||||
}
|
||||
|
||||
func (c *WaybillController) OnWaybillExcept(msg *mtpsapi.CallbackOrderExceptionMsg) (retVal *mtpsapi.CallbackResponse) {
|
||||
jxutils.CallMsgHandler(func() {
|
||||
order := &model.Waybill{
|
||||
VendorWaybillID: msg.MtPeisongID,
|
||||
VendorWaybillID2: utils.Int64ToStr(msg.DeliveryID),
|
||||
WaybillVendorID: model.VendorIDMTPS,
|
||||
CourierName: msg.CourierName,
|
||||
CourierMobile: msg.CourierPhone,
|
||||
Status: model.WaybillStatusUnknown, // todo 这里要再确定一下是否只要收到订单异常消息就只简单当成一个消息
|
||||
VendorStatus: utils.Int2Str(msg.ExceptionCode),
|
||||
StatusTime: utils.Timestamp2Time(msg.Timestamp),
|
||||
}
|
||||
order.VendorOrderID, order.OrderVendorID = jxutils.SplitUniversalOrderID(msg.OrderID)
|
||||
retVal = mtpsapi.Err2CallbackResponse(partner.CurOrderManager.OnWaybillStatusChanged(order), "mtps OnWaybillExcept")
|
||||
}, msg.OrderID)
|
||||
return retVal
|
||||
}
|
||||
|
||||
func (c *WaybillController) onWaybillMsg(msg *mtpsapi.CallbackOrderMsg) (retVal *mtpsapi.CallbackResponse) {
|
||||
order := c.callbackMsg2Waybill(msg)
|
||||
switch msg.Status {
|
||||
case mtpsapi.OrderStatusWaitingForSchedule:
|
||||
order.Status = model.WaybillStatusNew
|
||||
case mtpsapi.OrderStatusAccepted:
|
||||
order.DesiredFee, _ = c.calculateBillDeliveryFee(order)
|
||||
order.Status = model.WaybillStatusAccepted
|
||||
case mtpsapi.OrderStatusPickedUp:
|
||||
order.Status = model.WaybillStatusDelivering
|
||||
case mtpsapi.OrderStatusDeliverred:
|
||||
order.Status = model.WaybillStatusDelivered
|
||||
case mtpsapi.OrderStatusCanceled:
|
||||
order.Status = model.WaybillStatusCanceled
|
||||
default:
|
||||
globals.SugarLogger.Warnf("onWaybillMsg unknown msg:%v", msg)
|
||||
return mtpsapi.SuccessResponse
|
||||
}
|
||||
return mtpsapi.Err2CallbackResponse(partner.CurOrderManager.OnWaybillStatusChanged(order), order.VendorStatus)
|
||||
}
|
||||
|
||||
func (c *WaybillController) callbackMsg2Waybill(msg *mtpsapi.CallbackOrderMsg) (retVal *model.Waybill) {
|
||||
retVal = &model.Waybill{
|
||||
VendorWaybillID: msg.MtPeisongID,
|
||||
VendorWaybillID2: utils.Int64ToStr(msg.DeliveryID),
|
||||
WaybillVendorID: model.VendorIDMTPS,
|
||||
CourierName: msg.CourierName,
|
||||
CourierMobile: msg.CourierPhone,
|
||||
VendorStatus: utils.Int2Str(msg.Status),
|
||||
StatusTime: utils.Timestamp2Time(msg.Timestamp),
|
||||
Remark: msg.CancelReason,
|
||||
}
|
||||
retVal.VendorOrderID, retVal.OrderVendorID = jxutils.SplitUniversalOrderID(msg.OrderID)
|
||||
return retVal
|
||||
}
|
||||
|
||||
func (c *WaybillController) calculateOrderDeliveryFee(order *model.GoodsOrder, billTime time.Time, db orm.Ormer) (deliveryFee, addFee int64, err error) {
|
||||
var lists []orm.ParamsList
|
||||
if db == nil {
|
||||
db = orm.NewOrm()
|
||||
}
|
||||
JxStoreID := jxutils.GetJxStoreIDFromOrder(order)
|
||||
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 == 1 {
|
||||
deliveryFee = utils.Str2Int64(lists[0][0].(string))
|
||||
} else {
|
||||
globals.SugarLogger.Warnf("calculateDeliveryFee can not calculate delivery fee for orderID:%s, num:%d, error:%v", order.VendorOrderID, num, err)
|
||||
return 0, 0, ErrStoreNoPriceInfo
|
||||
}
|
||||
|
||||
lng := utils.Str2Float64(lists[0][1].(string))
|
||||
lat := utils.Str2Float64(lists[0][2].(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(order.Weight/1000-5))
|
||||
} else if order.Weight < 20*1000 {
|
||||
addFee += jxutils.StandardPrice2Int(2.5 + 1*float64(order.Weight/1000-10))
|
||||
} else {
|
||||
addFee += jxutils.StandardPrice2Int(2.5 + 10 + 2*float64(order.Weight/1000-20))
|
||||
}
|
||||
|
||||
hour, min, sec := billTime.Clock()
|
||||
totalSeconds := hour*3600 + min*60 + sec
|
||||
if totalSeconds >= 11*3600+30*60 && totalSeconds <= 13*3600 { // 11:30 -- 13: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 *WaybillController) 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 *WaybillController) CreateWaybill(order *model.GoodsOrder) (err error) {
|
||||
db := orm.NewOrm()
|
||||
_, addFee, err := c.calculateOrderDeliveryFee(order, time.Now(), db)
|
||||
if err == nil {
|
||||
if addFee <= maxAddFee {
|
||||
// 忽略坐标转换错误,即使是转换出错,也只能当成转换成功来处理,底层会有错误日志输出
|
||||
lngFloat, latFloat, _ := jxutils.IntCoordinate2MarsStandard(order.ConsigneeLng, order.ConsigneeLat, order.CoordinateType)
|
||||
billParams := &mtpsapi.CreateOrderByShopParam{
|
||||
OrderID: jxutils.ComposeUniversalOrderID(order.VendorOrderID, order.VendorID),
|
||||
DeliveryServiceCode: mtpsapi.DeliveryServiceCodeRapid,
|
||||
ReceiverName: order.ConsigneeName,
|
||||
ReceiverAddress: order.ConsigneeAddress,
|
||||
ReceiverPhone: order.ConsigneeMobile,
|
||||
CoordinateType: model.CoordinateTypeMars,
|
||||
ReceiverLng: jxutils.StandardCoordinate2Int(lngFloat),
|
||||
ReceiverLat: jxutils.StandardCoordinate2Int(latFloat),
|
||||
GoodsValue: jxutils.IntPrice2Standard(order.ActualPayPrice), // todo 超价处理
|
||||
GoodsWeight: float64(order.Weight) / 1000,
|
||||
// ExpectedDeliveryTime: order.ExpectedDeliveredTime.Unix(),
|
||||
OrderType: mtpsapi.OrderTypeASAP,
|
||||
}
|
||||
if billParams.DeliveryID, err = c.getDeliveryID(order, db); err == nil {
|
||||
if billParams.ShopID, err = c.getMTPSShopID(order, db); err == nil {
|
||||
globals.SugarLogger.Debug(billParams.ShopID)
|
||||
goods := &mtpsapi.GoodsDetail{
|
||||
Goods: []*mtpsapi.GoodsItem{},
|
||||
}
|
||||
goodItemMap := map[string]*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)
|
||||
// 好像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)), "poi_seq", fmt.Sprintf("#%d", order.OrderSeq))
|
||||
_, err = api.MtpsAPI.CreateOrderByShop(billParams, addParams)
|
||||
if err != nil {
|
||||
globals.SugarLogger.Debugf("CreateWaybill failed, orderID:%s, billParams:%v, addParams:%v, error:%v", order.VendorOrderID, billParams, addParams, err)
|
||||
|
||||
tmpLog := &legacymodel.TempLog{
|
||||
VendorOrderID: order.VendorOrderID,
|
||||
RefVendorOrderID: order.VendorOrderID,
|
||||
IntValue1: addFee,
|
||||
Msg: fmt.Sprintf("CreateWaybill failed, orderID:%s, billParams:%v, addParams:%v, error:%v", order.VendorOrderID, billParams, addParams, err),
|
||||
}
|
||||
db.Insert(tmpLog)
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
err = ErrAddFeeExceeded
|
||||
globals.SugarLogger.Infof("CreateWaybill orderID:%s addFee exceeded too much, it's %d", order.VendorOrderID, addFee)
|
||||
|
||||
tmpLog := &legacymodel.TempLog{
|
||||
VendorOrderID: order.VendorOrderID,
|
||||
RefVendorOrderID: order.VendorOrderID,
|
||||
IntValue1: addFee,
|
||||
Msg: fmt.Sprintf("CreateWaybill orderID:%s addFee exceeded too much, it's %d", order.VendorOrderID, addFee),
|
||||
}
|
||||
db.Insert(tmpLog)
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *WaybillController) CancelWaybill(bill *model.Waybill) (err error) {
|
||||
reasonID := mtpsapi.CancelReasonRidderSendNotIntime
|
||||
reasonMsg := "CancelReasonRidderSendNotIntime"
|
||||
if bill.Status < model.WaybillStatusAccepted {
|
||||
reasonID = mtpsapi.CancelReasonMerchantOther
|
||||
reasonMsg = "nobody accept order"
|
||||
} else if bill.Status < model.WaybillStatusCourierArrived {
|
||||
reasonID = mtpsapi.CancelReasonRideerGetGoodNotIntime
|
||||
reasonMsg = "CancelReasonRideerGetGoodNotIntime"
|
||||
}
|
||||
_, err = api.MtpsAPI.CancelOrder(utils.Str2Int64(bill.VendorWaybillID2), bill.VendorWaybillID, reasonID, reasonMsg)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *WaybillController) getDeliveryID(order *model.GoodsOrder, db orm.Ormer) (retVal int64, err error) {
|
||||
// jxorder表当前已经有50多万条记录了,加100万避免冲突
|
||||
// 508505
|
||||
return order.ID + 1000000, nil
|
||||
}
|
||||
|
||||
func (c *WaybillController) getMTPSShopID(order *model.GoodsOrder, db orm.Ormer) (retVal string, err error) {
|
||||
sql := "SELECT zs_store_id FROM jx_to_zs_store_map WHERE jx_store_id = ?"
|
||||
var lists []orm.ParamsList
|
||||
JxStoreID := jxutils.GetJxStoreIDFromOrder(order)
|
||||
num, err := db.Raw(sql, JxStoreID).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, JxStoreID, 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, JxStoreID, num, err),
|
||||
}
|
||||
db.Insert(tmpLog)
|
||||
}
|
||||
return retVal, err
|
||||
}
|
||||
42
business/partner/delivery/mtps/waybill_test.go
Normal file
42
business/partner/delivery/mtps/waybill_test.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package mtps
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.rosy.net.cn/jx-callback/business/jxcallback/orderman"
|
||||
"git.rosy.net.cn/jx-callback/business/model"
|
||||
"git.rosy.net.cn/jx-callback/globals"
|
||||
"git.rosy.net.cn/jx-callback/globals/api"
|
||||
"git.rosy.net.cn/jx-callback/globals/db"
|
||||
"github.com/astaxie/beego"
|
||||
)
|
||||
|
||||
func init() {
|
||||
beego.InitBeegoBeforeTest("/Users/xujianhua/go/src/git.rosy.net.cn/jx-callback/conf/app.conf")
|
||||
beego.BConfig.RunMode = "dev" // InitBeegoBeforeTest会将runmode设置为test
|
||||
|
||||
globals.Init()
|
||||
db.Init()
|
||||
api.Init()
|
||||
}
|
||||
|
||||
func TestCreateWaybill(t *testing.T) {
|
||||
orerID := "817109342000022"
|
||||
order, _ := orderman.CurOrderManager.LoadOrder(orerID, model.VendorIDJD)
|
||||
// globals.SugarLogger.Debug(order)
|
||||
c := new(WaybillController)
|
||||
if err := c.CreateWaybill(order); err != nil {
|
||||
t.Fatal(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestCancelWaybill(t *testing.T) {
|
||||
bill := &model.Waybill{
|
||||
VendorWaybillID: "1532332342088966",
|
||||
VendorWaybillID2: "55",
|
||||
}
|
||||
c := new(WaybillController)
|
||||
if err := c.CancelWaybill(bill); err != nil {
|
||||
t.Fatal(err.Error())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user