尽量减少对于api.JdAPI的直接依赖
This commit is contained in:
@@ -1,13 +1,9 @@
|
||||
package initdata
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"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/datares"
|
||||
@@ -16,10 +12,7 @@ import (
|
||||
"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/globals"
|
||||
"git.rosy.net.cn/jx-callback/globals/api"
|
||||
"github.com/qiniu/api.v7/storage"
|
||||
)
|
||||
|
||||
func TruncateTable(db *dao.DaoDB, tableName string) (err error) {
|
||||
@@ -142,107 +135,6 @@ func InitPlace(ctx *jxcontext.Context) (err error) {
|
||||
return err
|
||||
}
|
||||
|
||||
func RefreshSkuNameImg(ctx *jxcontext.Context, parentTask tasksch.ITask, isForce, isAsync, isContinueWhenError bool) (hint string, err error) {
|
||||
db := dao.GetDB()
|
||||
var skuNameList []*model.SkuName
|
||||
if err = dao.GetRows(db, &skuNameList, `
|
||||
SELECT t1.id, t1.img, MAX(t2.jd_id) jd_id
|
||||
FROM sku_name t1
|
||||
JOIN sku t2 ON t2.name_id = t1.id AND t2.deleted_at = ?
|
||||
WHERE t1.deleted_at = ?
|
||||
GROUP BY 1,2
|
||||
ORDER BY t1.id
|
||||
`, utils.DefaultTimeValue, utils.DefaultTimeValue); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
task := tasksch.NewParallelTask("RefreshSkuNameImg", tasksch.NewParallelConfig().SetIsContinueWhenError(isContinueWhenError), ctx,
|
||||
func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
|
||||
skuName := batchItemList[0].(*model.SkuName)
|
||||
if !jxutils.IsEmptyID(skuName.JdID) {
|
||||
if skuName.Img == "" || isForce {
|
||||
var imgList []*jdapi.SkuPageImg
|
||||
if imgList, err = api.JdAPI.GetSkuPageImageInfo(skuName.JdID); err == nil {
|
||||
if len(imgList) > 0 {
|
||||
skuName.Img = imgList[0].Big
|
||||
_, err = dao.UpdateEntity(db, skuName, "Img")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil, err
|
||||
}, skuNameList)
|
||||
tasksch.HandleTask(task, parentTask, true).Run()
|
||||
if !isAsync {
|
||||
_, err = task.GetResult(0)
|
||||
} else {
|
||||
hint = task.ID
|
||||
}
|
||||
return hint, err
|
||||
}
|
||||
|
||||
// func RefreshImgMd5(ctx *jxcontext.Context, parentTask tasksch.ITask, isForce, isAsync, isContinueWhenError bool) (hint string, err error) {
|
||||
// db := dao.GetDB()
|
||||
// var skuNameList []*model.SkuName
|
||||
// if err = dao.GetRows(db, &skuNameList, `
|
||||
// SELECT *
|
||||
// FROM sku_name
|
||||
// WHERE deleted_at = ? AND img <> ''
|
||||
// ORDER BY id
|
||||
// `, utils.DefaultTimeValue); err != nil {
|
||||
// return "", err
|
||||
// }
|
||||
|
||||
// task := tasksch.NewParallelTask("InitSkuName calculate md5", tasksch.NewParallelConfig().SetIsContinueWhenError(isContinueWhenError), ctx,
|
||||
// func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
|
||||
// skuName := batchItemList[0].(*model.SkuName)
|
||||
// if skuName.ImgHashCode == "" || isForce {
|
||||
// response, err := http.Get(skuName.Img)
|
||||
// if err == nil {
|
||||
// defer response.Body.Close()
|
||||
// if response.StatusCode == http.StatusOK {
|
||||
// data, err2 := ioutil.ReadAll(response.Body)
|
||||
// if err = err2; err == nil {
|
||||
// skuName.ImgHashCode = fmt.Sprintf("%X", md5.Sum(data))
|
||||
// db := dao.GetDB()
|
||||
// _, err = dao.UpdateEntity(db, skuName, "ImgHashCode")
|
||||
// }
|
||||
// } else {
|
||||
// err = platformapi.ErrHTTPCodeIsNot200
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// return nil, err
|
||||
// }, skuNameList)
|
||||
// tasksch.HandleTask(task, parentTask, true).Run()
|
||||
// if !isAsync {
|
||||
// _, err = task.GetResult(0)
|
||||
// } else {
|
||||
// hint = task.ID
|
||||
// }
|
||||
// return hint, err
|
||||
// }
|
||||
|
||||
func InitSkuName(ctx *jxcontext.Context, isForce, isAsync, isContinueWhenError bool) (hint string, err error) {
|
||||
rootTask := tasksch.NewSeqTask("InitSkuName", ctx,
|
||||
func(task *tasksch.SeqTask, step int, params ...interface{}) (result interface{}, err error) {
|
||||
switch step {
|
||||
case 0:
|
||||
_, err = RefreshSkuNameImg(ctx, task, isForce, false, isContinueWhenError)
|
||||
case 1:
|
||||
// _, err = RefreshImgMd5(ctx, task, isForce, false, isContinueWhenError)
|
||||
}
|
||||
return nil, err
|
||||
}, 2)
|
||||
tasksch.ManageTask(rootTask).Run()
|
||||
if !isAsync {
|
||||
_, err = rootTask.GetResult(0)
|
||||
} else {
|
||||
hint = rootTask.ID
|
||||
}
|
||||
return hint, err
|
||||
}
|
||||
|
||||
func InitVendorCategory(ctx *jxcontext.Context, vendorID int, isAsync bool) (hint string, err error) {
|
||||
if handler := partner.GetPurchasePlatformFromVendorID(vendorID); handler != nil {
|
||||
var cats []*model.SkuVendorCategory
|
||||
@@ -386,213 +278,3 @@ func UploadImg4Vendors(ctx *jxcontext.Context, isAsync, isContinueWhenError bool
|
||||
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,
|
||||
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)), &storage.PutExtra{}); 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 := jxutils.ComposeQiniuResURL(key) + "?imageMogr2/thumbnail/x800/gravity/Center/crop/800x800"
|
||||
for _, sku := range skuNameExt.Skus {
|
||||
jdCatID := 22410 // 其他国产水果
|
||||
if cat.Name == "进口水果" {
|
||||
jdCatID = 20342 // 其他进口水果
|
||||
}
|
||||
price := sku.LinkID
|
||||
sku.LinkID = 0
|
||||
skuName := jxutils.ComposeSkuName(skuNameExt.Prefix, skuNameExt.Name, sku.Comment, skuNameExt.Unit, sku.SpecQuality, sku.SpecUnit, jdapi.MaxSkuNameCharCount)
|
||||
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