112 lines
2.9 KiB
Go
112 lines
2.9 KiB
Go
package controller
|
|
|
|
import (
|
|
"git.rosy.net.cn/baseapi"
|
|
"git.rosy.net.cn/baseapi/utils"
|
|
"git.rosy.net.cn/jx-callback/business/model"
|
|
"git.rosy.net.cn/jx-callback/globals"
|
|
"github.com/astaxie/beego/orm"
|
|
)
|
|
|
|
const (
|
|
WaybillStatusUnknown = 0
|
|
|
|
WaybillStatusNew = 5
|
|
WaybillStatusAccepted = 10
|
|
WaybillStatusDelivering = 20
|
|
|
|
WaybillStatusDelivered = 105
|
|
WaybillStatusCanceled = 115
|
|
WaybillStatusFailed = 120
|
|
)
|
|
|
|
type WaybillController struct {
|
|
}
|
|
|
|
var (
|
|
WaybillMap SyncMapWithTimeout
|
|
)
|
|
|
|
func (w *WaybillController) OnWaybillNew(bill *model.Waybill) (err error) {
|
|
db := orm.NewOrm()
|
|
bill.Status = WaybillStatusNew
|
|
created, _, err := db.ReadOrCreate(bill, "VendorWaybillID", "WaybillVendorID")
|
|
if err == nil {
|
|
WaybillMap.Store(ComposeUniversalOrderID(bill.VendorWaybillID, bill.WaybillVendorID), bill.ID)
|
|
if !created {
|
|
baseapi.SugarLogger.Warnf("duplicated waybill:%v msg received", bill)
|
|
}
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (w *WaybillController) OnWaybillAccepted(msg *model.OrderStatus) (err error) {
|
|
msg.Status = WaybillStatusAccepted
|
|
return w.addWaybillStatus(msg)
|
|
}
|
|
|
|
func (w *WaybillController) OnWaybillAcceptCanceled(msg *model.OrderStatus) (err error) {
|
|
msg.Status = WaybillStatusNew
|
|
return w.addWaybillStatus(msg)
|
|
}
|
|
|
|
func (w *WaybillController) OnWaybillDelivering(msg *model.OrderStatus) (err error) {
|
|
msg.Status = WaybillStatusDelivering
|
|
return w.addWaybillStatus(msg)
|
|
}
|
|
|
|
func (w *WaybillController) OnWaybillDelivered(msg *model.OrderStatus) (err error) {
|
|
msg.Status = WaybillStatusDelivered
|
|
return w.addWaybillStatus(msg)
|
|
}
|
|
|
|
func (w *WaybillController) OnWaybillCanceled(msg *model.OrderStatus) (err error) {
|
|
msg.Status = WaybillStatusCanceled
|
|
return w.addWaybillStatus(msg)
|
|
}
|
|
|
|
func (w *WaybillController) OnWaybillFailed(msg *model.OrderStatus) (err error) {
|
|
msg.Status = WaybillStatusFailed
|
|
return w.addWaybillStatus(msg)
|
|
}
|
|
|
|
func (w *WaybillController) OnWaybillOtherStatus(msg *model.OrderStatus) (err error) {
|
|
msg.Status = OrderStatusEvent
|
|
return w.addWaybillStatus(msg)
|
|
}
|
|
|
|
func (w *WaybillController) addWaybillStatus(msg *model.OrderStatus) (err error) {
|
|
order := &model.Waybill{
|
|
VendorWaybillID: msg.VendorOrderID,
|
|
WaybillVendorID: msg.VendorID,
|
|
}
|
|
db := orm.NewOrm()
|
|
value, ok := OrderMap.Load(ComposeUniversalOrderID(msg.VendorOrderID, msg.VendorID))
|
|
if !ok {
|
|
globals.SugarLogger.Warnf("can not get order:%v, from cache", order)
|
|
err = db.Read(order, "VendorWaybillID", "WaybillVendorID")
|
|
} else {
|
|
order.ID = value.(int64)
|
|
}
|
|
|
|
if err == nil {
|
|
if msg.Status != OrderStatusEvent {
|
|
order.Status = msg.Status
|
|
utils.CallFuncLogError(func() error {
|
|
columns := []string{"Status"}
|
|
if msg.Status >= OrderStatusDelivered {
|
|
order.WaybillFinishedAt = msg.StatusTime
|
|
columns = append(columns, "WaybillFinishedAt")
|
|
}
|
|
_, err := db.Update(order, columns...)
|
|
return err
|
|
}, "update order")
|
|
}
|
|
utils.CallFuncLogError(func() error {
|
|
_, err := db.Insert(msg)
|
|
return err
|
|
}, "insert status")
|
|
}
|
|
return err
|
|
}
|