- 使用mapstructure进行map到struct的映射,简化API编写
This commit is contained in:
@@ -86,10 +86,10 @@ type ShippingFeeActDetail struct {
|
||||
type ShippingFeeActData struct {
|
||||
StartTime int64 `json:"start_time"` // 活动开始时间,单s位秒
|
||||
EndTime int64 `json:"end_time"` // 活动结束时间,单位秒
|
||||
WeeksTime string `json:"weeks_time"`
|
||||
Period string `json:"period"`
|
||||
WeeksTime string `json:"weeks_time,omitempty"`
|
||||
Period string `json:"period,omitempty"`
|
||||
ActDetail []*ShippingFeeActDetail `json:"act_detail"`
|
||||
MaxPrice float64 `json:"max_price"`
|
||||
MaxPrice float64 `json:"max_price,omitempty"`
|
||||
|
||||
AppPoiCode string `json:"-"`
|
||||
ActID string `json:"-"`
|
||||
@@ -108,8 +108,8 @@ type RetailActData struct {
|
||||
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
|
||||
Sequence int `json:"sequence,omitempty"`
|
||||
ItemID int64 `json:"item_id,omitempty"` // 活动ID,为什么这里又是int64
|
||||
|
||||
// 以下参数只有查询时用到
|
||||
OriginalPrice float64 `json:"-"` // 商品原价,单位元
|
||||
@@ -119,17 +119,11 @@ type RetailActData struct {
|
||||
}
|
||||
|
||||
type RetailActDataLimit struct {
|
||||
ItemID int64 `json:"item_id"` // 活动ID,为什么这里又是int64
|
||||
DayLimit int `json:"day_limit"` // 当日活动库存,只能为正整数或-1,-1表示无限库存
|
||||
OrderLimit int `json:"order_limit"` // 每单可购买的折扣商品数量
|
||||
ItemID int64 `json:"item_id"` // 活动ID,为什么这里又是int64
|
||||
DayLimit int `json:"day_limit,omitempty"` // 当日活动库存,只能为正整数或-1,-1表示无限库存
|
||||
OrderLimit int `json:"order_limit,omitempty"` // 每单可购买的折扣商品数量
|
||||
}
|
||||
|
||||
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,
|
||||
@@ -152,43 +146,11 @@ func (a *API) FullDiscountList(poiCode string, actType int) (actInfoList []*Full
|
||||
"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"])),
|
||||
})
|
||||
}
|
||||
}
|
||||
// decoder, _ := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
|
||||
// TagName: "json",
|
||||
// Result: &actInfoList,
|
||||
// })
|
||||
// if err = decoder.Decode(result); err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
err = utils.Map2StructByJson(result, &actInfoList, false)
|
||||
}
|
||||
return actInfoList, 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,
|
||||
@@ -219,15 +181,15 @@ func (a *API) FullDiscountFoodsList(poiCode, actID string) (actFoodsInfo *FullDi
|
||||
"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"]),
|
||||
}
|
||||
tmpActFoodsInfo := &FullDiscountFoodsInfo{}
|
||||
if err = utils.Map2StructByJson(result, tmpActFoodsInfo, false); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if actFoodsInfo == nil {
|
||||
actFoodsInfo = tmpActFoodsInfo
|
||||
} else {
|
||||
actFoodsInfo.AppFoods = append(actFoodsInfo.AppFoods, tmpActFoodsInfo.AppFoods...)
|
||||
}
|
||||
actFoodsInfo.AppFoods = append(actFoodsInfo.AppFoods, interface2SkuInfoList(resultMap["app_foods"])...)
|
||||
offset++
|
||||
break
|
||||
} else {
|
||||
@@ -237,18 +199,6 @@ func (a *API) FullDiscountFoodsList(poiCode, actID string) (actFoodsInfo *FullDi
|
||||
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) {
|
||||
@@ -275,11 +225,7 @@ func (a *API) FullDiscountFoodsDayLimit(poiCode, actID string, appFoodList []*Fu
|
||||
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,
|
||||
}))),
|
||||
"act_data": string(utils.MustMarshal(actData)),
|
||||
})
|
||||
return err
|
||||
}
|
||||
@@ -290,30 +236,7 @@ func (a *API) FulllDiscountShippingFeeList(poiCode string) (actList []*ShippingF
|
||||
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),
|
||||
})
|
||||
}
|
||||
}
|
||||
err = utils.Map2StructByJson(result, &actList, false)
|
||||
}
|
||||
return actList, err
|
||||
}
|
||||
@@ -322,10 +245,7 @@ func (a *API) FulllDiscountShippingFeeList(poiCode string) (actList []*ShippingF
|
||||
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,
|
||||
}))),
|
||||
"act_data": string(utils.MustMarshal(actData)),
|
||||
}, resultKeyMsg)
|
||||
if err == nil {
|
||||
return utils.MustInterface2Int64(result.([]interface{})[0].(map[string]interface{})["act_id"]), nil
|
||||
@@ -344,31 +264,12 @@ func (a *API) RetailDiscountList(poiCode string) (actList []*RetailActData, err
|
||||
"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)
|
||||
var tmpActList []*RetailActData
|
||||
if err = utils.Map2StructByJson(result, &tmpActList, false); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(resultList) < limit {
|
||||
actList = append(actList, tmpActList...)
|
||||
if len(tmpActList) < limit {
|
||||
break
|
||||
}
|
||||
offset++
|
||||
@@ -392,9 +293,7 @@ func (a *API) RetailDiscountDelete(poiCode string, actIDList []string) (err erro
|
||||
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,
|
||||
})),
|
||||
"act_data": string(utils.MustMarshal(actDataList)),
|
||||
})
|
||||
return err
|
||||
}
|
||||
@@ -403,9 +302,7 @@ func (a *API) RetailDiscountBatchStock(poiCode, actDataList []*RetailActDataLimi
|
||||
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,
|
||||
})),
|
||||
"act_data": string(utils.MustMarshal(actDataList)),
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user