- elm callback basic.
This commit is contained in:
86
business/elm/controller/order.go
Normal file
86
business/elm/controller/order.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"git.rosy.net.cn/baseapi/platform/elmapi"
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"git.rosy.net.cn/jx-callback/business/elm/models"
|
||||
"git.rosy.net.cn/jx-callback/globals"
|
||||
"github.com/astaxie/beego/orm"
|
||||
)
|
||||
|
||||
var (
|
||||
errResponseInternal = &elmapi.ELMCallbackResponse{Message: "internal error"}
|
||||
errResponseDataError = &elmapi.ELMCallbackResponse{Message: "elm data error"}
|
||||
errResponseDBError = &elmapi.ELMCallbackResponse{Message: "DB error"}
|
||||
errResponseCallELMAPIError = &elmapi.ELMCallbackResponse{Message: "call ELM API error"}
|
||||
)
|
||||
|
||||
type OrderController struct {
|
||||
}
|
||||
|
||||
func (o *OrderController) OrderMessage(msg *elmapi.ELMCallbackMsg) *elmapi.ELMCallbackResponse {
|
||||
var innerMsg map[string]interface{}
|
||||
err := utils.UnmarshalUseNumber([]byte(msg.Message), &innerMsg)
|
||||
if err != nil {
|
||||
sugarLogger.Warnf("OrderMessage unmarshal %v error:%v", msg, err)
|
||||
return errResponseDataError
|
||||
}
|
||||
if msg.Type == elmapi.OrderValid {
|
||||
return o.NewOrder(msg, innerMsg["id"].(string))
|
||||
} else if msg.Type >= elmapi.MerchantValid && msg.Type <= elmapi.OrderFinished {
|
||||
return o.OrderStatusChanged(msg, innerMsg["orderId"].(string))
|
||||
}
|
||||
return elmapi.ELMResponseOK
|
||||
}
|
||||
|
||||
func (o *OrderController) NewOrder(msg *elmapi.ELMCallbackMsg, orderId string) *elmapi.ELMCallbackResponse {
|
||||
db := orm.NewOrm()
|
||||
rec := &models.ELMOrder{
|
||||
OrderId: orderId,
|
||||
Type: msg.Type,
|
||||
}
|
||||
|
||||
created, _, err := db.ReadOrCreate(rec, "OrderId")
|
||||
if err != nil {
|
||||
sugarLogger.Warnf("error when calling ReadOrCreate, error:%v", err)
|
||||
return errResponseDBError
|
||||
}
|
||||
if created || rec.Type != msg.Type {
|
||||
result, err := gElmAPI.GetOrder(orderId)
|
||||
if err != nil {
|
||||
sugarLogger.Warnf("call GetOrder error:%v", err)
|
||||
return errResponseCallELMAPIError
|
||||
}
|
||||
err = globals.FreshFoodAPI.NewELMOrder(result)
|
||||
if err != nil {
|
||||
sugarLogger.Warnf("internal error:%v", err)
|
||||
return errResponseInternal
|
||||
}
|
||||
} else {
|
||||
sugarLogger.Infof("duplicate elm msg received:%v", msg)
|
||||
}
|
||||
return elmapi.ELMResponseOK
|
||||
}
|
||||
|
||||
func (o *OrderController) OrderStatusChanged(msg *elmapi.ELMCallbackMsg, orderId string) *elmapi.ELMCallbackResponse {
|
||||
db := orm.NewOrm()
|
||||
rec := &models.ELMOrder{
|
||||
OrderId: orderId,
|
||||
}
|
||||
|
||||
err := db.Read(rec, "OrderId")
|
||||
if err != nil {
|
||||
sugarLogger.Warnf("error when calling ReadOrCreate, error:%v", err)
|
||||
return errResponseDBError
|
||||
}
|
||||
if rec.Type != msg.Type {
|
||||
err = globals.FreshFoodAPI.ELMOrderStatus(orderId, msg.Type, utils.GetCurTimestamp())
|
||||
if err != nil {
|
||||
sugarLogger.Warnf("internal error:%v", err)
|
||||
return errResponseInternal
|
||||
}
|
||||
} else {
|
||||
sugarLogger.Infof("duplicate elm msg received:%v", msg)
|
||||
}
|
||||
return elmapi.ELMResponseOK
|
||||
}
|
||||
Reference in New Issue
Block a user