- refactor

- load pending orders, fixed bug.
This commit is contained in:
gazebo
2018-07-22 00:19:38 +08:00
parent b9e59e1197
commit 77c1dd07b5
18 changed files with 168 additions and 145 deletions

View File

@@ -35,10 +35,13 @@ func addOrderOrWaybillStatus(status *model.OrderStatus, db orm.Ormer) (isDuplica
created, _, err := db.ReadOrCreate(status, "VendorOrderID", "VendorID", "OrderType", "VendorStatus", "StatusTime")
if err == nil {
if !created {
globals.SugarLogger.Infof("duplicated event:%v", status)
isDuplicated = true
status.DuplicatedCount++
_, err = db.Update(status, "DuplicatedCount")
globals.SugarLogger.Infof("duplicated event:%v", status)
utils.CallFuncLogError(func() error {
_, err = db.Update(status, "DuplicatedCount")
return err
}, "addOrderOrWaybillStatus update DuplicatedCount")
}
}
if err != nil {
@@ -86,3 +89,64 @@ func GetDataCityCodeFromOrder(order *model.GoodsOrder) (retVal string, err error
}
return retVal, 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
}
for _, order := range orders {
isNoNewSent := false
orderNew := *order
orderNew.Status = model.OrderStatusNew
orderNew.StatusTime = order.OrderCreatedAt
routinePool.CallFunAsync(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
routinePool.CallFunAsync(func() {
scheduler.CurrentScheduler.OnOrderStatusChanged(model.Order2Status(&order2))
}, order.VendorOrderID)
}
billNew := *bill
billNew.Status = model.OrderStatusNew
billNew.StatusTime = order.OrderCreatedAt
routinePool.CallFunAsync(func() {
scheduler.CurrentScheduler.OnWaybillStatusChanged(&billNew)
}, bill.VendorOrderID)
if order.Status > model.OrderStatusNew && !isNoNewSent && order.StatusTime.Sub(bill.StatusTime) < 0 {
isNoNewSent = true
order2 := *order
routinePool.CallFunAsync(func() {
scheduler.CurrentScheduler.OnOrderStatusChanged(model.Order2Status(&order2))
}, order.VendorOrderID)
}
if bill.Status > model.WaybillStatusNew {
bill2 := *bill
routinePool.CallFunAsync(func() {
scheduler.CurrentScheduler.OnWaybillStatusChanged(&bill2)
}, bill.VendorOrderID)
}
if order.Status > model.OrderStatusNew {
isNoNewSent = true
order2 := *order
routinePool.CallFunAsync(func() {
scheduler.CurrentScheduler.OnOrderStatusChanged(model.Order2Status(&order2))
}, order.VendorOrderID)
}
}
}
}