auto refresh store's open and close time
This commit is contained in:
152
business/jxstore/misc/misc2.go
Normal file
152
business/jxstore/misc/misc2.go
Normal file
@@ -0,0 +1,152 @@
|
||||
package misc
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
"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/model"
|
||||
"git.rosy.net.cn/jx-callback/business/partner"
|
||||
"git.rosy.net.cn/jx-callback/globals"
|
||||
)
|
||||
|
||||
const (
|
||||
specialSkuName = "温馨提示"
|
||||
startOpStoreStockNumber = 0
|
||||
endOpStoreStockNumber = model.MaxStoreSkuStockQty
|
||||
startOpStoreTimeDefault = int16(5)
|
||||
endOpStoreTimeDefault = int16(2355)
|
||||
)
|
||||
|
||||
var (
|
||||
startOpStoreTime = startOpStoreTimeDefault
|
||||
endOpStoreTime = endOpStoreTimeDefault
|
||||
startOpStoreTimeList = []string {
|
||||
"00:00:00",
|
||||
}
|
||||
endOpStoreTimeList = []string {
|
||||
"06:00:00",
|
||||
}
|
||||
vendorList = map[int]bool {
|
||||
model.VendorIDMTWM: true,
|
||||
model.VendorIDEBAI: true,
|
||||
}
|
||||
)
|
||||
|
||||
func SetStoreOptime(startTime, endTime int16) {
|
||||
if startTime == 0 || endTime == 0 {
|
||||
startOpStoreTime = startOpStoreTimeDefault
|
||||
endOpStoreTime = endOpStoreTimeDefault
|
||||
fmt.Println("The store time can't be 0")
|
||||
} else {
|
||||
startOpStoreTime = startTime
|
||||
endOpStoreTime = endTime
|
||||
fmt.Println("SetStoreOptime:", startTime, endTime)
|
||||
}
|
||||
}
|
||||
|
||||
func AddOrDelExtraStoreOptime(vendorID, storeID int, vendorStoreID string, storeInfo *model.Store, needAddTime bool) {
|
||||
ctx := jxcontext.AdminCtx
|
||||
opTimeList := storeInfo.GetOpTimeList()
|
||||
if needAddTime {
|
||||
if len(opTimeList) == 2 {
|
||||
opTimeList[0] = startOpStoreTime
|
||||
opTimeList[1] = endOpStoreTime
|
||||
}
|
||||
}
|
||||
handler := partner.GetPurchasePlatformFromVendorID(vendorID).(partner.IStoreHandler)
|
||||
handler.UpdateStoreOpTime(ctx, storeID, vendorStoreID, opTimeList)
|
||||
}
|
||||
|
||||
func GetStockValue(startOrEnd bool) int {
|
||||
if startOrEnd {
|
||||
return startOpStoreStockNumber
|
||||
} else {
|
||||
return endOpStoreStockNumber
|
||||
}
|
||||
}
|
||||
|
||||
func GetSkuNameMap(vendorID, storeID int, vendorStoreID string) map[int]*partner.SkuNameInfo {
|
||||
skuNameMap := make(map[int]*partner.SkuNameInfo)
|
||||
ctx := jxcontext.AdminCtx
|
||||
singleStoreHandler, _ := partner.GetPurchasePlatformFromVendorID(vendorID).(partner.ISingleStoreStoreSkuHandler)
|
||||
skuNameList, err := singleStoreHandler.GetStoreSkusFullInfo(ctx, nil, storeID, vendorStoreID, nil)
|
||||
if err == nil {
|
||||
for _, value := range skuNameList {
|
||||
skuNameMap[value.NameID] = value
|
||||
}
|
||||
}
|
||||
return skuNameMap
|
||||
}
|
||||
|
||||
func StartOrEndOpStore(startOrEnd bool) {
|
||||
ctx := jxcontext.AdminCtx
|
||||
storeInfo, err := cms.GetStores(ctx, "", map[string]interface{}{}, 0, -1, utils.DefaultTimeValue, utils.DefaultTimeValue, 0, 0)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
} else {
|
||||
for _, storeListValue := range storeInfo.Stores {
|
||||
storeID := storeListValue.ID
|
||||
if storeListValue.StoreMaps != nil {
|
||||
for _, vendorListValue := range storeListValue.StoreMaps {
|
||||
vendorID, _ := strconv.Atoi(vendorListValue["vendorID"].(json.Number).String())
|
||||
vendorStoreID := vendorListValue["vendorStoreID"].(string)
|
||||
fmt.Println(storeID, vendorID, vendorStoreID, "----", vendorListValue)
|
||||
storeSkuHandler := partner.GetPurchasePlatformFromVendorID(vendorID).(partner.IPurchasePlatformStoreSkuHandler)
|
||||
storeSkuList, err := storeSkuHandler.GetStoreSkusBareInfo(ctx, nil, storeID, vendorStoreID, nil)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
} else {
|
||||
if _, ok := vendorList[vendorID]; ok {
|
||||
if len(storeSkuList) > 0 {
|
||||
if !startOrEnd {
|
||||
AddOrDelExtraStoreOptime(vendorID, storeID, vendorStoreID, &storeListValue.Store, false)
|
||||
}
|
||||
skuNameMap := GetSkuNameMap(vendorID, storeID, vendorStoreID)
|
||||
for _, skuValue := range storeSkuList {
|
||||
skuNameInfo := skuNameMap[skuValue.SkuID]
|
||||
needCheckName := skuNameInfo != nil
|
||||
skuName := ""
|
||||
if skuNameInfo != nil {
|
||||
skuName = skuNameInfo.Name
|
||||
}
|
||||
if (needCheckName && skuName != specialSkuName) || !needCheckName {
|
||||
skuValue.Stock = GetStockValue(startOrEnd)
|
||||
var storeSkuList = []*partner.StoreSkuInfo{skuValue}
|
||||
if successList, err := storeSkuHandler.UpdateStoreSkusStock(ctx, storeID, vendorStoreID, storeSkuList); err == nil {
|
||||
fmt.Println(successList, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
if startOrEnd {
|
||||
AddOrDelExtraStoreOptime(vendorID, storeID, vendorStoreID, &storeListValue.Store, true)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func InitEx() {
|
||||
ScheduleTimerFuncEx(StartOrEndOpStore, true, startOpStoreTimeList)
|
||||
ScheduleTimerFuncEx(StartOrEndOpStore, false, endOpStoreTimeList)
|
||||
}
|
||||
|
||||
func ScheduleTimerFuncEx(handler func(bool), startOrEnd bool, 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(startOrEnd)
|
||||
ScheduleTimerFuncEx(handler, startOrEnd, timeList)
|
||||
})
|
||||
}
|
||||
9
business/jxstore/misc/misc2_test.go
Normal file
9
business/jxstore/misc/misc2_test.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package misc
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestStartOrEndOpStore(t *testing.T) {
|
||||
StartOrEndOpStore(true)
|
||||
}
|
||||
Reference in New Issue
Block a user