添加商品改价回滚功能,优化图书上传

This commit is contained in:
wule_5900x committed 2026-06-28 21:05:32 +08:00
1 parent 8f5debb85c
commit 5467acadc0
7 files changed
+476 -140

No files matched your search

+255 -44
View File
@@ -92,6 +92,10 @@ namespace TemuKJD.WXForms.ProductForms
if (wxUploadRule == null)
wxUploadRule = new WXUploadRule();
dicProductBasePrice = NaiBa.Util.Helper.ConfigHelper.LoadConfig<Dictionary<string,string>>("wx_product_base_price");
if (dicProductBasePrice == null)
dicProductBasePrice = new Dictionary<string, string>();
this.txtPreDays.TextChanged -= txtPreDays_TextChanged;
this.chkPreSale.CheckedChanged -= chkPreSale_CheckedChanged;
this.chkNoPreSale.CheckedChanged -= chkNoPreSale_CheckedChanged;
@@ -782,8 +786,8 @@ namespace TemuKJD.WXForms.ProductForms
var pStr = JsonHelper.SerializeObject(prod);
Invoke(new Action(() =>
{
decimal? price = null;
try { price = prod["skuList"]?[0]["price"].ParseToDecimal() / 100; } catch { }
decimal? minPrice = prod["skuList"]?.Min(sk => sk["price"].ParseToDecimal() / 100);
decimal? maxPrice = prod["skuList"]?.Max(sk => sk["price"].ParseToDecimal() / 100);
var prodId = prod["productId"].ParseToString();
if (dgvSelectProduct.Rows.Cast<DataGridViewRow>().Any(r => r.Cells["colDraftId"].ParseToString() == prodId))
{
@@ -826,8 +830,9 @@ namespace TemuKJD.WXForms.ProductForms
(prod["recommendCategory"].IsJTokenNullOrEmpty() || prod["recommendCategory"]["categoryNames"].IsJTokenNullOrEmpty()) ?
"" : string.Join(" >> ", prod["recommendCategory"]["categoryNames"].Select(s => s.ParseToString())), // 类目名称
prod["spuCode"].ParseToString(),
//price,
prod["price"].ParseToDecimal() * 0.01m,
prod["skuList"].ParseToString(),
//prod["price"].ParseToDecimal() * 0.01m,
"" + minPrice + " - " + maxPrice,
prod["soldNum"].ParseToInt(),
StatusStr,
prod["presaleTag"].ParseToInt() == 1 ? "[预售]" : "",
@@ -1661,7 +1666,7 @@ namespace TemuKJD.WXForms.ProductForms
cancellationTokenSource = new CancellationTokenSource();
Task.Run(() =>
{
SetProdDown(rowLst);
ChangeProdPrice(rowLst);
Invoke(new Action(() =>
{
if (cancellationTokenSource.IsCancellationRequested)
@@ -1682,7 +1687,8 @@ namespace TemuKJD.WXForms.ProductForms
}
void SetProdPrice(List<DataGridViewRow> rowLst)
Dictionary<string, string> dicProductBasePrice = new Dictionary<string, string>();
void ChangeProdPrice(List<DataGridViewRow> rowLst)
{
Invoke(new Action(() =>
{
@@ -1740,60 +1746,265 @@ namespace TemuKJD.WXForms.ProductForms
foreach (var row in rowLstTmp)
{
var prodId = row.Cells["colDraftId"].Value.ParseToString();
prodIdLst.Add(prodId);
}
var skuPricesStr = row.Cells["colSkuMultiPrice"].Value.ParseToString();
var newPrice = txtPrice.Text.ParseToDecimal();
var downProdRst = WXPreSale.DownProd(shop, string.Join(",", prodIdLst));
Thread.Sleep(Randomx.GetId(1000, 1500));
if (!downProdRst.Contains("\"code\":0"))
{
if (downProdRst.Contains("biz magic invalid"))
if (dicProductBasePrice.ContainsKey(prodId))
{
WXShopListForm.Instance.CheckShopLogin(new ShopLoginInfo[] { shop }, false);
if (shop.LoginStatus != "登录成功")
Invoke(new Action(() =>
{
Invoke(new Action(() =>
{
foreach (var row in rowLstTmp)
{
row.Cells["colOpResult"].Value = "店铺掉线";
}
}));
continue;
}
row.Cells["colOpResult"].Value = "商品改价未还原,跳过";
}));
continue;
}
var errmsg = BaseRegex.GetStringJson(ref downProdRst, "msg");
if (string.IsNullOrWhiteSpace(errmsg))
errmsg = downProdRst;
var downProdRst = changePrice(shop, prodId, skuPricesStr, (newPrice * 100).ParseToInt());
Thread.Sleep(Randomx.GetId(200, 500));
if (!downProdRst.Contains("\"code\":0"))
{
if (downProdRst.Contains("biz magic invalid"))
{
WXShopListForm.Instance.CheckShopLogin(new ShopLoginInfo[] { shop }, false);
if (shop.LoginStatus != "登录成功")
{
Invoke(new Action(() =>
{
foreach (var r in rowLstTmp)
{
r.Cells["colOpResult"].Value = "店铺掉线";
}
}));
break;
}
}
var errmsg = BaseRegex.GetStringJson(ref downProdRst, "msg");
if (string.IsNullOrWhiteSpace(errmsg))
errmsg = downProdRst;
Invoke(new Action(() =>
{
row.Cells["colOpResult"].Value = "修改价格失败" + errmsg;
}));
}
// 改价成功
else
{
dicProductBasePrice[prodId] = skuPricesStr;
NaiBa.Util.Helper.ConfigHelper.SaveConfig("wx_product_base_price", dicProductBasePrice);
Invoke(new Action(() =>
{
row.Cells["colOpResult"].Value = "修改价格成功";
row.Cells["colPrice"].Value = newPrice;
}));
}
}
}
}
/// <summary>
/// 修改成指定价格或者还原缓存的原来价格
/// </summary>
/// <param name="shop"></param>
/// <param name="spuId"></param>
/// <param name="skuPrices"></param>
/// <param name="newPrice">新价格 单位分</param>
/// <param name="isRestore">是否是还原价格</param>
/// <returns></returns>
private string changePrice(ShopLoginInfo shop, string spuId, string skuPrices, int newPrice, bool isRestore = false)
{
try
{
var skuListObj = JArray.Parse(skuPrices);
JObject changePriceObj = new JObject();
JArray skuArr = new JArray();
foreach (var sku in skuListObj)
{
JObject obj = new JObject();
obj["spuId"] = spuId;
obj["skuId"] = sku["skuId"];
if (isRestore)
obj["salePrice"] = sku["price"].ParseToInt();
else
obj["salePrice"] = newPrice;
skuArr.Add(obj);
}
changePriceObj["skuList"] = skuArr;
changePriceObj["uploadSource"] = 6;
// string body = @"{""skuList"":[{""spuId"":" + spuId + @",""skuId"":""" + skuId + @""",""salePrice"":" + newPrice + @"}],""uploadSource"":6}";
var rst = OrderForms.WXOrderList.EditPrice(shop, changePriceObj);
if (rst.Contains("\"code\":0"))
{
return rst;
}
else
{
return rst;
}
}
catch (Exception ex)
{
return ex.Message;
}
}
private void btnRestorePrice_Click(object sender, EventArgs e)
{
var rowLst = dgvSelectProduct.Rows.Cast<DataGridViewRow>().Where(r => r.Cells["colCheck"].Value.ParseToBool() == true).ToList();
if (rowLst.Count <= 0)
{
BaseMsgForm.ShowError("请勾选需要操作的商品!");
return;
}
if (rowLst.Any(r => r.Cells["colMsg"].Value.ParseToString().Contains("在售") == false))
{
BaseMsgForm.ShowError("只能勾选在售商品!");
return;
}
if (BaseMsgForm.ShowConfirm("确认要还原商品价格?").DialogResult != DialogResult.OK)
return;
this.pbLoading.Visible = true;
this.btnSetPreSale.Enabled = false;
if (cancellationTokenSource != null && !cancellationTokenSource.IsCancellationRequested)
cancellationTokenSource.Cancel();
cancellationTokenSource = new CancellationTokenSource();
Task.Run(() =>
{
RestoreProdPrice(rowLst);
Invoke(new Action(() =>
{
if (cancellationTokenSource.IsCancellationRequested)
{
this.statusBar.lblTaskStatus.Text = "商品还原价格中断";
BaseMsgForm.ShowInfo("商品还原价格中断");
}
else
{
this.statusBar.lblTaskStatus.Text = "商品还原价格完毕";
BaseMsgForm.ShowInfo("商品还原价格完毕");
}
this.pbLoading.Visible = false;
this.btnSetPreSale.Enabled = true;
}));
}, cancellationTokenSource.Token);
}
void RestoreProdPrice(List<DataGridViewRow> rowLst)
{
Invoke(new Action(() =>
{
this.pbLoading.Visible = true;
this.statusBar.lblTaskStatus.Text = "正在还原价格" + "...".Substring(0, DateTime.Now.Second % 4);
this.statusBar.pgbTaskProgress.Maximum = dgvSelectProduct.Rows.Count;
this.statusBar.UpdateProgressBar(0);
}));
while (rowLst.Count > 0)
{
if (cancellationTokenSource.IsCancellationRequested)
break;
var r0 = rowLst.FirstOrDefault();
var shopId = r0.Cells["colSupplierId"].Value.ParseToString();
var shop = GlobalConfig.ShopLoginList.FirstOrDefault(s => s.ShopId == shopId);
var rowLstTmp = rowLst.Where(r => r.Cells["colSupplierId"].Value.ParseToString() == shopId).Take(50).ToList();
foreach (var r in rowLstTmp)
{
rowLst.Remove(r);
Invoke(new Action(() =>
{
foreach (var row in rowLstTmp)
{
if (downProdRst.Contains("\"code\":108002"))
{
var prodId = row.Cells["colDraftId"].Value.ParseToString();
if (downProdRst.Contains(prodId))
row.Cells["colOpResult"].Value = "修改价格失败,请检查商品是否已下架或不存在";
else
row.Cells["colOpResult"].Value = "修改价格成功";
}
else
row.Cells["colOpResult"].Value = errmsg;
}
this.statusBar.lblTaskStatus.Text = "正在还原价格" + "...".Substring(0, DateTime.Now.Second % 4);
this.statusBar.UpdateProgressBar(this.statusBar.pgbTaskProgress.Value + 1);
r.Cells["colOpResult"].Value = "";
}));
}
if (downProdRst.Contains("\"code\":0"))
if (shop == null)
{
Invoke(new Action(() =>
{
foreach (var row in rowLstTmp)
foreach (var r in rowLstTmp)
{
row.Cells["colOpResult"].Value = "修改价格成功";
r.Cells["colOpResult"].Value = "店铺不存在";
}
}));
continue;
}
if (shop.LoginStatus != "登录成功")
{
Invoke(new Action(() =>
{
foreach (var r in rowLstTmp)
{
r.Cells["colOpResult"].Value = "店铺掉线";
}
}));
continue;
}
List<string> prodIdLst = new List<string>();
foreach (var row in rowLstTmp)
{
var prodId = row.Cells["colDraftId"].Value.ParseToString();
if (!dicProductBasePrice.ContainsKey(prodId))
{
Invoke(new Action(() =>
{
row.Cells["colOpResult"].Value = "未找到价格修改记录,无法还原,跳过";
}));
continue;
}
var skuPricesStr = dicProductBasePrice[prodId];
var downProdRst = changePrice(shop, prodId, skuPricesStr, -1, true);
Thread.Sleep(Randomx.GetId(200, 500));
if (!downProdRst.Contains("\"code\":0"))
{
if (downProdRst.Contains("biz magic invalid"))
{
WXShopListForm.Instance.CheckShopLogin(new ShopLoginInfo[] { shop }, false);
if (shop.LoginStatus != "登录成功")
{
Invoke(new Action(() =>
{
foreach (var r in rowLstTmp)
{
r.Cells["colOpResult"].Value = "店铺掉线";
}
}));
break;
}
}
var errmsg = BaseRegex.GetStringJson(ref downProdRst, "msg");
if (string.IsNullOrWhiteSpace(errmsg))
errmsg = downProdRst;
Invoke(new Action(() =>
{
row.Cells["colOpResult"].Value = "还原失败" + errmsg;
}));
}
// 还原价格成功
else
{
dicProductBasePrice.Remove(prodId);
NaiBa.Util.Helper.ConfigHelper.SaveConfig("wx_product_base_price", dicProductBasePrice);
Invoke(new Action(() =>
{
row.Cells["colOpResult"].Value = "还原价格成功";
var skuListObj = JArray.Parse(skuPricesStr);
row.Cells["colPrice"].Value = skuListObj.Min(sk => sk["price"].ParseToInt() * 0.01m) + " - " + skuListObj.Max(sk => sk["price"].ParseToInt() * 0.01m);
}));
}
}
}
}