- big big refactor.
This commit is contained in:
@@ -44,7 +44,7 @@ func (c *Controller) OnCallbackMsg(msg *elmapi.CallbackMsg) (retVal *elmapi.Call
|
||||
retVal = elmapi.Err2CallbackResponse(err, "")
|
||||
} else {
|
||||
innerMsg.MsgType = msg.Type
|
||||
controller.RoutinePool.CallFun(func() {
|
||||
controller.CallMsgHandler(func() {
|
||||
retVal = new(OrderController).onOrderUserUrgeOrder(&innerMsg)
|
||||
}, innerMsg.OrderID)
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package elm
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"strings"
|
||||
|
||||
"git.rosy.net.cn/jx-callback/globals"
|
||||
"git.rosy.net.cn/jx-callback/globals/api"
|
||||
@@ -14,25 +15,28 @@ import (
|
||||
)
|
||||
|
||||
type OrderController struct {
|
||||
controller.OrderController
|
||||
}
|
||||
|
||||
func init() {
|
||||
controller.OrderManager.RegisterPurchasePlatform(controller.VendorIDJD, new(OrderController))
|
||||
}
|
||||
|
||||
func (c *OrderController) OnOrderStatusMsg(msg *elmapi.CallbackOrderStatusMsg) (retVal *elmapi.CallbackResponse) {
|
||||
controller.RoutinePool.CallFun(func() {
|
||||
controller.CallMsgHandler(func() {
|
||||
retVal = c.onOrderStatusMsg(msg)
|
||||
}, msg.OrderID)
|
||||
return retVal
|
||||
}
|
||||
|
||||
func (c *OrderController) OnOrderNewMsg(msg map[string]interface{}) (retVal *elmapi.CallbackResponse) {
|
||||
controller.RoutinePool.CallFun(func() {
|
||||
controller.CallMsgHandler(func() {
|
||||
retVal = c.onOrderNew(msg)
|
||||
}, msg["orderId"].(string))
|
||||
return retVal
|
||||
}
|
||||
|
||||
func (c *OrderController) OnOrderCancelRefundMsg(msg *elmapi.CallbackOrderCancelRefundMsg) (retVal *elmapi.CallbackResponse) {
|
||||
controller.RoutinePool.CallFun(func() {
|
||||
controller.CallMsgHandler(func() {
|
||||
retVal = c.onOrderCancelRefundMsg(msg)
|
||||
}, msg.OrderID)
|
||||
return retVal
|
||||
@@ -43,7 +47,7 @@ func (c *OrderController) orderStatusMsg2Status(msg *elmapi.CallbackOrderStatusM
|
||||
VendorOrderID: msg.OrderID,
|
||||
VendorID: controller.VendorIDELM,
|
||||
OrderType: controller.OrderTypeOrder,
|
||||
VendorStatus: fmt.Sprintf("%s-%d", msg.State, msg.MsgType),
|
||||
VendorStatus: c.stateAndType2Str(msg.State, msg.MsgType),
|
||||
StatusTime: utils.Timestamp2Time(msg.UpdateTime),
|
||||
}
|
||||
return orderStatus
|
||||
@@ -54,7 +58,6 @@ func (c *OrderController) cancelRefundMsg2Status(msg *elmapi.CallbackOrderCancel
|
||||
VendorOrderID: msg.OrderID,
|
||||
VendorID: controller.VendorIDELM,
|
||||
OrderType: controller.OrderTypeOrder,
|
||||
Status: controller.OrderStatusEvent,
|
||||
VendorStatus: utils.Int2Str(msg.MsgType),
|
||||
StatusTime: utils.Timestamp2Time(msg.UpdateTime),
|
||||
}
|
||||
@@ -62,34 +65,32 @@ func (c *OrderController) cancelRefundMsg2Status(msg *elmapi.CallbackOrderCancel
|
||||
}
|
||||
|
||||
func (c *OrderController) onOrderStatusMsg(msg *elmapi.CallbackOrderStatusMsg) (retVal *elmapi.CallbackResponse) {
|
||||
status := c.orderStatusMsg2Status(msg)
|
||||
switch msg.MsgType {
|
||||
case elmapi.MsgTypeOrderAccepted:
|
||||
retVal = c.onOrderAccepted(msg)
|
||||
case elmapi.MsgTypeOrderCanceled:
|
||||
retVal = c.onOrderCanceled(msg)
|
||||
case elmapi.MsgTypeOrderInvalid:
|
||||
retVal = c.onOrderInvalid(msg)
|
||||
case elmapi.MsgTypeOrderForceInvalid:
|
||||
retVal = c.onOrderForceInvalid(msg)
|
||||
status.Status = controller.OrderStatusAccepted
|
||||
case elmapi.MsgTypeOrderCanceled, elmapi.MsgTypeOrderInvalid, elmapi.MsgTypeOrderForceInvalid:
|
||||
status.Status = controller.OrderStatusCanceled
|
||||
case elmapi.MsgTypeOrderFinished:
|
||||
retVal = c.onOrderFinished(msg)
|
||||
status.Status = controller.OrderStatusFinished
|
||||
default:
|
||||
globals.SugarLogger.Warnf("elm msg:%d not handled", msg.MsgType)
|
||||
retVal = elmapi.SuccessResponse
|
||||
return elmapi.SuccessResponse
|
||||
}
|
||||
return retVal
|
||||
return elmapi.Err2CallbackResponse(controller.OrderManager.OnOrderStatusChanged(status), status.VendorStatus)
|
||||
}
|
||||
|
||||
func (c *OrderController) onOrderCancelRefundMsg(msg *elmapi.CallbackOrderCancelRefundMsg) (retVal *elmapi.CallbackResponse) {
|
||||
status := c.cancelRefundMsg2Status(msg)
|
||||
switch msg.MsgType {
|
||||
case elmapi.MsgTypeUserApplyCancel:
|
||||
retVal = c.onOrderUserApplyCancel(msg)
|
||||
status.Status = controller.OrderStatusApplyCancel
|
||||
case elmapi.MsgTypeUserApplyRefund:
|
||||
retVal = c.onOrderUserApplyRefund(msg)
|
||||
status.Status = controller.OrderStatusApplyRefund
|
||||
default:
|
||||
retVal = c.onOrderOtherCancelRefundStatus(msg)
|
||||
status.Status = controller.OrderStatusUnknown
|
||||
}
|
||||
return retVal
|
||||
return elmapi.Err2CallbackResponse(controller.OrderManager.OnOrderStatusChanged(status), status.VendorStatus)
|
||||
}
|
||||
|
||||
func (c *OrderController) getOrderInfo(msg *elmapi.CallbackOrderStatusMsg) (order *model.GoodsOrder, orderSkus []*model.OrderSku, err error) {
|
||||
@@ -100,18 +101,26 @@ func (c *OrderController) getOrderInfo(msg *elmapi.CallbackOrderStatusMsg) (orde
|
||||
if len(phoneList) > 0 {
|
||||
consigneeMobile = phoneList[0].(string)
|
||||
}
|
||||
|
||||
// globals.SugarLogger.Debug(result)
|
||||
order = &model.GoodsOrder{
|
||||
VendorOrderID: msg.OrderID,
|
||||
VendorID: controller.VendorIDELM,
|
||||
VendorStoreID: utils.Int64ToStr(utils.MustInterface2Int64(result["shopId"])),
|
||||
StoreID: int(utils.Str2Int64WithDefault(utils.Interface2String(result["openId"]), 0)),
|
||||
StoreName: result["shopName"].(string),
|
||||
ConsigneeName: result["consignee"].(string),
|
||||
ConsigneeMobile: consigneeMobile,
|
||||
VendorStatus: msg.State,
|
||||
OrderCreatedAt: utils.Str2Time(result["createdAt"].(string)),
|
||||
OriginalData: string(utils.MustMarshal(result)),
|
||||
VendorOrderID: msg.OrderID,
|
||||
VendorID: controller.VendorIDELM,
|
||||
VendorStoreID: utils.Int64ToStr(utils.MustInterface2Int64(result["shopId"])),
|
||||
StoreID: int(utils.Str2Int64WithDefault(utils.Interface2String(result["openId"]), 0)),
|
||||
StoreName: result["shopName"].(string),
|
||||
ConsigneeName: result["consignee"].(string),
|
||||
ConsigneeMobile: consigneeMobile,
|
||||
ConsigneeAddress: result["address"].(string),
|
||||
VendorStatus: msg.State,
|
||||
OrderCreatedAt: utils.Str2Time(result["createdAt"].(string)),
|
||||
OriginalData: string(utils.MustMarshal(result)),
|
||||
}
|
||||
deliveryGeo := strings.Split(utils.Interface2String(result["deliveryGeo"]), ",")
|
||||
if len(deliveryGeo) == 2 {
|
||||
order.CoordinateType = controller.CoordinateTypeMars
|
||||
order.ConsigneeLng = controller.StandardCoordinate2Int(utils.Str2Float64(deliveryGeo[0]))
|
||||
order.ConsigneeLat = controller.StandardCoordinate2Int(utils.Str2Float64(deliveryGeo[1]))
|
||||
}
|
||||
|
||||
orderSkus = []*model.OrderSku{}
|
||||
@@ -121,21 +130,23 @@ func (c *OrderController) getOrderInfo(msg *elmapi.CallbackOrderStatusMsg) (orde
|
||||
product := product2.(map[string]interface{})
|
||||
sku := &model.OrderSku{
|
||||
VendorOrderID: msg.OrderID,
|
||||
VendorID: controller.VendorIDJD,
|
||||
VendorID: controller.VendorIDELM,
|
||||
Count: int(utils.MustInterface2Int64(product["quantity"])),
|
||||
SkuID: int(utils.Str2Int64WithDefault(utils.Interface2String(product["extendCode"]), 0)),
|
||||
VendorSkuID: utils.Int64ToStr(utils.MustInterface2Int64(product["skuId"])),
|
||||
SkuName: product["name"].(string),
|
||||
SalePrice: int64(math.Round(utils.MustInterface2Float64(product["userPrice"]) * 100)),
|
||||
Weight: int(math.Round(utils.Interface2Float64(product["weight"]))),
|
||||
OrderCreatedAt: order.OrderCreatedAt,
|
||||
}
|
||||
orderSkus = append(orderSkus, sku)
|
||||
order.SkuCount++
|
||||
order.GoodsCount += sku.Count
|
||||
order.SalePrice += sku.SalePrice
|
||||
order.Weight += sku.Weight
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return order, orderSkus, err
|
||||
}
|
||||
|
||||
@@ -144,63 +155,25 @@ func (c *OrderController) onOrderNew(msg map[string]interface{}) (response *elma
|
||||
// todo 这里应该可以直接用msg里的内容,而不用再次去查
|
||||
fakeOrderMsg := &elmapi.CallbackOrderStatusMsg{
|
||||
OrderID: msg["orderId"].(string),
|
||||
State: elmapi.OrderStatusFake,
|
||||
State: c.stateAndType2Str(msg["status"].(string), elmapi.MsgTypeOrderValid),
|
||||
}
|
||||
order, orderSkus, err := c.getOrderInfo(fakeOrderMsg)
|
||||
if err == nil {
|
||||
err = c.OnOrderNew(c, order, orderSkus)
|
||||
err = controller.OrderManager.OnOrderNew(order, orderSkus)
|
||||
}
|
||||
return elmapi.Err2CallbackResponse(err, "elm onOrderNew")
|
||||
}
|
||||
|
||||
func (c *OrderController) onOrderAccepted(msg *elmapi.CallbackOrderStatusMsg) *elmapi.CallbackResponse {
|
||||
status := c.orderStatusMsg2Status(msg)
|
||||
err := c.OnOrderAccepted(c, status)
|
||||
if err == nil {
|
||||
status.VendorStatus = "fakeautopickup"
|
||||
err = c.OnOrderFinishedPickup(c, c.orderStatusMsg2Status(msg))
|
||||
}
|
||||
return elmapi.Err2CallbackResponse(err, "elm onOrderAccepted")
|
||||
}
|
||||
|
||||
func (c *OrderController) onOrderCanceled(msg *elmapi.CallbackOrderStatusMsg) *elmapi.CallbackResponse {
|
||||
return elmapi.Err2CallbackResponse(c.OnOrderCanceled(c, c.orderStatusMsg2Status(msg)), "elm onOrderCanceled")
|
||||
}
|
||||
|
||||
func (c *OrderController) onOrderInvalid(msg *elmapi.CallbackOrderStatusMsg) *elmapi.CallbackResponse {
|
||||
return c.onOrderCanceled(msg)
|
||||
}
|
||||
|
||||
func (c *OrderController) onOrderForceInvalid(msg *elmapi.CallbackOrderStatusMsg) *elmapi.CallbackResponse {
|
||||
return c.onOrderCanceled(msg)
|
||||
}
|
||||
|
||||
func (c *OrderController) onOrderFinished(msg *elmapi.CallbackOrderStatusMsg) *elmapi.CallbackResponse {
|
||||
return elmapi.Err2CallbackResponse(c.OnOrderDelivered(c, c.orderStatusMsg2Status(msg)), "elm onOrderFinished")
|
||||
}
|
||||
|
||||
func (c *OrderController) onOrderUserUrgeOrder(msg *elmapi.CallbackOrderUrgeMsg) *elmapi.CallbackResponse {
|
||||
orderStatus := &model.OrderStatus{
|
||||
status := &model.OrderStatus{
|
||||
VendorOrderID: msg.OrderID,
|
||||
VendorID: controller.VendorIDELM,
|
||||
OrderType: controller.OrderTypeOrder,
|
||||
Status: controller.OrderStatusEvent,
|
||||
Status: controller.OrderStatusApplyUrgeOrder,
|
||||
VendorStatus: utils.Int2Str(msg.MsgType),
|
||||
StatusTime: utils.Timestamp2Time(msg.UpdateTime),
|
||||
}
|
||||
return elmapi.Err2CallbackResponse(c.OnOrderUserUrgeOrder(c, orderStatus), "elm onOrderUserUrgeOrder")
|
||||
}
|
||||
|
||||
func (c *OrderController) onOrderUserApplyCancel(msg *elmapi.CallbackOrderCancelRefundMsg) *elmapi.CallbackResponse {
|
||||
return elmapi.Err2CallbackResponse(c.OnOrderUserApplyCancel(c, c.cancelRefundMsg2Status(msg)), "elm onOrderUserApplyCancel")
|
||||
}
|
||||
|
||||
func (c *OrderController) onOrderUserApplyRefund(msg *elmapi.CallbackOrderCancelRefundMsg) *elmapi.CallbackResponse {
|
||||
return elmapi.Err2CallbackResponse(c.OnOrderUserApplyRefund(c, c.cancelRefundMsg2Status(msg)), "elm onOrderUserApplyRefund")
|
||||
}
|
||||
|
||||
func (c *OrderController) onOrderOtherCancelRefundStatus(msg *elmapi.CallbackOrderCancelRefundMsg) *elmapi.CallbackResponse {
|
||||
return elmapi.Err2CallbackResponse(c.OnOrderOtherStatus(c, c.cancelRefundMsg2Status(msg)), "elm onOrderOtherCancelRefundStatus")
|
||||
return elmapi.Err2CallbackResponse(controller.OrderManager.OnOrderStatusChanged(status), status.VendorStatus)
|
||||
}
|
||||
|
||||
// PurchasePlatformHandler
|
||||
@@ -211,3 +184,7 @@ func (c *OrderController) AcceptOrRefuseOrder(order *model.GoodsOrder, isAcceptI
|
||||
api.ElmAPI.CancelOrder(order.VendorOrderID, elmapi.CancelOrderTypeOthers, "")
|
||||
}
|
||||
}
|
||||
|
||||
func (c *OrderController) stateAndType2Str(state string, msgType int) string {
|
||||
return fmt.Sprintf("%s-%d", state, msgType)
|
||||
}
|
||||
|
||||
@@ -10,83 +10,59 @@ import (
|
||||
)
|
||||
|
||||
type WaybillController struct {
|
||||
controller.WaybillController
|
||||
}
|
||||
|
||||
func init() {
|
||||
controller.WaybillManager.RegisterDeliveryProvider(controller.VendorIDELM, new(WaybillController))
|
||||
}
|
||||
|
||||
func (c *WaybillController) OnWaybillStatusMsg(msg *elmapi.CallbackWaybillStatusMsg) (retVal *elmapi.CallbackResponse) {
|
||||
controller.RoutinePool.CallFun(func() {
|
||||
controller.CallMsgHandler(func() {
|
||||
retVal = c.onWaybillStatusMsg(msg)
|
||||
}, msg.OrderID)
|
||||
return retVal
|
||||
}
|
||||
|
||||
func (c *WaybillController) onWaybillStatusMsg(msg *elmapi.CallbackWaybillStatusMsg) (retVal *elmapi.CallbackResponse) {
|
||||
order := c.callbackMsg2Waybill(msg)
|
||||
if msg.MsgType == elmapi.MsgTypeWaybillWait4DeliveryVendor {
|
||||
retVal = c.onWaybillWait4DeliveryVendor(msg)
|
||||
order.Status = controller.WaybillStatusNew
|
||||
} else if msg.MsgType == elmapi.MsgTypeWaybillPickingUp {
|
||||
retVal = c.onWaybillPickingUp(msg)
|
||||
order.Status = controller.WaybillStatusAccepted
|
||||
} else if msg.MsgType == elmapi.MsgTypeWaybillCourierArrived {
|
||||
order.Status = controller.WaybillStatusCourierArrived
|
||||
} else if msg.MsgType == elmapi.MsgTypeWaybillDelivering {
|
||||
retVal = c.onWaybillDelivering(msg)
|
||||
order.Status = controller.WaybillStatusDelivering
|
||||
} else if msg.MsgType == elmapi.MsgTypeWaybillDelivered {
|
||||
retVal = c.onWaybillDelivered(msg)
|
||||
order.Status = controller.WaybillStatusDelivered
|
||||
} else if msg.MsgType >= elmapi.MsgTypeWaybillCanceledByMerchant && msg.MsgType <= elmapi.MsgTypeWaybillCanceledBySystem {
|
||||
retVal = c.onWaybillCanceled(msg)
|
||||
order.Status = controller.WaybillStatusCanceled
|
||||
} else if msg.MsgType >= elmapi.MsgTypeWaybillFailedCallLate &&
|
||||
msg.MsgType <= elmapi.MsgTypeRejectedSystemError &&
|
||||
msg.MsgType != elmapi.MsgTypeDeiverBySelf {
|
||||
retVal = c.onWaybillFailed(msg)
|
||||
order.Status = controller.WaybillStatusFailed
|
||||
} else {
|
||||
// MsgTypeWait4Courier
|
||||
// MsgTypeDeiverBySelf
|
||||
retVal = c.onWaybillOtherStatus(msg)
|
||||
order.Status = controller.WaybillStatusUnknown
|
||||
}
|
||||
return retVal
|
||||
return elmapi.Err2CallbackResponse(controller.WaybillManager.OnWaybillStatusChanged(order), order.VendorStatus)
|
||||
}
|
||||
|
||||
func (c *WaybillController) onWaybillWait4DeliveryVendor(msg *elmapi.CallbackWaybillStatusMsg) (retVal *elmapi.CallbackResponse) {
|
||||
order := &model.Waybill{
|
||||
func (c *WaybillController) callbackMsg2Waybill(msg *elmapi.CallbackWaybillStatusMsg) (retVal *model.Waybill) {
|
||||
retVal = &model.Waybill{
|
||||
VendorOrderID: msg.OrderID,
|
||||
VendorID: controller.VendorIDELM,
|
||||
OrderVendorID: controller.VendorIDELM,
|
||||
VendorWaybillID: msg.OrderID,
|
||||
WaybillVendorID: controller.VendorIDELM,
|
||||
CourierName: msg.Name,
|
||||
CourierMobile: msg.Phone,
|
||||
VendorStatus: c.composeState(msg.State, msg.SubState, elmapi.MsgTypeWaybillWait4DeliveryVendor),
|
||||
WaybillCreatedAt: utils.Timestamp2Time(msg.UpdateAt / 1000),
|
||||
}
|
||||
return elmapi.Err2CallbackResponse(c.OnWaybillNew(order), "elm onWaybillWait4DeliveryVendor")
|
||||
return retVal
|
||||
}
|
||||
|
||||
func (c *WaybillController) onWaybillPickingUp(msg *elmapi.CallbackWaybillStatusMsg) (retVal *elmapi.CallbackResponse) {
|
||||
return elmapi.Err2CallbackResponse(c.OnWaybillAccepted(c.callbackMsg2Status(msg)), "elm onWaybillPickingUp")
|
||||
}
|
||||
|
||||
func (c *WaybillController) onWaybillDelivering(msg *elmapi.CallbackWaybillStatusMsg) (retVal *elmapi.CallbackResponse) {
|
||||
return elmapi.Err2CallbackResponse(c.OnWaybillDelivering(c.callbackMsg2Status(msg)), "elm onWaybillDelivering")
|
||||
}
|
||||
|
||||
func (c *WaybillController) onWaybillDelivered(msg *elmapi.CallbackWaybillStatusMsg) (retVal *elmapi.CallbackResponse) {
|
||||
return elmapi.Err2CallbackResponse(c.OnWaybillDelivered(c.callbackMsg2Status(msg)), "elm onWaybillDelivered")
|
||||
}
|
||||
|
||||
func (c *WaybillController) onWaybillCanceled(msg *elmapi.CallbackWaybillStatusMsg) (retVal *elmapi.CallbackResponse) {
|
||||
return elmapi.Err2CallbackResponse(c.OnWaybillCanceled(c.callbackMsg2Status(msg)), "elm onWaybillCanceled")
|
||||
}
|
||||
|
||||
func (c *WaybillController) onWaybillFailed(msg *elmapi.CallbackWaybillStatusMsg) (retVal *elmapi.CallbackResponse) {
|
||||
return elmapi.Err2CallbackResponse(c.OnWaybillFailed(c.callbackMsg2Status(msg)), "elm onWaybillFailed")
|
||||
}
|
||||
|
||||
func (c *WaybillController) onWaybillOtherStatus(msg *elmapi.CallbackWaybillStatusMsg) (retVal *elmapi.CallbackResponse) {
|
||||
return elmapi.Err2CallbackResponse(c.OnWaybillOtherStatus(c.callbackMsg2Status(msg)), "elm onWaybillOtherStatus")
|
||||
}
|
||||
|
||||
func (c *WaybillController) callbackMsg2Status(msg *elmapi.CallbackWaybillStatusMsg) (retVal *model.OrderStatus) {
|
||||
status := &model.OrderStatus{
|
||||
VendorOrderID: msg.OrderID,
|
||||
VendorID: controller.VendorIDELM,
|
||||
OrderType: controller.OrderTypeWaybill,
|
||||
VendorStatus: fmt.Sprintf("%s-%s-%d", msg.State, msg.SubState, msg.MsgType),
|
||||
StatusTime: utils.Timestamp2Time(msg.UpdateAt / 1000),
|
||||
}
|
||||
return status
|
||||
func (c *WaybillController) composeState(state, subState string, msgType int) string {
|
||||
return fmt.Sprintf("%s-%s-%d", state, subState, msgType)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user