old_flsystem/应用/Weixin.TBCirclePromotion/ImportDataForm.cs

136 lines
5.2 KiB
C#
Raw Normal View History

2022-09-20 03:10:29 +00:00
using Api.Framework;
using Api.Framework.Enums;
using Api.Framework.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using UI.Framework.Forms;
using Weixin.TBCirclePromotion.Entitys;
namespace Weixin.TBCirclePromotion
{
public partial class ImportDataForm : BaseForm
{
public ImportDataForm()
{
InitializeComponent();
}
private CancellationTokenSource tokenSource = new CancellationTokenSource();
private async void simpleButton1_Click(object sender, EventArgs e)
{
simpleButton1.Enabled = false;
try
{
if (string.IsNullOrWhiteSpace(richTextBox1.Text)) throw new Exception("请填写要导入的内容!");
var cps = CpsClient.Members.FirstOrDefault(f => f.cpstype == CpsType.);
if (cps == null) throw new Exception("没有可用的阿里妈妈推广位!");
await Task.Run(() =>
{
try
{
var api = CpsClient.CreateAlimamaRequest(cps);
if (api == null) throw new Exception("创建阿里妈妈API请求失败");
var tbAnalysis = new TBHelper.TbAnalysis();
AddJindu("正在分析数据...");
var goodsinfos = new List<fl_plugin_tbcirclepromotion_goodsinfos>();
var content = string.Empty;
UpdateUI(() => { content = richTextBox1.Text; });
var urls = CsharpHttpHelper.HttpExtend.RegexMatchesUrl(content);
if (urls != null && urls.Count != 0)
{
var session = ApiClient.GetSession();
AddJindu("准备导入数据", urls.Count);
AnalyzeGoods analyzeGoods = new AnalyzeGoods();
foreach (var item in urls)
{
var item_id = string.Empty;
try
{
item_id = tbAnalysis.FindItemIdByUrlAndTklAndMkl(item, api, null);
}
catch (Exception)
{ }
//标题搜索和商品id搜索
if (!string.IsNullOrEmpty(item_id))
{
var goods = analyzeGoods.FindGoodsInfoToGoodsId(item_id);
if (goods != null)
{
goods.state = Enums.StateType.广;
goods.goods_source = Enums.GoodsSourceType.;
goodsinfos.Add(goods);
}
}
AddJindu();
}
session.Saveable(goodsinfos).ExecuteCommand();
}
else
{
AddJindu($"导入数据无效,请检测是否为商品链接!");
return;
}
AddJindu($"成功导入{goodsinfos.Count}条 - 失败{(urls.Count - goodsinfos.Count)}条");
}
catch (Exception ex)
{
if (!this.IsDisposed) ShowError(ex);
}
}, tokenSource.Token);
}
catch (Exception)
{ }
simpleButton1.Enabled = true;
}
private object lock_obj = new object();
private void AddJindu(string text = "", int max = 0)
{
base.UpdateUI(() =>
{
try
{
lock (lock_obj)
{
if (!string.IsNullOrEmpty(text) && max == 0)
{
label31.Text = text;
}
else if (!string.IsNullOrEmpty(text) && max != 0)
{
label31.Text = text;
this.progressBar1.Value = 0;
this.progressBar1.Maximum = max;
}
else
{
if (this.progressBar1.Value < this.progressBar1.Maximum) this.progressBar1.Value++;
label31.Text = $"{this.progressBar1.Value}/{this.progressBar1.Maximum}";
}
}
Application.DoEvents();
}
catch (Exception)
{ }
});
}
private void ImportDataForm_FormClosing(object sender, FormClosingEventArgs e)
{
tokenSource.Cancel();
}
}
}