- SplitSkuName don't use comment as name
- add DownloadFileByURL - refactor UploadWeimobImg4SkuName - ignore RefreshMissingDadaStores error - add BuildSkuFromEbaiStore
This commit is contained in:
@@ -1,20 +1,28 @@
|
||||
package initdata
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/md5"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"git.rosy.net.cn/baseapi/platformapi/autonavi"
|
||||
"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/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/purchase/jd"
|
||||
"git.rosy.net.cn/jx-callback/business/partner/purchase/mtwm"
|
||||
"git.rosy.net.cn/jx-callback/globals"
|
||||
"git.rosy.net.cn/jx-callback/globals/api"
|
||||
"github.com/qiniu/api.v7/storage"
|
||||
)
|
||||
|
||||
func TruncateTable(db *dao.DaoDB, tableName string) (err error) {
|
||||
@@ -202,8 +210,10 @@ func UploadWeimobImg4SkuName(ctx *jxcontext.Context, nameIDs []int, isAsync, isC
|
||||
rootTask := tasksch.NewParallelTask("UploadWeimobImg4SkuName", tasksch.NewParallelConfig().SetIsContinueWhenError(isContinueWhenError).SetParallelCount(5), ctx.GetUserName(), func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
|
||||
skuName := batchItemList[0].(*model.SkuName)
|
||||
if skuName.Img != "" && skuName.ImgWeimob == "" {
|
||||
if skuName.ImgWeimob, err = api.WeimobAPI.UploadImgByURL(skuName.Img, ""); err == nil {
|
||||
_, err = dao.UpdateEntity(db, skuName, "ImgWeimob")
|
||||
if globals.EnableWscStoreWrite {
|
||||
if skuName.ImgWeimob, err = api.WeimobAPI.UploadImgByURL(skuName.Img, ""); err == nil {
|
||||
_, err = dao.UpdateEntity(db, skuName, "ImgWeimob")
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil, err
|
||||
@@ -217,3 +227,216 @@ func UploadWeimobImg4SkuName(ctx *jxcontext.Context, nameIDs []int, isAsync, isC
|
||||
}
|
||||
return hint, err
|
||||
}
|
||||
|
||||
func getSkuNameKey(prefix, name, comment, specUnit, unit string, specQuality float32) string {
|
||||
return fmt.Sprintf("%s-%s-%f-%s-%s", prefix, name, specQuality, specUnit, unit)
|
||||
}
|
||||
|
||||
func BuildSkuFromEbaiStore(ctx *jxcontext.Context, baiduShopID int64, isAsync, isContinueWhenError bool) (hint string, err error) {
|
||||
ebaiCatIDKey := "ebaiCatID"
|
||||
catList, err := api.EbaiAPI.PageGetCustomCatList(baiduShopID)
|
||||
if err != nil {
|
||||
return hint, err
|
||||
}
|
||||
catMap := make(map[string]map[string]interface{})
|
||||
|
||||
allSkuInfo := make(map[string]map[string]map[string]map[string]interface{})
|
||||
for _, cat := range catList {
|
||||
catName := utils.Interface2String(cat["cat_name"])
|
||||
if catName == "限时特惠" || catName == "应季热销" {
|
||||
catName = "应季水果"
|
||||
}
|
||||
skuNameMap := allSkuInfo[catName]
|
||||
if skuNameMap == nil {
|
||||
skuNameMap = make(map[string]map[string]map[string]interface{})
|
||||
allSkuInfo[catName] = skuNameMap
|
||||
}
|
||||
if catMap[catName] == nil {
|
||||
catMap[catName] = cat
|
||||
}
|
||||
skuList, err2 := api.EbaiAPI.PageGetCustomSkuList(baiduShopID, utils.Str2Int64(utils.Interface2String(cat["cat_id"])))
|
||||
if err = err2; err != nil {
|
||||
return hint, err
|
||||
}
|
||||
for _, sku := range skuList {
|
||||
_, _, _, specUnit, _, specQuality := jxutils.SplitSkuName(sku["upc_name"].(string))
|
||||
if specQuality > 0 {
|
||||
if catMap[catName][ebaiCatIDKey] == nil {
|
||||
catMap[catName][ebaiCatIDKey] = utils.Interface2String(sku["cat3_id"])
|
||||
}
|
||||
skuNameKey := sku["photos"].([]interface{})[0].(map[string]interface{})["url"].(string)
|
||||
if skuNameMap[skuNameKey] == nil {
|
||||
skuNameMap[skuNameKey] = make(map[string]map[string]interface{})
|
||||
}
|
||||
skuNameMap[skuNameKey][fmt.Sprintf("%f.%s", specQuality, specUnit)] = sku
|
||||
}
|
||||
}
|
||||
}
|
||||
allSkuInfo2 := make(map[*model.SkuCategory][]*model.SkuNameExt)
|
||||
catSeq := 0
|
||||
for catName, catSkuNameMap := range allSkuInfo {
|
||||
if len(catSkuNameMap) > 0 {
|
||||
skuCat := &model.SkuCategory{
|
||||
Name: catName,
|
||||
ParentID: 0,
|
||||
Level: 1,
|
||||
Type: model.SkuCategoryNormal,
|
||||
Seq: catSeq,
|
||||
EbaiCategoryID: utils.Str2Int64(utils.Interface2String(catMap[catName][ebaiCatIDKey])),
|
||||
}
|
||||
catSeq++
|
||||
for _, skuList := range catSkuNameMap {
|
||||
var skuNameExt *model.SkuNameExt
|
||||
for _, sku := range skuList {
|
||||
prefix, name, comment, specUnit, unit, specQuality := jxutils.SplitSkuName(sku["upc_name"].(string))
|
||||
if skuNameExt == nil {
|
||||
skuNameExt = &model.SkuNameExt{
|
||||
SkuName: model.SkuName{
|
||||
Prefix: prefix,
|
||||
Name: name,
|
||||
// CategoryID:
|
||||
IsGlobal: 1,
|
||||
Unit: unit,
|
||||
SpecQuality: specQuality,
|
||||
SpecUnit: specUnit,
|
||||
Price: int(jxutils.StandardPrice2Int(utils.MustInterface2Float64(sku["sale_price"]))),
|
||||
Img: sku["photos"].([]interface{})[0].(map[string]interface{})["url"].(string),
|
||||
Status: model.SkuStatusNormal,
|
||||
IsSpu: 0,
|
||||
},
|
||||
}
|
||||
if len(skuList) > 1 {
|
||||
skuNameExt.Unit = "份"
|
||||
skuNameExt.SpecQuality = 500
|
||||
skuNameExt.SpecUnit = "g"
|
||||
}
|
||||
if skuNameExt.Unit == "份" {
|
||||
tmpSpecQuality := specQuality
|
||||
if specUnit == "kg" || specUnit == "l" {
|
||||
tmpSpecQuality *= 1000
|
||||
}
|
||||
skuNameExt.Price = int(float32(skuNameExt.Price) * 500 / tmpSpecQuality)
|
||||
}
|
||||
}
|
||||
mySku := &model.Sku{
|
||||
// CategoryID:
|
||||
// NameID:
|
||||
Comment: comment,
|
||||
SpecQuality: specQuality,
|
||||
SpecUnit: specUnit,
|
||||
Weight: int(utils.Str2Int64(utils.Interface2String(sku["weight"]))),
|
||||
Status: model.SkuStatusNormal,
|
||||
|
||||
LinkID: int(jxutils.StandardPrice2Int(utils.MustInterface2Float64(sku["sale_price"]))), // 临时传递价格用
|
||||
}
|
||||
if sku["enabled"].(string) == "0" {
|
||||
mySku.Status = model.SkuStatusDontSale
|
||||
}
|
||||
skuNameExt.Skus = append(skuNameExt.Skus, mySku)
|
||||
}
|
||||
allSkuInfo2[skuCat] = append(allSkuInfo2[skuCat], skuNameExt)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
startOutSkuID := 1000
|
||||
var skuCatList []*model.SkuCategory
|
||||
for cat := range allSkuInfo2 {
|
||||
skuCatList = append(skuCatList, cat)
|
||||
}
|
||||
putPolicy := storage.PutPolicy{
|
||||
Scope: globals.QiniuBucket,
|
||||
// Expires: 10 * 60,
|
||||
}
|
||||
cfg := &storage.Config{}
|
||||
upToken := putPolicy.UploadToken(api.QiniuAPI)
|
||||
rootTask := tasksch.NewSeqTask("BuildSkuFromEbaiStore", ctx.GetUserName(), func(rootTask *tasksch.SeqTask, step int, params ...interface{}) (result interface{}, err error) {
|
||||
var catID string
|
||||
cat := skuCatList[step]
|
||||
skuNameExtList := allSkuInfo2[cat]
|
||||
catID, err = addShopCategory(0, cat.Name, int(cat.Level), cat.Seq, ctx.GetUserName())
|
||||
if err == nil {
|
||||
shopCategories := []int64{utils.Str2Int64(catID)}
|
||||
for _, skuNameExt := range skuNameExtList {
|
||||
var imgContent []byte
|
||||
skuNameExt.CategoryID = cat.ID
|
||||
skuNameExt.Img = strings.Replace(skuNameExt.Img, "https://", "http://", 1)
|
||||
if imgContent, _, err = jxutils.DownloadFileByURL(skuNameExt.Img); err != nil {
|
||||
globals.SugarLogger.Infof("download pic %s failed with error:%v", skuNameExt.Img, err)
|
||||
if !isContinueWhenError {
|
||||
return nil, err
|
||||
}
|
||||
err = nil
|
||||
continue
|
||||
}
|
||||
formUploader := storage.NewFormUploader(cfg)
|
||||
ret := storage.PutRet{}
|
||||
key := jxutils.GenPicFileName(".jpeg")
|
||||
for i := 0; i < 3; i++ {
|
||||
if err = formUploader.Put(context.Background(), &ret, upToken, key, bytes.NewReader(imgContent), int64(len(imgContent)), nil); err == nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
globals.SugarLogger.Infof("upload pic %s failed with error:%v", skuNameExt.Img, err)
|
||||
if !isContinueWhenError {
|
||||
return nil, err
|
||||
}
|
||||
err = nil
|
||||
continue
|
||||
}
|
||||
qiniuImgURL := "http://image.jxc4.com/" + key + "?imageMogr2/thumbnail/x800/gravity/Center/crop/800x800"
|
||||
for _, sku := range skuNameExt.Skus {
|
||||
jdCatID := 20308 // 其他国产水果
|
||||
if cat.Name == "进口水果" {
|
||||
jdCatID = 20327 // 其他进口水果
|
||||
}
|
||||
price := sku.LinkID
|
||||
sku.LinkID = 0
|
||||
skuName := jxutils.ComposeSkuName(skuNameExt.Prefix, skuNameExt.Name, sku.Comment, skuNameExt.Unit, sku.SpecQuality, sku.SpecUnit, jdapi.MaxSkuNameLen)
|
||||
fixedStatus := 1
|
||||
if sku.Status != model.SkuStatusNormal {
|
||||
fixedStatus = 2
|
||||
}
|
||||
var vendorSkuID string
|
||||
vendorSkuID, err = addSku(utils.Int2Str(startOutSkuID), jdCatID, shopCategories, jd.DefBrandID, skuName, price, jxutils.IntWeight2Float(sku.Weight), []string{qiniuImgURL}, fixedStatus, true, nil)
|
||||
if err == nil {
|
||||
globals.SugarLogger.Debugf("vendorSkuID=%s", vendorSkuID)
|
||||
} else {
|
||||
globals.SugarLogger.Infof("create %s failed with error:%v", skuName, err)
|
||||
if !isContinueWhenError {
|
||||
return nil, err
|
||||
}
|
||||
err = nil
|
||||
}
|
||||
// fmt.Printf("%s,[%s]%s-%f-%s, %s, %s\n", cat.Name, skuNameExt.Prefix, skuNameExt.Name, skuNameExt.SpecQuality, skuNameExt.SpecUnit, qiniuImgURL, skuName)
|
||||
startOutSkuID++
|
||||
rootTask.Cancel()
|
||||
return nil, nil
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
return nil, err
|
||||
}, len(skuCatList))
|
||||
tasksch.ManageTask(rootTask).Run()
|
||||
if !isAsync {
|
||||
_, err = rootTask.GetResult(0)
|
||||
} else {
|
||||
hint = rootTask.ID
|
||||
}
|
||||
return hint, err
|
||||
}
|
||||
|
||||
func addShopCategory(pid int64, shopCategoryName string, shopCategoryLevel, sort int, userName string) (catId string, err error) {
|
||||
globals.SugarLogger.Debug(pid, shopCategoryName, shopCategoryLevel, sort, userName)
|
||||
return "123", err
|
||||
// return api.JdAPI.AddShopCategory(pid, shopCategoryName, shopCategoryLevel, sort, userName)
|
||||
}
|
||||
|
||||
func addSku(outSkuId string, cagtegoryId int, shopCategories []int64, brandId int, skuName string, skuPrice int, weight float32, images []string, fixedStatus int, isSale bool, addParams map[string]interface{}) (skuId string, err error) {
|
||||
globals.SugarLogger.Debug(outSkuId, cagtegoryId, shopCategories, brandId, skuName, skuPrice, weight, images, fixedStatus, isSale, addParams)
|
||||
return "456", err
|
||||
// return api.JdAPI.AddSku(outSkuId, cagtegoryId, shopCategories, brandId, skuName, skuPrice, weight, images, fixedStatus, isSale, addParams)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user