package jdapi import ( "crypto/md5" "fmt" "time" "git.rosy.net.cn/baseapi/utils" ) const ( KeyStationNo = "stationNo" KeyOutStationNo = "outStationNo" KeyPromotionPrice = "promotionPrice" KeyLimitSkuCount = "limitSkuCount" KeyInfoId = "infoId" KeyOutInfoId = "outInfoId" ) const ( MaxPromotionSkuCount = 200 ) const ( PromotionStateNotConfirm = 1 PromotionStateConfirmed = 5 PromotionStateCanceled = 6 PromotionStateEnded = 9 ) type PromotionSkuResult struct { LimitDaily int LimitDevice int LimitPin int PlatformRatio int PromotionPrice int SkuId int64 StationNo int64 StoreRatio int } type PromotionInfo struct { BeginTime time.Time EndTime time.Time PromotionInfoId int64 PromotionState int PromotionType int Source string SkuResultList []*PromotionSkuResult } // 单品直降添加主活动信息接口 // https://opendj.jd.com/staticnew/widgets/resources.html?groupid=178&apiid=ee8685c9be9b4aa5bdc41468c5ebc33b func (a *API) CreatePromotionInfosSingle(name string, beginDate, endDate time.Time, outInfoId, advertising string) (infoId int64, err error) { return a.createPromotionInfos(PromotionTypeDirectDown, name, beginDate, endDate, outInfoId, advertising) } // 单品直降添加活动商品信息接口 // 最多200条 // https://opendj.jd.com/staticnew/widgets/resources.html?groupid=178&apiid=0ad0715e0aaa42489cbeac36398e916d func (a *API) CreatePromotionSkuSingle(infoId int64, outInfoId string, skus []map[string]interface{}) (skusResult []map[string]interface{}, err error) { return a.createPromotionSku(PromotionTypeDirectDown, infoId, outInfoId, skus) } // 单品直降活动提交保存接口 // https://opendj.jd.com/staticnew/widgets/resources.html?groupid=178&apiid=b84e14eb341e470db8ed9b60f78edb16 func (a *API) ConfirmPromotionSingle(infoId int64, outInfoId string) (err error) { return a.confirmPromotion(PromotionTypeDirectDown, infoId, outInfoId) } // 单品直降整个活动取消接口 // https://opendj.jd.com/staticnew/widgets/resources.html?groupid=178&apiid=3d02c2acad714bea86d20d498a1aa074 func (a *API) CancelPromotionSingle(infoId int64, outInfoId string) (err error) { return a.cancelPromotion(PromotionTypeDirectDown, infoId, outInfoId) } // 限时抢添加活动主信息接口 // https://opendj.jd.com/staticnew/widgets/resources.html?groupid=184&apiid=7d8b7ff86c9e457bb8a46963cb575769 func (a *API) CreatePromotionInfosLimitTime(name string, beginDate, endDate time.Time, outInfoId, advertising string) (infoId int64, err error) { return a.createPromotionInfos(PromotionTypeLimitedTime, name, beginDate, endDate, outInfoId, advertising) } // 限时抢添加活动规则信息接口(直降也是调用此接口) // https://opendj.jd.com/staticnew/widgets/resources.html?groupid=184&apiid=e80674791d3542f0a87502753c0d0592 func (a *API) CreatePromotionRules(infoId int64, outInfoId string, limitDevice, limitPin, limitCount, limitDaily int) (err error) { jdParams := map[string]interface{}{ "limitDevice": limitDevice, "limitPin": limitPin, "limitCount": limitCount, "limitDaily": limitDaily, "timeStamp": utils.GetCurTimeStr(), } if infoId != 0 { jdParams[KeyInfoId] = infoId } else { jdParams[KeyOutInfoId] = outInfoId } _, err = a.AccessAPINoPage("limitTime/createPromotionRules", jdParams, nil, nil, genNoPageResultParser("errorCode", "errorInfos", "", "0")) return err } // 限时抢添加活动商品信息接口 // 最多200条 // https://opendj.jd.com/staticnew/widgets/resources.html?groupid=184&apiid=65fecef4883c40c6b23bbdb6123f5d80 func (a *API) CreatePromotionSkuLimitTime(infoId int64, outInfoId string, skus []map[string]interface{}) (skusResult []map[string]interface{}, err error) { return a.createPromotionSku(PromotionTypeLimitedTime, infoId, outInfoId, skus) } // 限时抢活动提交保存接口 // https://opendj.jd.com/staticnew/widgets/resources.html?groupid=184&apiid=3991063e2f0d435094e9fe44754f3490 func (a *API) ConfirmPromotionLimitTime(infoId int64, outInfoId string) (err error) { return a.confirmPromotion(PromotionTypeLimitedTime, infoId, outInfoId) } // 限时抢整个活动取消接口 // https://opendj.jd.com/staticnew/widgets/resources.html?groupid=184&apiid=97b6678d30624f73bc13cb68987e6a6d func (a *API) CancelPromotionLimitTime(infoId int64, outInfoId string) (err error) { return a.cancelPromotion(PromotionTypeLimitedTime, infoId, outInfoId) } func (a *API) createPromotionInfos(promotionType int, name string, beginDate, endDate time.Time, outInfoId, advertising string) (infoId int64, err error) { if outInfoId == "" { outInfoId = fmt.Sprintf("%X", md5.Sum([]byte(name))) } jdParams := map[string]interface{}{ "promotionName": name, "beginDate": utils.Time2Str(beginDate), "endDate": utils.Time2Str(endDate), "advertising": advertising, "promotionType": promotionType, "timeStamp": utils.GetCurTimeStr(), KeyOutInfoId: outInfoId, } cmd := "" if promotionType == PromotionTypeDirectDown { cmd = "singlePromote/createPromotionInfos" } else if promotionType == PromotionTypeLimitedTime { cmd = "limitTime/createPromotionInfos" } else { panic("unknow promotionType!") } result, err := a.AccessAPINoPage(cmd, jdParams, nil, nil, genNoPageResultParser("errorCode", "errorInfos", "data", "0")) if err == nil { return utils.MustInterface2Int64(result), nil } return 0, err } // todo skusResult 返回值没有意义,为了兼容暂时保留 func (a *API) createPromotionSku(promotionType int, infoId int64, outInfoId string, skus []map[string]interface{}) (skusResult []map[string]interface{}, err error) { jdParams := map[string]interface{}{ "skus": skus, "timeStamp": utils.GetCurTimeStr(), } if infoId != 0 { jdParams[KeyInfoId] = infoId } else { jdParams[KeyOutInfoId] = outInfoId } cmd := "" if promotionType == PromotionTypeDirectDown { cmd = "singlePromote/createPromotionSku" } else if promotionType == PromotionTypeLimitedTime { cmd = "limitTime/createPromotionSku" } else { panic("unknow promotionType!") } _, err = a.AccessAPINoPage(cmd, jdParams, nil, nil, genNoPageResultParser("errorCode", "data", "", "0")) return nil, err } func (a *API) confirmPromotion(promotionType int, infoId int64, outInfoId string) (err error) { jdParams := map[string]interface{}{ "timeStamp": utils.GetCurTimeStr(), } if infoId != 0 { jdParams[KeyInfoId] = infoId } else { jdParams[KeyOutInfoId] = outInfoId } cmd := "" if promotionType == PromotionTypeDirectDown { cmd = "singlePromote/confirmPromotion" } else if promotionType == PromotionTypeLimitedTime { cmd = "limitTime/confirmPromotion" } else { panic("unknow promotionType!") } _, err = a.AccessAPINoPage(cmd, jdParams, nil, nil, genNoPageResultParser("errorCode", "errorInfos", "", "0")) return err } func (a *API) cancelPromotion(promotionType int, infoId int64, outInfoId string) (err error) { jdParams := map[string]interface{}{ "timeStamp": utils.GetCurTimeStr(), } if infoId != 0 { jdParams[KeyInfoId] = infoId } else { jdParams[KeyOutInfoId] = outInfoId } cmd := "" if promotionType == PromotionTypeDirectDown { cmd = "singlePromote/cancelPromotion" } else if promotionType == PromotionTypeLimitedTime { cmd = "limitTime/cancelPromotion" } else { panic("unknow promotionType!") } _, err = a.AccessAPINoPage(cmd, jdParams, nil, nil, genNoPageResultParser("errorCode", "errorInfos", "", "0")) return err } func (a *API) QueryPromotionInfo(promotionInfoId int64) (promotionInfo *PromotionInfo, err error) { jdParams := map[string]interface{}{ "promotionInfoId": promotionInfoId, } result, err := a.AccessAPINoPage("orderdiscount/queryPromotionInfo", jdParams, nil, nil, genNoPageResultParser("errorCode", "errorInfos", "data", "0")) if err == nil { data := result.(map[string]interface{}) // baseapi.SugarLogger.Debug(utils.Format4Output(data, false)) promotionInfo = &PromotionInfo{ BeginTime: utils.Timestamp2Time(utils.MustInterface2Int64(data["beginTime"].(map[string]interface{})["time"]) / 1000), EndTime: utils.Timestamp2Time(utils.MustInterface2Int64(data["endTime"].(map[string]interface{})["time"]) / 1000), PromotionInfoId: utils.MustInterface2Int64(data["promotionInfoId"]), PromotionState: int(utils.MustInterface2Int64(data["promotionState"])), PromotionType: int(utils.MustInterface2Int64(data["promotionType"])), Source: utils.Interface2String(data["source"]), } skuResultList := data["skuResultList"].([]interface{}) promotionInfo.SkuResultList = make([]*PromotionSkuResult, len(skuResultList)) for k, v := range skuResultList { skuResult := v.(map[string]interface{}) promotionInfo.SkuResultList[k] = &PromotionSkuResult{ LimitDaily: int(utils.MustInterface2Int64(skuResult["limitDaily"])), LimitDevice: int(utils.MustInterface2Int64(skuResult["limitDevice"])), LimitPin: int(utils.MustInterface2Int64(skuResult["limitPin"])), PlatformRatio: int(utils.MustInterface2Int64(skuResult["platformRatio"])), PromotionPrice: int(utils.MustInterface2Int64(skuResult["promotionPrice"])), SkuId: utils.MustInterface2Int64(skuResult["skuId"]), StationNo: utils.MustInterface2Int64(skuResult["stationNo"]), StoreRatio: int(utils.MustInterface2Int64(skuResult["storeRatio"])), } } return promotionInfo, nil } return nil, err }