406 lines
15 KiB
Go
406 lines
15 KiB
Go
package mtwmapi
|
||
|
||
import (
|
||
"strings"
|
||
|
||
"git.rosy.net.cn/baseapi/utils"
|
||
)
|
||
|
||
const (
|
||
ActTypeStoreFullDiscount = 0
|
||
ActTypeSkuFullDiscount = 1
|
||
)
|
||
|
||
const (
|
||
// 指定商品满减活动状态
|
||
SkuFullDiscountStatusExpired = 0 // 过期
|
||
SkuFullDiscountStatusValid = 1 // 生效
|
||
SkuFullDiscountStatusNew = 2 // 待生效
|
||
|
||
// 全店满减活动状态
|
||
StoreFullDiscountStatusNew = 0 // 待生效
|
||
StoreFullDiscountStatusValid = 1 // 生效
|
||
StoreFullDiscountStatusExpired = 2 // 过期
|
||
|
||
ShippingFeeStatusNotStart = "NOT_START" // 待生效
|
||
ShippingFeeStatusInProgress = "IN_PROGRESS" // 已生效
|
||
ShippingFeeStatusExpired = "EXPIRED" // 已过期
|
||
ShippingFeeStatusApprove = "APPROVE" // 待审批
|
||
ShippingFeeStatusFreeze = "FREEZE" // 冻结中
|
||
|
||
RetailDiscountStatusExpired = 0 // 已过期
|
||
RetailDiscountStatusValid = 1 // 已生效
|
||
RetailDiscountStatusNew = 2 // 待生效
|
||
|
||
UserTypeAll = 0 // 不限
|
||
UserTypeNewCommer = 1 // 门店新客
|
||
|
||
SettingTypeAsDiscount = 0 // 按折扣系数开展活动
|
||
SettingTypeAsPrice = 0 // 按折扣价格开展活动s
|
||
)
|
||
|
||
type FullDiscountActInfo 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 FullDiscountActDetail struct {
|
||
OriginalPrice float64 `json:"origin_price"` // 满
|
||
ActPrice float64 `json:"act_price"` // 减
|
||
}
|
||
|
||
type FullDiscountSku struct {
|
||
AppFoodCode string `json:"app_food_code"`
|
||
DayLimit int `json:"day_limit"`
|
||
Name string `json:"-"` // 设置操作时无用
|
||
}
|
||
|
||
type FullDiscountActData struct {
|
||
ActInfo *FullDiscountActInfo `json:"act_info"`
|
||
ActRemark string `json:"act_remark"`
|
||
AppPoiCode string `json:"app_poi_code"`
|
||
|
||
ActDetails []*FullDiscountActDetail `json:"act_details"`
|
||
}
|
||
|
||
type FullDiscountFoodsInfo struct {
|
||
ActInfo *FullDiscountActInfo `json:"act_info"`
|
||
ActRemark string `json:"act_remark"`
|
||
AppPoiCode string `json:"app_poi_code"`
|
||
|
||
AppFoods []*FullDiscountSku `json:"app_foods"`
|
||
}
|
||
|
||
type ShippingFeeActDetail struct {
|
||
LimitPrice float64 `json:"limit_price"`
|
||
DiscountPrice float64 `json:"discount_price"`
|
||
|
||
PoiCharge float64 `json:"-"`
|
||
MtCharge float64 `json:"-"`
|
||
}
|
||
|
||
type ShippingFeeActData struct {
|
||
StartTime int64 `json:"start_time"` // 活动开始时间,单s位秒
|
||
EndTime int64 `json:"end_time"` // 活动结束时间,单位秒
|
||
WeeksTime string `json:"weeks_time"`
|
||
Period string `json:"period"`
|
||
ActDetail []*ShippingFeeActDetail `json:"act_detail"`
|
||
MaxPrice float64 `json:"max_price"`
|
||
|
||
AppPoiCode string `json:"-"`
|
||
ActID string `json:"-"`
|
||
ActStatus string `json:"-"`
|
||
}
|
||
|
||
type RetailActData struct {
|
||
AppFoodCode string `json:"app_food_code"`
|
||
UserType int `json:"user_type"`
|
||
StartTime int64 `json:"start_time"` // 活动开始时间,单s位秒
|
||
EndTime int64 `json:"end_time"` // 活动结束时间,单位秒
|
||
OrderLimit int `json:"order_limit"`
|
||
DayLimit int `json:"day_limit"`
|
||
WeeksTime string `json:"weeks_time"`
|
||
Period string `json:"period"`
|
||
SettingType int `json:"setting_type"`
|
||
ActPrice float64 `json:"act_price"` // 折扣价格(单位元):必须为大于0的数字,且不能超过2位小数。
|
||
DiscountCoefficient float64 `json:"discount_coefficient"` // 折扣系数:必须大于0小于9.8,最多支持两位小数。如输入3,即为3折
|
||
Sequence int `json:"sequence"`
|
||
ItemID int64 `json:"item_id"` // 活动ID,为什么这里又是int64
|
||
|
||
// 以下参数只有查询时用到
|
||
OriginalPrice float64 `json:"-"` // 商品原价,单位元
|
||
Stock int `json:"-"` // 当日剩余活动商品数量。只有当发起查询时间处于活动生效时段内时(start_time、end_time、period、weeks_time均需满足),该字段才代表实际剩余活动商品数量,否则显示的是创建活动时规定的当日活动库存
|
||
Status int `json:"-"` // 活动状态,0:已过期,1:已生效,2:待生效。
|
||
Name string `json:"-"`
|
||
}
|
||
|
||
type RetailActDataLimit struct {
|
||
ItemID int64 `json:"item_id"` // 活动ID,为什么这里又是int64
|
||
DayLimit int `json:"day_limit"` // 当日活动库存,只能为正整数或-1,-1表示无限库存
|
||
OrderLimit int `json:"order_limit"` // 每单可购买的折扣商品数量
|
||
}
|
||
|
||
var (
|
||
ignoreShippingFeeActData = ShippingFeeActData{}
|
||
ignoreRetailActData = &RetailActData{}
|
||
ignoreRetailActDataLimit = &RetailActDataLimit{}
|
||
)
|
||
|
||
func (a *API) FullDiscountBatchSave(poiCode string, actInfo *FullDiscountActInfo, actList []*FullDiscountActDetail, actSkuList []*FullDiscountSku) (actID int64, err error) {
|
||
params := map[string]interface{}{
|
||
KeyAppPoiCode: poiCode,
|
||
"act_info": string(utils.MustMarshal(actInfo)),
|
||
"act_details": string(utils.MustMarshal(actList)),
|
||
}
|
||
if actInfo.ActType == ActTypeSkuFullDiscount {
|
||
params["app_foods"] = string(utils.MustMarshal(actSkuList))
|
||
}
|
||
result, err := a.AccessAPI2("act/full/discount/batchsave", false, params, resultKeyMsg)
|
||
if err == nil {
|
||
return utils.MustInterface2Int64(result.([]interface{})[0].(map[string]interface{})["act_id"]), nil
|
||
}
|
||
return 0, err
|
||
}
|
||
|
||
func (a *API) FullDiscountList(poiCode string, actType int) (actInfoList []*FullDiscountActData, 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 := &FullDiscountActData{
|
||
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, &FullDiscountActDetail{
|
||
OriginalPrice: float64(utils.MustInterface2Float64(vMap["origin_price"])),
|
||
ActPrice: float64(utils.MustInterface2Float64(vMap["act_price"])),
|
||
})
|
||
}
|
||
}
|
||
return actInfoList, nil
|
||
}
|
||
return nil, err
|
||
}
|
||
|
||
func interface2ActInfo(actInfo interface{}) *FullDiscountActInfo {
|
||
actInfoMap := actInfo.(map[string]interface{})
|
||
return &FullDiscountActInfo{
|
||
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 []*FullDiscountSku) (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 *FullDiscountFoodsInfo, err error) {
|
||
limit := 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": limit,
|
||
"offset": offset,
|
||
})
|
||
if err == nil && result != nil {
|
||
resultMap := result.(map[string]interface{})
|
||
if actFoodsInfo == nil {
|
||
actFoodsInfo = &FullDiscountFoodsInfo{
|
||
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 []*FullDiscountSku) {
|
||
for _, v := range value.([]interface{}) {
|
||
vMap := v.(map[string]interface{})
|
||
actSkuList = append(actSkuList, &FullDiscountSku{
|
||
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 []*FullDiscountSku) (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
|
||
}
|
||
|
||
// 阶梯满减配送费活动一个门店只可能有一个活动,所以不涉及actID的概念
|
||
// 批量创建阶梯满减配送费活动
|
||
func (a *API) FulllDiscountShippingFeeBatchSave(poiCode string, actData []*ShippingFeeActData) (err error) {
|
||
_, err = a.AccessAPI("act/full/discount/shippingfee/batchsave", false, map[string]interface{}{
|
||
KeyAppPoiCode: poiCode,
|
||
"act_data": string(utils.MustMarshal(utils.StructList2MapListWithIgnore(actData, map[string]interface{}{
|
||
"weeks_time": ignoreShippingFeeActData.WeeksTime,
|
||
"period": ignoreShippingFeeActData.Period,
|
||
"max_price": ignoreShippingFeeActData.MaxPrice,
|
||
}))),
|
||
})
|
||
return err
|
||
}
|
||
|
||
// 查询阶梯满减配送费活动
|
||
func (a *API) FulllDiscountShippingFeeList(poiCode string) (actList []*ShippingFeeActData, err error) {
|
||
result, err := a.AccessAPI("act/full/discount/shippingfee/list", true, map[string]interface{}{
|
||
KeyAppPoiCode: poiCode,
|
||
})
|
||
if err == nil {
|
||
for _, v := range result.([]interface{}) {
|
||
vMap := v.(map[string]interface{})
|
||
act := &ShippingFeeActData{
|
||
AppPoiCode: utils.Interface2String(vMap["app_poi_code"]),
|
||
ActID: utils.Interface2String(vMap["act_id"]),
|
||
StartTime: utils.MustInterface2Int64(vMap["start_time"]),
|
||
EndTime: utils.MustInterface2Int64(vMap["end_time"]),
|
||
WeeksTime: utils.Interface2String(vMap["weeks_time"]),
|
||
Period: utils.Interface2String(vMap["period"]),
|
||
|
||
MaxPrice: utils.Interface2Float64WithDefault(vMap["max_price"], 0),
|
||
ActStatus: utils.Interface2String(vMap["actStatus"]),
|
||
}
|
||
actList = append(actList, act)
|
||
for _, v := range vMap["act_detail"].([]interface{}) {
|
||
vMap := v.(map[string]interface{})
|
||
act.ActDetail = append(act.ActDetail, &ShippingFeeActDetail{
|
||
LimitPrice: utils.Interface2Float64WithDefault(vMap["limit_price"], 0),
|
||
DiscountPrice: utils.Interface2Float64WithDefault(vMap["discount_price"], 0),
|
||
PoiCharge: utils.Interface2Float64WithDefault(vMap["poi_charge"], 0),
|
||
MtCharge: utils.Interface2Float64WithDefault(vMap["mt_charge"], 0),
|
||
})
|
||
}
|
||
}
|
||
}
|
||
return actList, err
|
||
}
|
||
|
||
// 批量创建或更新零售折扣商品
|
||
func (a *API) RetailDiscountBatchSave(poiCode string, actData []*RetailActData) (actID int64, err error) {
|
||
result, err := a.AccessAPI2("act/retail/discount/batchsave", false, map[string]interface{}{
|
||
KeyAppPoiCode: poiCode,
|
||
"act_data": string(utils.MustMarshal(utils.StructList2MapListWithIgnore(actData, map[string]interface{}{
|
||
"sequence": ignoreRetailActData.Sequence,
|
||
"item_id": ignoreRetailActData.ItemID,
|
||
}))),
|
||
}, resultKeyMsg)
|
||
if err == nil {
|
||
return utils.MustInterface2Int64(result.([]interface{})[0].(map[string]interface{})["act_id"]), nil
|
||
}
|
||
return 0, err
|
||
}
|
||
|
||
// 查询门店零售折扣商品
|
||
func (a *API) RetailDiscountList(poiCode string) (actList []*RetailActData, err error) {
|
||
limit := 200
|
||
offset := 0
|
||
for {
|
||
result, err := a.AccessAPI("act/retail/discount/list", true, map[string]interface{}{
|
||
KeyAppPoiCode: poiCode,
|
||
"limit": limit,
|
||
"offset": offset,
|
||
})
|
||
if err == nil {
|
||
resultList := result.([]interface{})
|
||
for _, v := range resultList {
|
||
vMap := v.(map[string]interface{})
|
||
act := &RetailActData{
|
||
AppFoodCode: utils.Interface2String(vMap["app_food_code"]),
|
||
UserType: int(utils.MustInterface2Int64(vMap["user_type"])),
|
||
StartTime: utils.MustInterface2Int64(vMap["start_time"]),
|
||
EndTime: utils.MustInterface2Int64(vMap["end_time"]),
|
||
OrderLimit: int(utils.MustInterface2Int64(vMap["order_limit"])),
|
||
DayLimit: int(utils.MustInterface2Int64(vMap["day_limit"])),
|
||
WeeksTime: utils.Interface2String(vMap["weeks_time"]),
|
||
Period: utils.Interface2String(vMap["period"]),
|
||
SettingType: int(utils.MustInterface2Int64(vMap["setting_type"])),
|
||
ActPrice: utils.Interface2Float64WithDefault(vMap["act_price"], 0),
|
||
DiscountCoefficient: utils.Interface2Float64WithDefault(vMap["discount_coefficient"], 0),
|
||
ItemID: utils.MustInterface2Int64(vMap["item_id"]),
|
||
OriginalPrice: utils.Interface2Float64WithDefault(vMap["origin_price"], 0),
|
||
Stock: int(utils.MustInterface2Int64(vMap["stock"])),
|
||
Status: int(utils.MustInterface2Int64(vMap["status"])),
|
||
Name: utils.Interface2String(vMap["name"]),
|
||
Sequence: int(utils.MustInterface2Int64(vMap["sequence"])),
|
||
}
|
||
actList = append(actList, act)
|
||
}
|
||
if len(resultList) < limit {
|
||
break
|
||
}
|
||
offset++
|
||
} else {
|
||
return nil, err
|
||
}
|
||
}
|
||
return actList, err
|
||
}
|
||
|
||
// 批量删除零售折扣商品
|
||
func (a *API) RetailDiscountDelete(poiCode string, actIDList []string) (err error) {
|
||
_, err = a.AccessAPI("act/retail/discount/batchdelete", false, map[string]interface{}{
|
||
KeyAppPoiCode: poiCode,
|
||
"item_ids": strings.Join(actIDList, ","),
|
||
})
|
||
return err
|
||
}
|
||
|
||
// 批量修改零售折扣商品当日活动库存
|
||
func (a *API) RetailDiscountBatchStock(poiCode, actDataList []*RetailActDataLimit) (err error) {
|
||
_, err = a.AccessAPI("act/retail/discount/batchstock", false, map[string]interface{}{
|
||
KeyAppPoiCode: poiCode,
|
||
"act_data": string(utils.MustMarshalJSONIgnoreValues(actDataList, map[string]interface{}{
|
||
"order_limit": ignoreRetailActDataLimit.OrderLimit,
|
||
})),
|
||
})
|
||
return err
|
||
}
|
||
|
||
// 批量修改零售折扣商品每单限购数量
|
||
func (a *API) RetailDiscountBatchLimit(poiCode, actDataList []*RetailActDataLimit) (err error) {
|
||
_, err = a.AccessAPI("act/retail/discount/batchlimit", false, map[string]interface{}{
|
||
KeyAppPoiCode: poiCode,
|
||
"act_data": string(utils.MustMarshalJSONIgnoreValues(actDataList, map[string]interface{}{
|
||
"day_limit": ignoreRetailActDataLimit.DayLimit,
|
||
})),
|
||
})
|
||
return err
|
||
}
|