获取活动及折扣信息

This commit is contained in:
wule committed 2025-08-05 11:31:10 +08:00
1 parent e2fd294b85
commit f73f56eea4
3 files changed
+447 -323

No files matched your search

@@ -24,6 +24,7 @@ namespace TemuKJD.KJDForms.SalesCenterForms
private CheckBox chkSelectAll = null;
public CancellationTokenSource cancellationTokenSource { get; set; } = null;
public List<TemuActivityThematicInfo> lstActivityThematic { get; set; } = new List<TemuActivityThematicInfo>();
public List<string> lstShopIdSelect = new List<string>();
public MarketingActivityForm()
{
@@ -174,6 +175,29 @@ namespace TemuKJD.KJDForms.SalesCenterForms
}
}
private void dgvActivity_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex < 0 || e.RowIndex < 0)
return;
string headerText = dgvActivity.Columns[e.ColumnIndex].HeaderText;
string columnName = dgvActivity.Columns[e.ColumnIndex].Name;
long activityId = dgvActivity.Rows[e.RowIndex].Cells["colActivityId"].Value.ParseToLong();
var activity = lstActivityThematic.FirstOrDefault(s => s.activityThematicId == activityId);
var row = dgvActivity.Rows[e.RowIndex];
switch (columnName)
{
case "colCheckActivity":
row.Cells["colCheckActivity"].Value = !row.Cells["colCheckActivity"].Value.ParseToBool();
//chkSelectAll.CheckedChanged -= chkSelectAll_CheckedChanged;
//if (dgvShop.Rows.Cast<DataGridViewRow>().All(r => r.Cells["colSelect"].Value.ParseToBool()))
// chkSelectAll.Checked = true;
//else
// chkSelectAll.Checked = false;
//chkSelectAll.CheckedChanged += chkSelectAll_CheckedChanged;
break;
}
}
private void chkFullShop_CheckedChanged(object sender, EventArgs e)
{
@@ -208,11 +232,11 @@ namespace TemuKJD.KJDForms.SalesCenterForms
Task.Run(() =>
{
var lstShopIdTemp = new List<string>();
lstShopIdTemp.AddRange(lstShopId);
lstShopIdSelect.Clear();
lstShopIdSelect.AddRange(lstShopId);
int i = 0;
foreach (var shopId in lstShopIdTemp)
foreach (var shopId in lstShopIdSelect)
{
if (lstActivityThematic.Count > 0)
break;
@@ -220,7 +244,7 @@ namespace TemuKJD.KJDForms.SalesCenterForms
this.Invoke(new Action(() =>
{
this.statusBar.lblTaskStatus.Text = "正在查询活动...";
this.statusBar.pgbTaskProgress.Maximum = lstShopIdTemp.Count;
this.statusBar.pgbTaskProgress.Maximum = lstShopIdSelect.Count;
this.statusBar.UpdateProgressBar(i + 1);
i++;
}));
@@ -364,133 +388,29 @@ namespace TemuKJD.KJDForms.SalesCenterForms
}));
}
foreach (var shopId in lstShopIdTemp)
foreach (var actInfo in lstActivityThematic)
{
if (cancellationTokenSource.IsCancellationRequested)
break;
var shop = GlobalConfig.ShopLoginList.FirstOrDefault(s => s.ShopId == shopId);
if (shop == null)
continue;
shop.LastRefreshDate = "";
}
i = 0;
// 查询所有活动是否有店铺可以报名,可报名的才显示
foreach (var pInfo in lstActivityThematic)
{
this.Invoke(new Action(() =>
{
this.statusBar.lblTaskStatus.Text = "正在查询活动" + (i + 1) + "...";
this.statusBar.pgbTaskProgress.Maximum = lstActivityThematic.Count;
this.statusBar.UpdateProgressBar(i + 1);
i++;
}));
foreach (var shopId in lstShopIdTemp)
{
if (cancellationTokenSource.IsCancellationRequested)
break;
var shop = GlobalConfig.ShopLoginList.FirstOrDefault(s => s.ShopId == shopId);
if (shop == null)
continue;
#region 检测登录
if (shop.LoginStatus2 != "国际版在线")
{
continue;
}
if (ShopListForm.Instance == null)
ShopListForm.Instance = new ShopListForm();
if (shop.LastRefreshDate == "")
ShopListForm.Instance.CheckShopLogin(new string[] { shop.LoginName }, false);
if (shop.LoginStatus2 != "国际版在线")
{
Invoke(new Action(() =>
{
var rows = this.dgvShop.Rows.Cast<DataGridViewRow>().Where(r => r.Cells["colShopLoginName"].Value.ToString() == shop.LoginName);
foreach (var row in rows)
{
row.Cells["colMessage"].Value = "店铺掉线";
row.Cells["colLoginStatus"].Value = "国际版未登录";
row.Cells["colCanUse"].Value = false;
}
}));
continue;
}
#endregion
int tryCount = 0;
tryAgain:
Thread.Sleep(100 + Randomx.GetId(200));
string jsonStr = GetActivityMatch(shop, pInfo.activityThematicId.ParseToLong());
if (jsonStr.Contains("指定的值含有无效的控制字符"))
{
Invoke(new Action(() =>
{
var rows = this.dgvShop.Rows.Cast<DataGridViewRow>().Where(r => r.Cells["colShopLoginName"].Value.ToString() == shop.LoginName);
foreach (var row in rows)
{
row.Cells["colMessage"].Value = "店铺掉线";
row.Cells["colLoginStatus"].Value = "国际版未登录";
row.Cells["colCanUse"].Value = false;
}
}));
continue;
}
if (jsonStr.Contains("当前操作人数太多,请稍后重试") && tryCount < 5)
{
tryCount++;
goto tryAgain;
}
if (jsonStr.Contains("当前操作人数太多,请稍后重试"))
{
}
if (!jsonStr.Contains("\"success\":true"))
{
//isError = true;
//string sErr = BaseRegex.GetStringJson(ref jsonStr, "errorMsg");
//if (!string.IsNullOrWhiteSpace(sErr) && !errMsg.Contains(sErr))
// errMsg += sErr + " ";
//page++;
continue;
}
if (jsonStr.Contains("\"success\":true"))
{
//var jArr = new JArray();
//try
//{
// var jObj = JObject.Parse(jsonStr);
// jArr = jObj["result"]?["activityList"] as JArray;
// if (jArr == null)
// jArr = new JArray();
//}
//catch (Exception ex) { BaseMsgForm.ShowError(ex.Message); }
//foreach (var item in jArr)
//{
//}
break;
}
}
Invoke(new Action(() =>
{
int rowId = this.dgvActivity.Rows.Add(
false,
pInfo.activityThematicId,
pInfo.activityThematicName,
BaseClass.StampToDateTime((int)(pInfo.startTime.ParseToLong() / 1000)).ToString("yyyy/MM/dd HH:mm:ss") + "~" +
BaseClass.StampToDateTime((int)(pInfo.endTime.ParseToLong() / 1000)).ToString("yyyy/MM/dd HH:mm:ss") + "(" +
pInfo.durationDays + "天)",
(BaseClass.StampToDateTime((int)(pInfo.enrollDeadLine.ParseToLong() / 1000)) - DateTime.Now).Days + "天后截止",
pInfo.discount
actInfo.activityThematicId,
actInfo.activityThematicName,
BaseClass.StampToDateTime((int)(actInfo.startTime.ParseToLong() / 1000)).ToString("yyyy/MM/dd HH:mm:ss") + "~" +
BaseClass.StampToDateTime((int)(actInfo.endTime.ParseToLong() / 1000)).ToString("yyyy/MM/dd HH:mm:ss") + "(" +
actInfo.durationDays + "天)",
(BaseClass.StampToDateTime((int)(actInfo.enrollDeadLine.ParseToLong() / 1000)) - DateTime.Now).Days + "天后截止",
actInfo.discount
);
dgvActivity.Rows[rowId].Height = 40;
}));
}
//进详情页查询折扣比例信息,因部分活动可能无符合条件商品,查询可能速度较慢
if(chkGetDiscount.Checked)
GetDiscount();
Invoke(new Action(() =>
{
ucGroupBoxEx2.Enabled = true;
@@ -505,6 +425,193 @@ namespace TemuKJD.KJDForms.SalesCenterForms
}, cancellationTokenSource.Token);
}
// 查询活动折扣
void GetDiscount()
{
foreach (var shopId in lstShopIdSelect)
{
if (cancellationTokenSource.IsCancellationRequested)
break;
var shop = GlobalConfig.ShopLoginList.FirstOrDefault(s => s.ShopId == shopId);
if (shop == null)
continue;
shop.LastRefreshDate = "";
}
int i = 0;
// 查询所有活动是否有店铺可以报名,可报名的才显示
foreach (var actInfo in lstActivityThematic)
{
this.Invoke(new Action(() =>
{
this.statusBar.lblTaskStatus.Text = "正在查询活动折扣" + (i + 1) + "...";
this.statusBar.pgbTaskProgress.Maximum = lstActivityThematic.Count;
this.statusBar.UpdateProgressBar(i + 1);
i++;
}));
if (actInfo == null)
continue;
var actRow = this.dgvActivity.Rows.Cast<DataGridViewRow>()
.FirstOrDefault(r => r.Cells["colActivityId"].Value.ParseToLong() == actInfo?.activityThematicId);
if (actRow == null)
continue;
if (!string.IsNullOrWhiteSpace(actInfo.discount) && actInfo.discount != "-")
{
this.Invoke(new Action(() =>
{
actRow.Cells["colActivityMsg"].Value = "折扣查询成功";
}));
continue;
}
this.Invoke(new Action(() =>
{
actRow.Cells["colActivityMsg"].Value = "正在查询折扣...";
}));
foreach (var shopId in lstShopIdSelect)
{
if (cancellationTokenSource.IsCancellationRequested)
break;
var shop = GlobalConfig.ShopLoginList.FirstOrDefault(s => s.ShopId == shopId);
if (shop == null)
continue;
var shopRow = this.dgvShop.Rows.Cast<DataGridViewRow>()
.FirstOrDefault(r => r.Cells["colShopId"].Value.ParseToString() == shop.ShopId);
if (shopRow == null)
continue;
Invoke(new Action(() =>
{
shopRow.Cells["colMessage"].Value = "查询折扣,活动:" + actInfo.activityThematicName;
}));
#region 检测登录
if (shop.LoginStatus2 != "国际版在线")
{
continue;
}
if (ShopListForm.Instance == null)
ShopListForm.Instance = new ShopListForm();
if (shop.LastRefreshDate == "")
ShopListForm.Instance.CheckShopLogin(new string[] { shop.LoginName }, false);
if (shop.LoginStatus2 != "国际版在线")
{
Invoke(new Action(() =>
{
var shopRows = this.dgvShop.Rows.Cast<DataGridViewRow>().Where(r => r.Cells["colShopLoginName"].Value.ToString() == shop.LoginName);
foreach (var r in shopRows)
{
r.Cells["colMessage"].Value = "店铺掉线";
r.Cells["colLoginStatus"].Value = "国际版未登录";
r.Cells["colCanUse"].Value = false;
}
}));
continue;
}
#endregion
int tryCount = 0;
tryAgain:
Thread.Sleep(100 + Randomx.GetId(200));
string jsonStr = GetActivityMatch(shop, actInfo.activityThematicId.ParseToLong());
if (jsonStr.Contains("指定的值含有无效的控制字符"))
{
Invoke(new Action(() =>
{
var shopRows = this.dgvShop.Rows.Cast<DataGridViewRow>().Where(r => r.Cells["colShopLoginName"].Value.ToString() == shop.LoginName);
foreach (var r in shopRows)
{
r.Cells["colMessage"].Value = "店铺掉线";
r.Cells["colLoginStatus"].Value = "国际版未登录";
r.Cells["colCanUse"].Value = false;
}
}));
continue;
}
if (jsonStr.Contains("当前操作人数太多,请稍后重试") && tryCount < 5)
{
tryCount++;
goto tryAgain;
}
if (jsonStr.Contains("当前操作人数太多,请稍后重试"))
{
}
if (!jsonStr.Contains("\"success\":true"))
{
//isError = true;
//string sErr = BaseRegex.GetStringJson(ref jsonStr, "errorMsg");
//if (!string.IsNullOrWhiteSpace(sErr) && !errMsg.Contains(sErr))
// errMsg += sErr + " ";
//page++;
string sErr = BaseRegex.GetStringJson(ref jsonStr, "errorMsg");
if (string.IsNullOrEmpty(sErr))
sErr = BaseRegex.GetStringJson(ref jsonStr, "err_msg");
if (string.IsNullOrEmpty(sErr))
sErr = jsonStr;
Invoke(new Action(() =>
{
actRow.Cells["colActivityMsg"].Value = sErr;
shopRow.Cells["colMessage"].Value = "";
}));
continue;
}
if (jsonStr.Contains("\"success\":true"))
{
var jArr = new JArray();
try
{
var jObj = JObject.Parse(jsonStr);
jArr = jObj["result"]?["matchList"] as JArray;
if (jArr == null)
jArr = new JArray();
}
catch (Exception ex) { BaseMsgForm.ShowError(ex.Message); }
if (jArr.Count() == 0)
{
Invoke(new Action(() =>
{
shopRow.Cells["colMessage"].Value = "";
}));
continue;
}
try
{
var skuObj = jArr[0]["skcList"][0]["skuList"][0];
var suggestActivityPrice = skuObj["suggestActivityPrice"].ParseToInt();
var dailyPrice = skuObj["dailyPrice"].ParseToInt();
var discount = suggestActivityPrice * 100.0 / dailyPrice;
actInfo.discount = Math.Round(discount) + "折";
Invoke(new Action(() =>
{
actRow.Cells["colDiscount"].Value = actInfo.discount;
actRow.Cells["colActivityMsg"].Value = "折扣查询成功";
shopRow.Cells["colMessage"].Value = "";
}));
break;
}
catch (Exception ex) { BaseMsgForm.ShowError(ex.Message); }
}
}
if(string.IsNullOrEmpty(actInfo.discount) || !actInfo.discount.Contains("折"))
{
this.Invoke(new Action(() =>
{
actRow.Cells["colActivityMsg"].Value = "没有活动可用商品,折扣查询失败";
}));
}
}
}
/// <summary>
/// 获取活动列表
/// </summary>