Files
baseapi/platformapi/weimobapi/goods.go
2019-01-20 10:09:25 +08:00

347 lines
11 KiB
Go

package weimobapi
import (
"fmt"
"io/ioutil"
"net/http"
"git.rosy.net.cn/baseapi/platformapi"
"git.rosy.net.cn/baseapi/utils"
)
const (
DeductStockTypePlaceOrder = 1
DeductStockTypePay = 2
)
const (
GoodsTypeNormal = 0
GoodsTypeOversea = 1
)
type PendingSaveB2CGoodsVo struct {
FreightTemplateId int64 `json:"freightTemplateId"`
DeliveryTypeIdList []int64 `json:"deliveryTypeIdList"`
B2cGoodsType int `json:"b2cGoodsType"`
}
type PendingSaveB2CSkuVo struct {
Weight float32 `json:"weight"`
Volume float32 `json:"volume"`
}
type PendingSaveSkuVo struct {
OuterSkuCode string `json:"outerSkuCode"`
ImageURL string `json:"imageUrl"`
SalePrice float32 `json:"salePrice"`
OriginalPrice float32 `json:"originalPrice"`
CostPrice float32 `json:"costPrice"`
EditStockNum int `json:"editStockNum"`
B2cSku *PendingSaveB2CSkuVo `json:"b2cSku"`
}
type Category struct {
CategoryID int64 `json:"categoryId"`
Title string `json:"title"`
Level int `json:"level"`
ParentID int64 `json:"parentId"`
}
type GoodsClassify struct {
ClassifyID int64 `json:"classifyId"`
ImageURL string `json:"imageUrl"`
Title string `json:"title"`
Level int `json:"level"`
ChildrenClassify []*GoodsClassify `json:"childrenClassify"`
}
type DeliveryType struct {
DeliveryID int64 `json:"deliveryId"`
DeliveryTypeName string `json:"deliveryTypeName"`
DeliveryType int `json:"deliveryType"`
Selected bool `json:"selected"`
}
func (a *API) QueryGoodsList(pageNum, pageSize int, orderBy []map[string]interface{}, queryParameter map[string]interface{}) (retVal []map[string]interface{}, totalCount int, err error) {
apiParams := map[string]interface{}{
"pageNum": pageNum,
"pageSize": pageSize,
}
if orderBy != nil {
apiParams["orderBy"] = orderBy
}
if queryParameter != nil {
apiParams["queryParameter"] = queryParameter
}
result, err := a.AccessAPI("goods/queryGoodsList", apiParams)
if err == nil {
if pageList, ok := result.(map[string]interface{})["pageList"].([]interface{}); ok {
retVal = utils.Slice2MapSlice(pageList)
}
totalCount = int(utils.MustInterface2Int64(result.(map[string]interface{})["totalCount"]))
return retVal, totalCount, nil
}
return nil, 0, err
}
func (a *API) QueryCategoryTree() (retVal []*Category, err error) {
result, err := a.AccessAPI("category/queryCategoryTree", nil)
if err == nil {
categoryList := result.(map[string]interface{})["categoryList"].([]interface{})
retVal = make([]*Category, len(categoryList))
for k, v := range categoryList {
retVal[k] = map2Category(1, 0, v.(map[string]interface{}))
}
return retVal, nil
}
return nil, err
}
func map2Category(level int, parentID int64, mapData map[string]interface{}) *Category {
return &Category{
CategoryID: utils.MustInterface2Int64(mapData["categoryId"]),
Title: utils.Interface2String(mapData["title"]),
Level: level,
ParentID: parentID,
}
}
func (a *API) QueryChildrenCategory(categoryId int64) (retVal []*Category, err error) {
result, err := a.AccessAPI("category/queryChildrenCategory", map[string]interface{}{
"categoryId": categoryId,
})
if err == nil {
categoryList := result.(map[string]interface{})["categoryList"].([]interface{})
retVal = make([]*Category, len(categoryList))
for k, v := range categoryList {
retVal[k] = map2Category(2, categoryId, v.(map[string]interface{}))
}
return retVal, nil
}
return nil, err
}
func (a *API) QueryClassifyInfoList() (retVal []*GoodsClassify, err error) {
result, err := a.AccessAPI("goodsClassify/queryClassifyInfoList", nil)
if err == nil {
goodsClassifyList := interface2ClassifyList(result.(map[string]interface{})["goodsClassifyList"], nil)
return goodsClassifyList, nil
}
return nil, err
}
func interface2Classify(data interface{}) (clf *GoodsClassify) {
mapData, ok := data.(map[string]interface{})
if ok {
clf = &GoodsClassify{
ClassifyID: utils.MustInterface2Int64(mapData["classifyId"]),
ImageURL: utils.Interface2String(mapData["imageUrl"]),
Title: utils.Interface2String(mapData["title"]),
Level: int(utils.Interface2Int64WithDefault(mapData["level"], 0)),
ChildrenClassify: interface2ClassifyList(mapData["childrenClassify"], nil),
}
}
return clf
}
func interface2ClassifyList(data interface{}, interface2CatHandler func(data interface{}) (clf *GoodsClassify)) (clfs []*GoodsClassify) {
if interface2CatHandler == nil {
interface2CatHandler = interface2Classify
}
maps, ok := data.([]interface{})
if ok {
clfs = make([]*GoodsClassify, len(maps))
for index, v := range maps {
clfs[index] = interface2CatHandler(v)
}
}
return clfs
}
func (a *API) AddClassify(title string, parentID int64, imageURL string) (goodsClassifyID int64, err error) {
apiParams := map[string]interface{}{
"title": title,
}
if parentID > 0 {
apiParams["parentId"] = parentID
}
if imageURL != "" {
apiParams["imageUrl"] = imageURL
}
result, err := a.AccessAPI("goodsClassify/addClassify", apiParams)
if err == nil {
return utils.MustInterface2Int64(result.(map[string]interface{})["goodsClassifyId"]), nil
}
return 0, err
}
func (a *API) UpdateClassify(classifyID int64, title string, imageURL string) (err error) {
apiParams := map[string]interface{}{
"title": title,
"classifyId": classifyID,
}
if imageURL != "" {
apiParams["imageUrl"] = imageURL
}
_, err = a.AccessAPI("goodsClassify/updateClassify", apiParams)
return err
}
func (a *API) AddGoods(outerGoodsCode, title string, isMultiSku bool, goodsImageUrl []string, goodsDesc string, isPutAway bool, sort int, categoryId int64, classifyIdList []int64, b2cGoods *PendingSaveB2CGoodsVo, skuList []map[string]interface{}, addParams map[string]interface{}) (goodsId int64, skuMap map[string]int64, err error) {
goodsInfo := map[string]interface{}{
"outerGoodsCode": outerGoodsCode,
"title": title,
"isMultiSku": utils.Bool2Int(isMultiSku),
"goodsImageUrl": goodsImageUrl,
"goodsDesc": goodsDesc,
"isPutAway": 1 - utils.Bool2Int(isPutAway),
"sort": sort,
"categoryId": categoryId,
"b2cGoods": b2cGoods,
"skuList": skuList,
"selectedClassifyIdList": classifyIdList,
"initialSales": 100,
}
mergedMap := utils.MergeMaps(addParams, goodsInfo)
if _, ok := mergedMap["deductStockType"]; !ok {
mergedMap["deductStockType"] = DeductStockTypePay
}
result, err := a.AccessAPI("goods/addGoods", map[string]interface{}{
"goods": mergedMap,
})
if err == nil {
skuMap := make(map[string]int64)
skuList := result.(map[string]interface{})["skuList"].([]interface{})
for _, v := range skuList {
sku := v.(map[string]interface{})
skuMap[utils.Interface2String(sku[KeyOuterSkuCode])] = utils.MustInterface2Int64(sku[KeySkuID])
}
return utils.MustInterface2Int64(result.(map[string]interface{})["goodsId"]), skuMap, nil
}
return 0, nil, err
}
func (a *API) UpdateGoods(goodsID int64, title string, isMultiSku bool, goodsImageUrl []string, goodsDesc string, isPutAway bool, sort int, categoryId int64, classifyIdList []int64, b2cGoods *PendingSaveB2CGoodsVo, skuList []map[string]interface{}, addParams map[string]interface{}) (goodsId int64, skuMap map[string]int64, err error) {
goodsInfo := map[string]interface{}{
"goodsId": goodsID,
"title": title,
"isMultiSku": utils.Bool2Int(isMultiSku),
"goodsImageUrl": goodsImageUrl,
"goodsDesc": goodsDesc,
"isPutAway": 1 - utils.Bool2Int(isPutAway),
"sort": sort,
"categoryId": categoryId,
"b2cGoods": b2cGoods,
"skuList": skuList,
"selectedClassifyIdList": classifyIdList,
}
mergedMap := utils.MergeMaps(addParams, goodsInfo)
if _, ok := mergedMap["deductStockType"]; !ok {
mergedMap["deductStockType"] = DeductStockTypePay
}
result, err := a.AccessAPI("goods/updateGoods", map[string]interface{}{
"goods": mergedMap,
})
if err == nil {
skuMap := make(map[string]int64)
skuList := result.(map[string]interface{})["skuList"].([]interface{})
for _, v := range skuList {
sku := v.(map[string]interface{})
skuMap[utils.Interface2String(sku[KeyOuterSkuCode])] = utils.MustInterface2Int64(sku[KeySkuID])
}
return utils.MustInterface2Int64(result.(map[string]interface{})["goodsId"]), skuMap, nil
}
return 0, nil, err
}
func (a *API) UpdateGoodsShelfStatus(goodsIDs []int64, isPutAway bool) (err error) {
_, err = a.AccessAPI("goods/updateGoodsShelfStatus", map[string]interface{}{
"goodsIdList": goodsIDs,
"isPutAway": 1 - utils.Bool2Int(isPutAway),
})
return err
}
func (a *API) UpdateGoodsTitle(goodsID int64, title string) (err error) {
_, err = a.AccessAPI("goods/updateGoodsTitle", map[string]interface{}{
"goodsId": goodsID,
"title": title,
})
return err
}
func (a *API) FindDeliveryTypeList(goodsID int64) (retVal []*DeliveryType, err error) {
apiParams := map[string]interface{}{}
if goodsID > 0 {
apiParams["goodsId"] = goodsID
}
result, err := a.AccessAPI("goods/findDeliveryTypeList", apiParams)
if err == nil {
deliveryTypeList := result.(map[string]interface{})["deliveryTypeList"].([]interface{})
retVal = make([]*DeliveryType, len(deliveryTypeList))
for k, v := range deliveryTypeList {
mapData := v.(map[string]interface{})
retVal[k] = &DeliveryType{
DeliveryID: utils.MustInterface2Int64(mapData["deliveryId"]),
DeliveryType: int(utils.MustInterface2Int64(mapData["deliveryType"])),
DeliveryTypeName: utils.Interface2String(mapData["deliveryTypeName"]),
Selected: mapData["selected"].(bool),
}
}
return retVal, nil
}
return nil, err
}
func (a *API) uploadImg(imgData []byte, name string) (imgURL string, err error) {
apiParams := map[string]interface{}{
"file": imgData,
}
if name != "" {
apiParams["name"] = name
}
result, err := a.AccessAPI("goodsImage/uploadImg", apiParams)
if err == nil {
urlInfo := result.(map[string]interface{})["urlInfo"].([]interface{})
if len(urlInfo) > 0 {
urlInfo0 := urlInfo[0].(map[string]interface{})
if utils.MustInterface2Int64(urlInfo0["legalStatus"]) == 0 {
return utils.Interface2String(urlInfo0["url"]), nil
}
return "", fmt.Errorf("上传的图片:%s不合法", name)
}
return "", fmt.Errorf("上传的图片:%s返回为空", name)
}
return "", err
}
func (a *API) UploadImgByURL(uploadImgURL string, name string) (imgURL string, err error) {
response, err := http.Get(uploadImgURL)
if err == nil {
defer func() {
response.Body.Close()
}()
if response.StatusCode == http.StatusOK {
bodyData, err2 := ioutil.ReadAll(response.Body)
if err = err2; err == nil {
return a.uploadImg(bodyData, name)
}
} else {
err = platformapi.ErrHTTPCodeIsNot200
}
}
return "", err
}
func (a *API) FindFreightTemplateList(goodsID int64) (retVal map[string]interface{}, err error) {
apiParams := map[string]interface{}{}
if goodsID > 0 {
apiParams["goodsId"] = goodsID
}
result, err := a.AccessAPI("goods/findFreightTemplateList", apiParams)
if err == nil {
return result.(map[string]interface{}), nil
}
return nil, err
}