Browse Source

修复sku为空报错的问题

qianyi
shanji 3 years ago
parent
commit
1e95073740
  1. 9
      BBWY.Server.Business/PlatformSDK/JDBusiness.cs
  2. 154
      BBWY.Server.Business/Sync/ProductSyncBusiness.cs
  3. 10
      BBWY.Server.Business/Sync/RefundOrderSyncBusiness.cs
  4. 2
      BBWY.Test/Program.cs

9
BBWY.Server.Business/PlatformSDK/JDBusiness.cs

@ -131,6 +131,15 @@ namespace BBWY.Server.Business
var rep_skuList = jdClient.Execute(req_skuList, searchProductSkuRequest.AppToken, DateTime.Now.ToLocalTime());
if (rep_skuList.IsError)
throw new BusinessException(string.IsNullOrEmpty(rep_skuList.ErrorMsg) ? rep_skuList.ErrMsg : rep_skuList.ErrorMsg);
if (rep_skuList.Json == null ||
!rep_skuList.Json.ContainsKey("jingdong_sku_read_searchSkuList_responce") ||
rep_skuList.Json["jingdong_sku_read_searchSkuList_responce"]["page"] == null ||
rep_skuList.Json["jingdong_sku_read_searchSkuList_responce"]["page"]["data"] == null)
{
return new ProductSkuResponse[0];
}
return ((JArray)rep_skuList.Json["jingdong_sku_read_searchSkuList_responce"]["page"]["data"]).Select(s => new ProductSkuResponse()
{
Id = s.Value<string>("skuId"),

154
BBWY.Server.Business/Sync/ProductSyncBusiness.cs

@ -37,88 +37,98 @@ namespace BBWY.Server.Business.Sync
this.productBusiness = productBusiness;
}
private void SyncProduct(ShopResponse shop, ProductListResponse productList)
private void SyncProduct(ShopResponse shop, ProductListResponse productList, out string productId)
{
var shopId = long.Parse(shop.ShopId);
List<Product> insertProductList = new List<Product>();
List<IUpdate<Product>> updateProductList = new List<IUpdate<Product>>();
List<ProductSku> inserSkuList = new List<ProductSku>();
List<IUpdate<ProductSku>> updateProductSkuList = new List<IUpdate<ProductSku>>();
var productIds = productList.Items.Select(p => p.Id);
var dbProducts = fsql.Select<Product>().Where(p => p.ShopId == shopId && productIds.Contains(p.Id)).ToList();
var dbProductSkus = fsql.Select<ProductSku>().Where(s => s.ShopId == shopId && productIds.Contains(s.ProductId)).ToList();
foreach (var product in productList.Items)
productId = "";
try
{
var dbProduct = dbProducts.FirstOrDefault(dbp => dbp.Id == product.Id);
if (dbProduct == null)
{
insertProductList.Add(new Product()
{
Id = product.Id,
CreateTime = product.CreateTime ?? DateTime.Now,
Platform = shop.PlatformId,
ProductItemNum = product.ProductItemNum,
ShopId = shopId,
Title = product.Title,
State = product.State
});
}
else if (dbProduct.State != product.State)
{
var update = fsql.Update<Product>(product.Id).Set(p => p.State, dbProduct.State);
updateProductList.Add(update);
}
var shopId = long.Parse(shop.ShopId);
List<Product> insertProductList = new List<Product>();
List<IUpdate<Product>> updateProductList = new List<IUpdate<Product>>();
var skuList = productBusiness.GetProductSkuList(new SearchProductSkuRequest()
{
AppKey = shop.AppKey,
AppSecret = shop.AppSecret,
AppToken = shop.AppToken,
Platform = shop.PlatformId,
Spu = product.Id
});
List<ProductSku> inserSkuList = new List<ProductSku>();
List<IUpdate<ProductSku>> updateProductSkuList = new List<IUpdate<ProductSku>>();
var productIds = productList.Items.Select(p => p.Id);
var dbProducts = fsql.Select<Product>().Where(p => p.ShopId == shopId && productIds.Contains(p.Id)).ToList();
var dbProductSkus = fsql.Select<ProductSku>().Where(s => s.ShopId == shopId && productIds.Contains(s.ProductId)).ToList();
foreach (var sku in skuList)
foreach (var product in productList.Items)
{
var dbSku = dbProductSkus.FirstOrDefault(s => s.Id == sku.Id);
if (dbSku == null)
productId = product.Id;
var dbProduct = dbProducts.FirstOrDefault(dbp => dbp.Id == product.Id);
if (dbProduct == null)
{
inserSkuList.Add(new ProductSku()
insertProductList.Add(new Product()
{
Id = sku.Id,
CreateTime = sku.CreateTime ?? DateTime.Now,
Logo = sku.Logo,
Id = product.Id,
CreateTime = product.CreateTime ?? DateTime.Now,
Platform = shop.PlatformId,
Price = sku.Price,
ProductId = sku.ProductId,
ProductItemNum = product.ProductItemNum,
ShopId = shopId,
Title = sku.Title,
State = sku.State
Title = product.Title,
State = product.State
});
}
else if (dbSku.State != sku.State || dbSku.Price != sku.Price)
else if (dbProduct.State != product.State)
{
var update = fsql.Update<ProductSku>(dbSku.Id).Set(s => s.State, sku.State).Set(s => s.Price, sku.Price);
updateProductSkuList.Add(update);
var update = fsql.Update<Product>(product.Id).Set(p => p.State, dbProduct.State);
updateProductList.Add(update);
}
var skuList = productBusiness.GetProductSkuList(new SearchProductSkuRequest()
{
AppKey = shop.AppKey,
AppSecret = shop.AppSecret,
AppToken = shop.AppToken,
Platform = shop.PlatformId,
Spu = product.Id
});
if (skuList == null || skuList.Count() == 0)
continue;
foreach (var sku in skuList)
{
var dbSku = dbProductSkus.FirstOrDefault(s => s.Id == sku.Id);
if (dbSku == null)
{
inserSkuList.Add(new ProductSku()
{
Id = sku.Id,
CreateTime = sku.CreateTime ?? DateTime.Now,
Logo = sku.Logo,
Platform = shop.PlatformId,
Price = sku.Price,
ProductId = sku.ProductId,
ShopId = shopId,
Title = sku.Title,
State = sku.State
});
}
else if (dbSku.State != sku.State || dbSku.Price != sku.Price)
{
var update = fsql.Update<ProductSku>(dbSku.Id).Set(s => s.State, sku.State).Set(s => s.Price, sku.Price);
updateProductSkuList.Add(update);
}
}
}
}
fsql.Transaction(() =>
{
fsql.Insert(insertProductList).ExecuteAffrows();
fsql.Insert(inserSkuList).ExecuteAffrows();
if (updateProductList.Count > 0)
foreach (var update in updateProductList)
update.ExecuteAffrows();
if (updateProductSkuList.Count > 0)
foreach (var update in updateProductSkuList)
update.ExecuteAffrows();
fsql.Transaction(() =>
{
fsql.Insert(insertProductList).ExecuteAffrows();
fsql.Insert(inserSkuList).ExecuteAffrows();
if (updateProductList.Count > 0)
foreach (var update in updateProductList)
update.ExecuteAffrows();
if (updateProductSkuList.Count > 0)
foreach (var update in updateProductSkuList)
update.ExecuteAffrows();
});
});
}
catch (Exception ex)
{
throw new Exception(productId, ex);
}
}
private void SyncProduct(ShopResponse shop, bool IsSyncAllProduct = false)
@ -139,7 +149,7 @@ namespace BBWY.Server.Business.Sync
var productList = productBusiness.GetProductList(request);
if (productList == null || productList.Count == 0)
return;
SyncProduct(shop, productList);
SyncProduct(shop, productList, out string productId);
if (productList.Items.Count < 50 || !IsSyncAllProduct)
break;
request.PageIndex++;
@ -171,11 +181,11 @@ namespace BBWY.Server.Business.Sync
public void SyncAllShopAllProduct()
{
var shopList = venderBusiness.GetShopList();
//SyncProduct(shopList.FirstOrDefault(s => s.ShopId == "10388155"), true); //布莱特玩具专营店
foreach (var shop in shopList)
{
Task.Factory.StartNew(() => SyncProduct(shop, true), System.Threading.CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.ProductSyncTaskScheduler);
}
SyncProduct(shopList.FirstOrDefault(s => s.ShopName == "瑞源玩具专营店"), true); //瑞源玩具专营店
//foreach (var shop in shopList)
//{
// Task.Factory.StartNew(() => SyncProduct(shop, true), System.Threading.CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.ProductSyncTaskScheduler);
//}
}
}
}

