- 重构了一下misc中定时器相关的函数
This commit is contained in:
@@ -1088,3 +1088,46 @@ func (c *OrderManager) AmendMissingOrders(ctx *jxcontext.Context, vendorIDs []in
|
|||||||
}
|
}
|
||||||
return hint, err
|
return hint, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *OrderManager) RefreshOrderFinancial(ctx *jxcontext.Context, fromTime, toTime time.Time, isAsync, isContinueWhenError bool) (hint string, err error) {
|
||||||
|
sql := `
|
||||||
|
SELECT *
|
||||||
|
FROM goods_order
|
||||||
|
WHERE status = ? AND total_shop_money = 0
|
||||||
|
`
|
||||||
|
sqlParams := []interface{}{
|
||||||
|
model.OrderStatusFinished,
|
||||||
|
}
|
||||||
|
if !utils.IsTimeZero(fromTime) {
|
||||||
|
sql += " AND order_created_at >= ?"
|
||||||
|
sqlParams = append(sqlParams, fromTime)
|
||||||
|
}
|
||||||
|
if !utils.IsTimeZero(toTime) {
|
||||||
|
sql += " AND order_created_at <= ?"
|
||||||
|
sqlParams = append(sqlParams, toTime)
|
||||||
|
}
|
||||||
|
var orderList []*model.GoodsOrder
|
||||||
|
db := dao.GetDB()
|
||||||
|
if err = dao.GetRows(db, &orderList, sql, sqlParams...); err == nil && len(orderList) > 0 {
|
||||||
|
task := tasksch.NewParallelTask("RefreshOrderFinancial", tasksch.NewParallelConfig().SetIsContinueWhenError(isContinueWhenError), ctx,
|
||||||
|
func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
|
||||||
|
order := batchItemList[0].(*model.GoodsOrder)
|
||||||
|
handler := partner.GetPurchasePlatformFromVendorID(order.VendorID)
|
||||||
|
if handler != nil {
|
||||||
|
remoteOrder, err2 := handler.GetOrder(order.VendorOrderID)
|
||||||
|
if err = err2; err == nil {
|
||||||
|
order.TotalShopMoney = remoteOrder.TotalShopMoney
|
||||||
|
order.PmSubsidyMoney = remoteOrder.PmSubsidyMoney
|
||||||
|
err = partner.CurOrderManager.UpdateOrderFields(order, []string{"TotalShopMoney", "PmSubsidyMoney"})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}, orderList)
|
||||||
|
tasksch.HandleTask(task, nil, true).Run()
|
||||||
|
hint = task.ID
|
||||||
|
if !isAsync {
|
||||||
|
_, err = task.GetResult(0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return hint, err
|
||||||
|
}
|
||||||
|
|||||||
@@ -19,6 +19,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
dailyWorkTimeList = []string{
|
||||||
|
"21:00:00",
|
||||||
|
}
|
||||||
refreshPageActTimeList = []string{
|
refreshPageActTimeList = []string{
|
||||||
"7:00:00",
|
"7:00:00",
|
||||||
"8:00:00",
|
"8:00:00",
|
||||||
@@ -35,14 +38,26 @@ var (
|
|||||||
"19:00:00",
|
"19:00:00",
|
||||||
"22:00:00",
|
"22:00:00",
|
||||||
}
|
}
|
||||||
|
openRemoteStoreTimeList = []string{
|
||||||
|
"06:00:00",
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func Init() {
|
func Init() {
|
||||||
StartRefreshEbaiRealMobile()
|
|
||||||
StartDailyWork()
|
|
||||||
if globals.ReallyCallPlatformAPI {
|
if globals.ReallyCallPlatformAPI {
|
||||||
|
ScheduleTimerFunc(doDailyWork, dailyWorkTimeList)
|
||||||
|
|
||||||
|
ScheduleTimerFuncByInterval(func() {
|
||||||
|
RefreshRealMobile(jxcontext.AdminCtx, model.VendorIDEBAI, time.Now().Add(-24*time.Hour), utils.DefaultTimeValue, false, true)
|
||||||
|
}, 5*time.Second, 1*time.Hour)
|
||||||
|
|
||||||
ScheduleTimerFunc(func() {
|
ScheduleTimerFunc(func() {
|
||||||
cms.SaveAndSendAlarmVendorSnapshot(jxcontext.AdminCtx, nil, nil)
|
cms.EnableHaveRestStores(jxcontext.AdminCtx, false, true)
|
||||||
|
cms.OpenRemoteStoreByJxStatus(jxcontext.AdminCtx, nil, nil, false, false, true)
|
||||||
|
}, openRemoteStoreTimeList)
|
||||||
|
|
||||||
|
ScheduleTimerFunc(func() {
|
||||||
|
cms.SaveAndSendAlarmVendorSnapshot(jxcontext.AdminCtx, nil, nil, false)
|
||||||
}, cms.WatchVendorStoreTimeList)
|
}, cms.WatchVendorStoreTimeList)
|
||||||
|
|
||||||
ScheduleTimerFunc(func() {
|
ScheduleTimerFunc(func() {
|
||||||
@@ -51,22 +66,16 @@ func Init() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func StartRefreshEbaiRealMobile() {
|
func doDailyWork() {
|
||||||
if globals.ReallyCallPlatformAPI {
|
globals.SugarLogger.Debug("doDailyWork")
|
||||||
utils.AfterFuncWithRecover(5*time.Second, func() {
|
// cms.CurVendorSync.FullSyncStoresSkus(jxcontext.AdminCtx, dao.GetDB(), []int{model.VendorIDJD}, nil, true, true)
|
||||||
RefreshEbaiRealMobile()
|
cms.CurVendorSync.SyncStoresSkus(jxcontext.AdminCtx, dao.GetDB(), []int{model.VendorIDJD, model.VendorIDEBAI, model.VendorIDMTWM}, nil, nil, false, true, true)
|
||||||
})
|
netprinter.RebindAllPrinters(jxcontext.AdminCtx, false, true)
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func RefreshEbaiRealMobile() {
|
|
||||||
RefreshRealMobile(jxcontext.AdminCtx, model.VendorIDEBAI, time.Now().Add(-24*time.Hour), utils.DefaultTimeValue, true, true)
|
|
||||||
utils.AfterFuncWithRecover(1*time.Hour, func() {
|
|
||||||
RefreshEbaiRealMobile()
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func RefreshRealMobile(ctx *jxcontext.Context, vendorID int, fromTime, toTime time.Time, isAsync, isContinueWhenError bool) (hint string, err error) {
|
func RefreshRealMobile(ctx *jxcontext.Context, vendorID int, fromTime, toTime time.Time, isAsync, isContinueWhenError bool) (hint string, err error) {
|
||||||
|
globals.SugarLogger.Debug("RefreshRealMobile")
|
||||||
|
|
||||||
handler := partner.GetPurchasePlatformFromVendorID(vendorID)
|
handler := partner.GetPurchasePlatformFromVendorID(vendorID)
|
||||||
if handler == nil {
|
if handler == nil {
|
||||||
return "", fmt.Errorf("不合法的vendorID:%d", vendorID)
|
return "", fmt.Errorf("不合法的vendorID:%d", vendorID)
|
||||||
@@ -114,73 +123,6 @@ func RefreshRealMobile(ctx *jxcontext.Context, vendorID int, fromTime, toTime ti
|
|||||||
return hint, err
|
return hint, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func RefreshOrderFinancial(ctx *jxcontext.Context, fromTime, toTime time.Time, isAsync, isContinueWhenError bool) (hint string, err error) {
|
|
||||||
sql := `
|
|
||||||
SELECT *
|
|
||||||
FROM goods_order
|
|
||||||
WHERE status = ? AND total_shop_money = 0
|
|
||||||
`
|
|
||||||
sqlParams := []interface{}{
|
|
||||||
model.OrderStatusFinished,
|
|
||||||
}
|
|
||||||
if !utils.IsTimeZero(fromTime) {
|
|
||||||
sql += " AND order_created_at >= ?"
|
|
||||||
sqlParams = append(sqlParams, fromTime)
|
|
||||||
}
|
|
||||||
if !utils.IsTimeZero(toTime) {
|
|
||||||
sql += " AND order_created_at <= ?"
|
|
||||||
sqlParams = append(sqlParams, toTime)
|
|
||||||
}
|
|
||||||
var orderList []*model.GoodsOrder
|
|
||||||
db := dao.GetDB()
|
|
||||||
if err = dao.GetRows(db, &orderList, sql, sqlParams...); err == nil && len(orderList) > 0 {
|
|
||||||
task := tasksch.NewParallelTask("misc RefreshOrderFinancial", tasksch.NewParallelConfig().SetIsContinueWhenError(isContinueWhenError), ctx,
|
|
||||||
func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
|
|
||||||
order := batchItemList[0].(*model.GoodsOrder)
|
|
||||||
handler := partner.GetPurchasePlatformFromVendorID(order.VendorID)
|
|
||||||
if handler != nil {
|
|
||||||
remoteOrder, err2 := handler.GetOrder(order.VendorOrderID)
|
|
||||||
if err = err2; err == nil {
|
|
||||||
order.TotalShopMoney = remoteOrder.TotalShopMoney
|
|
||||||
order.PmSubsidyMoney = remoteOrder.PmSubsidyMoney
|
|
||||||
err = partner.CurOrderManager.UpdateOrderFields(order, []string{"TotalShopMoney", "PmSubsidyMoney"})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil, err
|
|
||||||
}, orderList)
|
|
||||||
tasksch.HandleTask(task, nil, true).Run()
|
|
||||||
hint = task.ID
|
|
||||||
if !isAsync {
|
|
||||||
_, err = task.GetResult(0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return hint, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func StartDailyWork() {
|
|
||||||
if globals.ReallyCallPlatformAPI {
|
|
||||||
now := time.Now()
|
|
||||||
runTime := time.Date(now.Year(), now.Month(), now.Day(), 21, 0, 0, 0, time.Local) // 凌晨00:25点开始执行
|
|
||||||
waitDuration := runTime.Sub(now)
|
|
||||||
if waitDuration < 5*time.Second {
|
|
||||||
waitDuration += 24 * time.Hour
|
|
||||||
}
|
|
||||||
globals.SugarLogger.Debugf("dailyWork waitDuration:%d minutes", waitDuration/time.Minute)
|
|
||||||
utils.AfterFuncWithRecover(waitDuration, func() {
|
|
||||||
doDailyWork()
|
|
||||||
StartDailyWork()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func doDailyWork() {
|
|
||||||
globals.SugarLogger.Debug("doDailyWork")
|
|
||||||
cms.EnableHaveRestStores(jxcontext.AdminCtx, true, true)
|
|
||||||
// cms.CurVendorSync.FullSyncStoresSkus(jxcontext.AdminCtx, dao.GetDB(), []int{model.VendorIDJD}, nil, true, true)
|
|
||||||
cms.CurVendorSync.SyncStoresSkus(jxcontext.AdminCtx, dao.GetDB(), []int{model.VendorIDJD, model.VendorIDEBAI, model.VendorIDMTWM}, nil, nil, false, true, true)
|
|
||||||
netprinter.RebindAllPrinters(jxcontext.AdminCtx, false, true)
|
|
||||||
}
|
|
||||||
|
|
||||||
func ScheduleTimerFunc(handler func(), timeList []string) {
|
func ScheduleTimerFunc(handler func(), timeList []string) {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
nextTime := jxutils.GetNextTimeFromList(now, timeList)
|
nextTime := jxutils.GetNextTimeFromList(now, timeList)
|
||||||
@@ -191,3 +133,16 @@ func ScheduleTimerFunc(handler func(), timeList []string) {
|
|||||||
ScheduleTimerFunc(handler, timeList)
|
ScheduleTimerFunc(handler, timeList)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ScheduleTimerFuncByInterval(handler func(), delay, inerval time.Duration) {
|
||||||
|
globals.SugarLogger.Debugf("ScheduleTimerFuncByInterval, delay:%v, inerval:%v", delay, inerval)
|
||||||
|
utils.AfterFuncWithRecover(delay, func() {
|
||||||
|
beginTime := time.Now()
|
||||||
|
handler()
|
||||||
|
delay = inerval - time.Now().Sub(beginTime)
|
||||||
|
if delay < time.Second {
|
||||||
|
delay = time.Second
|
||||||
|
}
|
||||||
|
ScheduleTimerFuncByInterval(handler, delay, inerval)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
13
business/jxstore/misc/misc_test.go
Normal file
13
business/jxstore/misc/misc_test.go
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package misc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestScheduleTimerFuncByInterval(t *testing.T) {
|
||||||
|
ScheduleTimerFuncByInterval(func() {
|
||||||
|
t.Log(time.Now())
|
||||||
|
}, time.Second, 7*time.Second)
|
||||||
|
time.Sleep(30 * time.Second)
|
||||||
|
}
|
||||||
@@ -451,7 +451,7 @@ func (c *OrderController) RefreshOrderFinancial() {
|
|||||||
c.callRefreshOrderFinancial(func(params *tOrderRefreshOrderFinancialParams) (retVal interface{}, errCode string, err error) {
|
c.callRefreshOrderFinancial(func(params *tOrderRefreshOrderFinancialParams) (retVal interface{}, errCode string, err error) {
|
||||||
timeList, err2 := jxutils.BatchStr2Time(params.FromTime, params.ToTime)
|
timeList, err2 := jxutils.BatchStr2Time(params.FromTime, params.ToTime)
|
||||||
if err = err2; err == nil {
|
if err = err2; err == nil {
|
||||||
retVal, err = misc.RefreshOrderFinancial(params.Ctx, timeList[0], timeList[1], params.IsAsync, params.IsContinueWhenError)
|
retVal, err = orderman.FixedOrderManager.RefreshOrderFinancial(params.Ctx, timeList[0], timeList[1], params.IsAsync, params.IsContinueWhenError)
|
||||||
}
|
}
|
||||||
return retVal, "", err
|
return retVal, "", err
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user