179 lines
6.5 KiB
Go
179 lines
6.5 KiB
Go
package misc
|
|
|
|
import (
|
|
"time"
|
|
|
|
"git.rosy.net.cn/baseapi"
|
|
"git.rosy.net.cn/baseapi/utils"
|
|
"git.rosy.net.cn/jx-callback/business/jxstore/cms"
|
|
"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/partner"
|
|
"git.rosy.net.cn/jx-callback/business/partner/putils"
|
|
"git.rosy.net.cn/jx-callback/globals"
|
|
)
|
|
|
|
const (
|
|
specialSkuName = "温馨提示"
|
|
startOpStoreStockNumber = 0
|
|
endOpStoreStockNumber = model.MaxStoreSkuStockQty
|
|
startOpStoreTimeDefaultMTWM = int16(2200)
|
|
endOpStoreTimeDefaultMTWM = int16(2355)
|
|
startOpStoreTimeDefaultEBAI = int16(5)
|
|
endOpStoreTimeDefaultEBAI = int16(2355)
|
|
)
|
|
|
|
var (
|
|
startOpStoreTimeListJXCS = []string{
|
|
"22:10:00",
|
|
}
|
|
endOpStoreTimeListJXCS = []string{
|
|
"06:10:00",
|
|
}
|
|
startOpStoreTimeListJXGY = []string{
|
|
"22:00:00",
|
|
}
|
|
endOpStoreTimeListJXGY = []string{
|
|
"06:00:00",
|
|
}
|
|
vendorList = map[int]bool{
|
|
model.VendorIDMTWM: true,
|
|
model.VendorIDEBAI: true,
|
|
}
|
|
)
|
|
|
|
func AddOrDelExtraStoreOptime(vendorID, storeID int, vendorStoreID string, storeInfo *model.Store, startOpStoreTime, endOpStoreTime int16, needAddTime bool) bool {
|
|
ctx := jxcontext.AdminCtx
|
|
opTimeList := storeInfo.GetOpTimeList()
|
|
if needAddTime {
|
|
opTimeList = []int16{startOpStoreTime, endOpStoreTime}
|
|
}
|
|
handler := partner.GetPurchasePlatformFromVendorID(vendorID).(partner.IStoreHandler)
|
|
return handler.UpdateStoreOpTime(ctx, storeID, vendorStoreID, opTimeList) == nil
|
|
}
|
|
|
|
func GetStockValue(isStart bool) int {
|
|
if isStart {
|
|
return startOpStoreStockNumber
|
|
} else {
|
|
return endOpStoreStockNumber
|
|
}
|
|
}
|
|
|
|
func GetOpStoreTime(vendorID int) (startTime, endTime int16) {
|
|
if vendorID == model.VendorIDMTWM {
|
|
return startOpStoreTimeDefaultMTWM, endOpStoreTimeDefaultMTWM
|
|
} else if vendorID == model.VendorIDEBAI {
|
|
return startOpStoreTimeDefaultEBAI, endOpStoreTimeDefaultEBAI
|
|
}
|
|
|
|
return 0, 0
|
|
}
|
|
|
|
func FilterSkuNameList(storeSkuNameList []*partner.SkuNameInfo) (filterStoreSkuNameList []*partner.SkuNameInfo) {
|
|
for _, skuNameInfo := range storeSkuNameList {
|
|
if skuNameInfo.Name != specialSkuName {
|
|
filterStoreSkuNameList = append(filterStoreSkuNameList, skuNameInfo)
|
|
}
|
|
}
|
|
return filterStoreSkuNameList
|
|
}
|
|
|
|
func StartOrEndOpStore(isStart bool, startTime, endTime int16, isAsync, isContinueWhenError bool) (retVal interface{}, err error) {
|
|
startProcessTime := time.Now().Unix()
|
|
baseapi.SugarLogger.Debugf("StartOrEndOpStore start time: %v", time.Now())
|
|
ctx := jxcontext.AdminCtx
|
|
storeInfo, err := cms.GetStores(ctx, "", map[string]interface{}{}, 0, -1, utils.DefaultTimeValue, utils.DefaultTimeValue, 0, 0)
|
|
|
|
if err != nil {
|
|
baseapi.SugarLogger.Errorf("StartOrEndOpStore cms.GetStores error:%v", err)
|
|
} else {
|
|
taskFunc := func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
|
|
storeListValue := batchItemList[0].(*cms.StoreExt)
|
|
storeID := storeListValue.ID
|
|
if storeListValue.StoreMaps != nil {
|
|
for _, vendorListValue := range storeListValue.StoreMaps {
|
|
vendorID := int(utils.MustInterface2Int64(vendorListValue["vendorID"]))
|
|
if _, ok := vendorList[vendorID]; ok {
|
|
startOpStoreTime, endOpStoreTime := GetOpStoreTime(vendorID)
|
|
if startTime != 0 && endTime != 0 {
|
|
startOpStoreTime = startTime
|
|
endOpStoreTime = endTime
|
|
baseapi.SugarLogger.Debugf("StartOrEndOpStore SetStoreOptime:%d do:%d", startTime, endTime)
|
|
}
|
|
vendorStoreID := utils.Interface2String(vendorListValue["vendorStoreID"])
|
|
baseapi.SugarLogger.Debugf("StartOrEndOpStore storeID:%d vendorID:%d vendorStoreID:%s", storeID, vendorID, vendorStoreID)
|
|
singleStoreHandler := partner.GetPurchasePlatformFromVendorID(vendorID).(partner.ISingleStoreStoreSkuHandler)
|
|
storeSkuNameList, err := singleStoreHandler.GetStoreSkusFullInfo(ctx, task, storeID, vendorStoreID, nil)
|
|
if err != nil {
|
|
baseapi.SugarLogger.Errorf("StartOrEndOpStore GetStoreSkusFullInfo error:%v storeID:%d vendorID:%d vendorStoreID:%s", err, storeID, vendorID, vendorStoreID)
|
|
} else {
|
|
filterStoreSkuNameList := FilterSkuNameList(storeSkuNameList)
|
|
storeSkuList := putils.StoreSkuFullList2Bare(filterStoreSkuNameList)
|
|
if len(storeSkuList) > 0 {
|
|
if !isStart {
|
|
AddOrDelExtraStoreOptime(vendorID, storeID, vendorStoreID, &storeListValue.Store, startOpStoreTime, endOpStoreTime, false)
|
|
}
|
|
|
|
_, err = putils.FreeBatchStoreSkuInfo(func(task tasksch.ITask, batchedStoreSkuList []*partner.StoreSkuInfo) (result interface{}, successCount int, err error) {
|
|
for _, skuValue := range batchedStoreSkuList {
|
|
skuValue.Stock = GetStockValue(isStart)
|
|
}
|
|
//var successList []*partner.StoreSkuInfo
|
|
if _, err = singleStoreHandler.UpdateStoreSkusStock(ctx, storeID, vendorStoreID, batchedStoreSkuList); err == nil {
|
|
//successList = batchedStoreSkuList
|
|
//baseapi.SugarLogger.Debugf("StartOrEndOpStore successList:%v error:%v", successList, err)
|
|
}
|
|
return nil, 0, err
|
|
}, ctx, task, storeSkuList, singleStoreHandler.GetStoreSkusBatchSize(partner.FuncUpdateStoreSkusStock), true)
|
|
|
|
if isStart {
|
|
AddOrDelExtraStoreOptime(vendorID, storeID, vendorStoreID, &storeListValue.Store, startOpStoreTime, endOpStoreTime, true)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return retVal, err
|
|
}
|
|
task := tasksch.NewParallelTask("StartOrEndOpStore", tasksch.NewParallelConfig().SetParallelCount(4), ctx, taskFunc, storeInfo.Stores)
|
|
tasksch.HandleTask(task, nil, true).Run()
|
|
if isAsync {
|
|
retVal = task.ID
|
|
} else {
|
|
_, err = task.GetResult(0)
|
|
if err != nil {
|
|
baseapi.SugarLogger.Debugf("StartOrEndOpStore tasksch error:%v", err)
|
|
}
|
|
retVal = "1"
|
|
}
|
|
}
|
|
endProcessTime := time.Now().Unix()
|
|
diff := endProcessTime - startProcessTime
|
|
baseapi.SugarLogger.Debugf("StartOrEndOpStore end time: %v", time.Now())
|
|
baseapi.SugarLogger.Debugf("StartOrEndOpStore cost time: %d sec", diff)
|
|
|
|
return retVal, err
|
|
}
|
|
|
|
func InitEx() {
|
|
startTimeList := startOpStoreTimeListJXCS
|
|
stopTimeList := endOpStoreTimeListJXCS
|
|
if !globals.IsMainProductEnv() {
|
|
startTimeList = startOpStoreTimeListJXGY
|
|
stopTimeList = endOpStoreTimeListJXGY
|
|
}
|
|
ScheduleTimerFunc("StartOpStore", func() {
|
|
if !IsImportantTaskRunning(TaskNameSyncStoreSku) {
|
|
StartOrEndOpStore(true, 0, 0, false, true)
|
|
}
|
|
}, startTimeList)
|
|
ScheduleTimerFunc("EndOpStore", func() {
|
|
if !IsImportantTaskRunning(TaskNameSyncStoreSku) {
|
|
StartOrEndOpStore(false, 0, 0, false, true)
|
|
}
|
|
}, stopTimeList)
|
|
}
|