master
wufan 2 months ago
parent 6590fb4d84
commit 1620edd261

@ -28679,6 +28679,8 @@ namespace TradeManageNew
#endregion
#region 二手商品上架Shopify
private static readonly object _lockObject = new object();
[WebMethod(EnableSession = true)]
public JsonModel<List<TradeUsedSale.Repositories.Models.DT_OrderUsedSalePlatform>> OrderUsedSalePlatformPage(string Code,string BarCode,bool? IsPrinted,TradeUsedSale.Enums.UsedSalePlatformStatus? Status,string WarehousePositionCode,int PageIndex=1,int PageSize=50)
@ -28752,105 +28754,178 @@ namespace TradeManageNew
public APIReturnModel OrderUsedSalePlatformPublish(List<int> Ids)
{
// PagesNew.Login(base.Session);
using (var db = new TradeUsedSale.Repositories.ErpDbContext())
{
var orderUsedSalePlatformList = db.DT_OrderUsedSalePlatform.Where(x => Ids.Contains(x.Id)).ToList();
bool lockTaken = false;
foreach (var orderUsedSalePlatform in orderUsedSalePlatformList)
try
{
Monitor.Enter(_lockObject, ref lockTaken);
using (var db = new TradeUsedSale.Repositories.ErpDbContext())
{
if (orderUsedSalePlatform.Status != TradeUsedSale.Enums.UsedSalePlatformStatus.NotListed)
{
continue;
}
//系统产品信息
var product = db.HW_GoodsInfo.FirstOrDefault(x => x.GoodsId == orderUsedSalePlatform.ProductId);
if (product is null)
{
var message = $"系统中不存在编码为[{orderUsedSalePlatform.ProductCode}]的产品信息无法推送至Shopify";
db.DT_OrderUsedSalePlatform.Where(x => x.Id == orderUsedSalePlatform.Id)
.Set(x => x.PublishMessage, message)
.Update();
continue;
}
//毛重
if (product.Weight is null || product.Weight <= 0)
{
var message = $"产品[{product.GoodsCode}]无毛重信息无法推送至Shopify";
db.DT_OrderUsedSalePlatform.Where(x => x.Id == orderUsedSalePlatform.Id)
.Set(x => x.PublishMessage, message)
.Update();
continue;
}
var orderUsedSalePlatformList = db.DT_OrderUsedSalePlatform.Where(x => Ids.Contains(x.Id)).ToList();
//建议售价
if (product.JYPrice is null || product.JYPrice <= 0)
{
var message = $"产品[{product.GoodsCode}]无建议售价无法推送至Shopify";
db.DT_OrderUsedSalePlatform.Where(x => x.Id == orderUsedSalePlatform.Id)
.Set(x => x.PublishMessage, message)
.Update();
continue;
}
//预估运费
var postFee = db.HW_PostFee.FirstOrDefault(x => x.GoodsId == orderUsedSalePlatform.ProductId);
if (postFee is null)
{
db.DT_OrderUsedSalePlatform.Where(x => x.Id == orderUsedSalePlatform.Id)
.Set(x => x.PublishMessage, $"产品[{product.GoodsCode}]无预估运费无法推送至Shopify")
.Update();
continue;
}
var fees = new List<decimal?>()
foreach (var orderUsedSalePlatform in orderUsedSalePlatformList)
{
postFee.Fee1,
//postFee.Fee2,
postFee.Fee3,
postFee.Fee4,
postFee.Fee5,
//postFee.Fee6,
postFee.Fee7,
postFee.Fee8,
}.Where(x => x != null).ToList();
var feeCost = fees.Any() ? fees.Min() : 0;
if (orderUsedSalePlatform.Status != TradeUsedSale.Enums.UsedSalePlatformStatus.NotListed)
{
continue;
}
//系统产品信息
var product = db.HW_GoodsInfo.FirstOrDefault(x => x.GoodsId == orderUsedSalePlatform.ProductId);
if (product is null)
{
var message = $"系统中不存在编码为[{orderUsedSalePlatform.ProductCode}]的产品信息无法推送至Shopify";
db.DT_OrderUsedSalePlatform.Where(x => x.Id == orderUsedSalePlatform.Id)
.Set(x => x.PublishMessage, message)
.Update();
continue;
}
//毛重
if (product.Weight is null || product.Weight <= 0)
{
var message = $"产品[{product.GoodsCode}]无毛重信息无法推送至Shopify";
db.DT_OrderUsedSalePlatform.Where(x => x.Id == orderUsedSalePlatform.Id)
.Set(x => x.PublishMessage, message)
.Update();
continue;
}
//建议售价
if (product.JYPrice is null || product.JYPrice <= 0)
{
var message = $"产品[{product.GoodsCode}]无建议售价无法推送至Shopify";
db.DT_OrderUsedSalePlatform.Where(x => x.Id == orderUsedSalePlatform.Id)
.Set(x => x.PublishMessage, message)
.Update();
continue;
}
//预估运费
var postFee = db.HW_PostFee.FirstOrDefault(x => x.GoodsId == orderUsedSalePlatform.ProductId);
if (postFee is null)
{
db.DT_OrderUsedSalePlatform.Where(x => x.Id == orderUsedSalePlatform.Id)
.Set(x => x.PublishMessage, $"产品[{product.GoodsCode}]无预估运费无法推送至Shopify")
.Update();
continue;
}
var fees = new List<decimal?>()
{
postFee.Fee1,
//postFee.Fee2,
postFee.Fee3,
postFee.Fee4,
postFee.Fee5,
//postFee.Fee6,
postFee.Fee7,
postFee.Fee8,
}.Where(x => x != null).ToList();
var feeCost = fees.Any() ? fees.Min() : 0;
if (feeCost <= 0)
{
db.DT_OrderUsedSalePlatform.Where(x => x.Id == orderUsedSalePlatform.Id)
.Set(x => x.PublishMessage, $"产品[{product.GoodsCode}]预估运费小于0无法推送至Shopify")
.Update();
continue;
}
string shopifyProductId;
//判断时候在Shopify已创建过产品
var shopifyProductGoodsMap =
db.DT_ShopifyProductGoodsMap.FirstOrDefault(x => x.SkuId == orderUsedSalePlatform.SkuId);
//获取SKU的资料信息
var shopifyProductProfile =
db.DT_ShopifyUsedSaleProduct.FirstOrDefault(x => x.Sku == orderUsedSalePlatform.SkuCode);
//Shopify客户端
var shopifyApiClient = new ShopifyAPIClient();
if (shopifyProductGoodsMap is null)
{
try
{
//在Shopify创建产品信息
var createShopifyProductParameter = new Body2
{
ProductBaseData = new ProductBaseData
{
SKU = shopifyProductProfile?.Sku ?? orderUsedSalePlatform.SkuCode,
Title = shopifyProductProfile?.Title ?? product.GoodsEnglisgName,
ImagePath = shopifyProductProfile?.ImagePath ?? product.FirstImgUrl,
ProductType = shopifyProductProfile?.Category,
Tags = shopifyProductProfile?.Tags,
Weight = product.Weight.Value,
JYPrice = product.JYPrice.Value,
ShippingFee = feeCost.Value,
}
};
var shopifyProductRes = shopifyApiClient
.CreateProductAsync(createShopifyProductParameter)
.ConfigureAwait(false)
.GetAwaiter()
.GetResult();
if (string.IsNullOrWhiteSpace(shopifyProductRes?.Data?.Id))
{
var message =
$"产品[{product.GoodsCode}]推送Shopify产品失败原因{shopifyProductRes.Message}";
db.DT_OrderUsedSalePlatform.Where(x => x.Id == orderUsedSalePlatform.Id)
.Set(x => x.PublishMessage, message)
.Update();
continue;
}
shopifyProductId = shopifyProductRes.Data.Id;
var newMap = new TradeUsedSale.Repositories.Models.DT_ShopifyProductGoodsMap
{
ShopifyProductId = shopifyProductId,
SkuId = orderUsedSalePlatform.SkuId,
SkuCode = orderUsedSalePlatform.SkuCode,
CreationTime = DateTime.Now
};
db.InsertWithInt32Identity(newMap);
}
catch (Exception ex)
{
var message = $"产品[{product.GoodsCode}]推送Shopify产品失败原因{ex.Message}";
db.DT_OrderUsedSalePlatform.Where(x => x.Id == orderUsedSalePlatform.Id)
.Set(x => x.PublishMessage, message)
.Update();
continue;
}
}
else
{
shopifyProductId = shopifyProductGoodsMap.ShopifyProductId;
}
if (feeCost <= 0)
{
db.DT_OrderUsedSalePlatform.Where(x => x.Id == orderUsedSalePlatform.Id)
.Set(x => x.PublishMessage, $"产品[{product.GoodsCode}]预估运费小于0无法推送至Shopify")
.Update();
continue;
}
string shopifyProductId;
//判断时候在Shopify已创建过产品
var shopifyProductGoodsMap =
db.DT_ShopifyProductGoodsMap.FirstOrDefault(x => x.SkuId == orderUsedSalePlatform.SkuId);
//获取SKU的资料信息
var shopifyProductProfile =
db.DT_ShopifyUsedSaleProduct.FirstOrDefault(x => x.Sku == orderUsedSalePlatform.SkuCode);
//Shopify客户端
var shopifyApiClient = new ShopifyAPIClient();
if (shopifyProductGoodsMap is null)
{
try
{
//在Shopify创建产品信息
var createShopifyProductParameter = new Body2
var imageUrls =
JsonConvert.DeserializeObject<List<string>>(orderUsedSalePlatform.ImageUrls);
//在Shopify创建产品变体信息
var createShopifyVariantParameter = new AddVariantRequest
{
ProductBaseData = new ProductBaseData
ProductBaseData = new ProductBaseData2
{
ShoifyGoodsId = shopifyProductId,
SKU = shopifyProductProfile?.Sku ?? orderUsedSalePlatform.SkuCode,
Title = shopifyProductProfile?.Title ?? product.GoodsEnglisgName,
ImagePath = shopifyProductProfile?.ImagePath ?? product.FirstImgUrl,
@ -28858,17 +28933,32 @@ namespace TradeManageNew
Tags = shopifyProductProfile?.Tags,
Weight = product.Weight.Value,
JYPrice = product.JYPrice.Value,
ShippingFee = feeCost.Value,
}
ShippingFee = feeCost.Value
},
Variants = new Variants
{
Return_quantity = 1,
ImgUrl = imageUrls.Count > 0 ? imageUrls[0] : null,
ImgUrl2 = imageUrls.Count > 1 ? imageUrls[1] : null,
ImgUrl3 = imageUrls.Count > 2 ? imageUrls[2] : null,
ImgUrl4 = imageUrls.Count > 3 ? imageUrls[3] : null,
ImgUrl5 = imageUrls.Count > 4 ? imageUrls[4] : null,
StoreName = orderUsedSalePlatform.WarehouseLocation ==
TradeUsedSale.Enums.WarehouseLocation.East
? "E"
: "W",
Code = orderUsedSalePlatform.BarCode,
},
};
var shopifyProductRes = shopifyApiClient.CreateProductAsync(createShopifyProductParameter)
.ConfigureAwait(false)
var productVariantRes = shopifyApiClient
.AddProductVariantAsync(createShopifyVariantParameter).ConfigureAwait(false)
.GetAwaiter()
.GetResult();
if (string.IsNullOrWhiteSpace(shopifyProductRes?.Data?.Id))
if (productVariantRes.Success == false)
{
var message = $"产品[{product.GoodsCode}]推送Shopify产品失败原因{shopifyProductRes.Message}";
var message = $"产品[{product.GoodsCode}]推送Shopify变体失败,原因:{productVariantRes.Message}";
db.DT_OrderUsedSalePlatform.Where(x => x.Id == orderUsedSalePlatform.Id)
.Set(x => x.PublishMessage, message)
.Update();
@ -28876,109 +28966,39 @@ namespace TradeManageNew
continue;
}
shopifyProductId = shopifyProductRes.Data.Id;
var newMap = new TradeUsedSale.Repositories.Models.DT_ShopifyProductGoodsMap
{
ShopifyProductId = shopifyProductId,
SkuId = orderUsedSalePlatform.SkuId,
SkuCode = orderUsedSalePlatform.SkuCode,
CreationTime = DateTime.Now
};
db.InsertWithInt32Identity(newMap);
}
catch (Exception ex)
{
var message = $"产品[{product.GoodsCode}]推送Shopify产品失败原因{ex.Message}";
//推送Shopify成功
db.DT_OrderUsedSalePlatform.Where(x => x.Id == orderUsedSalePlatform.Id)
.Set(x => x.PublishMessage, message)
.Set(x => x.Status, TradeUsedSale.Enums.UsedSalePlatformStatus.Listed)
.Set(x => x.PublishMessage, "推送成功")
.Update();
continue;
}
}
else
{
shopifyProductId = shopifyProductGoodsMap.ShopifyProductId;
}
try
{
var imageUrls = JsonConvert.DeserializeObject<List<string>>(orderUsedSalePlatform.ImageUrls);
//在Shopify创建产品变体信息
var createShopifyVariantParameter = new AddVariantRequest
catch (Exception ex)
{
ProductBaseData = new ProductBaseData2
{
ShoifyGoodsId = shopifyProductId,
SKU = shopifyProductProfile?.Sku ?? orderUsedSalePlatform.SkuCode,
Title = shopifyProductProfile?.Title ?? product.GoodsEnglisgName,
ImagePath = shopifyProductProfile?.ImagePath ?? product.FirstImgUrl,
ProductType = shopifyProductProfile?.Category,
Tags = shopifyProductProfile?.Tags,
Weight = product.Weight.Value,
JYPrice = product.JYPrice.Value,
ShippingFee = feeCost.Value
},
Variants = new Variants
{
Return_quantity = 1,
ImgUrl = imageUrls.Count > 0 ? imageUrls[0] : null,
ImgUrl2 = imageUrls.Count > 1 ? imageUrls[1] : null,
ImgUrl3 = imageUrls.Count > 2 ? imageUrls[2] : null,
ImgUrl4 = imageUrls.Count > 3 ? imageUrls[3] : null,
ImgUrl5 = imageUrls.Count > 4 ? imageUrls[4] : null,
StoreName = orderUsedSalePlatform.WarehouseLocation ==
TradeUsedSale.Enums.WarehouseLocation.East
? "E"
: "W",
Code = orderUsedSalePlatform.BarCode,
},
};
var productVariantRes = shopifyApiClient.AddProductVariantAsync(createShopifyVariantParameter).ConfigureAwait(false)
.GetAwaiter()
.GetResult();
var message = $"产品[{product.GoodsCode}]推送Shopify变体失败原因{ex.Message}";
if (productVariantRes.Success == false)
{
var message = $"产品[{product.GoodsCode}]推送Shopify变体失败原因{productVariantRes.Message}";
db.DT_OrderUsedSalePlatform.Where(x => x.Id == orderUsedSalePlatform.Id)
.Set(x => x.PublishMessage, message)
.Update();
continue;
}
//推送Shopify成功
db.DT_OrderUsedSalePlatform.Where(x => x.Id == orderUsedSalePlatform.Id)
.Set(x => x.Status, TradeUsedSale.Enums.UsedSalePlatformStatus.Listed)
.Set(x => x.PublishMessage, "推送成功")
.Update();
}
catch (Exception ex)
{
var message = $"产品[{product.GoodsCode}]推送Shopify变体失败原因{ex.Message}";
db.DT_OrderUsedSalePlatform.Where(x => x.Id == orderUsedSalePlatform.Id)
.Set(x => x.PublishMessage, message)
.Update();
continue;
}
}
return new APIReturnModel
{
Code = 1,
Message = "Success",
Datas = null
};
}
return new APIReturnModel
finally
{
Code = 1,
Message = "Success",
Datas = null
};
if (lockTaken)
{
Monitor.Exit(_lockObject);
}
}
}
[WebMethod(EnableSession = true)]

Loading…
Cancel
Save