diff --git a/platformapi/mtwmapi/act.go b/platformapi/mtwmapi/act.go new file mode 100644 index 00000000..0fc73d08 --- /dev/null +++ b/platformapi/mtwmapi/act.go @@ -0,0 +1,205 @@ +package mtwmapi + +import ( + "strings" + + "git.rosy.net.cn/baseapi/utils" +) + +const ( + ActTypeStoreMoneyOff = 0 + ActTypeSkuMoneyOff = 1 +) + +const ( + // 指定商品满减活动状态 + SkuMoneyOffStatusNew = 2 // 待生效 + SkuMoneyOffStatusValid = 1 // 生效 + SkuMoneyOffStatusExpired = 0 // 过期 + + // 全店满减活动状态 + StoreMoneyOffStatusNew = 0 // 待生效 + StoreMoneyOffStatusValid = 1 // 生效 + StoreMoneyOffStatusExpired = 2 // 过期 +) + +type ActInfo struct { + ActIDs string `json:"act_ids"` + ActName string `json:"act_name"` + StartTime int64 `json:"start_time"` // 活动开始时间,单s位秒 + EndTime int64 `json:"end_time"` // 活动结束时间,单位秒 + ActType int `json:"act_type"` + Status int `json:"-"` +} + +type ActDetail struct { + OriginalPrice float32 `json:"origin_price"` // 满 + ActPrice float32 `json:"act_price"` // 减 +} + +type ActSkuInfo struct { + AppFoodCode string `json:"app_food_code"` + DayLimit int `json:"day_limit"` + Name string `json:"-"` // 设置操作时无用 +} + +type ActWholeInfo struct { + ActInfo *ActInfo `json:"act_info"` + ActRemark string `json:"act_remark"` + AppPoiCode string `json:"app_poi_code"` + + ActDetails []*ActDetail `json:"act_details"` +} + +type ActFoodsInfo struct { + ActInfo *ActInfo `json:"act_info"` + ActRemark string `json:"act_remark"` + AppPoiCode string `json:"app_poi_code"` + + AppFoods []*ActSkuInfo `json:"app_foods"` +} + +type ActData struct { + ItemID int64 `json:"item_id"` // 活动ID,为什么这里又是int64 + DayLimit int `json:"day_limit"` // 当日活动库存,只能为正整数或-1,-1表示无限库存 +} + +func (a *API) FullDiscountBatchSave(poiCode string, actInfo *ActInfo, actList []*ActDetail, actSkuList []*ActSkuInfo) (err error) { + params := map[string]interface{}{ + KeyAppPoiCode: poiCode, + "act_info": string(utils.MustMarshal(actInfo)), + "act_details": string(utils.MustMarshal(actList)), + } + if actInfo.ActType == ActTypeSkuMoneyOff { + params["app_foods"] = string(utils.MustMarshal(actSkuList)) + } + _, err = a.AccessAPI("act/full/discount/batchsave", false, params) + return err +} + +func (a *API) FullDiscountList(poiCode string, actType int) (actInfoList []*ActWholeInfo, err error) { + result, err := a.AccessAPI("act/full/discount/list", false, map[string]interface{}{ + KeyAppPoiCode: poiCode, + "act_type": actType, + }) + if err == nil && result != nil { + for _, v := range result.([]interface{}) { + actMap := v.(map[string]interface{}) + actWholeInfo := &ActWholeInfo{ + ActInfo: interface2ActInfo(actMap["act_info"]), + ActRemark: utils.Interface2String(actMap["act_remark"]), + AppPoiCode: utils.Interface2String(actMap["app_poi_code"]), + } + actInfoList = append(actInfoList, actWholeInfo) + for _, v := range actMap["act_details"].([]interface{}) { + vMap := v.(map[string]interface{}) + actWholeInfo.ActDetails = append(actWholeInfo.ActDetails, &ActDetail{ + OriginalPrice: float32(utils.MustInterface2Float64(vMap["origin_price"])), + ActPrice: float32(utils.MustInterface2Float64(vMap["act_price"])), + }) + } + } + return actInfoList, nil + } + return nil, err +} + +func interface2ActInfo(actInfo interface{}) *ActInfo { + actInfoMap := actInfo.(map[string]interface{}) + return &ActInfo{ + ActIDs: utils.Interface2String(actInfoMap["act_ids"]), + StartTime: utils.MustInterface2Int64(actInfoMap["start_time"]), + EndTime: utils.MustInterface2Int64(actInfoMap["end_time"]), + Status: int(utils.MustInterface2Int64(actInfoMap["status"])), + } +} + +func (a *API) FullDiscountDelete(poiCode string, actIDList []string, actType int) (err error) { + _, err = a.AccessAPI("act/full/discount/delete", false, map[string]interface{}{ + KeyAppPoiCode: poiCode, + "act_ids": strings.Join(actIDList, ","), + "act_type": actType, + }) + return err +} + +func (a *API) FullDiscountFoodsBatchSave(poiCode, actID string, appFoodList []*ActSkuInfo) (err error) { + _, err = a.AccessAPI("act/full/discount/foods/batchsave", false, map[string]interface{}{ + KeyAppPoiCode: poiCode, + "act_id": actID, + "app_foods": string(utils.MustMarshal(appFoodList)), + }) + return err +} + +func (a *API) FullDiscountFoodsList(poiCode, actID string) (actFoodsInfo *ActFoodsInfo, err error) { + pageSize := 200 + for { + var result interface{} + offset := 0 + result, err = a.AccessAPI("act/full/discount/foods/list", false, map[string]interface{}{ + KeyAppPoiCode: poiCode, + "act_id": actID, + "limit": pageSize, + "offset": offset, + }) + if err == nil && result != nil { + resultMap := result.(map[string]interface{}) + if actFoodsInfo == nil { + actFoodsInfo = &ActFoodsInfo{ + ActInfo: interface2ActInfo(resultMap["act_info"]), + ActRemark: utils.Interface2String(resultMap["act_remark"]), + AppPoiCode: utils.Interface2String(resultMap["app_poi_code"]), + } + } + actFoodsInfo.AppFoods = append(actFoodsInfo.AppFoods, interface2SkuInfoList(resultMap["app_foods"])...) + offset++ + break + } else { + break + } + } + return nil, err +} + +func interface2SkuInfoList(value interface{}) (actSkuList []*ActSkuInfo) { + for _, v := range value.([]interface{}) { + vMap := v.(map[string]interface{}) + actSkuList = append(actSkuList, &ActSkuInfo{ + AppFoodCode: utils.Interface2String(vMap["app_food_code"]), + DayLimit: int(utils.Interface2Int64WithDefault(vMap["day_limit"], 0)), + Name: utils.Interface2String(vMap["name"]), + }) + } + return actSkuList +} + +// 批量删除活动商品至指定商品满减活动 +// appFoodCodeList 删除商品数量上限为100,如果删除门店多个活动商品,用英文逗号隔开 +func (a *API) FullDiscountFoodsDelete(poiCode, actID string, appFoodCodeList []string) (err error) { + _, err = a.AccessAPI("act/full/discount/foods/delete", false, map[string]interface{}{ + KeyAppPoiCode: poiCode, + "act_id": actID, + "app_food_codes": strings.Join(appFoodCodeList, ","), + }) + return err +} + +// 批量修改指定商品满减活动中商品的当日活动库存 +func (a *API) FullDiscountFoodsDayLimit(poiCode, actID string, appFoodList []*ActSkuInfo) (err error) { + _, err = a.AccessAPI("act/full/discount/foods/daylimit", false, map[string]interface{}{ + KeyAppPoiCode: poiCode, + "act_id": actID, + "app_foods": string(utils.MustMarshal(appFoodList)), + }) + return err +} + +// 批量修改零售折扣商品当日活动库存 +func (a *API) RetailDiscountBatchStock(poiCode, actDataList []*ActData) (err error) { + _, err = a.AccessAPI("act/retail/discount/batchstock", false, map[string]interface{}{ + KeyAppPoiCode: poiCode, + "act_data": string(utils.MustMarshal(actDataList)), + }) + return err +} diff --git a/platformapi/mtwmapi/act_test.go b/platformapi/mtwmapi/act_test.go new file mode 100644 index 00000000..20305e24 --- /dev/null +++ b/platformapi/mtwmapi/act_test.go @@ -0,0 +1,49 @@ +package mtwmapi + +import ( + "testing" + "time" + + "git.rosy.net.cn/baseapi/utils" +) + +func TestFullDiscountBatchSave(t *testing.T) { + err := api.FullDiscountBatchSave("6693359", &ActInfo{ + // ActIDs: "12345678", + ActName: "测试活动0402", + StartTime: time.Now().Unix(), + EndTime: time.Now().Add(24 * time.Hour).Unix(), + ActType: ActTypeStoreMoneyOff, + }, []*ActDetail{ + &ActDetail{ + OriginalPrice: 10000, + ActPrice: 1, + }, + }, /*[]*ActSkuInfo{ + &ActSkuInfo{ + AppFoodCode: "30902", + DayLimit: 1, + }, + }*/nil) + if err != nil { + t.Fatal(err) + } + // 30902 + // t.Log(utils.Format4Output(result, false)) +} + +func TestFullDiscountList(t *testing.T) { + result, err := api.FullDiscountList("6737142", ActTypeStoreMoneyOff) + if err != nil { + t.Fatal(err) + } + t.Log(utils.Format4Output(result, false)) +} + +func TestFullDiscountFoodsList(t *testing.T) { + result, err := api.FullDiscountFoodsList("6737142", "497726932") + if err != nil { + t.Fatal(err) + } + t.Log(utils.Format4Output(result, false)) +}