Files
KJD/TEMUKJD/KJDForms/SalesCenterForms/MarketingActivityForm.cs
T

537 lines
23 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using AllBase;
using AllBase.Helper;
using AllBase.Model.Result;
using NaiBa.Util.Extension;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using TemuKJD.Extension;
using TemuKJD.KJDForms.BaseForms;
using TemuKJD.KJDForms.ShopForms;
namespace TemuKJD.KJDForms.SalesCenterForms
{
public partial class MarketingActivityForm : Form
{
private CheckBox chkSelectAll = null;
public CancellationTokenSource cancellationTokenSource { get; set; } = null;
public List<TemuActivityThematicInfo> lstActivityThematic { get; set; } = new List<TemuActivityThematicInfo>();
public MarketingActivityForm()
{
InitializeComponent();
}
private void MarketingActivityForm_Load(object sender, EventArgs e)
{
chkSelectAll = dgvShop.Columns["colSelect"].AddHeaderCheckBox(HorizontalAlignment.Center, 0, 0, null, null, false);
dgvShop.Columns["colSelect"].HeaderText = "";
chkSelectAll.Width = chkSelectAll.Height = 13;
chkSelectAll.CheckedChanged += chkSelectAll_CheckedChanged;
this.dgvActivity.AddSelectorColumn("colCheckActivity");
this.dgvProduct.AddSelectorColumn("colCheckProduct");
}
private void chkSelectAll_CheckedChanged(object sender, EventArgs e)
{
if (!chkSelectAll.Checked)
{
foreach (DataGridViewRow row in dgvShop.Rows)
{
row.Cells["colSelect"].Value = false;
}
}
else
{
var list = GlobalConfig.ShopLoginList.Where(s => s.LoginStatus2 == "国际版在线").Select(ss => ss.ShopId);
foreach (DataGridViewRow row in dgvShop.Rows)
{
if (list.Any(l => l == row.Cells["colShopId"]?.Value?.ToString()))
row.Cells["colSelect"].Value = true;
}
}
}
private void MarketingActivityForm_VisibleChanged(object sender, EventArgs e)
{
if (this.Visible)
RefreshShop();
}
private void btnRefreshShop_Click(object sender, EventArgs e)
{
RefreshShop();
}
private void RefreshShop()
{
// 店铺Id
var selectIds = dgvShop.Rows.Cast<DataGridViewRow>().Where(row
=> row.Cells["colSelect"]?.Value.ParseToBool() == true).Select(r => r.Cells["colShopId"]?.Value.ToString()).ToList();
// 获取被删除的店铺
List<DataGridViewRow> dgvRowList = dgvShop.Rows.Cast<DataGridViewRow>().Where(row
=> !GlobalConfig.ShopLoginList.Any(s => s.ShopId == row.Cells["colShopId"]?.Value?.ToString())).ToList();
// 更新被删除的店铺列表
foreach (var row in dgvRowList)
{
dgvShop.Rows.Remove(row);
}
// 绑定其他店铺(含新导入的店铺)
foreach (ShopLoginInfo shopEntity in GlobalConfig.ShopLoginList)
{
if (!string.IsNullOrEmpty(shopEntity.ShopId))
BindShop(shopEntity, selectIds);
}
}
private void BindShop(ShopLoginInfo shopEntity, List<string> selectShopId = null)
{
if (shopEntity == null)
return;
if (selectShopId == null)
selectShopId = new List<string>();
bool canUse = shopEntity.LoginStatus2 == "国际版在线";
var row = dgvShop.Rows.Cast<DataGridViewRow>().FirstOrDefault(p => p.Cells.GetCellValueFromColumnName("colShopId").ParseToString() == shopEntity.ShopId);
if (row == null)
{
this.dgvShop.Rows.Add(
selectShopId.Contains(shopEntity.ShopId) && canUse,
shopEntity.LoginName,
shopEntity.ShopId,
shopEntity.ShopAccount,
shopEntity.LoginStatus2 == "国际版在线" ? "在线" : "未登录",
canUse,
string.Empty
);
}
else
{
row.Cells["colSelect"].Value = selectShopId.Contains(shopEntity.ShopId) && canUse;
row.Cells["colShopLoginName"].Value = shopEntity.LoginName;
row.Cells["colShopName"].Value = shopEntity.ShopAccount;
row.Cells["colLoginStatus"].Value = shopEntity.LoginStatus2 == "国际版在线" ? "在线" : "未登录";
row.Cells["colCanUse"].Value = canUse;
}
}
private void dgvShop_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex < 0 || e.RowIndex < 0)
return;
string columnName = dgvShop.Columns[e.ColumnIndex].Name;
switch (columnName)
{
case "colSelect":
dgvShop.CellContentClick -= dgvShop_CellContentClick;
dgvShop_CellContentClick(sender, e);
break;
default:
dgvShop.CellContentClick -= dgvShop_CellContentClick;
dgvShop.CellContentClick += dgvShop_CellContentClick;
break;
}
}
private void dgvShop_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex < 0 || e.RowIndex < 0)
return;
string headerText = dgvShop.Columns[e.ColumnIndex].HeaderText;
string columnName = dgvShop.Columns[e.ColumnIndex].Name;
long shopId = dgvShop.Rows[e.RowIndex].Cells["colShopId"].Value.ParseToLong();
string shopAccount = dgvShop.Rows[e.RowIndex].Cells["colShopName"].Value.ToString();
var shop = GlobalConfig.ShopLoginList.FirstOrDefault(s => s.ShopId == shopId.ToString());
var row = dgvShop.Rows[e.RowIndex];
switch (columnName)
{
case "colSelect":
if (row.Cells["colCanUse"].Value.ParseToBool())
{
row.Cells["colSelect"].Value = !row.Cells["colSelect"].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;
}
else
{
BaseMsgForm.ShowInfo("店铺国际版未登录!");
}
break;
}
}
private void chkFullShop_CheckedChanged(object sender, EventArgs e)
{
}
private void chkNoSaleProd_CheckedChanged(object sender, EventArgs e)
{
}
private void btnSearchActivity_Click(object sender, EventArgs e)
{
var lstShopId = dgvShop.Rows.Cast<DataGridViewRow>().Where(row
=> row.Cells["colSelect"]?.Value.ParseToBool() == true).Select(r => r.Cells["colShopId"]?.Value.ToString()).ToList();
if (lstShopId.Count == 0)
{
BaseMsgForm.ShowInfo("请选择店铺!");
return;
}
#region 重置控件
lstActivityThematic.Clear();
dgvActivity.Rows.Clear();
var dgvRows = this.dgvShop.Rows.Cast<DataGridViewRow>();
foreach (var row in dgvRows)
{
row.Cells["colMessage"].Value = "";
}
ucGroupBoxEx2.Enabled = false;
ucGroupBoxEx3.Enabled = false;
cancellationTokenSource = new CancellationTokenSource();
#endregion
Task.Run(() =>
{
var lstShopIdTemp = new List<string>();
lstShopIdTemp.AddRange(lstShopId);
int i = 0;
foreach (var shopId in lstShopIdTemp)
{
if (lstActivityThematic.Count > 0)
break;
this.Invoke(new Action(() =>
{
this.statusBar.lblTaskStatus.Text = "正在查询活动...";
this.statusBar.pgbTaskProgress.Maximum = lstShopIdTemp.Count;
this.statusBar.UpdateProgressBar(i + 1);
i++;
}));
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();
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
Invoke(new Action(() =>
{
var rows = this.dgvShop.Rows.Cast<DataGridViewRow>().Where(r => r.Cells["colShopId"].Value.ToString() == shop.ShopId);
foreach (var row in rows)
{
row.Cells["colMessage"].Value = "正在查询";
}
}));
int page = 1;
int totalPage = 1;
bool isError = false;
string errMsg = "";
while (page <= totalPage)
{
if (cancellationTokenSource.IsCancellationRequested)
break;
int tryCount = 0;
tryAgain:
Thread.Sleep(100 + Randomx.GetId(200));
string jsonStr = GetActivityList(shop);
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;
}
}));
errMsg = "店铺可能已掉线,指定的值含有无效的控制字符";
isError = true;
break;
}
if (jsonStr.Contains("当前操作人数太多,请稍后重试") && tryCount < 5)
{
tryCount++;
goto tryAgain;
}
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)
{
string sItem = item.ToString();
TemuActivityInfo pActivity = JsonHelper.DeserializeJsonToObject<TemuActivityInfo>(sItem); // 反序列化得到类
if (pActivity == null || pActivity.activityName != "大促进阶-限时活动")
continue;
foreach (var pInfo in pActivity.thematicList)
{
lstActivityThematic.Add(pInfo);
string name = pInfo.activityThematicName;
pInfo.discount = BaseRegex.GetMatchRegexString(ref name, "(\\d{1,3}折)");
pInfo.discount = string.IsNullOrWhiteSpace(pInfo.discount) ? "-" : pInfo.discount;
}
}
int total = BaseRegex.GetStringJson(ref jsonStr, "total").ParseToInt();
totalPage = Math.Ceiling(total / 10.0).ParseToInt();
}
page++;
}
Invoke(new Action(() =>
{
var rows = this.dgvShop.Rows.Cast<DataGridViewRow>().Where(r => r.Cells["colShopId"].Value.ToString() == shop.ShopId);
if (!isError)
{
foreach (var row in rows)
{
row.Cells["colMessage"].Value = "已查询";
}
}
else
{
foreach (var row in rows)
{
row.Cells["colMessage"].Value = "查询失败 " + errMsg;
}
}
}));
}
foreach (var shopId in lstShopIdTemp)
{
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
);
dgvActivity.Rows[rowId].Height = 40;
}));
}
Invoke(new Action(() =>
{
ucGroupBoxEx2.Enabled = true;
ucGroupBoxEx3.Enabled = true;
string sRst = "查询结束!";
if (cancellationTokenSource.IsCancellationRequested)
sRst = "已取消查询!";
BaseMsgForm.ShowInfo(sRst);
this.statusBar.lblTaskStatus.Text = sRst;
}));
}, cancellationTokenSource.Token);
}
/// <summary>
/// 获取活动列表
/// </summary>
/// <param name="shop"></param>
/// <returns></returns>
private string GetActivityList(ShopLoginInfo shop)
{
string url = "https://agentseller.temu.com/api/kiana/gamblers/marketing/enroll/activity/list";
string body;
body = @"{""needCanEnrollCnt"":true}";
string refere = "https://agentseller.temu.com/activity/marketing-activity";
return KJDHttpResponse.PostResponse2(shop, url, body, refere);
}
/// <summary>
/// 获取符合店铺的活动
/// </summary>
/// <param name="shop"></param>
/// <returns></returns>
private string GetActivityMatch(ShopLoginInfo shop, long? thematicId)
{
string url = "https://agentseller.temu.com/api/kiana/gamblers/marketing/enroll/scroll/match";
string body;
body = @"{""activityType"":13,""activityThematicId"":" + thematicId + @",""rowCount"":50}";
string refere = "https://agentseller.temu.com/activity/marketing-activity/detail-new?type=13&thematicId=" + thematicId;
return KJDHttpResponse.PostResponse2(shop, url, body, refere);
}
}
}