diff --git a/business/jxcallback/scheduler/defsch/defsch.go b/business/jxcallback/scheduler/defsch/defsch.go index 2cd7e3efb..83832bf3f 100644 --- a/business/jxcallback/scheduler/defsch/defsch.go +++ b/business/jxcallback/scheduler/defsch/defsch.go @@ -63,6 +63,15 @@ var ( FixedScheduler *DefScheduler ) +// type tTimerInfo struct { +// statusType int +// vendorID int +// status int + +// timer *time.Timer +// timerTime time.Time +// } + type WatchOrderInfo struct { order *model.GoodsOrder // order里的信息是保持更新的 @@ -79,6 +88,8 @@ type WatchOrderInfo struct { timer *time.Timer timerTime time.Time + // timerList []*tTimerInfo + retryCount int // 失败后尝试的次数,调试阶段可能出现死循化,阻止这种情况发生 } @@ -153,6 +164,40 @@ func (s *WatchOrderInfo) GetWaybillVendorIDs() (vendorIDs []int) { return vendorIDs } +// orderType,-1:全部 +// vendorID,-1:全部 +// status,-1:全部 +// func (w *WatchOrderInfo) StopTimer(statusType, vendorID, status int) { +// var newTimerList []*tTimerInfo +// for _, timerInfo := range w.timerList { +// if (statusType == -1 || statusType == timerInfo.statusType) || +// (vendorID == -1 || vendorID == timerInfo.vendorID) || +// (status == -1 || status <= timerInfo.status) { +// if timerInfo.timer != nil { +// timerInfo.timer.Stop() +// timerInfo.timer = nil +// } +// } else { +// newTimerList = append(newTimerList, timerInfo) +// } +// } +// w.timerList = newTimerList +// } + +func (w *WatchOrderInfo) GetCreateWaybillTimeout() (timeoutSecond int) { + if w.timerStatusType == scheduler.TimerStatusTypeWaybill && w.timerStatus == model.WaybillStatusNew { + timeoutSecond = int(w.timerTime.Sub(time.Now()) / time.Second) + } + // for _, timerInfo := range w.timerList { + // if timerInfo.statusType == scheduler.TimerStatusTypeWaybill && + // timerInfo.status == model.WaybillStatusNew { + // timeoutSecond = int(timerInfo.timerTime.Sub(time.Now()) / time.Second) + // break + // } + // } + return timeoutSecond +} + func init() { sch := &DefScheduler{} basesch.FixedBaseScheduler = &sch.BaseScheduler @@ -726,6 +771,82 @@ func (s *DefScheduler) loadSavedOrderFromMap(status *model.OrderStatus, isForceL return realSavedInfo } +// func (s *DefScheduler) resetTimer(savedOrderInfo *WatchOrderInfo, bill *model.Waybill, isPending bool) { +// order := savedOrderInfo.order +// status := order.Status +// statusType := scheduler.TimerStatusTypeOrder +// vendorID := order.VendorID +// statusTime := order.StatusTime +// if bill != nil { +// status = bill.Status +// statusType = scheduler.TimerStatusTypeWaybill +// vendorID = bill.WaybillVendorID +// statusTime = bill.StatusTime +// } +// globals.SugarLogger.Debugf("resetTimer, orderID:%s statusType:%d status:%d", order.VendorOrderID, statusType, status) +// config := s.mergeOrderStatusConfig(savedOrderInfo, statusTime, statusType, status) + +// stopStatusType := statusType +// stopStatus := status +// if statusType == scheduler.TimerStatusTypeOrder { +// if status > model.OrderStatusEndBegin { +// stopStatusType = -1 +// stopStatus = -1 +// } +// } +// if config == nil || config.TimerType != partner.TimerTypeByPass { +// savedOrderInfo.StopTimer(stopStatusType, -1, stopStatus) +// } + +// if config != nil && config.TimeoutAction != nil && config.TimerType != partner.TimerTypeByPass { +// if config.CallShouldSetTimer(savedOrderInfo, bill) { +// timeout := config.GetRefTimeout(statusTime, order.OrderCreatedAt) +// if config.TimeoutGap != 0 { +// timeout += time.Duration(rand.Intn(int(config.TimeoutGap))) * time.Second +// } +// if isPending && timeout < pendingOrderTimerMaxSecond*time.Second { // 如果是PENDING的订单,则将其分布到2--5秒内,让后续事件有机会执行 +// timeout = time.Duration(jxutils.MapValue2Scope(int64(timeout), -pendingOrderTimerMinMinSecond*1000, pendingOrderTimerMaxSecond*1000, pendingOrderTimerMinSecond*1000, pendingOrderTimerMaxSecond*1000)) * time.Millisecond +// } else if timeout < 0 { +// timeout = 0 +// } +// if timeout == 0 { +// config.CallTimeoutAction(savedOrderInfo, bill) +// } else { +// timerName := "" +// if statusType == model.OrderTypeOrder { +// timerName = model.OrderStatusName[status] +// } else if statusType == model.OrderTypeWaybill { +// timerName = model.WaybillStatusName[status] +// } +// timerInfo := &tTimerInfo{ +// statusType: statusType, +// vendorID: vendorID, +// status: status, +// timerTime: time.Now().Add(timeout), +// } +// timerInfo.timer = utils.AfterFuncWithRecover(timeout, func() { +// jxutils.CallMsgHandlerAsync(func() { +// globals.SugarLogger.Debugf("fire timer:%s, orderID:%s", timerName, order.VendorOrderID) +// ts := s.loadSavedOrderFromMap(model.Order2Status(order), true) +// config.CallTimeoutAction(ts, bill) +// timerInfo.timer = nil +// ts.StopTimer(statusType, vendorID, status) +// }, jxutils.ComposeUniversalOrderID(order.VendorOrderID, order.VendorID)) +// }) +// } +// globals.SugarLogger.Debugf("resetTimer, orderID:%s, statusType:%d, status:%d, timeout:%v", order.VendorOrderID, statusType, status, timeout) +// } else { +// globals.SugarLogger.Debugf("resetTimer, orderID:%s, statusType:%d, status:%d, should not set timer", order.VendorOrderID, statusType, status) +// } +// } else { +// globals.SugarLogger.Debugf("resetTimer bypass2, orderID:%s statusType:%d status:%v, config:%s", order.VendorOrderID, statusType, status, utils.Format4Output(config, true)) +// } +// } + +// func (s *DefScheduler) stopTimer(savedOrderInfo *WatchOrderInfo) { +// savedOrderInfo.StopTimer(0, -1, 0) +// } + func (s *DefScheduler) stopTimer(savedOrderInfo *WatchOrderInfo) { if savedOrderInfo.timer != nil { globals.SugarLogger.Debugf("stopTimer orderID:%s", savedOrderInfo.order.VendorOrderID) diff --git a/business/jxcallback/scheduler/defsch/defsch_ext.go b/business/jxcallback/scheduler/defsch/defsch_ext.go index 3b875dbad..0681eb9e8 100644 --- a/business/jxcallback/scheduler/defsch/defsch_ext.go +++ b/business/jxcallback/scheduler/defsch/defsch_ext.go @@ -226,9 +226,7 @@ func (s *DefScheduler) QueryOrderWaybillFeeInfoEx(ctx *jxcontext.Context, vendor var timeoutSecond int if savedOrderInfo := s.loadSavedOrderByID(vendorOrderID, vendorID, false); savedOrderInfo != nil { - if savedOrderInfo.timerStatusType == scheduler.TimerStatusTypeWaybill && savedOrderInfo.timerStatus == model.WaybillStatusNew { - timeoutSecond = int(savedOrderInfo.timerTime.Sub(time.Now()) / time.Second) - } + timeoutSecond = savedOrderInfo.GetCreateWaybillTimeout() } for _, storeCourier := range storeCourierList { var feeInfo *partner.WaybillFeeInfo