新SyncSkus基本能工作
This commit is contained in:
224
business/partner/purchase/jd/sku2.go
Normal file
224
business/partner/purchase/jd/sku2.go
Normal file
@@ -0,0 +1,224 @@
|
||||
package jd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.rosy.net.cn/baseapi/platformapi/jdapi"
|
||||
"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/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"
|
||||
"github.com/astaxie/beego"
|
||||
)
|
||||
|
||||
const (
|
||||
DefBrandID = 35247
|
||||
|
||||
DefJdCategoryID = 20362
|
||||
DefJdCategoryID4Jxgy = 22410 // 其他国产水果
|
||||
)
|
||||
|
||||
func getDefJdCategoryID() int {
|
||||
if beego.BConfig.RunMode == "jxgy" {
|
||||
return DefJdCategoryID4Jxgy
|
||||
}
|
||||
return DefJdCategoryID
|
||||
}
|
||||
|
||||
func jdCat2Jx(jdCat *jdapi.CategoryInfo) (jxCat *partner.BareCategoryInfo) {
|
||||
return &partner.BareCategoryInfo{
|
||||
VendorCatID: utils.Int64ToStr(jdCat.Id),
|
||||
Level: jdCat.Level,
|
||||
Name: jdCat.Name,
|
||||
Seq: jdCat.Sort,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *PurchaseHandler) GetAllCategories(ctx *jxcontext.Context, vendorOrgCode string) (cats []*partner.BareCategoryInfo, err error) {
|
||||
result, err := getAPI(vendorOrgCode).QueryCategoriesByOrgCode()
|
||||
if err == nil {
|
||||
catMap := make(map[int64]*partner.BareCategoryInfo)
|
||||
level := 1
|
||||
for {
|
||||
processedCount := 0
|
||||
for _, jdCat := range result {
|
||||
if jdCat.Level == level {
|
||||
processedCount++
|
||||
jxCat := jdCat2Jx(jdCat)
|
||||
if level == 1 {
|
||||
cats = append(cats, jxCat)
|
||||
} else {
|
||||
parentCat := catMap[jdCat.ParentId]
|
||||
if parentCat != nil {
|
||||
parentCat.Children = append(parentCat.Children, jxCat)
|
||||
}
|
||||
}
|
||||
catMap[jdCat.Id] = jxCat
|
||||
}
|
||||
}
|
||||
if processedCount == 0 {
|
||||
break
|
||||
}
|
||||
level++
|
||||
}
|
||||
}
|
||||
return cats, err
|
||||
}
|
||||
|
||||
func (p *PurchaseHandler) CreateCategory2(ctx *jxcontext.Context, cat *dao.SkuStoreCatInfo) (err error) {
|
||||
if globals.EnableJdStoreWrite {
|
||||
result, err2 := getAPI(cat.VendorOrgCode).AddShopCategory(utils.Str2Int64(cat.ParentVendorCatID), cat.Name, int(cat.Level), cat.Seq, ctx.GetUserName())
|
||||
if err = err2; err == nil {
|
||||
if jdID := utils.Str2Int64WithDefault(result, 0); jdID != 0 {
|
||||
cat.VendorCatID = utils.Int64ToStr(jdID)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
cat.VendorCatID = utils.Int64ToStr(jxutils.GenFakeID())
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (p *PurchaseHandler) UpdateCategory2(ctx *jxcontext.Context, cat *dao.SkuStoreCatInfo) (err error) {
|
||||
if globals.EnableJdStoreWrite {
|
||||
err = getAPI(cat.VendorOrgCode).UpdateShopCategory(utils.Str2Int64(cat.VendorCatID), cat.Name)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (p *PurchaseHandler) DeleteCategory2(ctx *jxcontext.Context, cat *dao.SkuStoreCatInfo) (err error) {
|
||||
if globals.EnableJdStoreWrite {
|
||||
err = getAPI(cat.VendorOrgCode).DelShopCategory(utils.Str2Int64(cat.VendorCatID))
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (p *PurchaseHandler) ReorderCategories2(ctx *jxcontext.Context, vendorOrgCode, vendorParentCatID string, vendorCatIDList []string) (err error) {
|
||||
if globals.EnableJdStoreWrite {
|
||||
err = getAPI(vendorOrgCode).ChangeShopCategoryOrder(utils.Str2Int64(vendorParentCatID), utils.StringSlice2Int64(vendorCatIDList))
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (p *PurchaseHandler) getVendorCategories(level int, pid int64) (vendorCats []*model.SkuVendorCategory, err error) {
|
||||
cats, err := getAPI("").QueryChildCategoriesForOP(pid)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, v := range cats {
|
||||
if v.Status == 1 {
|
||||
cat := &model.SkuVendorCategory{
|
||||
VendorID: model.VendorIDJD,
|
||||
Name: v.Name,
|
||||
Level: level,
|
||||
VendorCategoryID: utils.Int64ToStr(v.Id),
|
||||
}
|
||||
if level > 1 {
|
||||
cat.ParentID = utils.Int64ToStr(v.ParentId)
|
||||
if level == 3 {
|
||||
cat.IsLeaf = 1
|
||||
}
|
||||
}
|
||||
vendorCats = append(vendorCats, cat)
|
||||
if level < 3 {
|
||||
childVendorCats, err2 := p.getVendorCategories(level+1, v.Id)
|
||||
if err2 == nil && len(childVendorCats) > 0 {
|
||||
vendorCats = append(vendorCats, childVendorCats...)
|
||||
} else {
|
||||
cat.IsLeaf = 1
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return vendorCats, nil
|
||||
}
|
||||
|
||||
func (p *PurchaseHandler) GetVendorCategories(ctx *jxcontext.Context) (vendorCats []*model.SkuVendorCategory, err error) {
|
||||
vendorCats, err = p.getVendorCategories(1, 0)
|
||||
return vendorCats, err
|
||||
}
|
||||
|
||||
func skuInfo2Param(ctx *jxcontext.Context, sku *dao.StoreSkuSyncInfo) (param *jdapi.OpSkuParam) {
|
||||
param = &jdapi.OpSkuParam{
|
||||
TraceID: ctx.GetTrackInfo(),
|
||||
OutSkuID: utils.Int2Str(sku.SkuID),
|
||||
ShopCategories: []int64{utils.Str2Int64(sku.VendorCatID)},
|
||||
CategoryID: sku.VendorVendorCatID,
|
||||
BrandID: DefBrandID,
|
||||
SkuName: utils.LimitUTF8StringLen(sku.SkuName, jdapi.MaxSkuNameCharCount),
|
||||
SkuPrice: int(sku.Price),
|
||||
Weight: float64(jxutils.IntWeight2Float(sku.Weight)),
|
||||
FixedStatus: jxStatus2jdStatus(sku.MergedStatus),
|
||||
IsSale: jdapi.IsSaleNo, // todo ?
|
||||
|
||||
Upc: sku.Upc,
|
||||
Images: jxutils.BatchString2Slice(sku.Img, sku.Img2),
|
||||
}
|
||||
if param.CategoryID == 0 {
|
||||
param.CategoryID = int64(getDefJdCategoryID())
|
||||
}
|
||||
if sku.IsGlobal == 0 && len(sku.SellCities) > 0 {
|
||||
param.SellCities = utils.StringSlice2Int64(sku.SellCities)
|
||||
}
|
||||
if sku.DescImg != "" {
|
||||
param.ProductDesc = fmt.Sprintf(`<img src="%s" alt="一张图片" />`, sku.DescImg)
|
||||
}
|
||||
return param
|
||||
}
|
||||
|
||||
func (p *PurchaseHandler) CreateSku2(ctx *jxcontext.Context, sku *dao.StoreSkuSyncInfo) (err error) {
|
||||
param := skuInfo2Param(ctx, sku)
|
||||
if globals.EnableJdStoreWrite {
|
||||
sku.VendorSkuID, err = getAPI(sku.VendorOrgCode).AddSku2(param)
|
||||
} else {
|
||||
sku.VendorSkuID = utils.Int64ToStr(jxutils.GenFakeID())
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (p *PurchaseHandler) UpdateSku2(ctx *jxcontext.Context, sku *dao.StoreSkuSyncInfo) (err error) {
|
||||
param := skuInfo2Param(ctx, sku)
|
||||
if globals.EnableJdStoreWrite {
|
||||
_, err = getAPI(sku.VendorOrgCode).UpdateSku2(param)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (p *PurchaseHandler) DeleteSku2(ctx *jxcontext.Context, sku *dao.StoreSkuSyncInfo) (err error) {
|
||||
param := &jdapi.OpSkuParam{
|
||||
TraceID: ctx.GetTrackInfo(),
|
||||
OutSkuID: utils.Int2Str(sku.SkuID),
|
||||
FixedStatus: jdapi.SkuFixedStatusDeleted,
|
||||
}
|
||||
if globals.EnableJdStoreWrite {
|
||||
_, err = getAPI(sku.VendorOrgCode).UpdateSku2(param)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func jdStatus2jxStatus(jdStatus int) (jxStatus int) {
|
||||
switch jdStatus {
|
||||
case jdapi.SkuFixedStatusOnline:
|
||||
jxStatus = model.SkuStatusNormal
|
||||
case jdapi.SkuFixedStatusOffline:
|
||||
jxStatus = model.SkuStatusDontSale
|
||||
case jdapi.SkuFixedStatusDeleted:
|
||||
jxStatus = model.SkuStatusDeleted
|
||||
}
|
||||
return jxStatus
|
||||
}
|
||||
|
||||
func jxStatus2jdStatus(jxStatus int) (jdStatus int) {
|
||||
switch jxStatus {
|
||||
case model.SkuStatusNormal:
|
||||
jdStatus = jdapi.SkuFixedStatusOnline
|
||||
case model.SkuStatusDontSale:
|
||||
jdStatus = jdapi.SkuFixedStatusOffline
|
||||
case model.SkuStatusDeleted:
|
||||
jdStatus = jdapi.SkuFixedStatusDeleted
|
||||
}
|
||||
return jdStatus
|
||||
}
|
||||
Reference in New Issue
Block a user