Files
jx-callback/business/controller/mtps/waybill.go
gazebo 805925ff58 - mtps, dada create waybill
- create legacy jxorder(not finished).
2018-07-14 14:35:51 +08:00

148 lines
5.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package mtps
import (
"git.rosy.net.cn/baseapi/platformapi/mtpsapi"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/business/controller"
"git.rosy.net.cn/jx-callback/business/legacyorder"
"git.rosy.net.cn/jx-callback/business/model"
"git.rosy.net.cn/jx-callback/business/scheduler"
"git.rosy.net.cn/jx-callback/globals"
"git.rosy.net.cn/jx-callback/globals/api"
"github.com/astaxie/beego/orm"
)
type WaybillController struct {
}
func init() {
scheduler.CurrentScheduler.RegisterDeliveryPlatform(model.VendorIDMTPS, new(WaybillController))
}
func (c *WaybillController) OnWaybillMsg(msg *mtpsapi.CallbackOrderMsg) (retVal *mtpsapi.CallbackResponse) {
controller.CallMsgHandler(func() {
retVal = c.onWaybillMsg(msg)
}, msg.MtPeisongID)
return retVal
}
func (c *WaybillController) OnWaybillExcept(msg *mtpsapi.CallbackOrderExceptionMsg) (retVal *mtpsapi.CallbackResponse) {
controller.CallMsgHandler(func() {
order := &model.Waybill{
VendorWaybillID: msg.MtPeisongID,
VendorWaybillID2: utils.Int64ToStr(msg.DeliveryID),
WaybillVendorID: model.VendorIDMTPS,
CourierName: msg.CourierName,
CourierMobile: msg.CourierPhone,
Status: model.WaybillStatusFailed,
VendorStatus: utils.Int2Str(msg.ExceptionCode),
WaybillCreatedAt: utils.Timestamp2Time(msg.Timestamp),
}
order.VendorOrderID, order.OrderVendorID = controller.SplitUniversalOrderID(msg.OrderID)
retVal = mtpsapi.Err2CallbackResponse(controller.WaybillManager.OnWaybillStatusChanged(order), "mtps OnWaybillExcept")
}, msg.MtPeisongID)
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.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("unknown msg:%v", msg)
return mtpsapi.SuccessResponse
}
return mtpsapi.Err2CallbackResponse(controller.WaybillManager.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),
WaybillCreatedAt: utils.Timestamp2Time(msg.Timestamp),
}
retVal.VendorOrderID, retVal.OrderVendorID = controller.SplitUniversalOrderID(msg.OrderID)
return retVal
}
//
func (c *WaybillController) CreateWaybill(order *model.GoodsOrder) (err error) {
db := orm.NewOrm()
// 忽略坐标转换错误,即使是转换出错,也只能当成转换成功来处理,底层会有错误日志输出
lngFloat, latFloat, _ := controller.IntCoordinate2MarsStandard(order.ConsigneeLng, order.ConsigneeLat, order.CoordinateType)
billParams := &mtpsapi.CreateOrderByShopParam{
OrderID: controller.ComposeUniversalOrderID(order.VendorOrderID, order.VendorID),
DeliveryServiceCode: mtpsapi.DeliveryServiceCodeRapid,
ReceiverName: order.ConsigneeName,
ReceiverAddress: order.ConsigneeAddress,
ReceiverPhone: order.ConsigneeMobile,
CoordinateType: model.CoordinateTypeMars,
ReceiverLng: controller.StandardCoordinate2Int(lngFloat),
ReceiverLat: controller.StandardCoordinate2Int(latFloat),
GoodsValue: controller.IntPrice2Standard(order.SalePrice), // 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 {
goods := &mtpsapi.GoodsDetail{
Goods: []*mtpsapi.GoodsItem{},
}
for _, sku := range order.Skus {
goodItem := &mtpsapi.GoodsItem{
GoodCount: sku.Count,
GoodName: sku.SkuName,
GoodPrice: controller.IntPrice2Standard(sku.SalePrice),
GoodUnit: "", //这个应该不是必须的商品名里已经有UNIT的字样了
}
goods.Goods = append(goods.Goods, goodItem)
}
addParams := utils.Params2Map("note", order.BuyerComment, "good_detail", string(utils.MustMarshal(goods)))
_, err = api.MtpsAPI.CreateOrderByShop(billParams, addParams)
}
}
return err
}
func (c *WaybillController) CancelWaybill(bill *model.Waybill) (err error) {
return nil
}
// 生成mtps deliveryid为了兼容现在取jxorder中的id
func (c *WaybillController) getDeliveryID(order *model.GoodsOrder, db orm.Ormer) (retVal int64, err error) {
jxorder := &legacyorder.Jxorder2{
OrderId: utils.Str2Int64(order.VendorOrderID),
}
err = utils.CallFuncLogError(func() error {
err2 := db.Read(order, "OrderId")
return err2
}, "getDeliveryID")
return int64(jxorder.Id), err
}
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
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("can not find mtps store info for store:%d", order.JxStoreID)
}
return retVal, err
}