Files
KJD/TEMUKJD/KJDForms/ShopForms/MaterialManageForm.cs
T

371 lines
16 KiB
C#

using AllBase.Helper;
using TemuKJD.Extension;
using TemuKJD.KJDForms.BaseForms;
using NaiBa.Util.Extension;
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 Work.BLL.Upload;
using Work.DouyinApiClass;
namespace TemuKJD.KJDForms.ShopForms
{
public partial class MaterialManageForm : Form
{
private CheckBox chkSelectAll = null;
CancellationTokenSource cancellationTokenSource = null;
public MaterialManageForm()
{
InitializeComponent();
}
private void MaterialManageForm_Load(object sender, EventArgs e)
{
chkSelectAll = this.dgvShop.Columns["colSelect"].AddHeaderCheckBox(HorizontalAlignment.Center, 0, 0, null, null, false, null, true);
chkSelectAll.Width = chkSelectAll.Height = 13;
chkSelectAll.CheckedChanged += chkSelectAll_CheckedChanged;
dtEndTime.Value = DateTime.Now.Date.AddDays(-8);
dtBaginTime.Value = DateTime.Now.Date.AddYears(-1);
}
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.ApiAuthStatus == "授权成功").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;
}
}
}
public void LoadShops()
{
// 获取被删除的店铺
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);
}
for (int i = 0; i < GlobalConfig.ShopLoginList.Count; i++)
{
var shop = GlobalConfig.ShopLoginList[i];
var row = dgvShop.Rows.Cast<DataGridViewRow>().FirstOrDefault(r => r.Cells["colShopId"].Value?.ToString() == shop.ShopId);
if (row == null)
dgvShop.Rows.Add(false, shop.ShopId, shop.ShopAccount, shop.LoginStatus);
else
{
row.Cells["colAuthStatus"].Value = shop.ApiAuthStatus;
}
}
}
private void btnRefreshShop_Click(object sender, EventArgs e)
{
LoadShops();
}
private void MaterialManageForm_VisibleChanged(object sender, EventArgs e)
{
if (Visible)
LoadShops();
}
private void dgvShop_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex < 0 || e.RowIndex < 0)
return;
string colId = dgvShop.Columns[e.ColumnIndex].Name;
if (colId == "colSelect")
{
dgvShop.Rows[e.RowIndex].Cells["colSelect"].Value = !dgvShop.Rows[e.RowIndex].Cells["colSelect"].Value.ParseToBool();
}
}
private void btnGetProductCount_Click(object sender, EventArgs e)
{
var lstShopId = dgvShop.Rows.Cast<DataGridViewRow>()
.Where(r => r.Cells["colSelect"].Value.ParseToBool())
.Select(r => r.Cells["colShopId"].Value?.ToString()).ToList();
if (lstShopId.Count == 0)
{
BaseMsgForm.ShowInfo("请选择要获取素材的店铺");
return;
}
this.btnGetProductCount.Enabled = false;
foreach (DataGridViewRow row in dgvShop.Rows)
{
if (lstShopId.Contains(row.Cells["colShopId"]?.Value))
row.Cells["colMessage"].Value = "";
}
Task.Run(() =>
{
GetShopMaterial();
Invoke(new Action(() =>
{
btnGetProductCount.Enabled = true;
BaseMsgForm.ShowInfo("素材获取完成");
}));
});
}
void GetShopMaterial()
{
}
private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
{
}
private void btnClear_Click(object sender, EventArgs e)
{
var lstShopId = dgvShop.Rows.Cast<DataGridViewRow>()
.Where(r => r.Cells["colSelect"].Value.ParseToBool())
.Select(r => r.Cells["colShopId"].Value?.ToString()).ToList();
if (lstShopId.Count == 0)
{
BaseMsgForm.ShowInfo("请选择要清理素材的店铺");
return;
}
this.btnClear.Enabled = false;
foreach (DataGridViewRow row in dgvShop.Rows)
{
if (lstShopId.Contains(row.Cells["colShopId"]?.Value))
row.Cells["colMessage"].Value = "";
}
int n = 0;
cancellationTokenSource = new CancellationTokenSource();
Task.Run(() =>
{
var shopList = GlobalConfig.ShopLoginList.Where(s => s.ApiAuthStatus == "授权成功" && lstShopId.Contains(s.ShopId));
foreach (var shop in shopList)
{
DataGridViewRow row = null;
this.Invoke(new Action(() =>
{
n++;
var total = lstShopId.Count();
this.statusBar.lblTaskStatus.Text = "正在清理素材...";
this.statusBar.pgbTaskProgress.Value = n;
this.statusBar.pgbTaskProgress.Text = n + "/" + total;
this.statusBar.pgbTaskProgress.Maximum = total;
row = this.dgvShop.Rows.Cast<DataGridViewRow>().FirstOrDefault(r => r.Cells["colShopId"].Value.ToString() == shop.ShopId);
if (row != null) { row.Cells["colMessage"].Value = "正在获取素材文件夹"; }
}));
if (shop.PayStatus != 1)
{
if (row != null) { Invoke(new Action(() => { row.Cells["colMessage"].Value = "当前为试用版,请购买专业版"; })); }
goto operateEnd;
}
List<string> folderIds = new List<string>();
for (int page = 1; page < 9999; page++)
{
if (cancellationTokenSource.IsCancellationRequested)
{
if (row != null) { Invoke(new Action(() => { row.Cells["colMessage"].Value = "获取素材文件夹中断"; })); }
goto operateEnd;
}
var folder = new UploadMaterialBLL().GetApiMaterialFolder(shop, "0", 50, page, 1,
this.dtBaginTime.Value.ToString("yyyy-MM-dd HH:mm:ss"), this.dtEndTime.Value.AddDays(1).ToString("yyyy-MM-dd HH:mm:ss"));
if (!folder.Contains("\"code\":10000"))
folder = new UploadMaterialBLL().GetApiMaterialFolder(shop, "0", 50, 1, 1,
this.dtBaginTime.Value.ToString("yyyy-MM-dd HH:mm:ss"), this.dtEndTime.Value.AddDays(1).ToString("yyyy-MM-dd HH:mm:ss"));
Thread.Sleep(500);
if (folder.Contains("\"code\":10000"))
{
var JObj = AllBase.JsonHelper.JObjectParse(folder);
if (JObj?["data"]?["folder_info_list"] != null)
{
if (JObj?["data"]?["folder_info_list"].Count() == 0)
break;
foreach (Newtonsoft.Json.Linq.JObject j in JObj?["data"]?["folder_info_list"])
{
folderIds.Add(j["folder_id"]?.ToString());
}
}
else
break;
}
}
this.Invoke(new Action(() =>
{
if (row != null) { row.Cells["colMessage"].Value = "正在删除素材文件夹"; }
}));
string deleteIdStr = "";
foreach (var folderid in folderIds)
{
if (cancellationTokenSource.IsCancellationRequested)
{
if (row != null) { Invoke(new Action(() => { row.Cells["colMessage"].Value = "删除素材文件夹中断"; })); }
goto operateEnd;
}
deleteIdStr += "\\\"" + folderid + "\\\",";
if (folderid == folderIds.LastOrDefault() || deleteIdStr.TrimEnd(',').Split(',').Count() == 20)
{
DouyinApi api = new DouyinApi(shop);
api.JsonData = new Dictionary<string, string>();
api.JsonData.Add("folder_ids", "[" + deleteIdStr.TrimEnd(',') + "]");
string html = api.MaterialDeleteFolder();
Thread.Sleep(500);
// {"code":10000,"data":{"failed_map":{},"success_ids":["70344526568442923861819","70673777705522138001819","70677148692769999721819","70682264025747458561819","70684999170644871991819","70689259618189970281819","70696976534408563231819","70700172511513316241819","70703300876101225031819","70707122616226450031819","70711553799834012461819","70722448124932426591819","70726708088621960061819","70752688859723205501819","70763331833771133521819"]},"err_no":0,"log_id":"20220326131033010212035232207F377B","message":"success","msg":"success","sub_code":"","sub_msg":""}
if (html.Contains("\"code\":10000"))
{
if (!html.Contains("\"failed_map\":{}"))
{
AllBase.LogSave.AddFile(html);
}
//AllBase.FileBase.AppendAllText("material_failed_log_" + DateTime.Now.ToString("yyyyMMdd") + ".txt", html + "\r\n");
}
else
{
}
deleteIdStr = "";
}
}
this.Invoke(new Action(() =>
{
if (row != null) { row.Cells["colMessage"].Value = "素材文件夹删除完成"; }
}));
Thread.Sleep(2000);
// 删除根目录素材文件
this.Invoke(new Action(() =>
{
if (row != null) { row.Cells["colMessage"].Value = "正在获取根目录素材文件"; }
}));
List<string> MaterialIds = new List<string>();
for (int page = 1; page < 9999; page++)
{
if (cancellationTokenSource.IsCancellationRequested)
{
if (row != null) { Invoke(new Action(() => { row.Cells["colMessage"].Value = "获取根目录素材文件中断"; })); }
goto operateEnd;
}
var Material = new UploadMaterialBLL().GetApiMaterial(shop, "0", 100, page, 1,
this.dtBaginTime.Value.ToString("yyyy-MM-dd HH:mm:ss"), this.dtEndTime.Value.AddDays(1).ToString("yyyy-MM-dd HH:mm:ss"));
if (!Material.Contains("\"code\":10000"))
Material = new UploadMaterialBLL().GetApiMaterial(shop, "0", 100, page, 1,
this.dtBaginTime.Value.ToString("yyyy-MM-dd HH:mm:ss"), this.dtEndTime.Value.AddDays(1).ToString("yyyy-MM-dd HH:mm:ss"));
Thread.Sleep(500);
if (Material.Contains("\"code\":10000"))
{
var JObj = AllBase.JsonHelper.JObjectParse(Material);
if (JObj?["data"]?["material_info_list"] != null)
{
if (JObj?["data"]?["material_info_list"].Count() == 0)
break;
foreach (Newtonsoft.Json.Linq.JObject j in JObj?["data"]?["material_info_list"])
{
MaterialIds.Add(j["material_id"]?.ToString());
}
}
}
}
this.Invoke(new Action(() =>
{
if (row != null) { row.Cells["colMessage"].Value = "正在删除根目录素材文件"; }
}));
string deleteMaterialIdStr = "";
foreach (var materialId in MaterialIds)
{
if (cancellationTokenSource.IsCancellationRequested)
{
if (row != null) { Invoke(new Action(() => { row.Cells["colMessage"].Value = "删除根目录素材文件中断"; })); }
goto operateEnd;
}
deleteMaterialIdStr += "\\\"" + materialId + "\\\",";
if (materialId == MaterialIds.LastOrDefault() || deleteMaterialIdStr.TrimEnd(',').Split(',').Count() == 100)
{
DouyinApi api = new DouyinApi(shop);
api.JsonData = new Dictionary<string, string>();
api.JsonData.Add("material_ids", "[" + deleteMaterialIdStr.TrimEnd(',') + "]");
string html = api.MaterialDeleteMaterial();
Thread.Sleep(500);
if (html.Contains("\"code\":10000"))
{
if (!html.Contains("\"failed_map\":{}"))
{
AllBase.LogSave.AddFile(html);
}
}
else
{
}
deleteMaterialIdStr = "";
}
}
this.Invoke(new Action(() =>
{
row = this.dgvShop.Rows.Cast<DataGridViewRow>().FirstOrDefault(r => r.Cells["colShopId"].Value.ToString() == shop.ShopId);
if (row != null) { row.Cells["colMessage"].Value = "素材文件夹删除完成"; }
}));
Thread.Sleep(2000);
//var xx = new UploadMaterialBLL().GetApiMaterial(shop, "70771032852027148841819", 50);
}
operateEnd:
Invoke(new Action(() =>
{
btnClear.Enabled = true;
if (cancellationTokenSource.IsCancellationRequested)
BaseMsgForm.ShowInfo("清理素材中断");
else
BaseMsgForm.ShowInfo("清理素材完成");
}));
}, cancellationTokenSource.Token);
}
private void btnStop_Click(object sender, EventArgs e)
{
if (cancellationTokenSource != null)
cancellationTokenSource.Cancel();
}
}
}