using Api.Framework; using Api.Framework.Cps; using Api.Framework.Enums; using Api.Framework.Model; using Api.Framework.Tools; using DevExpress.XtraEditors; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text.RegularExpressions; using System.Windows.Forms; using TBRebate.Entitys; using TBRebate.Properties; using UI.Framework.Controls; using UI.Framework.Forms; namespace TBRebate { public partial class MainForm : BaseForm { public MainForm() { InitializeComponent(); this.Text = Resources.MainFormTitle; } 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 apis = ApiClient.GetSession().FindNoticeapiRobots().ToArray(); var rst = new List(); rst.Add("请选择(钉钉/微信群机器人API名称)"); foreach (var item in apis) rst.Add(item.name); comboBox4.DataSource = rst; #region 淘礼金模式加载 if (!Util.AppConfig_ExistItem("ShowTlj")) Util.AppConfig_AddItem("ShowTlj", "0"); xtraTabPage4.PageVisible = ShowTlj(); if (xtraTabPage4.PageVisible) { this.comboBoxEdit1.Properties.Items.AddRange(Enum.GetNames(typeof(TLJModel)).ToList()); bck_tlj_onoff.Checked = Class1.Config.TljOnOff; comboBoxEdit1.SelectedIndex = (int)Class1.Config.TljModel; this.textEdit41.Text = Class1.Config.TljName; this.memoEdit4.Text = Class1.Config.TljItemIds; this.numericUpDown6.Value = Class1.Config.TljCreateNum; this.numericUpDown5.Value = Class1.Config.TljGetLimit; this.numericUpDown7.Value = Class1.Config.TljExpireDate; this.memoEdit1.Text = Class1.Config.TljSearchSuccess; this.memoEdit2.Text = Class1.Config.TljPaymentTip; this.memoEdit3.Text = Class1.Config.TljSettlementTip; } #endregion this.settingControl1.Bind(Class1.Config, 110); this.buttonCheck.Checked = Class1.Config.AShop_SameCommodity_Switch; this.numericUpDown3.Value = Class1.Config.AShop_SameCommodity_Number; this.comboBox1.SelectedIndex = Class1.Config.AShop_SameCommodity_OperateType == OperateType.拉入黑名单 ? 0 : 1; this.checkBox1.Checked = Class1.Config.AShop_SameCommodity_UserTop; this.buttonCheck1.Checked = Class1.Config.AShop_DifferentCommodity_Switch; this.numericUpDown4.Value = Class1.Config.AShop_DifferentCommodity_Number; this.comboBox2.SelectedIndex = Class1.Config.AShop_DifferentCommodity_OperateType == OperateType.拉入黑名单 ? 0 : 1; this.checkBox2.Checked = Class1.Config.AShop_DifferentCommodity_UserTop; this.buttonCheck2.Checked = Class1.Config.ReceivingTimeCheck_Switch; this.numericUpDown1.Value = Class1.Config.ReceivingTimeCheck_Hour; this.comboBox3.SelectedIndex = Class1.Config.ReceivingTimeCheck_OperateType == OperateType.拉入黑名单 ? 0 : Class1.Config.ReceivingTimeCheck_OperateType == OperateType.通知钉钉群 ? 1 : 2; this.numericUpDown2.Value = Class1.Config.ReceivingTimeCheck_FreezeTime; this.checkBox3.Checked = Class1.Config.ReceivingTimeCheck_UserTop; this.comboBox4.SelectedIndex = rst.IndexOf(Class1.Config.notice_robotname) == -1 ? 0 : rst.IndexOf(Class1.Config.notice_robotname); this.memoEdit5.Text = string.Join(",", Class1.Config.ItemIDRestrictList); this.memoEdit6.Text = string.Join(",", Class1.Config.SellerIDRestrictlist); #region 加载所有的账号 pageControl1.Bind((page, size) => { var session = ApiClient.GetSession(); var parm = session.NewParamMap(); parm.setPageParamters(page, size); int totalNumber = 0; var result = session.Queryable().Select(f => new tbinfo_temp() { ID = f.id, chattype = f.type, nick = f.nick, name = f.name }).ToPageList(page, size, ref totalNumber); foreach (var item in result) { var adzones = session.Find("select * from fl_adzone_info where robot_id = @robot_id and custom_type = @custom_type and alliance_id = @alliance_id", new { robot_id = item.ID, custom_type = Resources.SoftwareType, alliance_id = (int)CpsType.阿里妈妈 }); foreach (var adzone in adzones) { if (adzone.extend == "chief")//主 { item.onoff = adzone.onoff; if (adzone.adzone_group != 0)//组 { var _adzone = session.FindAdzoneGroups(CpsType.阿里妈妈, true).Where(f => string.IsNullOrWhiteSpace(f.remark)).FirstOrDefault(f => f.id == adzone.adzone_group); if (_adzone != null) { item.is_chief_group = true; item.pid_chief = _adzone.adzones; item.pid_chief_name = _adzone.name; } } else//单 { item.is_chief_group = false; item.pid_chief_cps_name = adzone.adzone_pid_cps_name; item.pid_chief = adzone.adzone_pid; item.pid_chief_name = adzone.adzone_name; } } else//副 { item.onoff = adzone.onoff; if (adzone.adzone_group != 0)//组 { var _adzone = session.FindAdzoneGroups(CpsType.阿里妈妈, true).Where(f => string.IsNullOrWhiteSpace(f.remark)).FirstOrDefault(f => f.id == adzone.adzone_group); if (_adzone != null) { item.is_deputy_group = true; item.pid_deputy = _adzone.adzones; item.pid_deputy_name = _adzone.name; } } else//单 { item.is_deputy_group = false; item.pid_deputy_cps_name = adzone.adzone_pid_cps_name; item.pid_deputy = adzone.adzone_pid; item.pid_deputy_name = adzone.adzone_name; } } } var activity = session.FindSingle("select * from fl_adzone_info where robot_id = @robot_id and custom_type = @custom_type", new { robot_id = item.ID, custom_type = Resources.TbActivitySoftwareType }); if (activity != null) { var _adzone = session.FindAdzoneGroups(CpsType.阿里妈妈, true).Where(f => f.remark == fl_alimama_activity_type.天猫精选.ToString()).FirstOrDefault(f => f.id == activity.adzone_group); if (_adzone != null) { item.adzone_activity_name = _adzone.name; item.adzone_activity_pid = _adzone.adzones; } } } return new PageControl.SerchResult() { Result = result, Total = totalNumber }; }, gridControl1, 40, true, true); label1.Visible = gridView1.RowCount == 0; #endregion } catch (Exception ex) { ShowError(ex); } } private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e) { try { #region 双击单元格设置推广位 var selectRow = gridView1.GetSelectedRows()[0]; var id = this.gridView1.GetRowCellValue(selectRow, "ID").ToString();//获取列的数据 //点击数大于2弹出窗体进行推广位的选择 if (e.Clicks >= 2) { var session = ApiClient.GetSession(); if (e.Column.FieldName == "pid_chief_name" || e.Column.FieldName == "pid_deputy_name") { var tgwObj = CpsClient.SelectTuiguangwei(CpsType.阿里妈妈, isgroup: true); if (tgwObj != null) { if (tgwObj is Tuiguangwei) { var tgw = tgwObj as Tuiguangwei; var adzones = session.FindAdzoneInfos(true).Where(f => f.alliance_id == (int)CpsType.阿里妈妈 && f.custom_type == Resources.SoftwareType && f.robot_id == long.Parse(id)).ToList(); var pidName = e.Column.FieldName == "pid_chief_name" ? "chief" : "deputy"; var adzone = adzones.FirstOrDefault(f => f.extend == pidName); #region 这里判断存在两个推广位(主/副),不存在的(主副数据将创建对象) if (adzones != null && adzones.Count < 2) { var pidNames = new string[] { "chief", "deputy" }; if (adzone == null) { adzones = new List(); foreach (var item in pidNames) { var adzoneInfo = CreateAdzoneInfo(string.Empty, string.Empty, string.Empty, id, item); session.SaveOrUpdate(adzoneInfo); adzones.Add(adzoneInfo); } } else { var unCreate = pidNames.FirstOrDefault(f => f != adzone.extend);//获取数据库中不存在的(主/副)推广位数据,下方添加不存在的对象 var adzoneInfo = CreateAdzoneInfo(string.Empty, string.Empty, string.Empty, id, unCreate); session.SaveOrUpdate(adzoneInfo); adzones.Add(adzoneInfo); } } #endregion #region 有出现两个重复无法修复的情况,删除一个 var repetAdzone = adzones.Where(f => f.extend == pidName).ToList(); if (repetAdzone.Count > 1) { for (int i = 1; i <= repetAdzone.Count - 1; i++) { session.Deleteable(repetAdzone[i]).ExecuteCommand(); } adzones = session.FindAdzoneInfos(true).Where(f => f.alliance_id == (int)CpsType.阿里妈妈 && f.custom_type == Resources.SoftwareType && f.robot_id == long.Parse(id)).ToList(); } #endregion adzone = adzones.FirstOrDefault(f => f.extend == pidName); if (adzone != null)//数据库中存在的情况 { adzone.adzone_pid_cps_name = tgw.Member.username; adzone.adzone_pid = tgw.Pid; adzone.adzone_name = tgw.Name; adzone.adzone_group = 0; session.SaveOrUpdate(adzone); } } else if (tgwObj is fl_adzone_group) { var adzone_group = tgwObj as fl_adzone_group; var adzones = session.FindAdzoneInfos(true).Where(f => f.alliance_id == (int)CpsType.阿里妈妈 && f.custom_type == Resources.SoftwareType && f.robot_id == long.Parse(id)).ToList(); var pidName = e.Column.FieldName == "pid_chief_name" ? "chief" : "deputy"; var adzone = adzones.FirstOrDefault(f => f.extend == pidName); #region 这里判断存在两个推广位(主/副),不存在的(主副数据将创建对象) if (adzones != null && adzones.Count < 2) { var pidNames = new string[] { "chief", "deputy" }; if (adzone == null) { adzones = new List(); foreach (var item in pidNames) { var adzoneInfo = CreateAdzoneInfo(string.Empty, string.Empty, string.Empty, id, item); session.SaveOrUpdate(adzoneInfo); adzones.Add(adzoneInfo); } } else { var unCreate = pidNames.FirstOrDefault(f => f != adzone.extend);//获取数据库中不存在的(主/副)推广位数据,下方添加不存在的对象 var adzoneInfo = CreateAdzoneInfo(string.Empty, string.Empty, string.Empty, id, unCreate); session.SaveOrUpdate(adzoneInfo); adzones.Add(adzoneInfo); } } #endregion adzone = adzones.FirstOrDefault(f => f.extend == pidName); if (adzone != null)//数据库中存在的情况 { adzone.adzone_name = string.Empty; adzone.adzone_pid = string.Empty; adzone.adzone_pid_cps_name = string.Empty; adzone.adzone_group = adzone_group.id; session.SaveOrUpdate(adzone); } } pageControl1.GotoPage(1); label1.Visible = gridView1.RowCount == 0; session.FindTbInfoTempGroups(true); session.FindAdzoneInfos(true); session.FindAlimamaAutoBindPid(true);//刷新推广位 session.FindAdzoneGroups(CpsType.阿里妈妈, true); } } else if (e.Column.FieldName == "adzone_activity_name")//设置活动推广位 { var tgwObj = CpsClient.SelectTuiguangwei(CpsType.阿里妈妈, isalone: false, isgroup: true, isactivity: true); if (tgwObj != null) { if (tgwObj is fl_adzone_group) { var adzone_group = tgwObj as fl_adzone_group; var adzoneInfo = session.FindAdzoneInfos(true).FirstOrDefault(f => f.alliance_id == (int)CpsType.阿里妈妈 && f.custom_type == Resources.TbActivitySoftwareType && f.robot_id == long.Parse(id)); if (adzoneInfo == null) { adzoneInfo = new fl_adzone_info() { custom_type = Resources.TbActivitySoftwareType, //自定义类型 adzone_name = string.Empty, //推广位名称 adzone_pid = string.Empty, //推广位pid adzone_pid_cps_name = string.Empty, //推广位cps名称 alliance_id = (int)CpsType.阿里妈妈, //联盟id robot_id = int.Parse(id), //机器人id group_id = string.Empty, //群id member_id = 0, //私人id is_download = false, //不下载 onoff = false, //不禁用 extend = string.Empty, adzone_group = adzone_group.id }; } else { adzoneInfo.adzone_group = adzone_group.id; } session.SaveOrUpdate(adzoneInfo); } pageControl1.GotoPage(1); label1.Visible = gridView1.RowCount == 0; session.FindAdzoneGroups(CpsType.阿里妈妈, true); session.FindAdzoneInfos(true); session.FindAlimamaAutoBindPid(true);//刷新推广位 session.FindTbInfoTempGroups(true); } } } #endregion #region 开关单元格单击修改状态 if (e.Column.FieldName == "onoff") { if (e.Clicks == 1) { if (!(bool)e.CellValue && MessageBox.Show("该微信推广位确定禁用?", "系统提示", MessageBoxButtons.OKCancel) == DialogResult.Cancel) return; var session = ApiClient.GetSession(); var adzones = session.FindAdzoneInfos().Where(f => f.alliance_id == (int)CpsType.阿里妈妈 && f.custom_type == Resources.SoftwareType && f.robot_id == long.Parse(id)).ToList(); #region 这里判断存在两个推广位(主/副),不存在的(主副数据将创建对象) if (adzones != null && adzones.Count < 2) { var pidNames = new string[] { "chief", "deputy" }; if (adzones.Count == 0) { adzones = new List(); foreach (var item in pidNames) { var adzoneInfo = CreateAdzoneInfo(string.Empty, string.Empty, string.Empty, id, item, false); session.SaveOrUpdate(adzoneInfo); adzones.Add(adzoneInfo); } } else { var unCreate = pidNames.FirstOrDefault(f => f != adzones[0].extend);//获取数据库中不存在的(主/副)推广位数据,下方添加不存在的对象 var adzoneInfo = CreateAdzoneInfo(string.Empty, string.Empty, string.Empty, id, unCreate, adzones[0].onoff); session.SaveOrUpdate(adzoneInfo); adzones.Add(adzoneInfo); } } #endregion if (adzones != null)//数据库中存在 { session.ExcuteSQL("update fl_adzone_info set onoff = @onoff where robot_id = @robot_id and alliance_id = @alliance_id and custom_type = @custom_type", new { onoff = !adzones[0].onoff, robot_id = id, alliance_id = (int)CpsType.阿里妈妈, custom_type = Resources.SoftwareType }); } session.FindTbInfoTempGroups(true); pageControl1.GotoPage(1); label1.Visible = gridView1.RowCount == 0; session.FindAdzoneInfos(true); session.FindAlimamaAutoBindPid(true); } } #endregion } catch (Exception ex) { ShowError(ex); } } /// /// 创建fl_adzone_info对象 /// /// /// /// /// /// /// private fl_adzone_info CreateAdzoneInfo(string adzone_name, string adzone_pid, string adzone_pid_cps_name, string rid, string item, bool onoff = false) { return new fl_adzone_info() { custom_type = Resources.SoftwareType, //自定义类型 adzone_name = adzone_name, //推广位名称 adzone_pid = adzone_pid, //推广位pid adzone_pid_cps_name = adzone_pid_cps_name, //推广位cps名称 alliance_id = (int)CpsType.阿里妈妈, //联盟id robot_id = int.Parse(rid), //机器人id group_id = string.Empty, //群id member_id = 0, //私人id is_download = false, //不下载 onoff = onoff, //不禁用 extend = item }; } /// /// 修改显示内容的事件 /// /// /// private void gridView1_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e) { try { if ((e.Column.FieldName == "pid_chief_name" || e.Column.FieldName == "pid_deputy_name" || e.Column.FieldName == "adzone_activity_name") && (e.Value == null || string.IsNullOrEmpty(e.Value.ToString()))) e.DisplayText = "双击设置"; } catch (Exception ex) { ShowError(ex); return; } } private void MainForm_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e) { try { if (xtraTabPage4.PageVisible && (string.IsNullOrWhiteSpace(textEdit41.Text) || textEdit41.Text.Trim().Length < 5)) { ShowErrorAutoClose("淘礼金名称长度不能小于5并且不能大于10个字符"); e.Cancel = true; } Class1.Config.AShop_SameCommodity_Switch = buttonCheck.Checked; Class1.Config.AShop_SameCommodity_Number = (int)numericUpDown3.Value; Class1.Config.AShop_SameCommodity_OperateType = comboBox1.SelectedIndex == 0 ? OperateType.拉入黑名单 : OperateType.通知钉钉群; Class1.Config.AShop_SameCommodity_UserTop = checkBox1.Checked; Class1.Config.AShop_DifferentCommodity_Switch = buttonCheck1.Checked; Class1.Config.AShop_DifferentCommodity_Number = (int)numericUpDown4.Value; Class1.Config.AShop_DifferentCommodity_OperateType = comboBox2.SelectedIndex == 0 ? OperateType.拉入黑名单 : OperateType.通知钉钉群; Class1.Config.AShop_DifferentCommodity_UserTop = checkBox2.Checked; Class1.Config.ReceivingTimeCheck_Switch = buttonCheck2.Checked; Class1.Config.ReceivingTimeCheck_Hour = (int)numericUpDown1.Value; Class1.Config.ReceivingTimeCheck_OperateType = comboBox3.SelectedIndex == 0 ? OperateType.拉入黑名单 : comboBox3.SelectedIndex == 1 ? OperateType.通知钉钉群 : OperateType.订单冻结; Class1.Config.ReceivingTimeCheck_FreezeTime = (int)numericUpDown2.Value; Class1.Config.ReceivingTimeCheck_UserTop = checkBox3.Checked; Class1.Config.notice_robotname = comboBox4.SelectedValue.ToString(); Class1.Config.ItemIDRestrictList = memoEdit5.Text.Trim().Replace(",", ",").Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(f => f.Trim()).ToList(); Class1.Config.SellerIDRestrictlist = memoEdit6.Text.Trim().Replace(",", ",").Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(f => f.Trim()).ToList(); if (xtraTabPage4.PageVisible) { Class1.Config.TljName = textEdit41.Text; Class1.Config.TljOnOff = bck_tlj_onoff.Checked; Class1.Config.TljModel = (TLJModel)comboBoxEdit1.SelectedIndex; Class1.Config.TljItemIds = memoEdit4.Text; Class1.Config.TljCreateNum = (int)numericUpDown6.Value; Class1.Config.TljGetLimit = (int)numericUpDown5.Value; Class1.Config.TljExpireDate = (int)numericUpDown7.Value; Class1.Config.TljSearchSuccess = memoEdit1.Text; Class1.Config.TljPaymentTip = memoEdit2.Text; Class1.Config.TljSettlementTip = memoEdit3.Text; } OrderHelper.RefreshTbConfig(true); Util.Save(Class1.Config); } catch (Exception ex) { ShowError(ex); } } /// /// 检测是否与发单推广位有重复 /// /// /// private void simpleButton1_Click(object sender, EventArgs e) { // try // { // var session = ApiClient.GetSession(); // var adzones = session.FindAdzoneInfos().Where(f => f.alliance_id == (int)CpsType.阿里妈妈 && f.custom_type == Resources.SoftwareType); // var tbCricleAdzones = session.FindAdzoneInfos().Where(f => f.alliance_id == (int)CpsType.阿里妈妈 && f.custom_type == Resources.TbCricleSoftwareType).ToList();//发单推广位集合 // var circleToolsAdzones = session.FindAdzoneInfos().Where(f => f.alliance_id == (int)CpsType.阿里妈妈 && f.custom_type == Resources.CricleToolsSoftwareType).ToList();//朋友圈推广位集合 // tbCricleAdzones.AddRange(circleToolsAdzones); // if (tbCricleAdzones != null && adzones != null) // { // var pidNames = tbCricleAdzones.Where(a => adzones.Where(t => a.adzone_pid == t.adzone_pid).Any()).Select(f => f.adzone_name).ToList(); // var namestr = string.Join("\r\n", pidNames); // if (namestr.Length != 0) // ShowSuccess(@"以下推广位和发单推广设置有冲突 //请及时处理,否则订单将无法自动绑定 //" + namestr); // else ShowSuccess("不存在冲突推广位"); // } // } // catch (Exception ex) // { // ShowError(ex); // } } private void 删除选中淘宝推广位ToolStripMenuItem_Click(object sender, EventArgs e) { try { var session = ApiClient.GetSession(); var selectRow = gridView1.GetSelectedRows()[0]; var rid = this.gridView1.GetRowCellValue(selectRow, "ID").ToString();//获取列的数据 var robotName = this.gridView1.GetRowCellValue(selectRow, "name").ToString(); session.ExcuteSQL("delete from fl_adzone_info where robot_id = @rid and custom_type = @custom_type", new { rid = rid, custom_type = Resources.TbActivitySoftwareType }); ShowSuccess("删除成功"); pageControl1.GotoPage(1); session.FindAdzoneGroups(CpsType.阿里妈妈, true); session.FindTbInfoTempGroups(true); Tools.ClearActivityTuiguangwei(robotName); session.FindAdzoneInfos(true); } catch (Exception ex) { ShowError(ex); } } private void comboBox3_SelectedIndexChanged(object sender, EventArgs e) { labelControl7.Visible = labelControl8.Visible = numericUpDown2.Visible = comboBox3.SelectedIndex == 2; checkBox3.Visible = comboBox3.SelectedIndex == 0; } private void groupBox1_Enter(object sender, EventArgs e) { } private void comboBox2_SelectedIndexChanged(object sender, EventArgs e) { checkBox2.Visible = comboBox2.SelectedIndex == 0; } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { checkBox1.Visible = comboBox1.SelectedIndex == 0; } private void comboBoxEdit1_SelectedIndexChanged(object sender, EventArgs e) { //try //{ // if (comboBoxEdit1.SelectedIndex == 0)//"首单使用淘礼金" // { // } // else if (comboBoxEdit1.SelectedIndex == 1)//"随机分配淘礼金", // { // } //} //catch (Exception ex) //{ //} } private void MainForm_KeyDown(object sender, KeyEventArgs e) { } public bool ShowTlj() { var ShowTlj = Util.AppConfig_GetValue("ShowTlj"); if (ShowTlj != null && ShowTlj == "1") return true; else if (Class1.Config.TljOnOff) return true; return false; } private void xtraTabControl1_KeyDown(object sender, KeyEventArgs e) { try { if (e.Alt) { if (e.KeyCode == Keys.Oem3) { var ShowTlj = Util.AppConfig_GetValue("ShowTlj"); var state = ShowTlj == "1" ? "0" : "1"; string mess = string.Empty; if (Util.AppConfig_ModifyItem("ShowTlj", state)) mess = state == "1" ? "成功激活淘礼金" : "成功隐藏淘礼金"; else mess = state == "1" ? "激活淘礼金失败" : "隐藏淘礼金失败"; ShowSuccess(mess); } } } catch (Exception ex) { ShowError(ex); } } private void hyperlinkLabelControl1_1_Click(object sender, EventArgs e) { try { var label = sender as HyperlinkLabelControl; if (label != null) { var reg = Regex.Match(label.Name, @"^hyperlinkLabelControl(?<序列>\d)_.$"); if (reg.Success) { var item = reg.Groups["序列"].Value; if (item == "1") memoEdit1.Text += label.Text; if (item == "2") memoEdit2.Text += label.Text; if (item == "3") memoEdit3.Text += label.Text; } } } catch (Exception ex) { ShowError(ex); } } } }