old_flsystem/应用/AllRebatesActivity/MainForm.cs

425 lines
19 KiB
C#
Raw Normal View History

2022-09-20 03:10:29 +00:00
using AllRebatesActivity.Properties;
using Api.Framework;
using Api.Framework.Cps;
using Api.Framework.Enums;
using Api.Framework.Model;
using Api.Framework.Tools;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using UI.Framework.Controls;
using UI.Framework.Forms;
namespace AllRebatesActivity
{
public partial class MainForm : BaseForm
{
SqlSugarClient session = null;
public MainForm()
{
InitializeComponent();
this.Text = Resources.MainFormTitle;
session = ApiClient.GetSession();
}
public void CloseForm()
{
try
{
if (!this.IsDisposed)
{
this.Invoke(new Action(delegate
{
this.Close();
this.Dispose();
}));
}
}
catch (Exception ex)
{
ShowError(ex);
}
}
private void MainForm_Load(object sender, EventArgs e)
{
try
{
var adzone = ApiClient.GetSession().FindAdzoneInfos().FirstOrDefault(f => f.custom_type == Resources.SoftwareType && f.alliance_id == (int)CpsType.);
if (adzone != null)
{
textBox2.Text = adzone.adzone_name;
textBox2.Tag = adzone;
}
#region
pageControl1.Bind((page, size) =>
{
var session = ApiClient.GetSession();
var parm = session.NewParamMap();
parm.setPageParamters(page, size);
var result = session.FindPage<fl_plugin_allrebatesactivity_info>($"select * from fl_plugin_allrebatesactivity_info order by time_end", parm);
return new PageControl.SerchResult() { Result = result.DataList, Total = result.Total };
}, gridControl1, 50, true, true);
label21.Visible = gridView1.RowCount == 0;
#endregion
time_begin.Value = DateTime.Now;
time_end.Value = DateTime.Now.AddDays(30);
_RefreshMemberGroup();
_RefreshRobotList();
}
catch (Exception ex)
{
ShowError(ex);
}
}
/// <summary>
/// 更新会员组数据
/// </summary>
/// <param name="data"></param>
private void _RefreshMemberGroup(List<long> data = null)
{
try
{
var session = ApiClient.GetSession();
checkedComboBoxEdit1.Properties.Items.Clear();
var memberGroups = session.Queryable<fl_member_group>().ToList();
foreach (var item in memberGroups)
{
if (data != null && data.Contains(item.id))
checkedComboBoxEdit1.Properties.Items.Add(item.id, item.name, CheckState.Checked, true);
else
checkedComboBoxEdit1.Properties.Items.Add(item.id, item.name, CheckState.Unchecked, true);
}
}
catch (Exception ex)
{
ShowError(ex);
}
}
private void _RefreshRobotList(List<string> data = null)
{
try
{
checkedComboBoxEdit_ChatType.Properties.Items.Clear();
var clients = Chat.Framework.ChatClient.WXClient;
foreach (var item in clients.Values)
{
if (data != null && data.FirstOrDefault(f => f == item.WeixinHao) != null)
checkedComboBoxEdit_ChatType.Properties.Items.Add(item.WeixinHao, item.User.Nick, CheckState.Checked, true);
else
checkedComboBoxEdit_ChatType.Properties.Items.Add(item.WeixinHao, item.User.Nick, CheckState.Unchecked, true);
}
}
catch (Exception ex)
{
ShowError(ex);
}
}
private void gridView1_DoubleClick(object sender, EventArgs e)
{
try
{
#region enum的值加载到列表中
if (gridView1.GetSelectedRows().Length != 0)
{
var selectRow = gridView1.GetSelectedRows()[0];
var id = this.gridView1.GetRowCellValue(selectRow, "id").ToString();//获取列的数据
var allrebatesactivity_info = session.Queryable<fl_plugin_allrebatesactivity_info>().First(f => f.id == int.Parse(id));
var chatList = allrebatesactivity_info.robots.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(f => f.Trim()).ToList();
_RefreshRobotList(chatList);
_RefreshMemberGroup(allrebatesactivity_info.groups.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(f => long.Parse(f)).ToList());
text_name.Text = allrebatesactivity_info.name;
time_begin.Value = allrebatesactivity_info.time_begin;
time_end.Value = allrebatesactivity_info.time_end;
textBox1.Text = $@"https://item.taobao.com/item.htm?id={allrebatesactivity_info.goods_id}";
label11.Tag = allrebatesactivity_info.title;
label11.Text = allrebatesactivity_info.title.Substring(0, 23) + "...";
label13.Text = allrebatesactivity_info.goods_id;
label14.Text = allrebatesactivity_info.price.ToString("0.00");
label15.Text = allrebatesactivity_info.coupon.ToString("0.00");
label16.Text = allrebatesactivity_info.brokerage_money.ToString("0.00");
numericUpDown2.Value = allrebatesactivity_info.limit_quantity_number;
label19.Text = ((decimal)allrebatesactivity_info.brokerage_ratio / 100m).ToString("0.00") + "%";
numericUpDown1.Value = (decimal)allrebatesactivity_info.return_money;
}
else
{
_RefreshRobotList();
}
#endregion
}
catch (Exception ex)
{
ShowError(ex);
}
}
private void simpleButton_Click(object sender, EventArgs e)
{
try
{
if (string.IsNullOrWhiteSpace(text_name.Text)) throw new Exception("活动名不能给空");
if (string.IsNullOrWhiteSpace(checkedComboBoxEdit_ChatType.Text)) throw new Exception("请选择活动机器人");
if (time_begin.Value >= time_end.Value) throw new Exception("活动结束时间不能晚于开始时间");
if (string.IsNullOrWhiteSpace(checkedComboBoxEdit1.Text)) throw new Exception("请选择参加用户组");
if (string.IsNullOrWhiteSpace(textBox1.Text) || string.IsNullOrWhiteSpace(label13.Text)) throw new Exception("请填写商品链接");
var name = text_name.Text.Trim();
var mess = string.Empty;
var allrebatesactivity_info = session.Queryable<fl_plugin_allrebatesactivity_info>().First(f => f.name == name);
if (allrebatesactivity_info == null)
{
allrebatesactivity_info = new fl_plugin_allrebatesactivity_info()
{
name = name,
robots = checkedComboBoxEdit_ChatType.EditValue.ToString(),
time_begin = DateTime.Parse(time_begin.Text),
time_end = DateTime.Parse(time_end.Text),
brokerage_money = double.Parse(label16.Text),
limit_quantity_number = (int)numericUpDown2.Value,
brokerage_ratio = (int)(decimal.Parse(label19.Text.Replace("%", "")) * 100m),
coupon = double.Parse(label15.Text),
goods_id = label13.Text,
title = label11.Tag.ToString(),
price = double.Parse(label14.Text),
return_money = (double)numericUpDown1.Value,
groups = checkedComboBoxEdit1.EditValue.ToString()
};
mess = "免单任务增加成功";
}
else
{
allrebatesactivity_info.name = name;
allrebatesactivity_info.robots = checkedComboBoxEdit_ChatType.EditValue.ToString();
allrebatesactivity_info.time_begin = DateTime.Parse(time_begin.Text);
allrebatesactivity_info.time_end = DateTime.Parse(time_end.Text);
allrebatesactivity_info.brokerage_money = double.Parse(label16.Text);
allrebatesactivity_info.limit_quantity_number = (int)numericUpDown2.Value;
allrebatesactivity_info.brokerage_ratio = (int)(decimal.Parse(label19.Text.Replace("%", "")) * 100m);
allrebatesactivity_info.coupon = double.Parse(label15.Text);
allrebatesactivity_info.goods_id = label13.Text;
allrebatesactivity_info.title = label11.Tag.ToString();
allrebatesactivity_info.price = double.Parse(label14.Text);
allrebatesactivity_info.return_money = (double)numericUpDown1.Value;
allrebatesactivity_info.groups = checkedComboBoxEdit1.EditValue.ToString();
mess = "免单任务修改成功";
}
session.Saveable<fl_plugin_allrebatesactivity_info>(allrebatesactivity_info).ExecuteCommand();
ShowSuccess(mess);
pageControl1.GotoPage();
label21.Visible = gridView1.RowCount == 0;
}
catch (Exception ex)
{
ShowError(ex);
}
}
private void linkLabel5_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
try
{
var tgwObj = CpsClient.SelectTuiguangwei(CpsType.);
if (tgwObj != null)
{
var tgw = tgwObj as Tuiguangwei;
var session = ApiClient.GetSession();
var adzone = session.FindAdzoneInfos().FirstOrDefault(f => f.alliance_id == (int)CpsType. && f.custom_type == Resources.SoftwareType);
if (adzone != null)//数据库中存在的情况
{
adzone.adzone_pid_cps_name = tgw.Member.username;
adzone.adzone_pid = tgw.Pid;
adzone.adzone_name = tgw.Name;
session.SaveOrUpdate(adzone);
}
else
{
try
{
adzone = new fl_adzone_info()
{
custom_type = Resources.SoftwareType, //自定义类型
adzone_name = tgw.Name, //推广位名称
adzone_pid = tgw.Pid, //推广位pid
adzone_pid_cps_name = tgw.Member.username, //推广位cps名称
alliance_id = (int)CpsType., //联盟id
robot_id = 0, //机器人id
group_id = string.Empty, //群id
member_id = 0, //私人id
is_download = false, //不下载
onoff = false //不禁用
};
session.SaveOrUpdate(adzone);
if (0 == adzone.id)
throw new Exception("对不起,推广位设置异常,请稍后重试!");
}
catch (Exception ex)
{
ShowError(ex);
}
}
textBox2.Text = adzone.adzone_name;
textBox2.Tag = adzone;
session.FindAdzoneInfos(true);
}
}
catch (Exception ex)
{
ShowError(ex);
}
}
private void simpleButton1_Click(object sender, EventArgs e)
{
try
{
if (textBox2.Tag != null)
{
var adzone = textBox2.Tag as fl_adzone_info;
var tbAnalysis = new TBHelper.TbAnalysis();
if (string.IsNullOrWhiteSpace(textBox1.Text)) throw new Exception("请添加要解析的宝贝链接!");
var tb_cps = CpsClient.Members.FirstOrDefault(f => f.cpstype == CpsType. && f.username == adzone.adzone_pid_cps_name);
if (tb_cps == null) throw new Exception("推广位无法使用!");
var api = CpsClient.CreateAlimamaRequest(tb_cps);
if (api == null) throw new Exception("创建阿里妈妈API请求失败");
var itemId = string.Empty;
try
{
itemId = tbAnalysis.FindItemIdByUrlAndTklAndMkl(textBox1.Text, api, adzone.adzone_pid.Split('_'));
}
catch (Exception)
{ }
if (string.IsNullOrWhiteSpace(itemId)) throw new Exception("该商品宝贝无法解析");
var activityId = string.Empty;
var urls = CsharpHttpHelper.HttpExtend.RegexMatchesUrl(textBox1.Text);
if (urls != null && urls.Count != 0)
{
foreach (var item in urls)
{
if (item.ToLower().Contains("uland.taobao"))
{
var reg = Regex.Match(item, @"activityId=(?<活动ID>[A-Za-z0-9]+)", RegexOptions.IgnoreCase);
if (reg.Success)
activityId = reg.Groups["活动ID"].Value;
break;
}
}
}
AnalyzeGoods analyze = new AnalyzeGoods();
var goodsinfo = analyze.FindGoodsInfoToItemId(itemId, adzone.adzone_pid_cps_name, adzone.adzone_pid);
if (goodsinfo != null)
{
label11.Tag = goodsinfo.goods_name;
label11.Text = goodsinfo.goods_name.Length >= 24 ? goodsinfo.goods_name.Substring(0, 23) + "..." : goodsinfo.goods_name;
label13.Text = goodsinfo.goods_id;
label14.Text = goodsinfo.normal_price.ToString("0.00");
label15.Text = goodsinfo.coupon_discount.ToString("0.00");
label19.Text = goodsinfo.promotion_rate.ToString("0.00") + "%";
decimal final_price = 0;
if (goodsinfo.coupon_discount > goodsinfo.normal_price)
final_price = (decimal)goodsinfo.normal_price;
else
final_price = ((decimal)goodsinfo.normal_price - (decimal)goodsinfo.coupon_discount);
label16.Text = (final_price * ((decimal)goodsinfo.promotion_rate / 100m)).ToString("0.00");
}
else throw new Exception("该商品宝贝无返利");
}
else throw new Exception("请先设置推广位!");
}
catch (Exception ex)
{
ShowError(ex);
}
}
private void gridView1_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
{
try
{
if (e.Column.Caption == "活动状态")
{
var activity_time_begin = DateTime.Parse(gridView1.GetRowCellDisplayText(e.ListSourceRowIndex, "time_begin"));
var activity_time_end = DateTime.Parse(gridView1.GetRowCellDisplayText(e.ListSourceRowIndex, "time_end"));
var now = DateTime.Now;
if (now >= activity_time_end)
e.DisplayText = "活动已结束";
else if (now < activity_time_begin)
e.DisplayText = "活动未开始";
else if (activity_time_begin <= now && now < activity_time_end)
e.DisplayText = "活动进行中";
}
}
catch (Exception ex)
{
ShowError(ex);
}
}
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
var rows = gridView1.GetSelectedRows();
if (rows.Length != 0)
{
var row = this.gridView1.GetRow(rows[0]) as fl_plugin_allrebatesactivity_info;
var session = ApiClient.GetSession();
session.Deleteable(row).ExecuteCommand();
ShowSuccess("删除成功");
pageControl1.GotoPage();
}
}
catch (Exception ex)
{
ShowError(ex);
}
}
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
var rows = gridView1.GetSelectedRows();
if (rows.Length != 0)
{
var session = ApiClient.GetSession();
session.Deleteable<fl_plugin_allrebatesactivity_info>().ExecuteCommand();
ShowSuccess("删除成功");
pageControl1.GotoPage();
}
}
catch (Exception ex)
{
ShowError(ex);
}
}
private void textBox1_Enter(object sender, EventArgs e)
{
textBox1.SelectAll();
}
}
}