357 lines
12 KiB
Go
357 lines
12 KiB
Go
package mtwm
|
||
|
||
import (
|
||
"git.rosy.net.cn/baseapi/platformapi/mtwmapi"
|
||
"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/globals"
|
||
"git.rosy.net.cn/jx-callback/globals/api"
|
||
)
|
||
|
||
func (p *PurchaseHandler) GetStoreSkusBatchSize(funcID int) (batchSize int) {
|
||
switch funcID {
|
||
case partner.FuncUpdateStoreSkusStock, partner.FuncUpdateStoreSkusStatus, partner.FuncUpdateStoreSkusPrice:
|
||
batchSize = mtwmapi.MaxStoreSkuBatchSize
|
||
case partner.FuncGetStoreSkusBareInfo:
|
||
batchSize = 1
|
||
case partner.FuncDeleteStoreSkus:
|
||
batchSize = 1 // 可考虑用批量操作
|
||
case partner.FuncCreateStoreSkus, partner.FuncUpdateStoreSkus:
|
||
batchSize = 1 // 可考虑用批量操作
|
||
case partner.FuncGetStoreSkusFullInfo:
|
||
batchSize = 1
|
||
}
|
||
return batchSize
|
||
}
|
||
|
||
// 门店分类
|
||
func (p *PurchaseHandler) GetStoreAllCategories(ctx *jxcontext.Context, storeID int, vendorStoreID string) (cats []*partner.BareCategoryInfo, err error) {
|
||
remoteCats, err := api.MtwmAPI.RetailCatList(vendorStoreID)
|
||
if err == nil {
|
||
cats = convertVendorCatList(remoteCats)
|
||
}
|
||
return cats, err
|
||
}
|
||
|
||
func convertVendorCatList(remoteCats []*mtwmapi.RetailCategoryInfo) (cats []*partner.BareCategoryInfo) {
|
||
for _, rCat := range remoteCats {
|
||
cat := &partner.BareCategoryInfo{
|
||
VendorCatID: rCat.Name,
|
||
Name: rCat.Name,
|
||
Level: rCat.Level,
|
||
Seq: rCat.Sequence,
|
||
Children: convertVendorCatList(rCat.Children),
|
||
}
|
||
cats = append(cats, cat)
|
||
}
|
||
return cats
|
||
}
|
||
|
||
func (p *PurchaseHandler) CreateStoreCategory(ctx *jxcontext.Context, storeID int, vendorStoreID string, storeCat *dao.SkuStoreCatInfo) (err error) {
|
||
level := 1
|
||
if storeCat.ParentCatName != "" {
|
||
level = 2
|
||
}
|
||
catName := storeCat.Name
|
||
subCatName := ""
|
||
originName := ""
|
||
if storeCat.StoreCatSyncStatus&model.SyncFlagNewMask == 0 {
|
||
originName = storeCat.VendorCatID
|
||
}
|
||
if level == 2 {
|
||
originName = storeCat.ParentCatName
|
||
catName = storeCat.ParentCatName
|
||
subCatName = storeCat.Name
|
||
if storeCat.StoreCatSyncStatus&model.SyncFlagNewMask == 0 {
|
||
originName = storeCat.VendorCatID
|
||
catName = storeCat.Name
|
||
subCatName = ""
|
||
}
|
||
}
|
||
if catName == "" {
|
||
panic("catName is empty")
|
||
}
|
||
globals.SugarLogger.Debugf("mtwm CreateStoreCategory vendorStoreID:%s, originName:%s, catName:%s, subCatName:%s, seq:%d", vendorStoreID, originName, catName, subCatName, storeCat.Seq)
|
||
if globals.EnableMtwmStoreWrite {
|
||
err = api.MtwmAPI.RetailCatUpdate(vendorStoreID, originName, catName, subCatName, storeCat.Seq)
|
||
}
|
||
if err == nil {
|
||
storeCat.VendorCatID = storeCat.Name
|
||
}
|
||
return err
|
||
}
|
||
|
||
func (p *PurchaseHandler) UpdateStoreCategory(ctx *jxcontext.Context, storeID int, vendorStoreID string, storeCat *dao.SkuStoreCatInfo) (err error) {
|
||
return p.CreateStoreCategory(ctx, storeID, vendorStoreID, storeCat)
|
||
}
|
||
|
||
func (p *PurchaseHandler) DeleteStoreCategory(ctx *jxcontext.Context, storeID int, vendorStoreID, vendorCatID string) (err error) {
|
||
if globals.EnableMtwmStoreWrite {
|
||
err = api.MtwmAPI.RetailCatDelete(vendorStoreID, vendorCatID)
|
||
}
|
||
return err
|
||
}
|
||
|
||
// 门店商品
|
||
|
||
// 多门店平台不需要实现这个接口
|
||
func (p *PurchaseHandler) UpdateStoreSkus(ctx *jxcontext.Context, storeID int, vendorStoreID string, storeSkuList []*dao.StoreSkuSyncInfo) (err error) {
|
||
return p.CreateStoreSkus(ctx, storeID, vendorStoreID, storeSkuList)
|
||
}
|
||
|
||
// 对于多门店平台来说,storeSkuList中只有SkuID与VendorSkuID有意义
|
||
func (p *PurchaseHandler) CreateStoreSkus(ctx *jxcontext.Context, storeID int, vendorStoreID string, storeSkuList []*dao.StoreSkuSyncInfo) (err error) {
|
||
foodDataList := make([]map[string]interface{}, len(storeSkuList))
|
||
for i, storeSku := range storeSkuList {
|
||
foodData := make(map[string]interface{})
|
||
foodDataList[i] = foodData
|
||
foodData[mtwmapi.KeyAppFoodCode] = utils.Int2Str(storeSku.SkuID)
|
||
skus := []map[string]interface{}{
|
||
map[string]interface{}{
|
||
"sku_id": foodData[mtwmapi.KeyAppFoodCode],
|
||
},
|
||
}
|
||
foodData["skus"] = skus
|
||
foodData["name"] = utils.LimitUTF8StringLen(storeSku.Name, 30)
|
||
foodData["description"] = storeSku.Comment
|
||
foodData["price"] = storeSku.Price
|
||
foodData["min_order_count"] = 1
|
||
foodData["unit"] = storeSku.Unit
|
||
foodData["box_num"] = 0
|
||
foodData["box_price"] = 0.0
|
||
foodData["category_name"] = storeSku.VendorCatID
|
||
foodData["is_sold_out"] = skuStatusJX2Mtwm(storeSku.StoreSkuStatus)
|
||
foodData["picture"] = storeSku.Img
|
||
if storeSku.DescImg != "" {
|
||
foodData["picture_contents"] = storeSku.DescImg
|
||
}
|
||
foodData["sequence"] = storeSku.Price
|
||
if storeSku.VendorVendorCatID != 0 {
|
||
foodData["tag_id"] = utils.Int64ToStr(storeSku.VendorVendorCatID)
|
||
} else {
|
||
// foodData["tag_id"] = utils.Int64ToStr(defVendorCatID)
|
||
}
|
||
skus[0]["spec"] = jxutils.ComposeSkuSpec(storeSku.SpecQuality, storeSku.SpecUnit)
|
||
skus[0]["price"] = foodData["price"]
|
||
skus[0]["stock"] = "*"
|
||
skus[0]["upc"] = storeSku.Upc
|
||
if foodData["tag_id"] != nil {
|
||
skus[0]["weight"] = storeSku.Weight // weight字段仅限服饰鞋帽、美妆、日用品、母婴、生鲜果蔬、生活超市下的便利店/超市门店品类的商家使用
|
||
}
|
||
}
|
||
if globals.EnableMtwmStoreWrite {
|
||
err = api.MtwmAPI.RetailBatchInitData(vendorStoreID, foodDataList)
|
||
}
|
||
if err == nil {
|
||
for _, storeSku := range storeSkuList {
|
||
storeSku.VendorSkuID = utils.Int2Str(storeSku.SkuID)
|
||
}
|
||
}
|
||
return err
|
||
}
|
||
|
||
func (p *PurchaseHandler) DeleteStoreSkus(ctx *jxcontext.Context, storeID int, vendorStoreID string, storeSkuList []*partner.StoreSkuInfo) (err error) {
|
||
if globals.EnableMtwmStoreWrite {
|
||
if len(storeSkuList) == 1 {
|
||
err = api.MtwmAPI.RetailDelete(vendorStoreID, storeSkuList[0].VendorSkuID)
|
||
} else {
|
||
err = api.MtwmAPI.RetailCatSkuBatchDelete(vendorStoreID, nil, nil, partner.BareStoreSkuInfoList(storeSkuList).GetVendorSkuIDList())
|
||
}
|
||
if mtwmapi.IsErrSkuNotExist(err) {
|
||
err = nil
|
||
}
|
||
}
|
||
return err
|
||
}
|
||
|
||
func (p *PurchaseHandler) UpdateStoreSkusStatus(ctx *jxcontext.Context, storeID int, vendorStoreID string, storeSkuList []*partner.StoreSkuInfo) (err error) {
|
||
var validSkus, invalidSkus []*mtwmapi.BareStoreFoodInfo
|
||
for _, storeSku := range storeSkuList {
|
||
skuInfo := &mtwmapi.BareStoreFoodInfo{
|
||
AppFoodCode: storeSku.VendorSkuID,
|
||
Skus: []*mtwmapi.BareStoreSkuInfo{
|
||
&mtwmapi.BareStoreSkuInfo{
|
||
SkuID: storeSku.VendorSkuID,
|
||
},
|
||
},
|
||
}
|
||
if storeSku.Status == model.SkuStatusNormal {
|
||
validSkus = append(validSkus, skuInfo)
|
||
} else {
|
||
invalidSkus = append(invalidSkus, skuInfo)
|
||
}
|
||
}
|
||
if globals.EnableMtwmStoreWrite {
|
||
if len(invalidSkus) > 0 {
|
||
err = api.MtwmAPI.RetailSkuSellStatus2(vendorStoreID, invalidSkus, 1)
|
||
}
|
||
if err == nil && len(validSkus) > 0 {
|
||
err = api.MtwmAPI.RetailSkuSellStatus2(vendorStoreID, validSkus, 0)
|
||
}
|
||
}
|
||
return err
|
||
}
|
||
|
||
func (p *PurchaseHandler) UpdateStoreSkusPrice(ctx *jxcontext.Context, storeID int, vendorStoreID string, storeSkuList []*partner.StoreSkuInfo) (err error) {
|
||
var priceList []*mtwmapi.BareStoreFoodInfo
|
||
for _, storeSku := range storeSkuList {
|
||
skuInfo := &mtwmapi.BareStoreFoodInfo{
|
||
AppFoodCode: storeSku.VendorSkuID,
|
||
Skus: []*mtwmapi.BareStoreSkuInfo{
|
||
&mtwmapi.BareStoreSkuInfo{
|
||
SkuID: storeSku.VendorSkuID,
|
||
Price: utils.Int64ToStr(storeSku.Price),
|
||
},
|
||
},
|
||
}
|
||
priceList = append(priceList, skuInfo)
|
||
}
|
||
if globals.EnableMtwmStoreWrite {
|
||
err = api.MtwmAPI.RetailSkuPrice(vendorStoreID, priceList)
|
||
}
|
||
return err
|
||
}
|
||
|
||
func (p *PurchaseHandler) UpdateStoreSkusStock(ctx *jxcontext.Context, storeID int, vendorStoreID string, storeSkuList []*partner.StoreSkuInfo) (err error) {
|
||
var stockList []*mtwmapi.BareStoreFoodInfo
|
||
for _, storeSku := range storeSkuList {
|
||
skuInfo := &mtwmapi.BareStoreFoodInfo{
|
||
AppFoodCode: storeSku.VendorSkuID,
|
||
Skus: []*mtwmapi.BareStoreSkuInfo{
|
||
&mtwmapi.BareStoreSkuInfo{
|
||
SkuID: storeSku.VendorSkuID,
|
||
Stock: utils.Int2Str(storeSku.Stock),
|
||
},
|
||
},
|
||
}
|
||
stockList = append(stockList, skuInfo)
|
||
}
|
||
if globals.EnableMtwmStoreWrite {
|
||
err = api.MtwmAPI.RetailSkuStock(vendorStoreID, stockList)
|
||
}
|
||
return err
|
||
}
|
||
|
||
func (p *PurchaseHandler) GetStoreSkusBareInfo(ctx *jxcontext.Context, parentTask tasksch.ITask, storeID int, vendorStoreID string, inStoreSkuList []*partner.StoreSkuInfo) (outStoreSkuList []*partner.StoreSkuInfo, err error) {
|
||
vendorSkuIDList := partner.BareStoreSkuInfoList(inStoreSkuList).GetVendorSkuIDList()
|
||
var vendorFoodList []*mtwmapi.AppFood
|
||
if len(vendorSkuIDList) > 1 {
|
||
task := tasksch.NewParallelTask("获取饿百平台门店商品信息", nil, ctx,
|
||
func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
|
||
vendorSkuID := batchItemList[0].(string)
|
||
skuInfo, err := api.MtwmAPI.RetailGet(vendorStoreID, vendorSkuID)
|
||
if err == nil {
|
||
vendorFoodList = []*mtwmapi.AppFood{skuInfo}
|
||
return vendorFoodList, nil
|
||
}
|
||
return nil, err
|
||
}, vendorSkuIDList)
|
||
tasksch.HandleTask(task, parentTask, false).Run()
|
||
_, err = task.GetResult(0)
|
||
} else if len(vendorSkuIDList) == 1 {
|
||
skuInfo, err2 := api.MtwmAPI.RetailGet(vendorStoreID, vendorSkuIDList[0])
|
||
if err = err2; err == nil {
|
||
vendorFoodList = []*mtwmapi.AppFood{skuInfo}
|
||
}
|
||
} else {
|
||
return nil, nil
|
||
}
|
||
if err == nil {
|
||
storeSkuMap := make(map[string]*partner.StoreSkuInfo)
|
||
for _, v := range inStoreSkuList {
|
||
storeSkuMap[v.VendorSkuID] = v
|
||
}
|
||
for _, foodInfo := range vendorFoodList {
|
||
vendorSku := foodInfo.SkuList[0]
|
||
storeSku := storeSkuMap[vendorSku.SkuID]
|
||
storeSku.Stock = int(utils.Str2Int64WithDefault(vendorSku.Stock, 0))
|
||
storeSku.Price = jxutils.StandardPrice2Int(utils.Str2Float64WithDefault(vendorSku.Price, 0))
|
||
storeSku.Status = mtwmSkuStatus2Jx(foodInfo.IsSoldOut)
|
||
outStoreSkuList = append(outStoreSkuList, storeSku)
|
||
}
|
||
}
|
||
return outStoreSkuList, err
|
||
}
|
||
|
||
func mtwmSkuStatus2Jx(mtwmSkuStatus int) (jxSkuStatus int) {
|
||
if mtwmSkuStatus == 0 {
|
||
jxSkuStatus = model.SkuStatusNormal
|
||
} else {
|
||
jxSkuStatus = model.SkuStatusDontSale
|
||
}
|
||
return jxSkuStatus
|
||
}
|
||
|
||
func (p *PurchaseHandler) GetStoreSkusFullInfo(ctx *jxcontext.Context, parentTask tasksch.ITask, storeID int, vendorStoreID string, skuIDs []int) (skuNameList []*partner.SkuNameInfo, err error) {
|
||
if len(skuIDs) == 0 {
|
||
for {
|
||
result, err := api.MtwmAPI.RetailList(vendorStoreID, len(skuNameList), mtwmapi.GeneralMaxLimit)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
skuNameList = append(skuNameList, vendorSkuList2Jx(result)...)
|
||
if len(result) < mtwmapi.GeneralMaxLimit {
|
||
break
|
||
}
|
||
}
|
||
} else {
|
||
skuInfo, err := api.MtwmAPI.RetailGet(vendorStoreID, utils.Int2Str(skuIDs[0]))
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
skuNameList = append(skuNameList, vendorSku2Jx(skuInfo))
|
||
}
|
||
return skuNameList, err
|
||
}
|
||
|
||
func vendorSku2Jx(vendorSku *mtwmapi.AppFood) (skuName *partner.SkuNameInfo) {
|
||
prefix, name, comment, specUnit, unit, specQuality := jxutils.SplitSkuName(vendorSku.Name)
|
||
mtwmSku := vendorSku.SkuList[0]
|
||
weight := mtwmSku.Weight
|
||
if weight <= 0 {
|
||
weight = jxutils.FormatSkuWeight(specQuality, specUnit)
|
||
}
|
||
skuName = &partner.SkuNameInfo{
|
||
Prefix: prefix,
|
||
Name: name,
|
||
Unit: unit,
|
||
SkuList: []*partner.SkuInfo{
|
||
&partner.SkuInfo{
|
||
StoreSkuInfo: partner.StoreSkuInfo{
|
||
VendorSkuID: mtwmSku.SkuID,
|
||
SkuID: int(utils.Str2Int64WithDefault(mtwmSku.SkuID, 0)),
|
||
|
||
Stock: int(utils.Str2Int64WithDefault(mtwmSku.Stock, 0)),
|
||
Price: jxutils.StandardPrice2Int(utils.Str2Float64WithDefault(mtwmSku.Price, 0)),
|
||
Status: mtwmSkuStatus2Jx(vendorSku.IsSoldOut),
|
||
},
|
||
SkuName: vendorSku.Name,
|
||
Comment: comment,
|
||
SpecQuality: float64(specQuality),
|
||
SpecUnit: specUnit,
|
||
Weight: weight,
|
||
},
|
||
},
|
||
PictureList: vendorSku.PictureList,
|
||
}
|
||
if vendorSku.CategoryName != "" {
|
||
skuName.VendorCatIDList = []string{vendorSku.CategoryName}
|
||
if vendorSku.SecondaryCategoryName != "" {
|
||
skuName.VendorCatIDList = append(skuName.VendorCatIDList, vendorSku.SecondaryCategoryName)
|
||
}
|
||
}
|
||
return skuName
|
||
}
|
||
|
||
func vendorSkuList2Jx(vendorSkuList []*mtwmapi.AppFood) (skuNameList []*partner.SkuNameInfo) {
|
||
for _, vendorSku := range vendorSkuList {
|
||
skuNameList = append(skuNameList, vendorSku2Jx(vendorSku))
|
||
}
|
||
return skuNameList
|
||
}
|