Files
jx-callback/business/jxstore/misc/misc2.go

145 lines
5.2 KiB
Go

package misc
import (
"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/jxcontext"
"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/baseapi"
)
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 AddOrDelExtraStoreOptime(vendorID, storeID int, vendorStoreID string, storeInfo *model.Store, needAddTime bool) {
ctx := jxcontext.AdminCtx
opTimeList := storeInfo.GetOpTimeList()
if needAddTime {
if len(opTimeList) == 0 {
opTimeList = append(opTimeList, startOpStoreTime, endOpStoreTime)
} else 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 StartOrEndOpStore(startOrEnd bool, startTime, endTime int16) {
if startTime == 0 || endTime == 0 {
startOpStoreTime = startOpStoreTimeDefault
endOpStoreTime = endOpStoreTimeDefault
baseapi.SugarLogger.Debugf("The store time can't be 0")
} else {
startOpStoreTime = startTime
endOpStoreTime = endTime
baseapi.SugarLogger.Debugf("SetStoreOptime:%d do:%d", startTime, endTime)
}
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("storeInfo error:%v", 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())
if _, ok := vendorList[vendorID]; ok {
vendorStoreID := vendorListValue["vendorStoreID"].(string)
baseapi.SugarLogger.Debugf("storeID:%d vendorID:%d vendorStoreID:%s vendorListValue:%v", storeID, vendorID, vendorStoreID, vendorListValue)
storeSkuHandler := partner.GetPurchasePlatformFromVendorID(vendorID).(partner.IPurchasePlatformStoreSkuHandler)
// storeSkuList, err := storeSkuHandler.GetStoreSkusBareInfo(ctx, nil, storeID, vendorStoreID, nil)
singleStoreHandler := partner.GetPurchasePlatformFromVendorID(vendorID).(partner.ISingleStoreStoreSkuHandler)
storeSkuNameList, err := singleStoreHandler.GetStoreSkusFullInfo(ctx, nil, storeID, vendorStoreID, nil)
storeSkuList := putils.StoreSkuFullList2Bare(storeSkuNameList)
if err != nil {
baseapi.SugarLogger.Errorf("storeSkuList error:%v", err)
} else {
if len(storeSkuList) > 0 {
if !startOrEnd {
AddOrDelExtraStoreOptime(vendorID, storeID, vendorStoreID, &storeListValue.Store, false)
}
skuNameMap := make(map[int]*partner.SkuNameInfo)
for _, value := range storeSkuNameList {
skuNameMap[value.NameID] = value
}
isContinueWhenError := true
_, err = putils.FreeBatchStoreSkuInfo(func(batchedStoreSkuList []*partner.StoreSkuInfo) (result interface{}, err error) {
var filterBatchedStoreSkuList []*partner.StoreSkuInfo
for _, skuValue := range batchedStoreSkuList {
skuNameInfo := skuNameMap[skuValue.SkuID]
needCheckName := skuNameInfo != nil
skuName := ""
if skuNameInfo != nil {
skuName = skuNameInfo.Name
}
if (needCheckName && skuName != specialSkuName) || !needCheckName {
skuValue.Stock = GetStockValue(startOrEnd)
filterBatchedStoreSkuList = append(filterBatchedStoreSkuList, skuValue)
}
}
var successList []*partner.StoreSkuInfo
if successList, err = storeSkuHandler.UpdateStoreSkusStock(ctx, storeID, vendorStoreID, filterBatchedStoreSkuList); err == nil {
successList = batchedStoreSkuList
baseapi.SugarLogger.Debugf("successList:%v error:%v", successList, err)
}
return nil, err
}, ctx, nil, storeSkuList, storeSkuHandler.GetStoreSkusBatchSize(partner.FuncUpdateStoreSkusStock), isContinueWhenError)
if startOrEnd {
AddOrDelExtraStoreOptime(vendorID, storeID, vendorStoreID, &storeListValue.Store, true)
}
}
}
}
}
}
}
}
}
func InitEx() {
ScheduleTimerFunc(func() {
StartOrEndOpStore(true, 0, 0)
}, startOpStoreTimeList)
ScheduleTimerFunc(func() {
StartOrEndOpStore(false, 0, 0)
}, endOpStoreTimeList)
}