From ba680cdf8ec253b7a37ae655636ab79e1c4035a3 Mon Sep 17 00:00:00 2001 From: gazebo Date: Wed, 26 Sep 2018 12:18:59 +0800 Subject: [PATCH] - use skuName price as unit price for storesku. --- business/jxstore/cms/cms.go | 5 ++ business/jxstore/cms/store_sku.go | 6 +- business/jxutils/jxutils_cms.go | 7 ++ business/partner/purchase/ebai/store_sku.go | 89 +++++++++++++++++---- 4 files changed, 87 insertions(+), 20 deletions(-) diff --git a/business/jxstore/cms/cms.go b/business/jxstore/cms/cms.go index 371f70b17..b4f3ed751 100644 --- a/business/jxstore/cms/cms.go +++ b/business/jxstore/cms/cms.go @@ -3,6 +3,7 @@ package cms import ( "crypto/md5" "fmt" + "math" "strconv" "time" @@ -132,3 +133,7 @@ func genPicFileName(suffix string) string { func genTmpID() int64 { return time.Now().UnixNano() / 1000000 } + +func isFakeVendorThingID(id int64) bool { + return id == 0 || (int(math.Log10(float64(id))) == int(math.Log10(float64(genTmpID())))) +} diff --git a/business/jxstore/cms/store_sku.go b/business/jxstore/cms/store_sku.go index 798067c1f..1fe0178e4 100644 --- a/business/jxstore/cms/store_sku.go +++ b/business/jxstore/cms/store_sku.go @@ -173,10 +173,10 @@ func GetStoreSkus(storeID int, isFocused bool, keyword string, params map[string t1.img, t1.elm_img_hash_code, CONCAT("[", GROUP_CONCAT(DISTINCT CONCAT('{"id":', t2.id, ',"status":', t2.status, ',"createdAt":"', - CONCAT(REPLACE(t4.created_at," ","T"),"+08:00"), '","updatedAt":"', CONCAT(REPLACE(t4.updated_at," ","T"),"+08:00"), - '","lastOperator":"', t4.last_operator, '","specQuality":', t2.spec_quality, ',"specUnit":"', t2.spec_unit, '","weight":', t2.weight, + CONCAT(REPLACE(IF(t4.created_at IS NULL, '1970-01-01 00:00:00', t4.created_at)," ","T"),"+08:00"), '","updatedAt":"', CONCAT(REPLACE(IF(t4.updated_at IS NULL, '1970-01-01 00:00:00', t4.updated_at)," ","T"),"+08:00"), + '","lastOperator":"', IF(t4.last_operator IS NULL, '', t4.last_operator), '","specQuality":', t2.spec_quality, ',"specUnit":"', t2.spec_unit, '","weight":', t2.weight, ',"jdID":', t2.jd_id, ',"categoryID":', t2.category_id, ',"nameID":', t2.name_id, ',"subStoreID":', IF(t4.sub_store_id IS NULL, 0, t4.sub_store_id), - ',"price":', IF(t4.price IS NULL, 0, t4.price), ',"unitPrice":', IF(t4.unit_price IS NULL, 0, t4.unit_price), + ',"price":', IF(t4.price IS NULL, 0, t4.price), ',"unitPrice":', IF(t4.unit_price IS NULL, t1.price, t4.unit_price), ',"storeSkuStatus":', IF(t4.status IS NULL, 0, t4.status), "}")), "]") skus_str ` + sql + ` ORDER BY t1.id diff --git a/business/jxutils/jxutils_cms.go b/business/jxutils/jxutils_cms.go index b71d3f91a..99bf1c636 100644 --- a/business/jxutils/jxutils_cms.go +++ b/business/jxutils/jxutils_cms.go @@ -18,6 +18,13 @@ func MergeStoreStatus(status int, vendorStatus int) int { return vendorStatus } +func MergeSkuStatus(skuStatus int, storeSkuStatus int) int { + if skuStatus < storeSkuStatus { + return skuStatus + } + return storeSkuStatus +} + func SplitSlice(list interface{}, batchCount int) (listInList [][]interface{}) { typeInfo := reflect.TypeOf(list) if typeInfo.Kind() != reflect.Slice { diff --git a/business/partner/purchase/ebai/store_sku.go b/business/partner/purchase/ebai/store_sku.go index e9d3ebb05..a5f9b2d07 100644 --- a/business/partner/purchase/ebai/store_sku.go +++ b/business/partner/purchase/ebai/store_sku.go @@ -3,9 +3,11 @@ package ebai import ( "git.rosy.net.cn/baseapi/platformapi/ebaiapi" "git.rosy.net.cn/baseapi/utils" + "git.rosy.net.cn/jx-callback/business/jxutils" "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/globals" "git.rosy.net.cn/jx-callback/globals/api" ) @@ -38,9 +40,27 @@ type tStoreSkuFullInfo struct { ParentCatEbaiID int64 `orm:"column(parent_cat_ebai_id)"` ParentCatEbaiSyncStatus int ParentCatLevel int + + EbaiCat1ID string `orm:"column(ebai_cat1_id)"` + EbaiCat2ID string `orm:"column(ebai_cat2_id)"` + EbaiCat3ID string `orm:"column(ebai_cat3_id)"` } +var ( + defCatMap = map[int]string{ + 1: "151301831158987", + 2: "15347484581335", + 3: "15347484581339", + } +) + func (p *PurchaseHandler) SyncStoreSkus(db *dao.DaoDB, storeIDs []int, skuIDs []int, isForce bool, userName string) (err error) { + for _, storeID := range storeIDs { + err = p.syncOneStoreSkus(db, storeID, skuIDs, isForce, userName) + if err != nil { + break + } + } return err } @@ -51,7 +71,8 @@ func (p *PurchaseHandler) syncOneStoreSkus(db *dao.DaoDB, storeID int, skuIDs [] t4.name cat_name, t4.ebai_category_id, t4.id cat_id, t4.level cat_level, t5.ebai_id cat_ebai_id, t5.ebai_sync_status cat_ebai_sync_status, t4p.name parent_cat_name, - t4p.id parent_cat_id, t4p.level parent_cat_level, t5p.ebai_id parent_cat_ebai_id, t5p.ebai_sync_status parent_cat_ebai_sync_status + t4p.id parent_cat_id, t4p.level parent_cat_level, t5p.ebai_id parent_cat_ebai_id, t5p.ebai_sync_status parent_cat_ebai_sync_status, + cat1.vendor_category_id ebai_cat1_id, cat2.vendor_category_id ebai_cat2_id, cat2.parent_id ebai_cat3_id FROM store_sku_bind t1 JOIN sku t2 ON t1.sku_id = t2.id JOIN sku_name t3 ON t2.name_id = t3.id @@ -59,9 +80,15 @@ func (p *PurchaseHandler) syncOneStoreSkus(db *dao.DaoDB, storeID int, skuIDs [] JOIN sku_category t4p ON t4p.id = t4.parent_id LEFT JOIN store_sku_category_map t5 ON t5.store_id = t1.store_id AND t5.sku_category_id = t4.id LEFT JOIN store_sku_category_map t5p ON t5p.store_id = t1.store_id AND t5p.sku_category_id = t4p.id + + LEFT JOIN sku_vendor_category cat1 ON t3.category_id = cat1.vendor_category_id AND cat1.vendor_id = ? + LEFT JOIN sku_vendor_category cat2 ON cat1.parent_id = cat2.vendor_category_id AND cat1.vendor_id = ? + WHERE t1.store_id = ? AND (t1.ebai_sync_status <> 0) ` sqlParams := []interface{}{ + model.VendorIDEBAI, + model.VendorIDEBAI, storeID, } if skuIDs != nil && len(skuIDs) > 0 { @@ -71,6 +98,7 @@ func (p *PurchaseHandler) syncOneStoreSkus(db *dao.DaoDB, storeID int, skuIDs [] strStoreID := utils.Int2Str(storeID) var storeSkuInfoList []*tStoreSkuFullInfo if err = dao.GetRows(db, &storeSkuInfoList, sql, sqlParams...); err == nil { + globals.SugarLogger.Debug(utils.Format4Output(storeSkuInfoList, false)) level1CatList2Add := make(map[string]interface{}) level2CatList2Add := make(map[string]interface{}) for _, storeSku := range storeSkuInfoList { @@ -79,22 +107,24 @@ func (p *PurchaseHandler) syncOneStoreSkus(db *dao.DaoDB, storeID int, skuIDs [] level1CatList2Add[utils.Int2Str(storeSku.ParentCatID)] = map[string]interface{}{ ebaiapi.KeyShopID: strStoreID, "parent_category_id": 0, - "name": storeSku.ParentCatName, - "shop_custom_id": storeSku.ParentCatID, + "name": utils.FilterMb4(storeSku.ParentCatName), + "shop_custom_id": utils.Int2Str(storeSku.ParentCatID), "rank": storeSku.ParentCatLevel + 1, // 饿百是从1开始 } } } } + globals.SugarLogger.Debug(utils.Format4Output(level1CatList2Add, false)) level1CatList := utils.MapKV2List(level1CatList2Add) task := tasksch.RunTask("syncOneStoreSkus level1 cat", false, nil, 0, 1, userName, func(batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) { cat := batchItemList[0].(map[string]interface{}) + globals.SugarLogger.Debug(strStoreID, 0, cat["name"].(string), cat["rank"].(int), cat["shop_custom_id"].(string)) ebaiCatID, err2 := api.EbaiAPI.ShopCategoryCreate(strStoreID, 0, cat["name"].(string), cat["rank"].(int), cat["shop_custom_id"].(string)) - if err2 == nil { + if err = err2; err == nil { cat["category_id"] = ebaiCatID return nil, nil } - return nil, err2 + return nil, err }, level1CatList) if _, err = task.GetResult(0); err == nil { for _, storeSku := range storeSkuInfoList { @@ -103,8 +133,8 @@ func (p *PurchaseHandler) syncOneStoreSkus(db *dao.DaoDB, storeID int, skuIDs [] level2CatList2Add[utils.Int2Str(storeSku.CatID)] = map[string]interface{}{ ebaiapi.KeyShopID: strStoreID, "parent_category_id": level1CatList2Add[utils.Int2Str(storeSku.ParentCatID)].(map[string]interface{})["category_id"], - "name": storeSku.CatName, - "shop_custom_id": storeSku.CatID, + "name": utils.FilterMb4(storeSku.CatName), + "shop_custom_id": utils.Int2Str(storeSku.CatID), "rank": storeSku.CatLevel + 1, // 饿百是从1开始 } } @@ -114,11 +144,11 @@ func (p *PurchaseHandler) syncOneStoreSkus(db *dao.DaoDB, storeID int, skuIDs [] task = tasksch.RunTask("syncOneStoreSkus level2 cat", false, nil, 0, 1, userName, func(batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) { cat := batchItemList[0].(map[string]interface{}) ebaiCatID, err2 := api.EbaiAPI.ShopCategoryCreate(strStoreID, 0, cat["name"].(string), cat["rank"].(int), cat["shop_custom_id"].(string)) - if err2 == nil { + if err = err2; err == nil { cat["category_id"] = ebaiCatID return nil, nil } - return nil, err2 + return nil, err }, level2CatList) if _, err = task.GetResult(0); err == nil { task = tasksch.RunTask("syncOneStoreSkus skus", false, nil, 0, 1, userName, func(batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) { @@ -133,10 +163,10 @@ func (p *PurchaseHandler) syncOneStoreSkus(db *dao.DaoDB, storeID int, skuIDs [] if storeSku.EbaiSyncStatus&model.SyncFlagDeletedMask != 0 { err = api.EbaiAPI.SkuDelete(strStoreID, utils.Int64ToStr(storeSku.EbaiID)) } else if storeSku.EbaiSyncStatus&model.SyncFlagNewMask != 0 { - storeSku.EbaiID, err = api.EbaiAPI.SkuCreate(strStoreID, storeSku.SkuID, map[string]interface{}{}) + storeSku.EbaiID, err = api.EbaiAPI.SkuCreate(strStoreID, storeSku.SkuID, genSkuParamsFromStoreSkuInfo(storeSku)) updateFields = append(updateFields, model.FieldEbaiID) } else if storeSku.EbaiSyncStatus&model.SyncFlagModifiedMask != 0 { - _, err = api.EbaiAPI.SkuUpdate(strStoreID, map[string]interface{}{}) + _, err = api.EbaiAPI.SkuUpdate(strStoreID, genSkuParamsFromStoreSkuInfo(storeSku)) } if err == nil { _, err = dao.UpdateEntity(nil, &storeSku.StoreSkuBind, updateFields...) @@ -177,9 +207,34 @@ func (p *PurchaseHandler) ReadStoreSku(storeID, skuID int) (skuNameExt *model.Sk return skuNameExt, err } -// func genSkuParamsFromStoreSkuInfo(storeSku *tStoreSkuFullInfo) map[string]interface{} { -// return map[string]interface{}{ -// "" -// } -// return nil -// } +func genSkuParamsFromStoreSkuInfo(storeSku *tStoreSkuFullInfo) map[string]interface{} { + return map[string]interface{}{ + "name": jxutils.ComposeSkuName(storeSku.Prefix, storeSku.Name, storeSku.Comment, storeSku.Unit, storeSku.SpecQuality, storeSku.SpecUnit, 0), + "status": jxSkuStatus2Ebai(jxutils.MergeSkuStatus(storeSku.SkuStatus, storeSku.Status)), + "left_num": ebaiapi.MaxLeftNum, + "sale_price": storeSku.Price, + "cat1": getEbaiCat(storeSku.EbaiCat1ID, 1), + "cat2": getEbaiCat(storeSku.EbaiCat2ID, 2), + "cat3": getEbaiCat(storeSku.EbaiCat3ID, 3), + "photos": []map[string]interface{}{ + map[string]interface{}{ + "is_master": true, + "url": storeSku.Img, + }, + }, + } +} + +func jxSkuStatus2Ebai(status int) int { + if status <= 0 { + return ebaiapi.SkuStatusOffline + } + return ebaiapi.SkuStatusOnline +} + +func getEbaiCat(catID string, level int) string { + if catID == "" { + return defCatMap[level] + } + return catID +}