using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; using DevExpress.XtraEditors; using UI.Framework.Forms; using Api.Framework; using Api.Framework.Tools; using Api.Framework.Model; using BackupAndImport.Entitys; namespace BackupAndImport { public partial class EditRobotNameForm : BaseForm { public Dictionary RobotNameDic = null; public EditRobotNameForm() { InitializeComponent(); RobotNameDic = new Dictionary(); } private void gridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e) { List robots = null; try { robots = gridControl1.DataSource as List; if (e.Column.Caption == "新机器人账号" && e.Value != null) { var selectRow = gridView1.GetSelectedRows()[0]; var name = this.gridView1.GetRowCellValue(selectRow, "name").ToString();//获取name列的数据 var newName = e.Value.ToString().Trim(); if (!string.IsNullOrWhiteSpace(newName)) { //var session = ApiClient.GetSession(); var robot = robots.FirstOrDefault(f => f.name == newName); if (robot == null) robot = robots.Where(f => f.name != name).FirstOrDefault(f => f.newName == newName); if (robot != null) { robot = robots.FirstOrDefault(f => f.name == name); if (robot != null) robot.newName = string.Empty; throw new Exception("设置的账号已被使用"); } var isExit = RobotNameDic.Values.FirstOrDefault(f => f == newName); if (!string.IsNullOrWhiteSpace(isExit)) throw new Exception("设置的账号已被使用"); if (RobotNameDic.ContainsKey(name)) RobotNameDic[name] = newName; else RobotNameDic.Add(name, newName); } else { if (RobotNameDic.ContainsKey(name)) RobotNameDic.Remove(name); } } } catch (Exception ex) { ShowError(ex); } gridControl1.DataSource = robots; } private void EditRobotNameForm_Load(object sender, EventArgs e) { try { var session = ApiClient.GetSession(); var robotUpdateName = new List(); var robots = session.FindRobots(); if (robots != null) { robots = robots.Where(f => f.type == Api.Framework.SDK.ChatType.微信 && f.remark != "PCWechat HOOK").ToList(); if (robots != null && robots.Count != 0) { foreach (var item in robots) { robotUpdateName.Add(new RobotUpdateName() { name = item.name, nick = item.nick, newName = string.Empty }); } } } gridControl1.DataSource = robotUpdateName; label3.Visible = gridView1.RowCount == 0; } catch (Exception ex) { ShowError(ex); } } private void simpleButton1_Click(object sender, EventArgs e) { if (RobotNameDic.Count == 0 && XtraMessageBox.Show(@"当前没有修改的数据,是否取消?", "温馨提示", MessageBoxButtons.YesNo) == DialogResult.No) return; this.Close(); } } }