10
BBWY.Server.Business/Sync/RefundOrderSyncBusiness.cs

@ -156,11 +156,11 @@ namespace BBWY.Server.Business
public void SyncAllShopRefundOrder()
{
var shopList = venderBusiness.GetShopList();
SyncRefundOrder(shopList.FirstOrDefault(s => s.ShopName == "赟娅墨森专卖店"), string.Empty, isAuto: true);
//foreach (var shop in shopList)
//{
// Task.Factory.StartNew(() => SyncRefundOrder(shop, string.Empty, isAuto: true), System.Threading.CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.SyncRefundOrderTaskScheduler);
//}
//SyncRefundOrder(shopList.FirstOrDefault(s => s.ShopName == "赟娅墨森专卖店"), string.Empty, isAuto: true);
foreach (var shop in shopList)
{
Task.Factory.StartNew(() => SyncRefundOrder(shop, string.Empty, isAuto: true), System.Threading.CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.SyncRefundOrderTaskScheduler);
}
}
}
}

2
BBWY.Test/Program.cs

@ -2,6 +2,7 @@
using Jd.Api.Request;
using Jd.Api.Response;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
namespace BBWY.Test
{
@ -14,6 +15,7 @@ namespace BBWY.Test
static void Main(string[] args)
{
var appKey = "120EA9EC65AB017567D78CC1139EEEA5";
var appSecret = "866a9877f5f24b03b537483b4defe75d";
var token = "2ace3023200c4ea9aa682bbf8bffee18jztm";

Loading…
Cancel
Save