ab56a9bcf7
SVN-Revision: r240
517 lines
22 KiB
C#
517 lines
22 KiB
C#
using Lskj.Control;
|
|
using MySql.Data.MySqlClient;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Data.SqlClient;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace AfkDataService
|
|
{
|
|
public partial class FrmMain : Form
|
|
{
|
|
#region 变量
|
|
/// <summary>
|
|
/// 同步到ls循环timer事件
|
|
/// </summary>
|
|
public System.Timers.Timer toLsTimer = new System.Timers.Timer();
|
|
/// <summary>
|
|
/// 同步到afk循环timer事件
|
|
/// </summary>
|
|
public System.Timers.Timer toAfkTimer = new System.Timers.Timer();
|
|
/// <summary>
|
|
/// sqlserver连接字符串
|
|
/// </summary>
|
|
public string sqlServerConStr { get; set; }
|
|
/// <summary>
|
|
/// mySql连接字符串
|
|
/// </summary>
|
|
public string mySqlConStr { get; set; }
|
|
#endregion
|
|
public FrmMain()
|
|
{
|
|
InitializeComponent();
|
|
this.Load += OnFrmMainLoad;
|
|
this.startBtn.Appearance.BackColor = Color.FromArgb(140, 208, 1);
|
|
this.startBtn.MouseHover += new EventHandler(OnStartBtn_MouseHover);
|
|
this.startBtn.MouseLeave += new EventHandler(OnStartBtn_MouseLeave);
|
|
this.startBtn.Click += new EventHandler(OnStartBtn_Click);
|
|
this.intervalTime.LostFocus += new EventHandler(OnIntervalTime_LostFocus);
|
|
}
|
|
#region 事件
|
|
/// <summary>
|
|
/// 窗体初始化
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void OnFrmMainLoad(object sender, EventArgs e)
|
|
{
|
|
InitConnectInfo();
|
|
}
|
|
/// <summary>
|
|
/// 开始按钮点击
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void OnStartBtn_Click(object sender, EventArgs e)
|
|
{
|
|
this.startBtn.Enabled = false;
|
|
try
|
|
{
|
|
this.sqlServerConStr = GetSqlConnection();
|
|
this.mySqlConStr = GetMySqlConnection();
|
|
//初始化同步到朗速的timer
|
|
this.toLsTimer.Elapsed += ToLsTimer_Elapsed;
|
|
this.toLsTimer.Interval = 100;//初始化为100,以确保立刻执行,过程中进行修改
|
|
this.toLsTimer.AutoReset = true;
|
|
this.toLsTimer.Start();
|
|
//初始化同步到中航的timer
|
|
this.toAfkTimer.Elapsed += ToAfkTimer_Elapsed;
|
|
this.toAfkTimer.Interval = 100;
|
|
this.toAfkTimer.AutoReset = true;
|
|
this.toAfkTimer.Start();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageUtil.Show($"初始化平台失败,原因:{ex.Message}");
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 朗速同步到中航的逻辑
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void ToAfkTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
|
|
{
|
|
this.toAfkTimer.Enabled = false;
|
|
try
|
|
{
|
|
if (this.toAfkTimer.Interval == 100)
|
|
{
|
|
this.toAfkTimer.Interval = Convert.ToInt32(intervalTime.Text) * 1000;
|
|
}
|
|
this.toAfkTextBox.Invoke(new Action(() =>
|
|
{
|
|
this.toAfkTextBox.Clear();
|
|
}));
|
|
AfkDataService.SqlHelper SqlHelper = new AfkDataService.SqlHelper(new SqlConnection(this.sqlServerConStr));
|
|
AfkDataService.MySqlHelper MySqlHelper = new AfkDataService.MySqlHelper(new MySqlConnection(this.mySqlConStr));
|
|
DataTable dataDirectTable = SqlHelper.ExecuteDataTable("select * from p_synchrTab where disableTag = 'true' and directionTag = '1' ORDER BY id");
|
|
string selectCond = string.Format("directionTag='{0}'", ((int)DataDirection.LsToAfk));
|
|
IEnumerable<IGrouping<string, DataRow>> performDrGroup = dataDirectTable.Select(selectCond).GroupBy(n => n["id"] + "");//需要执行的数据条数
|
|
string uuid = Guid.NewGuid().ToString().Replace("-", "").ToLower();//每一次循环的uuid相同
|
|
foreach (IGrouping<string, DataRow> groupItem in performDrGroup)
|
|
{
|
|
SendMessage(toAfkTextBox, $"准备同步ID:{groupItem.Key},同步表数量{groupItem.Count()}\r\n");
|
|
List<AbutmentModel> abutmentModels = new List<AbutmentModel>();//同步分组对象集合
|
|
SynchData synchData = new SynchData(SqlHelper, MySqlHelper);
|
|
synchData.frmMain = this;
|
|
synchData.SqlConnectStr = this.sqlServerConStr;
|
|
synchData.MySqlConnectStr = this.mySqlConStr;
|
|
foreach (DataRow item in groupItem)
|
|
{
|
|
AbutmentModel abutmentModel;
|
|
try
|
|
{
|
|
abutmentModel = new AbutmentModel(item, DataDirection.LsToAfk, this.afkDataBase.Text + "", this.sqlServerConStr, this.mySqlConStr);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.Message);
|
|
SendMessage(toAfkTextBox, $"同步配置{item["explain"]}信息获取失败,原因:{ex.Message}\r\n");
|
|
continue;
|
|
}
|
|
abutmentModel.uuid = uuid;
|
|
abutmentModels.Add(abutmentModel);
|
|
}
|
|
bool isSucess = false;
|
|
foreach (AbutmentModel item in abutmentModels)//同步数据
|
|
{
|
|
SendMessage(toAfkTextBox, $"正在同步表{item.lsTabName}==>{item.afkTabName}\r\n");
|
|
isSucess = synchData.SyncDataTable(item);
|
|
if (!isSucess) break;
|
|
}
|
|
if (!isSucess) continue;
|
|
foreach (AbutmentModel item in abutmentModels)//同步日志
|
|
{
|
|
SendMessage(toAfkTextBox, $"正在反写日志==>{item.logTabName}\r\n");
|
|
isSucess = synchData.SendLog(item);
|
|
if (!isSucess) break;
|
|
}
|
|
if (!isSucess) continue;
|
|
foreach (AbutmentModel item in abutmentModels)//完成后改变标记
|
|
{
|
|
isSucess = synchData.AfterSync(item);
|
|
if (!isSucess) break;
|
|
}
|
|
if (!isSucess) continue;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.Message);
|
|
}
|
|
this.toAfkTimer.Enabled = true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// afk同步到朗速的数据逻辑
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void ToLsTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
|
|
{
|
|
this.toLsTimer.Enabled = false;
|
|
try
|
|
{
|
|
if (this.toLsTimer.Interval == 100)
|
|
{
|
|
this.toLsTimer.Interval = Convert.ToInt32(intervalTime.Text) * 1000;
|
|
}
|
|
this.toLsTextBox.Invoke(new Action(() =>
|
|
{
|
|
this.toLsTextBox.Clear();
|
|
}));
|
|
AfkDataService.SqlHelper SqlHelper = new AfkDataService.SqlHelper(new SqlConnection(this.sqlServerConStr));
|
|
AfkDataService.MySqlHelper MySqlHelper = new AfkDataService.MySqlHelper(new MySqlConnection(this.mySqlConStr));
|
|
DataTable dataDirectTable = SqlHelper.ExecuteDataTable("select * from p_synchrTab where disableTag = 'true' and directionTag = '0' ORDER BY id");
|
|
string selectCond = string.Format("directionTag='{0}'", ((int)DataDirection.AfkToLs));
|
|
IEnumerable<IGrouping<string, DataRow>> performDrGroup = dataDirectTable.Select(selectCond).GroupBy(n => n["id"] + "");//需要执行的数据条数
|
|
string uuid = Guid.NewGuid().ToString().Replace("-", "").ToLower();//每一次循环的uuid相同
|
|
foreach (IGrouping<string, DataRow> groupItem in performDrGroup)
|
|
{
|
|
SendMessage(toLsTextBox, $"准备同步ID:{groupItem.Key},同步表数量{groupItem.Count()}\r\n");
|
|
List<AbutmentModel> abutmentModels = new List<AbutmentModel>();//同步分组对象集合
|
|
SynchData synchData = new SynchData(SqlHelper, MySqlHelper);
|
|
synchData.frmMain = this;
|
|
synchData.SqlConnectStr = this.sqlServerConStr;
|
|
synchData.MySqlConnectStr = this.mySqlConStr;
|
|
foreach (DataRow item in groupItem)
|
|
{
|
|
AbutmentModel abutmentModel;
|
|
try
|
|
{
|
|
abutmentModel = new AbutmentModel(item, DataDirection.AfkToLs, this.afkDataBase.Text + "", this.sqlServerConStr, this.mySqlConStr);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.Message);
|
|
SendMessage(toLsTextBox, $"同步配置{item["explain"]}信息获取失败,原因:{ex.Message}\r\n");
|
|
continue;
|
|
}
|
|
abutmentModel.uuid = uuid;
|
|
abutmentModels.Add(abutmentModel);
|
|
}
|
|
bool isSucess = false;
|
|
foreach (AbutmentModel item in abutmentModels)//同步数据
|
|
{
|
|
SendMessage(toLsTextBox, $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}:正在同步表{item.afkTabName}==>{item.lsTempTabName}\r\n");
|
|
isSucess = synchData.SyncDataTable(item);
|
|
if (!isSucess) break;
|
|
}
|
|
if (!isSucess) continue;
|
|
foreach (AbutmentModel item in abutmentModels)//存储过程
|
|
{
|
|
SendMessage(toLsTextBox, $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}:正在执行业务逻辑判断\r\n");
|
|
isSucess = synchData.ExecBatchAfter(item);
|
|
if (!isSucess) break;
|
|
}
|
|
if (!isSucess) continue;
|
|
foreach (AbutmentModel item in abutmentModels)//同步日志
|
|
{
|
|
SendMessage(toLsTextBox, $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}:正在反写日志==>{item.logTabName}\r\n");
|
|
isSucess = synchData.SendLog(item);
|
|
if (!isSucess) break;
|
|
}
|
|
if (!isSucess) continue;
|
|
foreach (AbutmentModel item in abutmentModels)//完成后改变标记
|
|
{
|
|
isSucess = synchData.AfterSync(item);
|
|
if (!isSucess) break;
|
|
}
|
|
if (!isSucess) continue;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.Message);
|
|
}
|
|
this.toLsTimer.Enabled = true;
|
|
}
|
|
/// <summary>
|
|
/// 开始按钮颜色变化
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void OnStartBtn_MouseLeave(object sender, EventArgs e)
|
|
{
|
|
DevExpress.XtraEditors.SimpleButton btn = (DevExpress.XtraEditors.SimpleButton)sender;
|
|
btn.Appearance.BackColor = Color.FromArgb(52, 142, 216);
|
|
}
|
|
/// <summary>
|
|
/// 开始按钮颜色变化
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void OnStartBtn_MouseHover(object sender, EventArgs e)
|
|
{
|
|
DevExpress.XtraEditors.SimpleButton btn = (DevExpress.XtraEditors.SimpleButton)sender;
|
|
btn.Appearance.BackColor = Color.FromArgb(140, 208, 1);
|
|
}
|
|
// <summary>
|
|
/// 时间间隔TextBox失去焦点事件
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void OnIntervalTime_LostFocus(object sender, EventArgs e)
|
|
{
|
|
string checkStr = intervalTime.Text.ToString();
|
|
if (string.IsNullOrEmpty(checkStr)) intervalTime.Text = "30";
|
|
int check = Convert.ToInt32(intervalTime.Text.ToString());
|
|
if (check < 30)
|
|
{
|
|
intervalTime.Text = "30";
|
|
}
|
|
IniHelper.Write("parameter", "time", intervalTime.Text.ToString());
|
|
}
|
|
#endregion
|
|
#region 方法
|
|
/// <summary>
|
|
/// 初始化连接信息
|
|
/// </summary>
|
|
public void InitConnectInfo()
|
|
{
|
|
//afk
|
|
afkSeverId.Text = IniHelper.Read("Setting.ini", "AFK", "serverName");//地址
|
|
afkPortNumber.Text = IniHelper.Read("Setting.ini", "AFK", "port"); //端口
|
|
afkDataBase.Text = IniHelper.Read("Setting.ini", "AFK", "dbName");//数据库名
|
|
afkName.Text = IniHelper.Read("Setting.ini", "AFK", "user");//用户名
|
|
afkPassword.Text = IniHelper.Read("Setting.ini", "AFK", "password");//密码
|
|
//ls
|
|
lsSeverId.Text = IniHelper.Read("Setting.ini", "LS", "serverName");//地址
|
|
lsDataBase.Text = IniHelper.Read("Setting.ini", "LS", "dbName");//数据库名
|
|
lsName.Text = IniHelper.Read("Setting.ini", "LS", "user");//用户名
|
|
lsPassword.Text = IniHelper.Read("Setting.ini", "LS", "password");//密码
|
|
//parameter
|
|
string time = IniHelper.Read("Setting.ini", "parameter", "time");//间隔时间
|
|
if (string.IsNullOrEmpty(time)) time = "30";
|
|
if (Convert.ToInt32(time) < 30) time = "30";
|
|
intervalTime.Text = time;
|
|
}
|
|
#region 数据库连接方法
|
|
/// <summary>
|
|
/// 获取sqlserver服务器的连接字符串
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public string GetSqlConnection()
|
|
{
|
|
string serverName = this.lsSeverId.Text.Trim();//地址
|
|
string dbName = this.lsDataBase.Text.Trim();//数据库名
|
|
string user = this.lsName.Text.Trim();//用户名
|
|
string password = this.lsPassword.Text.Trim();//密码
|
|
return string.Format("Server={0};Database={1};Persist Security Info=True;User ID={2};Password={3};Connection Timeout=5;MultipleActiveResultSets=true", serverName, dbName, user, password);
|
|
}
|
|
/// <summary>
|
|
/// 获取mysql服务器的连接字符串
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public string GetMySqlConnection()
|
|
{
|
|
string serverName = this.afkSeverId.Text.Trim();//地址
|
|
string port = this.afkPortNumber.Text.Trim();//端口
|
|
string dbName = this.afkDataBase.Text.Trim();//数据库名
|
|
string user = this.afkName.Text.Trim();//用户名
|
|
string password = this.afkPassword.Text.Trim();//密码
|
|
IniHelper.Write("AFK", "serverName", serverName);
|
|
IniHelper.Write("AFK", "port", port);
|
|
IniHelper.Write("AFK", "dbName", dbName);
|
|
IniHelper.Write("AFK", "user", user);
|
|
IniHelper.Write("AFK", "password", password);
|
|
return string.Format("Data Source={0};Port={1};Database={2};User ID={3};Password={4};Charset=utf8;Convert Zero Datetime=True;", serverName, port, dbName, user, password);
|
|
}
|
|
///// <summary>
|
|
///// 创建sqlserver数据库连接
|
|
///// </summary>
|
|
///// <param name="isSave"></param>
|
|
///// <returns></returns>
|
|
//public bool ConnectSqlServer()
|
|
//{
|
|
// string serverName = this.lsSeverId.Text.Trim();//地址
|
|
// string dbName = this.lsDataBase.Text.Trim();//数据库名
|
|
// string user = this.lsName.Text.Trim();//用户名
|
|
// string password = this.lsPassword.Text.Trim();//密码
|
|
// if (string.IsNullOrWhiteSpace(serverName))
|
|
// {
|
|
// MessageUtil.Show("请重新输入朗速数据库地址");
|
|
// return false;
|
|
// }
|
|
// if (string.IsNullOrWhiteSpace(dbName))
|
|
// {
|
|
// MessageUtil.Show("请重新输入朗速数据库库名");
|
|
// return false;
|
|
// }
|
|
// if (string.IsNullOrWhiteSpace(user))
|
|
// {
|
|
// MessageUtil.Show("请重新输入朗速数据库用户名");
|
|
// return false;
|
|
// }
|
|
// // 测试数据库连接
|
|
// try
|
|
// {
|
|
// if (ConnectingToSqlServer())
|
|
// {
|
|
// IniHelper.Write("LS", "serverName", serverName);
|
|
// IniHelper.Write("LS", "dbName", dbName);
|
|
// IniHelper.Write("LS", "user", user);
|
|
// IniHelper.Write("LS", "password", password);
|
|
// return true;
|
|
// }
|
|
// else
|
|
// {
|
|
// MessageUtil.Show("连接失败,请检查朗速方配置");
|
|
// return false;
|
|
// }
|
|
// }
|
|
// catch (Exception ex)
|
|
// {
|
|
// MessageUtil.Show($"连接失败,原因:{ex.Message},请检查朗速方配置");
|
|
// }
|
|
// return true;
|
|
//}
|
|
//public bool ConnectingToSqlServer()
|
|
//{
|
|
// string connStr = GetSqlConnection();
|
|
// sqlServerConStr = connStr;
|
|
// if (SqlHelper._connection != null)
|
|
// {
|
|
// try
|
|
// {
|
|
// SqlHelper._connection.Close();
|
|
// SqlHelper._connection.Dispose();
|
|
// }
|
|
// catch (Exception)
|
|
// {
|
|
// }
|
|
// SqlHelper._connection = null;
|
|
// }
|
|
// try
|
|
// {
|
|
// SqlHelper._connection = new SqlConnection(connStr);
|
|
// SqlHelper._connection.Open();
|
|
// return true;
|
|
// }
|
|
// catch (Exception ex)
|
|
// {
|
|
// Console.WriteLine(ex.Message);
|
|
// }
|
|
// return false;
|
|
//}
|
|
///// <summary>
|
|
///// 创建Mysql数据库连接
|
|
///// </summary>
|
|
///// <param name="isSave"></param>
|
|
///// <returns></returns>
|
|
//public bool ConnectMySql()
|
|
//{
|
|
// string serverName = this.afkSeverId.Text.Trim();//地址
|
|
// string port = this.afkPortNumber.Text.Trim();//端口
|
|
// string dbName = this.afkDataBase.Text.Trim();//数据库名
|
|
// string user = this.afkName.Text.Trim();//用户名
|
|
// string password = this.afkPassword.Text.Trim();//密码
|
|
// if (string.IsNullOrWhiteSpace(serverName))
|
|
// {
|
|
// MessageUtil.Show("请重新输入Afk数据库地址");
|
|
// return false;
|
|
// }
|
|
// if (string.IsNullOrWhiteSpace(port))
|
|
// {
|
|
// MessageUtil.Show("请重新输入Afk数据库端口");
|
|
// return false;
|
|
// }
|
|
// if (string.IsNullOrWhiteSpace(dbName))
|
|
// {
|
|
// MessageUtil.Show("请重新输入Afk数据库库名");
|
|
// return false;
|
|
// }
|
|
// if (string.IsNullOrWhiteSpace(user))
|
|
// {
|
|
// MessageUtil.Show("请重新输入Afk数据库用户名");
|
|
// return false;
|
|
// }
|
|
// // 测试数据库连接
|
|
// try
|
|
// {
|
|
// if (ConnectingToMySql())
|
|
// {
|
|
|
|
// IniHelper.Write("AFK", "serverName", serverName);
|
|
// IniHelper.Write("AFK", "port", port);
|
|
// IniHelper.Write("AFK", "dbName", dbName);
|
|
// IniHelper.Write("AFK", "user", user);
|
|
// IniHelper.Write("AFK", "password", password);
|
|
// return true;
|
|
// }
|
|
// else
|
|
// {
|
|
// MessageUtil.Show("连接失败,请检查AFK方配置");
|
|
// return false;
|
|
// }
|
|
// }
|
|
// catch (Exception ex)
|
|
// {
|
|
// MessageUtil.Show($"连接失败,原因:{ex.Message},请检查AFK方配置");
|
|
// }
|
|
// return true;
|
|
//}
|
|
//public bool ConnectingToMySql()
|
|
//{
|
|
// string connStr = GetMySqlConnection();
|
|
// mySqlConStr = connStr;
|
|
// if (MySqlHelper._connection != null)
|
|
// {
|
|
// try
|
|
// {
|
|
// MySqlHelper._connection.Close();
|
|
// MySqlHelper._connection.Dispose();
|
|
// }
|
|
// catch (Exception)
|
|
// {
|
|
// }
|
|
// MySqlHelper._connection = null;
|
|
// }
|
|
// try
|
|
// {
|
|
// MySqlHelper._connection = new MySqlConnection(connStr);
|
|
// MySqlHelper._connection.Open();
|
|
// return true;
|
|
// }
|
|
// catch (Exception ex)
|
|
// {
|
|
// Console.WriteLine(ex.Message);
|
|
// }
|
|
// return false;
|
|
//}
|
|
#endregion
|
|
public void SendMessage(TextBox textBox, string message)
|
|
{
|
|
if (textBox.InvokeRequired)
|
|
{
|
|
textBox.Invoke(new Action(() =>
|
|
{
|
|
textBox.AppendText(message);
|
|
}));
|
|
}
|
|
else
|
|
{
|
|
textBox.AppendText(message);
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
}
|