Files
jx-callback/business/jxstore/misc/misc.go
2019-07-11 10:44:07 +08:00

169 lines
5.6 KiB
Go

package misc
import (
"fmt"
"strings"
"time"
"git.rosy.net.cn/baseapi/utils"
"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/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"
)
func Init() {
StartRefreshEbaiRealMobile()
StartDailyWork()
if globals.ReallyCallPlatformAPI {
ScheduleTimerFunc(func() {
cms.SaveAndSendAlarmVendorSnapshot(jxcontext.AdminCtx, nil, nil)
}, cms.WatchVendorStoreTimeList)
}
}
func StartRefreshEbaiRealMobile() {
if globals.ReallyCallPlatformAPI {
utils.AfterFuncWithRecover(5*time.Second, func() {
RefreshEbaiRealMobile()
})
}
}
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) {
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 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) {
now := time.Now()
nextTime := jxutils.GetNextTimeFromList(now, timeList)
duration := nextTime.Sub(now) + 1*time.Second
globals.SugarLogger.Debugf("ScheduleTimerFunc, duration:%v", duration)
utils.AfterFuncWithRecover(duration, func() {
handler()
ScheduleTimerFunc(handler, timeList)
})
}