- refactor as baseapi.

This commit is contained in:
gazebo
2018-06-22 22:38:30 +08:00
parent 50790b3686
commit 65593521b9
7 changed files with 82 additions and 82 deletions

View File

@@ -3,7 +3,7 @@ package controller
import (
"encoding/json"
"git.rosy.net.cn/baseapi/platform/jdapi"
"git.rosy.net.cn/baseapi/platformapi/jdapi"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/business/jd/models"
"git.rosy.net.cn/jx-callback/compat/corm"
@@ -18,7 +18,7 @@ const (
var (
errChecker corm.DBErrorChecker
orderMsgChan chan jdapi.JDOrderMsg
orderMsgChan chan jdapi.CallbackOrderMsg
)
type OrderController struct {
@@ -27,7 +27,7 @@ type OrderController struct {
func InitOrder() {
errChecker = new(corm.MysqlErrorChecker)
orderMsgChan = make(chan jdapi.JDOrderMsg, 128)
orderMsgChan = make(chan jdapi.CallbackOrderMsg, 128)
go orderMsgHandlerRoutine()
// todo 这样操作在有多个进程时,会有问题
@@ -43,10 +43,10 @@ func handlePendingOrderMsg() {
globals.SugarLogger.Errorf("can not get jdorder from db, error:%v", err)
} else {
for _, jdOrderInfo := range ordersInfo {
orderMsg := &jdapi.JDOrderMsg{
Id: jdOrderInfo.Id,
BillId: utils.Int64ToStr(jdOrderInfo.JdOrderId),
StatusId: utils.Int2Str(jdOrderInfo.OrderStatus),
orderMsg := &jdapi.CallbackOrderMsg{
ID: jdOrderInfo.Id,
BillID: utils.Int64ToStr(jdOrderInfo.JdOrderId),
StatusID: utils.Int2Str(jdOrderInfo.OrderStatus),
Timestamp: jdOrderInfo.OrderStatusTime,
}
@@ -63,17 +63,17 @@ func orderMsgHandlerRoutine() {
}
}
func addOrderMsg(orderMsg *jdapi.JDOrderMsg) {
func addOrderMsg(orderMsg *jdapi.CallbackOrderMsg) {
globals.SugarLogger.Debugf("addOrderMsg:%v", orderMsg)
orderMsgChan <- *orderMsg
}
func handleOrderMsg(orderMsg *jdapi.JDOrderMsg) {
func handleOrderMsg(orderMsg *jdapi.CallbackOrderMsg) {
globals.SugarLogger.Debugf("handleOrderMsg:%v", orderMsg)
switch orderMsg.StatusId {
case jdapi.JdOrderStatusNew:
switch orderMsg.StatusID {
case jdapi.OrderStatusNew:
newOrder(orderMsg)
case jdapi.JdOrderStatusAdjust:
case jdapi.OrderStatusAdjust:
adjustOrder(orderMsg)
default:
normalOrderStatus(orderMsg)
@@ -81,16 +81,16 @@ func handleOrderMsg(orderMsg *jdapi.JDOrderMsg) {
}
// --------------
func (c *OrderController) OrderStatus(order *jdapi.JDOrderMsg) *jdapi.JDCallbackResponse {
if order.StatusId != jdapi.JdOrderStatusNew && order.StatusId != jdapi.JdOrderStatusAdjust {
func (c *OrderController) OrderStatus(order *jdapi.CallbackOrderMsg) *jdapi.CallbackResponse {
if order.StatusID != jdapi.OrderStatusNew && order.StatusID != jdapi.OrderStatusAdjust {
err := normalOrderStatus(order)
if err != nil {
return &jdapi.JDCallbackResponse{jdapi.JDerrorCodeAccessFailed, err.Error(), ""}
return &jdapi.CallbackResponse{jdapi.ResponseCodeAccessFailed, err.Error(), ""}
}
} else {
db := orm.NewOrm()
jdorderid := utils.Str2Int64(order.BillId)
status := utils.Str2Int(order.StatusId)
jdorderid := utils.Str2Int64(order.BillID)
status := utils.Str2Int(order.StatusID)
rec := &models.Jdorder{
Code: MsgNotHandledCode,
JdOrderId: jdorderid,
@@ -99,19 +99,19 @@ func (c *OrderController) OrderStatus(order *jdapi.JDOrderMsg) *jdapi.JDCallback
}
if created, _, err := db.ReadOrCreate(rec, "Jdorderid"); err == nil {
order.Id = rec.Id
order.ID = rec.Id
if created {
if order.StatusId != jdapi.JdOrderStatusNew && order.StatusId != jdapi.JdOrderStatusAdjust {
if order.StatusID != jdapi.OrderStatusNew && order.StatusID != jdapi.OrderStatusAdjust {
globals.SugarLogger.Warnf("order:%v get before create", order)
oldStatusId := order.StatusId
order.StatusId = jdapi.JdOrderStatusNew
oldStatusId := order.StatusID
order.StatusID = jdapi.OrderStatusNew
addOrderMsg(order)
order.StatusId = oldStatusId
order.StatusID = oldStatusId
}
addOrderMsg(order)
} else {
if rec.OrderStatus != status {
if order.StatusId == jdapi.JdOrderStatusNew {
if order.StatusID == jdapi.OrderStatusNew {
globals.SugarLogger.Warnf("order:%v get after some other message:%d", order, rec.OrderStatus)
} else {
rec.OrderStatus = status
@@ -129,36 +129,36 @@ func (c *OrderController) OrderStatus(order *jdapi.JDOrderMsg) *jdapi.JDCallback
}
} else {
globals.SugarLogger.Errorf("error when calling ReadOrCreate:%v", err)
return &jdapi.JDCallbackResponse{jdapi.JDerrorCodeAccessFailed, err.Error(), ""}
return &jdapi.CallbackResponse{jdapi.ResponseCodeAccessFailed, err.Error(), ""}
}
}
return jdapi.SuccessResponse
}
func (c *OrderController) OrderDeliveryStatus(jdOrderDeliveryStatusMsg *jdapi.JDDeliveryStatusMsg) *jdapi.JDCallbackResponse {
func (c *OrderController) OrderDeliveryStatus(jdOrderDeliveryStatusMsg *jdapi.CallbackDeliveryStatusMsg) *jdapi.CallbackResponse {
err := globals2.FreshFoodAPI.JDOrderDeliveryStatus(jdOrderDeliveryStatusMsg)
if err != nil {
globals.SugarLogger.Errorf("Error when calling JDOrderDeliveryStatus, error:%v", err)
return &jdapi.JDCallbackResponse{jdapi.JDerrorCodeAccessFailed, err.Error(), ""}
return &jdapi.CallbackResponse{jdapi.ResponseCodeAccessFailed, err.Error(), ""}
}
return jdapi.SuccessResponse
}
//-----------
func acceptOrder(order *jdapi.JDOrderMsg) {
globals2.Jdapi.OrderAcceptOperate(order.BillId, true)
func acceptOrder(order *jdapi.CallbackOrderMsg) {
globals2.Jdapi.OrderAcceptOperate(order.BillID, true)
}
func newOrder(order *jdapi.JDOrderMsg) error {
result, err := globals2.Jdapi.LegacyQuerySingleOrder(order.BillId)
func newOrder(order *jdapi.CallbackOrderMsg) error {
result, err := globals2.Jdapi.LegacyQuerySingleOrder(order.BillID)
acceptOrder(order)
if err != nil {
globals.SugarLogger.Errorf("error when query jd order:%s, error:%v", order.BillId, err)
globals.SugarLogger.Errorf("error when query jd order:%s, error:%v", order.BillID, err)
} else {
rec := &models.Jdorder{
Id: order.Id,
Id: order.ID,
}
rec.Code, _ = result["code"].(string)
@@ -195,20 +195,20 @@ func newOrder(order *jdapi.JDOrderMsg) error {
globals.SugarLogger.Errorf("Error when calling NewJDOrder error:%v", err)
}
} else {
globals.SugarLogger.Warnf("can not get jdorder info:%v", order.BillId)
globals.SugarLogger.Warnf("can not get jdorder info:%v", order.BillID)
}
}
return err
}
func adjustOrder(order *jdapi.JDOrderMsg) error {
func adjustOrder(order *jdapi.CallbackOrderMsg) error {
return newOrder(order)
}
func normalOrderStatus(order *jdapi.JDOrderMsg) error {
func normalOrderStatus(order *jdapi.CallbackOrderMsg) error {
db := orm.NewOrm()
rec := &models.Jdorder{
JdOrderId: utils.Str2Int64(order.BillId),
JdOrderId: utils.Str2Int64(order.BillID),
}
err := db.Read(rec, "JdOrderId")
@@ -217,12 +217,12 @@ func normalOrderStatus(order *jdapi.JDOrderMsg) error {
return err
}
if rec.OrderStatus == utils.Str2Int(order.StatusId) {
if rec.OrderStatus == utils.Str2Int(order.StatusID) {
globals.SugarLogger.Infof("Duplicate message order:%v", order)
return nil
}
rec.OrderStatus = utils.Str2Int(order.StatusId)
rec.OrderStatus = utils.Str2Int(order.StatusID)
rec.OrderStatusTime = order.Timestamp
err = globals2.FreshFoodAPI.JDOrderStatus(rec)
if err != nil {