209 lines
6.5 KiB
Go
209 lines
6.5 KiB
Go
package misc
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"git.rosy.net.cn/baseapi/utils"
|
|
"git.rosy.net.cn/jx-callback/business/jxstore/act"
|
|
"git.rosy.net.cn/jx-callback/business/jxstore/cms"
|
|
"git.rosy.net.cn/jx-callback/business/jxutils"
|
|
"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
|
|
"git.rosy.net.cn/jx-callback/business/jxutils/netprinter"
|
|
"git.rosy.net.cn/jx-callback/business/jxutils/tasksch"
|
|
"git.rosy.net.cn/jx-callback/business/model"
|
|
"git.rosy.net.cn/jx-callback/business/model/dao"
|
|
"git.rosy.net.cn/jx-callback/business/partner"
|
|
"git.rosy.net.cn/jx-callback/globals"
|
|
)
|
|
|
|
const (
|
|
SpecialTaskID = "Running"
|
|
TaskNameSyncStoreSku = "SyncStoreSku"
|
|
)
|
|
|
|
var (
|
|
dailyWorkTimeList = []string{
|
|
"20:30:00",
|
|
}
|
|
refreshPageActTimeList = []string{
|
|
"7:00:00",
|
|
"8:00:00",
|
|
"9:00:00",
|
|
"10:00:00",
|
|
"11:00:00",
|
|
"12:00:00",
|
|
"13:00:00",
|
|
"14:00:00",
|
|
"15:00:00",
|
|
"16:00:00",
|
|
"17:00:00",
|
|
"18:00:00",
|
|
"19:00:00",
|
|
"22:00:00",
|
|
}
|
|
openRemoteStoreTimeList = []string{
|
|
"23:30:00",
|
|
}
|
|
updateActStatusTimeList = []string{
|
|
"00:01:00",
|
|
}
|
|
|
|
autoSaleStoreSkuTimeList = []string{
|
|
cms.AutoSaleAtStr,
|
|
}
|
|
|
|
importantTaskMap = &sync.Map{}
|
|
)
|
|
|
|
func GetImportantTaskID(taskName string) string {
|
|
if value, ok := importantTaskMap.Load(taskName); ok {
|
|
return value.(string)
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func SaveImportantTaskID(taskName, taskID string) {
|
|
importantTaskMap.Store(taskName, taskID)
|
|
}
|
|
|
|
func IsImportantTaskRunning(taskName string) bool {
|
|
taskID := GetImportantTaskID(taskName)
|
|
if taskID == "" {
|
|
return false
|
|
} else if taskID == SpecialTaskID {
|
|
return true
|
|
}
|
|
return tasksch.IsTaskRunning(taskID)
|
|
}
|
|
|
|
func Init() {
|
|
if globals.IsProductEnv() {
|
|
ScheduleTimerFunc("doDailyWork", 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("auto enable remote store", func() {
|
|
cms.EnableHaveRestStores(jxcontext.AdminCtx, false, true)
|
|
cms.OpenRemoteStoreByJxStatus(jxcontext.AdminCtx, nil, nil, false, false, true)
|
|
}, openRemoteStoreTimeList)
|
|
|
|
ScheduleTimerFunc("SaveAndSendAlarmVendorSnapshot", func() {
|
|
cms.SaveAndSendAlarmVendorSnapshot(jxcontext.AdminCtx, nil, nil, false)
|
|
}, cms.WatchVendorStoreTimeList)
|
|
|
|
ScheduleTimerFunc("RefreshPageActs", func() {
|
|
act.RefreshPageActs(jxcontext.AdminCtx, []int{model.VendorIDEBAI}, time.Now().Add(-30*24*time.Hour), false)
|
|
}, refreshPageActTimeList)
|
|
ScheduleTimerFunc("UpdateActStatusByTime", func() {
|
|
dao.UpdateActStatusByTime(dao.GetDB(), time.Now().Add(-48*time.Hour))
|
|
}, updateActStatusTimeList)
|
|
InitEx()
|
|
}
|
|
ScheduleTimerFunc("AutoSaleStoreSku", func() {
|
|
cms.AutoSaleStoreSku(jxcontext.AdminCtx, nil, false)
|
|
}, autoSaleStoreSkuTimeList)
|
|
}
|
|
|
|
func doDailyWork() {
|
|
globals.SugarLogger.Debug("doDailyWork")
|
|
netprinter.RebindAllPrinters(jxcontext.AdminCtx, false, true)
|
|
// cms.CurVendorSync.FullSyncStoresSkus(jxcontext.AdminCtx, dao.GetDB(), []int{model.VendorIDJD}, nil, true, true)
|
|
|
|
SaveImportantTaskID(TaskNameSyncStoreSku, SpecialTaskID)
|
|
taskID, _ := cms.CurVendorSync.SyncStoresSkus2(jxcontext.AdminCtx, dao.GetDB(), []int{model.VendorIDJD, model.VendorIDEBAI, model.VendorIDMTWM}, nil, nil, []int{27379, 30768, 30888}, model.SyncFlagSaleMask|model.SyncFlagPriceMask, true, true)
|
|
SaveImportantTaskID(TaskNameSyncStoreSku, taskID)
|
|
}
|
|
|
|
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)
|
|
if handler == nil {
|
|
return "", fmt.Errorf("不合法的vendorID:%d", vendorID)
|
|
}
|
|
sql := `
|
|
SELECT *
|
|
FROM goods_order
|
|
WHERE vendor_id = ? AND consignee_mobile2 = ''
|
|
`
|
|
sqlParams := []interface{}{
|
|
vendorID,
|
|
}
|
|
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 RefreshRealMobile", tasksch.NewParallelConfig().SetIsContinueWhenError(isContinueWhenError), ctx,
|
|
func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
|
|
order := batchItemList[0].(*model.GoodsOrder)
|
|
mobile, err2 := handler.GetOrderRealMobile(ctx, order)
|
|
if err = err2; err == nil {
|
|
mobile = jxutils.FormalizeMobile(mobile)
|
|
if !jxutils.IsMobileFake(mobile) && strings.Index(order.ConsigneeMobile, mobile) == -1 {
|
|
order.ConsigneeMobile2 = mobile
|
|
_, err = dao.UpdateEntity(db, order, "ConsigneeMobile2")
|
|
}
|
|
} else {
|
|
globals.SugarLogger.Infof("RefreshRealMobile orderID:%s failed with error:%v", order.VendorOrderID, err)
|
|
}
|
|
return nil, err
|
|
}, orderList)
|
|
tasksch.HandleTask(task, nil, true).Run()
|
|
hint = task.ID
|
|
if !isAsync {
|
|
_, err = task.GetResult(0)
|
|
}
|
|
}
|
|
return hint, err
|
|
}
|
|
|
|
// 按时间序列循环
|
|
func ScheduleTimerFunc(name string, handler func(), timeList []string) {
|
|
now := time.Now()
|
|
nextTime := jxutils.GetNextTimeFromList(now, timeList)
|
|
duration := nextTime.Sub(now) + 1*time.Second
|
|
globals.SugarLogger.Debugf("ScheduleTimerFunc, func:%s, duration:%v", name, duration)
|
|
utils.AfterFuncWithRecover(duration, func() {
|
|
globals.SugarLogger.Debugf("ScheduleTimerFunc func:%s", name)
|
|
handler()
|
|
ScheduleTimerFunc(name, handler, timeList)
|
|
})
|
|
}
|
|
|
|
// 按时间调度一次
|
|
func ScheduleTimerFuncOnce(name string, handler func(), timeStr string) {
|
|
now := time.Now()
|
|
nextTime := jxutils.GetNextTimeFromList(now, []string{timeStr})
|
|
duration := nextTime.Sub(now) + 1*time.Second
|
|
globals.SugarLogger.Debugf("ScheduleTimerFuncOnce, func:%s, duration:%v", name, duration)
|
|
utils.AfterFuncWithRecover(duration, func() {
|
|
globals.SugarLogger.Debugf("ScheduleTimerFuncOnce func:%s", name)
|
|
handler()
|
|
})
|
|
}
|
|
|
|
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)
|
|
})
|
|
}
|