定时刷新商品可售状态

This commit is contained in:
苏尹岚
2019-11-14 18:03:34 +08:00
parent 3da95b29f8
commit bcc534289a
5 changed files with 83 additions and 50 deletions

View File

@@ -3,7 +3,6 @@ package cms
import (
"errors"
"fmt"
"time"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/business/jxutils"
@@ -657,29 +656,47 @@ func (v *VendorSync) SyncSkuNames(ctx *jxcontext.Context, nameIDs []int, isForce
func (v *VendorSync) ChangeStoreSkuSaleStatus(ctx *jxcontext.Context, isAsync, isContinueWhenError bool) (err error) {
var (
storeIDs []int
skuIDs []int
vendorIDs = []int{0, 1, 3}
storeIDs []int
skuIDs []int
)
vendorIDs := partner.GetPurchasePlatformVendorIDs()
db := dao.GetDB()
now := time.Now().Hour()*100 + time.Now().Minute()
storeSkuList, err := dao.GetStoresSkusInfoBySaleTime(db)
for _, v := range storeSkuList {
if (now >= v.TimeBegin1 && now <= v.TimeEnd1) || (now >= v.TimeBegin2 && now <= v.TimeEnd2) {
skuIDs = append(skuIDs, v.StoreSkuBind.SkuID)
storeIDs = append(storeIDs, v.StoreSkuBind.StoreID)
v.StoreSkuBind.Status = model.SkuStatusDontSale
setStoreSkuBindStatus(&v.StoreSkuBind, model.SyncFlagSaleMask)
dao.UpdateEntity(db, &v.StoreSkuBind)
} else {
v.StoreSkuBind.Status = model.SkuStatusNormal
setStoreSkuBindStatus(&v.StoreSkuBind, model.SyncFlagSaleMask)
dao.UpdateEntity(db, &v.StoreSkuBind)
}
storeIDs = append(storeIDs, v.StoreID)
skuIDs = append(skuIDs, v.SkuID)
setStoreSkuBindStatus(v, model.SyncFlagSaleMask)
dao.UpdateEntity(db, v)
}
if len(storeSkuList) > 0 {
v.SyncStoresSkus(ctx, db, vendorIDs, storeIDs, skuIDs, false, isAsync, isContinueWhenError)
} else {
return errors.New("未查询到设置了可售时间的商品!")
}
v.SyncStoresSkus(ctx, db, vendorIDs, storeIDs, skuIDs, false, isAsync, isContinueWhenError)
if err != nil {
return err
}
return nil
}
func GetTimeMixByInt(begin1, end1, begin2, end2 int) (beginAt, endAt int) {
if (begin1 > begin2 && begin1 > end2) || (begin2 > end1 && end2 > end1) {
return 0, 0
}
if begin1 > begin2 {
beginAt = begin1
if end1 > end2 {
endAt = end2
} else {
endAt = end1
}
} else {
beginAt = begin2
if end1 > end2 {
endAt = end2
} else {
endAt = end1
}
}
return beginAt, endAt
}

View File

@@ -299,6 +299,7 @@ func syncStoreSkuNew(ctx *jxcontext.Context, parentTask tasksch.ITask, isFull bo
} else {
skus, err = dao.GetStoreSkus(db, vendorID, storeID, skuIDs)
}
if err != nil || len(skus) == 0 {
return err
}
@@ -328,8 +329,13 @@ func syncStoreSkuNew(ctx *jxcontext.Context, parentTask tasksch.ITask, isFull bo
if reorderHandler != nil {
reorderSkuMap = make(map[string][]*dao.StoreSkuSyncInfo)
}
now := time.Now().Hour()*100 + time.Now().Minute()
for _, sku := range skus {
if isUpdateSkuSaleStatus(sku, now) {
sku.MergedStatus = model.SkuStatusDontSale
} else {
sku.MergedStatus = model.SkuStatusNormal
}
var bareSku *partner.StoreSkuInfo
isNeedReorder := false
if isStoreSkuSyncNeedDelete(sku) {
@@ -795,3 +801,24 @@ func GetSensitiveWord(singleStoreHandler partner.ISingleStoreStoreSkuHandler, st
return ""
}
func isUpdateSkuSaleStatus(sku *dao.StoreSkuSyncInfo, now int) bool {
//商品可售时间的差集与门店营业时间的交集为不可售,其余为原本状态
openTime := sku.OpenTime
closeTime := sku.CloseTime
saleBeginTime := sku.StatusSaleBegin
saleEndTime := sku.StatusSaleEnd
beginAt1, endAt1 := GetTimeMixByInt(0, saleBeginTime, openTime, closeTime)
beginAt2, endAt2 := GetTimeMixByInt(saleEndTime, 2400, openTime, closeTime)
if beginAt1 != 0 && endAt1 != 0 {
if now > beginAt1 && now < endAt1 {
return true
}
}
if beginAt2 != 0 && endAt2 != 0 {
if now > beginAt2 && now < endAt2 {
return true
}
}
return false
}

View File

@@ -47,13 +47,6 @@ var (
"22:00:00",
}
ChangeStoreSkuSaleStatusList = []string{
"0:00:00",
"1:00:00",
"2:00:00",
"3:00:00",
"4:00:00",
"5:00:00",
"6:00:00",
"7:00:00",
"8:00:00",
"9:00:00",
@@ -68,9 +61,6 @@ var (
"18:00:00",
"19:00:00",
"20:00:00",
"21:00:00",
"22:00:00",
"23:00:00",
}
openRemoteStoreTimeList = []string{
"23:30:00",
@@ -144,7 +134,7 @@ func Init() {
"04:05:06",
})
ScheduleTimerFunc("ChangeStoreSkuSaleStatus", func() {
cms.CurVendorSync.ChangeStoreSkuSaleStatus(jxcontext.AdminCtx, false, false)
cms.CurVendorSync.ChangeStoreSkuSaleStatus(jxcontext.AdminCtx, true, false)
}, ChangeStoreSkuSaleStatusList)
}
}

View File

@@ -79,9 +79,13 @@ type StoreSkuSyncInfo struct {
StoreCatSyncStatus int8
VendorCatID string `orm:"column(vendor_cat_id)"`
VendorPrice int64
MergedStatus int
SkuName string
VendorPrice int64
MergedStatus int
SkuName string
StatusSaleBegin int `json:"statusSaleBegin"` //商品可售时间范围
StatusSaleEnd int `json:"statusSaleEnd"`
OpenTime int //门店营业时间
CloseTime int
}
type MissingStoreSkuInfo struct {
@@ -107,14 +111,6 @@ type StoreSkuNameInfo struct {
UnitPrice int64
}
type StoreSkuBindWithSaleTime struct {
StoreSkuBind model.StoreSkuBind
TimeBegin1 int
TimeEnd1 int
TimeBegin2 int
TimeEnd2 int
}
// todo 应该通过需要同步的skuid来驱动同步分类而不是当前这种分开的逻辑
// 单门店模式厂商适用
// 从store_sku_bind中得到所有依赖的商家分类信息
@@ -224,7 +220,9 @@ func GetStoreSkus2(db *DaoDB, vendorID, storeID int, skuIDs []int, mustDirty boo
fieldPrefix := ConvertDBFieldPrefix(model.VendorNames[vendorID])
sql := `
SELECT t1.id bind_id, t1.sku_id, t1.price, t1.unit_price, t1.status store_sku_status, %s.%s_id vendor_sku_id,
t1.%s_sync_status store_sku_sync_status, t1.store_id, t1.deleted_at bind_deleted_at,
t1.%s_sync_status store_sku_sync_status, t1.store_id, t1.deleted_at bind_deleted_at,t1.status_sale_begin,t1.status_sale_end,
IF(t6.open_time2 <> 0 AND t6.close_time2 <>0,IF(t6.open_time1 < t6.open_time2,t6.open_time1,t6.open_time2),t6.open_time1) open_time,
IF(t6.open_time2 <> 0 AND t6.close_time2 <>0,IF(t6.close_time1 > t6.close_time2,t6.close_time1,t6.close_time2),t6.close_time1) close_time,
t2.*,
t3.id name_id, t3.prefix, t3.name, t3.unit, t3.upc,
IF(t11.%s <> '', t11.%s, t3.img) img,
@@ -257,6 +255,7 @@ func GetStoreSkus2(db *DaoDB, vendorID, storeID int, skuIDs []int, mustDirty boo
LEFT JOIN data_resource t11 ON t11.main_url = t3.img
LEFT JOIN data_resource t12 ON t12.main_url = t3.img2
LEFT JOIN data_resource t13 ON t13.main_url = t3.desc_img
LEFT JOIN store t6 ON t6.id = t1.store_id
`
sqlParams := []interface{}{
utils.DefaultTimeValue,
@@ -593,20 +592,17 @@ func (s *StoreSkuSyncInfo) GetSeq() int {
return int(s.VendorPrice)
}
func GetStoresSkusInfoBySaleTime(db *DaoDB) (storeSkuBindList []*StoreSkuBindWithSaleTime, err error) {
func GetStoresSkusInfoBySaleTime(db *DaoDB) (storeSkuBindList []*model.StoreSkuBind, err error) {
sql := `
SELECT t1.*,
IF(t2.open_time1 <= t1.status_sale_begin,t2.open_time1,0) time_begin1,
IF(t2.open_time1 <= t1.status_sale_begin,t1.status_sale_begin,0) time_end1,
IF(t2.close_time1 >= t1.status_sale_end,t1.status_sale_end,0) time_begin2,
IF(t2.close_time1 >= t1.status_sale_end,t2.close_time1,0) time_end2
SELECT t1.*
FROM store_sku_bind t1
JOIN store t2 ON t1.store_id = t2.id
WHERE t1.status_sale_begin <> 0 AND t1.status_sale_end <> 0
AND t1.status != ?
AND t1.status = ?
AND t1.deleted_at = ?
`
sqlParams := []interface{}{
model.SkuStatusDeleted,
model.SkuStatusNormal,
utils.DefaultTimeValue,
}
err = GetRows(db, &storeSkuBindList, sql, sqlParams...)
return storeSkuBindList, err

View File

@@ -112,6 +112,9 @@ type StoreSkuBind struct {
// WscPrice int `json:"wscPrice"`
AutoSaleAt time.Time `orm:"type(datetime);null" json:"autoSaleAt"`
StatusSaleBegin int `json:"statusSaleBegin"` //商品可售时间范围
StatusSaleEnd int `json:"statusSaleEnd"`
}
func (*StoreSkuBind) TableUnique() [][]string {