135 lines
5.1 KiB
Go
135 lines
5.1 KiB
Go
package jd
|
|
|
|
import (
|
|
"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/storeskulock"
|
|
"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"
|
|
)
|
|
|
|
type tStoreSkuBindExt struct {
|
|
model.StoreSkuBind
|
|
PricePercentage int
|
|
VendorStoreID string `orm:"column(vendor_store_id)"`
|
|
JdID int64 `orm:"column(jd_id)"`
|
|
}
|
|
|
|
// 京东到家,以有库存表示关注(认领)
|
|
func (p *PurchaseHandler) SyncStoreSkus(ctx *jxcontext.Context, parentTask tasksch.ITask, storeID int, skuIDs []int, isAsync, isContinueWhenError bool) (hint string, err error) {
|
|
globals.SugarLogger.Debugf("jd SyncStoresSkus, storeID:%d, skuIDs:%v", storeID, skuIDs)
|
|
sqlWhere0 := `
|
|
WHERE (t1.jd_sync_status <> 0) AND t1.store_id = ?
|
|
`
|
|
sqlWhere := sqlWhere0
|
|
sqlWhereParams := []interface{}{
|
|
storeID,
|
|
}
|
|
|
|
if len(skuIDs) > 0 {
|
|
sqlWhere += " AND t1.sku_id IN (" + dao.GenQuestionMarks(len(skuIDs)) + ")"
|
|
sqlWhereParams = append(sqlWhereParams, skuIDs)
|
|
}
|
|
|
|
sql := `
|
|
SELECT t3.jd_id, t1.*, t2.price_percentage, t2.vendor_store_id
|
|
FROM store_sku_bind t1
|
|
JOIN store_map t2 ON t1.store_id = t2.store_id AND t2.vendor_id = ? AND t2.deleted_at = ?
|
|
JOIN sku t3 ON t1.sku_id = t3.id
|
|
` + sqlWhere + " ORDER BY t1.updated_at"
|
|
var storeSkus []*tStoreSkuBindExt
|
|
sqlParams := []interface{}{
|
|
model.VendorIDJD,
|
|
utils.DefaultTimeValue,
|
|
}
|
|
db := dao.GetDB()
|
|
if err = dao.GetRows(db, &storeSkus, sql, append(sqlParams, sqlWhereParams...)...); err != nil {
|
|
return "", err
|
|
}
|
|
task := tasksch.NewParallelTask("SyncStoresSkus京东", tasksch.NewParallelConfig().SetBatchSize(jdapi.MaxStoreSkuBatchSize).SetIsContinueWhenError(isContinueWhenError), ctx.GetUserName(), func(t *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (interface{}, error) {
|
|
var skuPriceInfoList []*jdapi.SkuPriceInfo
|
|
var skuVendibilityList []*jdapi.StockVendibility
|
|
var skuStockList []*jdapi.SkuStock
|
|
stationNo := batchItemList[0].(*tStoreSkuBindExt).VendorStoreID
|
|
var batchSkuIDs []int
|
|
for _, v := range batchItemList {
|
|
storeSku := v.(*tStoreSkuBindExt)
|
|
alreadyAddStock := false
|
|
if storeSku.JdSyncStatus&model.SyncFlagChangedMask != 0 {
|
|
batchSkuIDs = append(batchSkuIDs, storeSku.SkuID)
|
|
if storeSku.JdSyncStatus&(model.SyncFlagDeletedMask|model.SyncFlagNewMask) != 0 { // 关注或取消关注
|
|
stock := &jdapi.SkuStock{
|
|
OutSkuId: utils.Int2Str(storeSku.SkuID),
|
|
StockQty: model.MaxStoreSkuStockQty,
|
|
}
|
|
if storeSku.DeletedAt != utils.DefaultTimeValue {
|
|
stock.StockQty = 0
|
|
} else {
|
|
alreadyAddStock = true
|
|
}
|
|
if stock.StockQty != 0 || !storeskulock.IsJdStoreSkuLocked(stationNo, storeSku.JdID) {
|
|
skuStockList = append(skuStockList, stock)
|
|
}
|
|
}
|
|
if storeSku.JdSyncStatus&(model.SyncFlagPriceMask|model.SyncFlagNewMask) != 0 {
|
|
skuPriceInfoList = append(skuPriceInfoList, &jdapi.SkuPriceInfo{
|
|
OutSkuId: utils.Int2Str(storeSku.SkuID),
|
|
Price: jxutils.CaculateSkuVendorPrice(storeSku.Price, storeSku.PricePercentage),
|
|
})
|
|
}
|
|
if storeSku.JdSyncStatus&(model.SyncFlagSaleMask|model.SyncFlagNewMask) != 0 {
|
|
vendibility := &jdapi.StockVendibility{
|
|
OutSkuId: utils.Int2Str(storeSku.SkuID),
|
|
DoSale: true,
|
|
}
|
|
if storeSku.Status != model.StoreSkuBindStatusNormal {
|
|
vendibility.DoSale = false
|
|
} else if !alreadyAddStock { // 如果是设置可售则自动将库存加满
|
|
stock := &jdapi.SkuStock{
|
|
OutSkuId: utils.Int2Str(storeSku.SkuID),
|
|
StockQty: model.MaxStoreSkuStockQty,
|
|
}
|
|
skuStockList = append(skuStockList, stock)
|
|
}
|
|
if vendibility.DoSale || !storeskulock.IsJdStoreSkuLocked(stationNo, storeSku.JdID) {
|
|
skuVendibilityList = append(skuVendibilityList, vendibility)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if globals.EnableStoreWrite {
|
|
// todo 以下可以优化为并行操作
|
|
globals.SugarLogger.Debug(utils.Format4Output(skuVendibilityList, false), utils.Format4Output(skuPriceInfoList, false), utils.Format4Output(skuStockList, false))
|
|
if len(skuVendibilityList) > 0 {
|
|
_, err = api.JdAPI.BatchUpdateVendibility("", stationNo, skuVendibilityList, ctx.GetUserName())
|
|
}
|
|
if err == nil && len(skuPriceInfoList) > 0 {
|
|
_, err = api.JdAPI.UpdateVendorStationPrice("", stationNo, skuPriceInfoList)
|
|
}
|
|
if err == nil && len(skuStockList) > 0 {
|
|
_, err = api.JdAPI.BatchUpdateCurrentQtys("", stationNo, skuStockList, ctx.GetUserName())
|
|
}
|
|
}
|
|
if err == nil && len(batchSkuIDs) > 0 {
|
|
db := dao.GetDB() // 多线程问题
|
|
sql := `
|
|
UPDATE store_sku_bind t1
|
|
SET t1.jd_sync_status = 0
|
|
` + sqlWhere0 + " AND t1.sku_id IN (" + dao.GenQuestionMarks(len(batchSkuIDs)) + ")"
|
|
_, err = dao.ExecuteSQL(db, sql, storeID, batchSkuIDs)
|
|
}
|
|
return nil, err
|
|
}, storeSkus)
|
|
ctx.SetTaskOrAddChild(task, parentTask)
|
|
task.Run()
|
|
if !isAsync {
|
|
_, err = task.GetResult(0)
|
|
}
|
|
return task.ID, err
|
|
}
|