From 5697cdbcff4b1d966d2f5e41058920ee92694b63 Mon Sep 17 00:00:00 2001 From: gazebo Date: Tue, 26 Nov 2019 09:47:02 +0800 Subject: [PATCH] =?UTF-8?q?=E9=98=B2=E6=AD=A2=E4=BA=AC=E4=B8=9C=E6=B4=BB?= =?UTF-8?q?=E5=8A=A8=E9=87=8D=E5=A4=8D=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- business/partner/purchase/jd/act.go | 56 +++++++++++++++++------------ 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/business/partner/purchase/jd/act.go b/business/partner/purchase/jd/act.go index 2f69f6afd..668e21d05 100644 --- a/business/partner/purchase/jd/act.go +++ b/business/partner/purchase/jd/act.go @@ -18,6 +18,10 @@ import ( "git.rosy.net.cn/jx-callback/business/model" ) +const ( + actMapDuration = 2 * time.Hour +) + type LogicUpdateInfo struct { Item interface{} KVs map[string]interface{} @@ -31,6 +35,8 @@ var ( jdapi.PromotionStateCanceled: model.ActStatusCanceled, jdapi.PromotionStateEnded: model.ActStatusEnded, } + + actMap jxutils.SyncMapWithTimeout ) func splitPromotionSku(skus []*jdapi.PromotionSku, maxCount int) (skusList [][]*jdapi.PromotionSku) { @@ -62,22 +68,25 @@ func jdSkuActStatus2Jx(jdActState int) int { func CreatePromotionInfos(promotionType int, name string, beginDate, endDate time.Time, outInfoId, advertising, traceId string) (infoId int64, err error) { if globals.EnableJdStoreWrite { if promotionType == model.ActSkuDirectDown { - return getAPI("").CreatePromotionInfosSingle(name, beginDate, endDate, outInfoId, advertising, traceId) + infoId, err = getAPI("").CreatePromotionInfosSingle(name, beginDate, endDate, outInfoId, advertising, traceId) } else { - return getAPI("").CreatePromotionInfosLimitTime(name, beginDate, endDate, outInfoId, advertising, traceId) + infoId, err = getAPI("").CreatePromotionInfosLimitTime(name, beginDate, endDate, outInfoId, advertising, traceId) } } else { infoId = jxutils.GenFakeID() } + if err == nil { + actMap.StoreWithTimeout(infoId, 1, actMapDuration) + } return infoId, err } func CreatePromotionRules(promotionType int, infoId int64, outInfoId string, limitDevice, limitPin, limitCount, limitDaily int, traceId string) (err error) { if globals.EnableJdStoreWrite { if promotionType == model.ActSkuDirectDown { - return getAPI("").CreatePromotionRulesSingle(infoId, outInfoId, limitDevice, limitPin, limitCount, limitDaily, traceId) + err = getAPI("").CreatePromotionRulesSingle(infoId, outInfoId, limitDevice, limitPin, limitCount, limitDaily, traceId) } else { - return getAPI("").CreatePromotionRulesLimitTime(infoId, outInfoId, limitDevice, limitPin, limitCount, limitDaily, traceId) + err = getAPI("").CreatePromotionRulesLimitTime(infoId, outInfoId, limitDevice, limitPin, limitCount, limitDaily, traceId) } } return err @@ -133,9 +142,9 @@ func ConfirmPromotion(promotionType int, infoId int64, outInfoId, traceId string func CancelPromotion(promotionType int, infoId int64, outInfoId, traceId string) (err error) { if globals.EnableJdStoreWrite { if promotionType == model.ActSkuDirectDown { - return getAPI("").CancelPromotionSingle(infoId, outInfoId, traceId) + err = getAPI("").CancelPromotionSingle(infoId, outInfoId, traceId) } else { - return getAPI("").CancelPromotionLimitTime(infoId, outInfoId, traceId) + err = getAPI("").CancelPromotionLimitTime(infoId, outInfoId, traceId) } } return err @@ -144,9 +153,9 @@ func CancelPromotion(promotionType int, infoId int64, outInfoId, traceId string) func AdjustPromotionTime(promotionType int, infoId int64, outInfoId string, endDate time.Time, traceId string) (err error) { if globals.EnableJdStoreWrite { if promotionType == model.ActSkuDirectDown { - return getAPI("").AdjustPromotionTimeSingle(infoId, outInfoId, endDate, traceId) + err = getAPI("").AdjustPromotionTimeSingle(infoId, outInfoId, endDate, traceId) } else { - return getAPI("").AdjustPromotionTimeLimitTime(infoId, outInfoId, endDate, traceId) + err = getAPI("").AdjustPromotionTimeLimitTime(infoId, outInfoId, endDate, traceId) } } return err @@ -155,9 +164,9 @@ func AdjustPromotionTime(promotionType int, infoId int64, outInfoId string, endD func AdjustPromotionSku(promotionType int, infoId int64, outInfoId string, skus []*jdapi.PromotionSku, traceId string) (skusResult []*jdapi.PromotionSku, err error) { if globals.EnableJdStoreWrite { if promotionType == model.ActSkuDirectDown { - return getAPI("").AdjustPromotionSkuSingle(infoId, outInfoId, skus, traceId) + skusResult, err = getAPI("").AdjustPromotionSkuSingle(infoId, outInfoId, skus, traceId) } else { - return getAPI("").AdjustPromotionSkuLimitTime(infoId, outInfoId, skus, traceId) + skusResult, err = getAPI("").AdjustPromotionSkuLimitTime(infoId, outInfoId, skus, traceId) } } return skusResult, err @@ -312,19 +321,22 @@ func OnActMsg(msg *jdapi.CallbackOrderMsg) (retVal *jdapi.CallbackResponse) { func (c *PurchaseHandler) onActMsg(msg *jdapi.CallbackOrderMsg) (retVal *jdapi.CallbackResponse) { if msg.StatusID == jdapi.PromotionStatusSingleOK || msg.StatusID == jdapi.PromotionStatusLimitTimeOK { promotionID := msg.BillID - // 等几秒再执行的原因是防止通过后台自己创建时,本地还没有建好,消息就过来,导致重复记录 - // 可能的问题是在重启时丢失消息 - utils.AfterFuncWithRecover(5*time.Second, func() { - if !partner.CurActManager.IsVendorActExist(jxcontext.AdminCtx, promotionID, model.VendorIDJD) { - act, actStoreSkuList, err := getActFromJD(promotionID) - if err == nil && len(actStoreSkuList) > 0 { - _, err = partner.CurActManager.CreateActFromVendor(jxcontext.AdminCtx, act, actStoreSkuList) + intPromotionID := utils.Str2Int64(promotionID) + if _, ok := actMap.Load(intPromotionID); !ok { + utils.CallFuncAsync(func() { + if !partner.CurActManager.IsVendorActExist(jxcontext.AdminCtx, promotionID, model.VendorIDJD) { + act, actStoreSkuList, err := getActFromJD(promotionID) + if err == nil && len(actStoreSkuList) > 0 { + _, err = partner.CurActManager.CreateActFromVendor(jxcontext.AdminCtx, act, actStoreSkuList) + } + if err != nil { + retVal = jdapi.Err2CallbackResponse(err, promotionID) + } } - if err != nil { - retVal = jdapi.Err2CallbackResponse(err, promotionID) - } - } - }) + }) + } else { + actMap.Delete(intPromotionID) + } } return retVal }