old_flsystem/应用/AutoAnswer/EditUserDefinedForm.cs

232 lines
11 KiB
C#

using Api.Framework;
using Api.Framework.SDK;
using Api.Framework.Tools;
using AutoAnswer.Entitys;
using AutoAnswer.Properties;
using Chat.Framework;
using Chat.Framework.QQSdk;
using Chat.Framework.WXSdk.Implement;
using DevExpress.XtraEditors;
using Microsoft.JScript;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UI.Framework.Forms;
namespace AutoAnswer
{
public partial class EditUserDefinedForm : BaseForm
{
private string _id { get; set; }
public EditUserDefinedForm(string id = "")
{
InitializeComponent();
this.Text = string.IsNullOrEmpty(id) ? Resources.AddUserDefinedFormTitle : Resources.UpdateUserDefinedFormTitle;
this._id = id;
}
private fl_plugin_autoanswer_definedlib userdefinedlibrary = null;
/// <summary>
/// 窗体加载事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void EditUserDefinedForm_Load(object sender, EventArgs e)
{
#region enum的值加载到列表中
if (!string.IsNullOrEmpty(_id))
{
var session = ApiClient.GetSession();
userdefinedlibrary = session.Find<fl_plugin_autoanswer_definedlib>($"select * from fl_plugin_autoanswer_definedlib where id = '{_id}'").FirstOrDefault();
if (userdefinedlibrary == null) throw new Exception("数据异常");
var chatList = userdefinedlibrary.chat_type.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList();
foreach (ChatType item in Enum.GetValues(typeof(ChatType)))
{
if (item == ChatType. || item == ChatType.)
continue;
if (chatList.FirstOrDefault(f => f == ((int)item).ToString()) != null)
checkedComboBoxEdit_ChatType.Properties.Items.Add(((int)item).ToString(), item.ToString(), CheckState.Checked, true);
else
checkedComboBoxEdit_ChatType.Properties.Items.Add(((int)item).ToString(), item.ToString(), CheckState.Unchecked, true);
}
ViewRobot(userdefinedlibrary.specific_robot_setting_text);
tBox_antistop.Text = GlobalObject.unescape(userdefinedlibrary.antistop);
radioGroup1.SelectedIndex = userdefinedlibrary.match_pattern;
cbox_alertAdmin.Checked = (userdefinedlibrary.is_alert_admin == 1);
memoEdit1.Text = GlobalObject.unescape(userdefinedlibrary.content);
}
else
{
foreach (ChatType item in Enum.GetValues(typeof(ChatType)))
{
if (item == ChatType. || item == ChatType.)
continue;
checkedComboBoxEdit_ChatType.Properties.Items.Add(((int)item).ToString(), item.ToString(), CheckState.Unchecked, true);
}
radioGroup1.SelectedIndex = 1;
}
#endregion
}
private void checkedComboBoxEdit_ChatType_Leave(object sender, EventArgs e)
{
ViewRobot(userdefinedlibrary == null ? string.Empty : userdefinedlibrary.specific_robot_setting_text);
}
private void ViewRobot(string specificRobotSetting = "")
{
try
{
checkedComboBoxEdit_Robot.Properties.Items.Clear();
var settingList = new List<string>();
if (!string.IsNullOrWhiteSpace(specificRobotSetting))
settingList = specificRobotSetting.Split(new string[] { "[分割]" }, StringSplitOptions.RemoveEmptyEntries).ToList();
//获取到勾选的平台名称数组
var str_split = checkedComboBoxEdit_ChatType.Properties.GetCheckedItems().ToString().Replace("4", "2").Replace(", ", ",").Replace(" ,", ",").Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Distinct().ToList();
if (str_split.Count != 0)
{
foreach (var item in str_split)
{
switch ((ChatType)Enum.Parse(typeof(ChatType), item))
{
case ChatType.QQ:
foreach (QQBase v in ChatClient.QQClients.Values)
{
CheckState check = settingList.Contains($"{(int)v.QQ}_QQ") ? CheckState.Checked : CheckState.Unchecked;
checkedComboBoxEdit_Robot.Properties.Items.Add($"{(int)v.QQ}_QQ", $"QQ:{v.Nickname}({v.QQ})", check, true);
}
break;
case ChatType.:
case ChatType.:
{
foreach (WeixinBase v in ChatClient.WXClient.Values)
{
CheckState check = settingList.Contains($"{v.WeixinHao}_微信") ? CheckState.Checked : CheckState.Unchecked;
checkedComboBoxEdit_Robot.Properties.Items.Add($"{v.WeixinHao}_微信", $"微信:{v.User.Nick}({v.WeixinHao})", check, true);
}
}
break;
case ChatType.:
case ChatType.:
break;
default:
break;
}
}
}
else
{
}
}
catch (Exception ex)
{ }
}
/// <summary>
/// 关闭窗口时保存数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void EditUserDefinedForm_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
if (string.IsNullOrWhiteSpace(tBox_antistop.Text.Trim())) throw new Exception("关键词不能为空");
if (string.IsNullOrWhiteSpace(memoEdit1.Text.Replace("-----------分割线-----------", ""))) throw new Exception("回复内容列表不能为空");
if (string.IsNullOrEmpty(checkedComboBoxEdit_ChatType.EditValue.ToString()) && !cbox_alertAdmin.Checked) throw new Exception("请选择回复平台或通知管理员");
//获取到勾选的平台名称数组
if (userdefinedlibrary == null)
userdefinedlibrary = new fl_plugin_autoanswer_definedlib();
userdefinedlibrary.antistop = GlobalObject.escape(tBox_antistop.Text.Trim());
userdefinedlibrary.chat_type = checkedComboBoxEdit_ChatType.Properties.GetCheckedItems().ToString().Replace(", ", ",").Replace(" ,", ",");
var CheckedRobots = checkedComboBoxEdit_Robot.Properties.GetCheckedItems().ToString().Replace(", ", ",").Replace(" ,", ",");
userdefinedlibrary.specific_robot_setting_text = CheckedRobots;
userdefinedlibrary.content = GlobalObject.escape(memoEdit1.Text.Trim());
userdefinedlibrary.is_alert_admin = (cbox_alertAdmin.Checked ? 1 : 0);
userdefinedlibrary.match_pattern = radioGroup1.SelectedIndex;
var session = ApiClient.GetSession();
session.Saveable(userdefinedlibrary).ExecuteCommand();
}
catch (Exception ex)
{
if (XtraMessageBox.Show(ex.Message + " 是否关闭?", "操作失败", MessageBoxButtons.YesNo) == DialogResult.No)
e.Cancel = true;
}
}
private void hyperlinkLabelControl2_Click(object sender, EventArgs e)
{
memoEdit1.Text = memoEdit1.Text.Insert(memoEdit1.SelectionStart, @"
-----------分割线-----------
");
memoEdit1.SelectionStart = memoEdit1.SelectionStart;
}
private void hyperlinkLabelControl5_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Multiselect = true;//等于true表示可以选择多个文件
dlg.DefaultExt = ".jpg";
dlg.Filter = "图片|*.jpg;*.png;*.gif;*.jpeg;*.bmp";
if (dlg.ShowDialog() == DialogResult.OK)
{
StringBuilder strb = new StringBuilder();
foreach (string file in dlg.FileNames)
{
strb.AppendLine($"[图片={file}]");
}
memoEdit1.Text = memoEdit1.Text.Insert(memoEdit1.SelectionStart, strb.ToString().Trim());
memoEdit1.SelectionStart = memoEdit1.SelectionStart;
}
}
catch (Exception ex)
{
ShowError(ex);
}
}
private void hyperlinkLabelControl1_Click(object sender, EventArgs e)
{
try
{
var label = sender as HyperlinkLabelControl;
var flag = label.Text.Substring(label.Text.Length - 3, 2);
CommonForm commonForm = new CommonForm(flag);
if (commonForm.ShowDialog() == DialogResult.OK)
{
if (!string.IsNullOrWhiteSpace(commonForm.Strs))
{
var strs = commonForm.Strs.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).ToList();
if (flag == "图片")
strs = strs.Select(f => $"[图片={f}]").ToList();
else if (flag == "视频")
strs = strs.Select(f => $"[视频={f}]").ToList();
else if (flag == "语音")
strs = strs.Select(f => $"[语音={f}]").ToList();
memoEdit1.Text = memoEdit1.Text.Insert(memoEdit1.SelectionStart, string.Join("\r\n", strs).Trim());
memoEdit1.SelectionStart = memoEdit1.SelectionStart;
}
}
}
catch (Exception ex)
{
ShowError(ex);
}
}
}
}