master
wufan 2 months ago
parent 6590fb4d84
commit 1620edd261

@ -28680,6 +28680,8 @@ namespace TradeManageNew
#region 二手商品上架Shopify #region 二手商品上架Shopify
private static readonly object _lockObject = new object();
[WebMethod(EnableSession = true)] [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) 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) public APIReturnModel OrderUsedSalePlatformPublish(List<int> Ids)
{ {
// PagesNew.Login(base.Session); // PagesNew.Login(base.Session);
using (var db = new TradeUsedSale.Repositories.ErpDbContext()) bool lockTaken = false;
try
{ {
var orderUsedSalePlatformList = db.DT_OrderUsedSalePlatform.Where(x => Ids.Contains(x.Id)).ToList(); Monitor.Enter(_lockObject, ref lockTaken);
foreach (var orderUsedSalePlatform in orderUsedSalePlatformList) using (var db = new TradeUsedSale.Repositories.ErpDbContext())
{ {
if (orderUsedSalePlatform.Status != TradeUsedSale.Enums.UsedSalePlatformStatus.NotListed) var orderUsedSalePlatformList = db.DT_OrderUsedSalePlatform.Where(x => Ids.Contains(x.Id)).ToList();
{
continue;
}
//系统产品信息 foreach (var orderUsedSalePlatform in orderUsedSalePlatformList)
var product = db.HW_GoodsInfo.FirstOrDefault(x => x.GoodsId == orderUsedSalePlatform.ProductId);
if (product is null)
{ {
var message = $"系统中不存在编码为[{orderUsedSalePlatform.ProductCode}]的产品信息无法推送至Shopify"; if (orderUsedSalePlatform.Status != TradeUsedSale.Enums.UsedSalePlatformStatus.NotListed)
db.DT_OrderUsedSalePlatform.Where(x => x.Id == orderUsedSalePlatform.Id) {
.Set(x => x.PublishMessage, message) continue;
.Update(); }
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.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; //建议售价
} 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 postFee = db.HW_PostFee.FirstOrDefault(x => x.GoodsId == orderUsedSalePlatform.ProductId);
var fees = new List<decimal?>() if (postFee is null)
{ {
postFee.Fee1, db.DT_OrderUsedSalePlatform.Where(x => x.Id == orderUsedSalePlatform.Id)
//postFee.Fee2, .Set(x => x.PublishMessage, $"产品[{product.GoodsCode}]无预估运费无法推送至Shopify")
postFee.Fee3, .Update();
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) continue;
{ }
db.DT_OrderUsedSalePlatform.Where(x => x.Id == orderUsedSalePlatform.Id)
.Set(x => x.PublishMessage, $"产品[{product.GoodsCode}]预估运费小于0无法推送至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;
string shopifyProductId; if (feeCost <= 0)
{
db.DT_OrderUsedSalePlatform.Where(x => x.Id == orderUsedSalePlatform.Id)
.Set(x => x.PublishMessage, $"产品[{product.GoodsCode}]预估运费小于0无法推送至Shopify")
.Update();
continue;
}
//判断时候在Shopify已创建过产品 string shopifyProductId;
var shopifyProductGoodsMap =
db.DT_ShopifyProductGoodsMap.FirstOrDefault(x => x.SkuId == orderUsedSalePlatform.SkuId);
//获取SKU的资料信息 //判断时候在Shopify已创建过产品
var shopifyProductProfile = var shopifyProductGoodsMap =
db.DT_ShopifyUsedSaleProduct.FirstOrDefault(x => x.Sku == orderUsedSalePlatform.SkuCode); 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;
}
//Shopify客户端
var shopifyApiClient = new ShopifyAPIClient();
if (shopifyProductGoodsMap is null)
{
try try
{ {
//在Shopify创建产品信息 var imageUrls =
var createShopifyProductParameter = new Body2 JsonConvert.DeserializeObject<List<string>>(orderUsedSalePlatform.ImageUrls);
//在Shopify创建产品变体信息
var createShopifyVariantParameter = new AddVariantRequest
{ {
ProductBaseData = new ProductBaseData ProductBaseData = new ProductBaseData2
{ {
ShoifyGoodsId = shopifyProductId,
SKU = shopifyProductProfile?.Sku ?? orderUsedSalePlatform.SkuCode, SKU = shopifyProductProfile?.Sku ?? orderUsedSalePlatform.SkuCode,
Title = shopifyProductProfile?.Title ?? product.GoodsEnglisgName, Title = shopifyProductProfile?.Title ?? product.GoodsEnglisgName,
ImagePath = shopifyProductProfile?.ImagePath ?? product.FirstImgUrl, ImagePath = shopifyProductProfile?.ImagePath ?? product.FirstImgUrl,
@ -28858,17 +28933,32 @@ namespace TradeManageNew
Tags = shopifyProductProfile?.Tags, Tags = shopifyProductProfile?.Tags,
Weight = product.Weight.Value, Weight = product.Weight.Value,
JYPrice = product.JYPrice.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() .GetAwaiter()
.GetResult(); .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) db.DT_OrderUsedSalePlatform.Where(x => x.Id == orderUsedSalePlatform.Id)
.Set(x => x.PublishMessage, message) .Set(x => x.PublishMessage, message)
.Update(); .Update();
@ -28876,109 +28966,39 @@ namespace TradeManageNew
continue; continue;
} }
shopifyProductId = shopifyProductRes.Data.Id; //推送Shopify成功
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) 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(); .Update();
continue;
} }
} catch (Exception ex)
else
{
shopifyProductId = shopifyProductGoodsMap.ShopifyProductId;
}
try
{
var imageUrls = JsonConvert.DeserializeObject<List<string>>(orderUsedSalePlatform.ImageUrls);
//在Shopify创建产品变体信息
var createShopifyVariantParameter = new AddVariantRequest
{ {
ProductBaseData = new ProductBaseData2 var message = $"产品[{product.GoodsCode}]推送Shopify变体失败原因{ex.Message}";
{
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();
if (productVariantRes.Success == false)
{
var message = $"产品[{product.GoodsCode}]推送Shopify变体失败原因{productVariantRes.Message}";
db.DT_OrderUsedSalePlatform.Where(x => x.Id == orderUsedSalePlatform.Id) db.DT_OrderUsedSalePlatform.Where(x => x.Id == orderUsedSalePlatform.Id)
.Set(x => x.PublishMessage, message) .Set(x => x.PublishMessage, message)
.Update(); .Update();
continue; 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 return new APIReturnModel
{
Code = 1,
Message = "Success",
Datas = null
};
}
finally
{ {
Code = 1, if (lockTaken)
Message = "Success", {
Datas = null Monitor.Exit(_lockObject);
}; }
}
} }
[WebMethod(EnableSession = true)] [WebMethod(EnableSession = true)]

Loading…
Cancel
Save