Files
baseapi/platformapi/weimobapi/goods.go
2019-11-11 09:59:47 +08:00

427 lines
14 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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,omitempty"`
SalePrice float64 `json:"salePrice"`
OriginalPrice float64 `json:"originalPrice,omitempty"`
CostPrice float64 `json:"costPrice"`
EditStockNum int `json:"editStockNum"`
B2cSku *PendingSaveB2CSkuVo `json:"b2cSku"`
}
type PendingSaveGoodsVo struct {
Title string `json:"title"`
OuterGoodsCode string `json:"outerGoodsCode,omitempty"`
IsMultiSku int `json:"isMultiSku"`
GoodsImageURL []string `json:"goodsImageUrl"`
GoodsDesc string `json:"goodsDesc"`
InitialSales int `json:"initialSales,omitempty"`
DeductStockType int `json:"deductStockType"`
IsPutAway int `json:"isPutAway"`
Sort int `json:"Sort,omitempty"`
CategoryID int64 `json:"categoryId"`
B2cGoods []*PendingSaveB2CGoodsVo `json:"b2cGoods"`
SkuList []*PendingSaveSkuVo `json:"skuList"`
}
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"`
}
type GoodsInfo struct {
AvaliableStockNum int `json:"avaliableStockNum"`
DefaultImageURL string `json:"defaultImageUrl"`
ExistEmptyStock bool `json:"existEmptyStock"`
GoodsID int64 `json:"goodsId"`
IsAllStockEmpty bool `json:"isAllStockEmpty"`
IsCanSell bool `json:"isCanSell"`
IsExistEmptyStock bool `json:"isExistEmptyStock"`
IsMultiSku int `json:"isMultiSku"`
IsPutAway int `json:"isPutAway"`
MaxPrice float64 `json:"maxPrice"`
MinPrice float64 `json:"minPrice"`
PutAwayDate int64 `json:"putAwayDate"`
PutAwayForBackend int `json:"putAwayForBackend"`
SalesNum int `json:"salesNum"`
SortNum int `json:"sortNum"`
Title string `json:"title"`
}
type QueryGoodsListParam struct {
PageNum int `json:"pageNum"`
PageSize int `json:"pageSize"`
OrderBy
GoodsParameter
}
type OrderBy struct {
Field string `json:"field"` //排序字段名: sortNum按照sort字段排序 putAwayDate按照上下架时间排序 salesNum按照销量排序 availableStockNum按照库存排序 price按照价格排序
Sort string `json:"sort"` //升降/降序 DESC/ASC只能是这两个值不区分大小写
}
type GoodsParameter struct {
GoodsStatus int `json:"goodsStatus"` //商品状态0-上架中 1-下架中 2-已售罄
GoodsClassifyID int `json:"goodsClassifyId"` //分组id
Search string `json:"search"` //搜索框输入内容商品名称
GoodsPrice float32 `json:"goodsPrice"`
}
func (a *API) QueryGoodsList(queryParam *QueryGoodsListParam) (goodsList []*GoodsInfo, totalCount int, err error) {
param := make(map[string]interface{})
if queryParam != nil {
param = utils.Struct2FlatMap(queryParam)
}
result, err := a.AccessAPI("goods/queryGoodsList", param)
if err == nil {
data := result.(map[string]interface{})
totalCount = int(utils.MustInterface2Int64(data["totalCount"]))
err = utils.Map2StructByJson(data["pageList"], &goodsList, false)
}
return goodsList, totalCount, err
}
func (a *API) QueryGoodsDetail(goodsId int64) (retVal map[string]interface{}, err error) {
apiParams := map[string]interface{}{
"goodsId": goodsId,
}
result, err := a.AccessAPI("goods/queryGoodsDetail", apiParams)
if err == nil {
return result.(map[string]interface{})["goods"].(map[string]interface{}), nil
}
return nil, 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 getDataFromCUGoodsResult(result interface{}) (goodsId int64, skuMap map[string]int64, err error) {
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
}
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 {
return getDataFromCUGoodsResult(result)
}
return 0, nil, err
}
func (a *API) AddGoods2(goodsInfo *PendingSaveGoodsVo) (goodsId int64, skuMap map[string]int64, err error) {
if goodsInfo.DeductStockType == 0 {
goodsInfo.DeductStockType = DeductStockTypePay
}
result, err := a.AccessAPI("goods/addGoods", map[string]interface{}{
"goods": goodsInfo,
})
if err == nil {
return getDataFromCUGoodsResult(result)
}
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 {
return getDataFromCUGoodsResult(result)
}
return 0, nil, err
}
func (a *API) UpdateGoods2(goodsInfo *PendingSaveGoodsVo) (goodsId int64, skuMap map[string]int64, err error) {
if goodsInfo.DeductStockType == 0 {
goodsInfo.DeductStockType = DeductStockTypePay
}
result, err := a.AccessAPI("goods/updateGoods", map[string]interface{}{
"goods": goodsInfo,
})
if err == nil {
return getDataFromCUGoodsResult(result)
}
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
}