Files
jx-callback/business/partner/purchase/yb/store_sku.go
2020-03-23 17:16:32 +08:00

239 lines
8.1 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 yb
import (
"fmt"
"git.rosy.net.cn/baseapi/platformapi/yinbaoapi"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/business/jxutils"
"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
"git.rosy.net.cn/jx-callback/business/jxutils/tasksch"
"git.rosy.net.cn/jx-callback/business/model"
"git.rosy.net.cn/jx-callback/business/model/dao"
"git.rosy.net.cn/jx-callback/business/partner"
"git.rosy.net.cn/jx-callback/business/partner/putils"
"git.rosy.net.cn/jx-callback/globals"
"git.rosy.net.cn/jx-callback/globals/api"
)
var (
stock float64 = 100
)
func (p *PurchaseHandler) CreateStoreSkus(ctx *jxcontext.Context, storeID int, vendorStoreID string, storeSkuList []*dao.StoreSkuSyncInfo) (failedList []*partner.StoreSkuInfoWithErr, err error) {
storeSku := storeSkuList[0]
buildYbConfigs(storeID)
result, err := api.YinBaoAPI.AddProductInfo(buildProductInfoParam(storeSku))
if err != nil {
failedList = putils.GetErrMsg2FailedSingleList(storeSkuList, err, storeID, model.VendorChineseNames[model.VendorIDYB], "创建商品")
} else {
storeSku.VendorSkuID = utils.Int64ToStr(result.UID)
}
return failedList, err
}
func (p *PurchaseHandler) UpdateStoreSkus(ctx *jxcontext.Context, storeID int, vendorStoreID string, storeSkuList []*dao.StoreSkuSyncInfo) (failedList []*partner.StoreSkuInfoWithErr, err error) {
return failedList, err
}
func (p *PurchaseHandler) GetStoreSkusFullInfo(ctx *jxcontext.Context, parentTask tasksch.ITask, storeID int, vendorStoreID string, storeSkuList []*partner.StoreSkuInfo) (skuNameList []*partner.SkuNameInfo, err error) {
buildYbConfigs(storeID)
if storeSkuList != nil {
if len(storeSkuList) == 1 {
storeSku := storeSkuList[0]
result, err := api.YinBaoAPI.QueryProductByBarcode(utils.Int2Str(storeSku.SkuID))
resultp, err := api.YinBaoAPI.QueryProductImagesByBarcode(utils.Int2Str(storeSku.SkuID))
if err != nil {
return nil, err
}
if skuName := vendorSku2Jx(result, resultp); skuName != nil {
skuNameList = append(skuNameList, skuName)
}
} else {
var barcodes []string
for _, v := range storeSkuList {
barcodes = append(barcodes, utils.Int2Str(v.SkuID))
}
results, err := api.YinBaoAPI.QueryProductByBarcodes(barcodes)
if err != nil {
return nil, err
}
for _, v := range results {
resultp, err := api.YinBaoAPI.QueryProductImagesByBarcode(v.Barcode)
if err != nil {
return nil, err
}
if skuName := vendorSku2Jx(v, resultp); skuName != nil {
skuNameList = append(skuNameList, skuName)
}
}
}
} else {
result, err := api.YinBaoAPI.QueryProductPages(nil)
if err != nil {
return nil, err
}
if result.PostBackParameter.ParameterType == yinbaoapi.PageMaxID {
for _, v := range result.Result {
resultp, err := api.YinBaoAPI.QueryProductImagesByBarcode(v.Barcode)
if err != nil {
return nil, err
}
vv := &yinbaoapi.QueryProductByBarcodeResult{}
err = utils.Map2StructByJson(utils.Struct2MapByJson(v), vv, false)
if skuName := vendorSku2Jx(vv, resultp); skuName != nil {
skuNameList = append(skuNameList, skuName)
}
}
} else {
loopPages(result.PostBackParameter.ParameterType, result.PostBackParameter.ParameterValue, skuNameList)
}
}
return skuNameList, err
}
func (p *PurchaseHandler) UpdateStoreSkusStatus(ctx *jxcontext.Context, vendorOrgCode string, storeID int, vendorStoreID string, storeSkuList []*partner.StoreSkuInfo, status int) (failedList []*partner.StoreSkuInfoWithErr, err error) {
if globals.EnableYbStoreWrite {
}
return failedList, err
}
func (p *PurchaseHandler) UpdateStoreSkusPrice(ctx *jxcontext.Context, vendorOrgCode string, storeID int, vendorStoreID string, storeSkuList []*partner.StoreSkuInfo) (failedList []*partner.StoreSkuInfoWithErr, err error) {
if globals.EnableYbStoreWrite {
}
return failedList, err
}
func (p *PurchaseHandler) UpdateStoreSkusStock(ctx *jxcontext.Context, vendorOrgCode string, storeID int, vendorStoreID string, storeSkuList []*partner.StoreSkuInfo) (failedList []*partner.StoreSkuInfoWithErr, err error) {
if globals.EnableYbStoreWrite {
}
return failedList, err
}
func (p *PurchaseHandler) CreateStoreCategory(ctx *jxcontext.Context, storeID int, vendorStoreID string, storeCat *dao.SkuStoreCatInfo) (err error) {
return err
}
func (p *PurchaseHandler) UpdateStoreCategory(ctx *jxcontext.Context, storeID int, vendorStoreID string, storeCat *dao.SkuStoreCatInfo) (err error) {
return err
}
func (p *PurchaseHandler) DeleteStoreCategory(ctx *jxcontext.Context, storeID int, vendorStoreID, vendorCatID string, level int) (err error) {
return err
}
func (p *PurchaseHandler) IsErrSkuExist(err error) (isExist bool) {
return yinbaoapi.IsErrSkuExist(err)
}
func (p *PurchaseHandler) GetStoreSkusBatchSize(funcID int) (batchSize int) {
return 1
}
func ybSkuStatus2Jx(ybStatus int) (jxSkuStatus int) {
if ybStatus == yinbaoapi.SkuStatusEnable {
jxSkuStatus = model.SkuStatusNormal
} else if ybStatus == yinbaoapi.SkuStatusDisabled {
jxSkuStatus = model.SkuStatusDontSale
} else if ybStatus == yinbaoapi.SkuStatusDeleted {
jxSkuStatus = model.SkuStatusDeleted
}
return jxSkuStatus
}
func vendorSku2Jx(result *yinbaoapi.QueryProductByBarcodeResult, resultp []*yinbaoapi.QueryProductImagesByBarcodeResult) (skuName *partner.SkuNameInfo) {
var picList []string
if result == nil {
globals.SugarLogger.Warnf("vendorSku2Jx, strange result:%s", utils.Format4Output(result, true))
return nil
}
if len(resultp) > 0 {
for _, v := range resultp {
picList = append(picList, v.ImageURL)
}
}
prefix, name, comment, specUnit, unit, specQuality := jxutils.SplitSkuName(result.Name)
skuID := int(utils.Str2Int64WithDefault(result.Barcode, 0))
skuName = &partner.SkuNameInfo{
Prefix: prefix,
Name: name,
Unit: unit,
SkuList: []*partner.SkuInfo{
&partner.SkuInfo{
StoreSkuInfo: partner.StoreSkuInfo{
VendorSkuID: utils.Int64ToStr(result.UID),
SkuID: skuID,
Stock: int(utils.Float64TwoInt64(result.Stock)),
VendorPrice: jxutils.StandardPrice2Int(result.SellPrice),
Status: ybSkuStatus2Jx(result.Enable),
},
SkuName: result.Name,
Comment: comment,
SpecQuality: float64(specQuality),
SpecUnit: specUnit,
Weight: int(utils.Float64TwoInt64(float64(specQuality))),
},
},
PictureList: picList,
}
return skuName
}
func loopPages(parameterType, parameterValue string, skuNameList []*partner.SkuNameInfo) (err error) {
var postBackParameter = &yinbaoapi.PostBackParameter{
ParameterType: parameterType,
ParameterValue: parameterValue,
}
resultPages, err := api.YinBaoAPI.QueryProductPages(postBackParameter)
if err != nil {
return err
}
for _, v := range resultPages.Result {
resultp, err := api.YinBaoAPI.QueryProductImagesByBarcode(v.Barcode)
if err != nil {
return err
}
vv := &yinbaoapi.QueryProductByBarcodeResult{}
err = utils.Map2StructByJson(utils.Struct2MapByJson(v), vv, false)
if skuName := vendorSku2Jx(vv, resultp); skuName != nil {
skuNameList = append(skuNameList, skuName)
}
}
if resultPages.PostBackParameter.ParameterType != yinbaoapi.PageMaxID {
err = loopPages(resultPages.PostBackParameter.ParameterType, resultPages.PostBackParameter.ParameterValue, skuNameList)
}
return err
}
func buildProductInfoParam(storeSku *dao.StoreSkuSyncInfo) (productInfoParam *yinbaoapi.ProductInfoParam) {
var (
buyPrice float64 = utils.Str2Float64(utils.Int64ToStr(storeSku.Price)) / 100
sellPrice float64 = utils.Str2Float64(utils.Int64ToStr(storeSku.VendorPrice)) / 100
)
productInfoParam = &yinbaoapi.ProductInfoParam{}
productInfo := &yinbaoapi.ProductInfo{
Stock: &stock,
Name: storeSku.SkuName,
Barcode: utils.Int2Str(storeSku.SkuID),
BuyPrice: &buyPrice,
SellPrice: &sellPrice,
}
productInfoParam.ProductInfo = productInfo
return productInfoParam
}
func buildYbConfigs(storeID int) (err error) {
if storeID == 0 {
return fmt.Errorf("门店ID不能为空平台[%v]", model.VendorIDYB)
}
store, err := dao.GetStoreDetail(dao.GetDB(), storeID, model.VendorIDYB)
if err != nil {
return err
}
api.YinBaoAPI = yinbaoapi.New(store.YbAppKey, store.YbAppID)
return err
}