- fixed load pending orders.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
@@ -21,6 +22,26 @@ var (
|
||||
WaybillManager *WaybillController
|
||||
)
|
||||
|
||||
type StatusTimer interface {
|
||||
GetStatusTime() time.Time
|
||||
}
|
||||
|
||||
type StatusTimerSlice []StatusTimer
|
||||
|
||||
func (s StatusTimerSlice) Len() int {
|
||||
return len(s)
|
||||
}
|
||||
|
||||
func (s StatusTimerSlice) Less(i, j int) bool {
|
||||
return s[i].GetStatusTime().Sub(s[j].GetStatusTime()) < 0
|
||||
}
|
||||
|
||||
func (s StatusTimerSlice) Swap(i, j int) {
|
||||
tmp := s[i]
|
||||
s[i] = s[j]
|
||||
s[j] = tmp
|
||||
}
|
||||
|
||||
func init() {
|
||||
OrderManager = NewOrderManager()
|
||||
WaybillManager = NewWaybillManager()
|
||||
@@ -48,69 +69,48 @@ func addOrderOrWaybillStatus(status *model.OrderStatus, db orm.Ormer) (isDuplica
|
||||
return isDuplicated, err
|
||||
}
|
||||
|
||||
// todo 可以考虑改成完全按StatusTime来发送事件
|
||||
func LoadPendingOrders() {
|
||||
orders := OrderManager.LoadPendingOrders()
|
||||
globals.SugarLogger.Infof("LoadPendingOrders orders count:%d", len(orders))
|
||||
bills := WaybillManager.LoadPendingWaybills()
|
||||
globals.SugarLogger.Infof("LoadPendingOrders waybills count:%d", len(bills))
|
||||
|
||||
billsMap := map[string][]*model.Waybill{}
|
||||
for _, v := range bills {
|
||||
savedBills := []*model.Waybill{v}
|
||||
if bills, ok := billsMap[v.VendorOrderID]; ok {
|
||||
savedBills = append(bills, v)
|
||||
}
|
||||
billsMap[v.VendorOrderID] = savedBills
|
||||
}
|
||||
var sortOrders StatusTimerSlice
|
||||
for _, order := range orders {
|
||||
isNoNewSent := false
|
||||
orderNew := *order
|
||||
orderNew.Status = model.OrderStatusNew
|
||||
orderNew.StatusTime = order.OrderCreatedAt
|
||||
jxutils.CallMsgHandlerAsync(func() {
|
||||
scheduler.CurrentScheduler.OnOrderNew(&orderNew)
|
||||
}, order.VendorOrderID)
|
||||
for _, bill := range billsMap[order.VendorOrderID] {
|
||||
if order.Status > model.OrderStatusNew && !isNoNewSent && order.StatusTime.Sub(bill.WaybillCreatedAt) < 0 {
|
||||
isNoNewSent = true
|
||||
order2 := *order
|
||||
jxutils.CallMsgHandlerAsync(func() {
|
||||
scheduler.CurrentScheduler.OnOrderStatusChanged(model.Order2Status(&order2))
|
||||
}, order.VendorOrderID)
|
||||
}
|
||||
billNew := *bill
|
||||
billNew.Status = model.OrderStatusNew
|
||||
billNew.StatusTime = order.OrderCreatedAt
|
||||
jxutils.CallMsgHandlerAsync(func() {
|
||||
scheduler.CurrentScheduler.OnWaybillStatusChanged(&billNew)
|
||||
}, bill.VendorOrderID)
|
||||
if order.Status > model.OrderStatusNew && !isNoNewSent && order.StatusTime.Sub(bill.StatusTime) < 0 {
|
||||
isNoNewSent = true
|
||||
order2 := *order
|
||||
jxutils.CallMsgHandlerAsync(func() {
|
||||
scheduler.CurrentScheduler.OnOrderStatusChanged(model.Order2Status(&order2))
|
||||
}, order.VendorOrderID)
|
||||
}
|
||||
if bill.Status > model.WaybillStatusNew {
|
||||
bill2 := *bill
|
||||
jxutils.CallMsgHandlerAsync(func() {
|
||||
scheduler.CurrentScheduler.OnWaybillStatusChanged(&bill2)
|
||||
}, bill.VendorOrderID)
|
||||
}
|
||||
if order.Status > model.OrderStatusNew && !isNoNewSent {
|
||||
isNoNewSent = true
|
||||
order2 := *order
|
||||
jxutils.CallMsgHandlerAsync(func() {
|
||||
scheduler.CurrentScheduler.OnOrderStatusChanged(model.Order2Status(&order2))
|
||||
}, order.VendorOrderID)
|
||||
}
|
||||
}
|
||||
if order.Status > model.OrderStatusNew && !isNoNewSent {
|
||||
if order.Status > model.OrderStatusNew {
|
||||
order2 := *order
|
||||
sortOrders = append(sortOrders, &order2)
|
||||
}
|
||||
order.Status = model.OrderStatusNew
|
||||
order.StatusTime = order.OrderCreatedAt
|
||||
sortOrders = append(sortOrders, order)
|
||||
}
|
||||
for _, bill := range bills {
|
||||
if bill.Status > model.WaybillStatusNew {
|
||||
bill2 := *bill
|
||||
sortOrders = append(sortOrders, &bill2)
|
||||
}
|
||||
bill.Status = model.WaybillStatusNew
|
||||
bill.StatusTime = bill.WaybillCreatedAt
|
||||
sortOrders = append(sortOrders, bill)
|
||||
}
|
||||
sort.Sort(sortOrders)
|
||||
for _, item := range sortOrders {
|
||||
if order, ok := item.(*model.GoodsOrder); ok {
|
||||
if order.Status == model.OrderStatusNew {
|
||||
jxutils.CallMsgHandlerAsync(func() {
|
||||
scheduler.CurrentScheduler.OnOrderNew(order)
|
||||
}, order.VendorOrderID)
|
||||
} else {
|
||||
jxutils.CallMsgHandlerAsync(func() {
|
||||
scheduler.CurrentScheduler.OnOrderStatusChanged(model.Order2Status(order))
|
||||
}, order.VendorOrderID)
|
||||
}
|
||||
} else {
|
||||
bill := item.(*model.Waybill)
|
||||
jxutils.CallMsgHandlerAsync(func() {
|
||||
scheduler.CurrentScheduler.OnOrderStatusChanged(model.Order2Status(&order2))
|
||||
}, order.VendorOrderID)
|
||||
scheduler.CurrentScheduler.OnWaybillStatusChanged(bill)
|
||||
}, bill.VendorOrderID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user