Files
lserp_cs_6.0/其他程序/Lserp_UpdateEmp/Lserp_UpdateEmp/Form1.cs
T
cyf ab56a9bcf7 基线 SVN r240
SVN-Revision: r240
2025-02-06 06:46:06 +00:00

272 lines
11 KiB
C#

using Lskj.Core;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;
using Lserp_UpdateServer;
namespace lserp_UpdateDepart
{
public partial class Form1 : Form
{
private string DomainN, Port, Timer, Deptnumber, Key;
private bool NewInitSuccess = false;
private Int64 NewTxtMaxLength = 10000;
private IniFiles ini = new IniFiles(Application.StartupPath + @"\MyConfig.INI");
String jsonObj = "";
bool flog = false;//用于判断是否加载完成就刷新
public Form1()
{
InitializeComponent();
}
public static string GetPostUpdUrl(string DomainN, string Port,string Key)
{
string url = string.Format("http://" + DomainN + ":" + Port + "/api/v2/employee/update/?key={0}", Key);
return url;
}
/// <summary>
/// 请求离职地址
/// </summary>
public static string GetPostDelUrl(string DomainN, string Port, string Key)
{
string url = string.Format("http://" + DomainN + ":" + Port + "/api/v2/employee/delete/?key={0}", Key);
return url;
}
private void Form1_Load(object sender, EventArgs e)
{
this.Initialization(); // 初始化同步平台
// 创建后台线程
Thread backThread = new Thread(UpdateEmp);
backThread.IsBackground = true;
backThread.Start();
}
/// <summary>
/// 删除离职员工方法
/// </summary>
public void DelEmp()
{
string Delsql = string.Format("select LoginAccount as pin from P_EmployeeTab where sign=1 and SyncFlag=1");
DataTable delTable = SqlHelper.ExecuteDataTable(Delsql);
if (delTable.Rows.Count > 0)
{
string url = GetPostDelUrl(DomainN, Port, Key);
foreach (DataRow item in delTable.Rows)
{
jsonObj = JsonUtil.ToJsonObject(item);
string deljson = jsonObj.Substring(1, jsonObj.Length - 2);
ResponseMode mode = NewEmp.PostMethodToObj(url, deljson);
if (0 == mode.ret)
{
string delareabak = DateTime.Now + string.Format(" 离职人员{0} 信息删除成功.\r\n", item["pin"]);
RefreshLogUIThread(delareabak);
SqlHelper.ExecuteNonQuery(string.Format("update P_EmployeeTab set p_emp_areabak = '{1}' where LoginAccount='{0}'", item["pin"], delareabak));
SqlHelper.ExecuteNonQuery(string.Format("update P_EmployeeTab set SyncFlag = {1} where LoginAccount='{0}'", item["pin"], 0));
}
else
{
string errorMsg = mode != null ? mode.msg : "未知原因.";
RefreshLogUIThread(DateTime.Now + string.Format(" 信息删除失败,错误原因:{0}.\r\n", errorMsg));
SqlHelper.ExecuteNonQuery(string.Format("update P_EmployeeTab set p_emp_areabak = '{1}' where LoginAccount='{0}'", item["pin"], errorMsg));
}
}
}
}
/// <summary>
/// 同步人员方法
/// </summary>
public void UpdateEmp()
{
if (!NewInitSuccess) return;
DataTable DT = SqlHelper.ExecuteDataTable("select name from syscolumns where id=object_id('P_EmployeeTab') and name='SyncFlag'");
bool flag = DT.Rows.Count > 0;
if (!flag)
{
SqlHelper.ExecuteNonQuery("ALTER TABLE P_EmployeeTab ADD SyncFlag INT DEFAULT(0)");
}
DataTable DT2 = SqlHelper.ExecuteDataTable("select name from syscolumns where id=object_id('P_EmployeeTab') and name='p_emp_areabak'");
bool fg = DT2.Rows.Count > 0;
if (!fg)
{
SqlHelper.ExecuteNonQuery("ALTER TABLE P_EmployeeTab ADD p_emp_areabak varchar(5000) DEFAULT(0)");
}
RefreshLogUIThread(DateTime.Now + " 开始同步.\r\n");
this.timer1.Enabled = false;
//st32ring sqlValue = string.Format("select LoginAccount as pin,p_emp_EmployeeName as name,{0} as deptnumber from P_EmployeeTab where ISNULL(SyncFlag,0)=0 and {0}!='null' and {0}!='' and sign=0",
//·);
string sqlValue = string.Format("select LoginAccount as pin,p_emp_EmployeeName as name,{0} as deptnumber from P_EmployeeTab where ISNULL(SyncFlag,0)=0 and isnull(sign,0)=0", Deptnumber);
flog = false;
DataTable dtTable = SqlHelper.ExecuteDataTable(sqlValue);
try
{
//删除离职员工方法
DelEmp();
//同步在职员工方法
foreach (DataRow item in dtTable.Rows)
{
string url = GetPostUpdUrl(DomainN, Port, Key);
string Str = item["deptnumber"].ToString();
String[] result = Str.Split(',');
for (int i = 0; i < result.Length; i++)
{
item["deptnumber"] = result[i];
jsonObj = JsonUtil.ToJsonObject(item);
ResponseMode mode = NewEmp.PostMethodToObj(url, jsonObj);
if (0 == mode.ret)
{
sqlValue = string.Format("update P_EmployeeTab set SyncFlag = 1 where LoginAccount='{0}'", item["pin"]);
SqlHelper.ExecuteNonQuery(sqlValue);
string updareabak = DateTime.Now + string.Format(" {0} 信息读取成功.\r\n", item["name"]);
RefreshLogUIThread(updareabak);
SqlHelper.ExecuteNonQuery(string.Format("update P_EmployeeTab set p_emp_areabak = '{1}' where LoginAccount='{0}'", item["pin"], updareabak));
}
else
{
string errorMsg = mode != null ? mode.msg : "未知原因.";
RefreshLogUIThread(DateTime.Now + string.Format(" 信息读取失败,错误原因:{0}.\r\n", errorMsg));
SqlHelper.ExecuteNonQuery(string.Format("update P_EmployeeTab set p_emp_areabak = '{1}' where LoginAccount='{0}'", item["pin"], errorMsg));
}
}
}
}
catch (Exception ex)
{
RefreshLogUIThread(DateTime.Now + string.Format(" 信息读取失败.\r\n" + " 原因:" + ex.Message + Environment.NewLine + ex.InnerException.ToString()));
}
flog = true;
RefreshLogUIThread(DateTime.Now + " 本轮人员更新完成.\r\n");
RefreshLogUIThread("--------------------分割线------------------------\r\n");
}
/// <summary>
/// 初始化同步平台
/// </summary>
private void Initialization()
{
try
{
RefreshLog(DateTime.Now + " 正在初始化平台.\r\n");
if (DBConfig.Instance.CreateConnection())
{
DataTable DT = SqlHelper.ExecuteDataTable("SELECT Name FROM SysObjects Where XType='U' and Name='P_EmployeeTab' ORDER BY Name");
// select name from syscolumns where id=object_id('P_EmployeeTab') and name='SyncFlag'
bool flag = DT.Rows.Count > 0;
if (flag)
{
//获取ini配置的值
DomainN = ini.IniReadValue("同步人员", "DomainN");
Port = ini.IniReadValue("同步人员", "Port");
Timer = ini.IniReadValue("同步人员", "Timer");
Deptnumber = ini.IniReadValue("同步人员", "Deptnumber");
Key = ini.IniReadValue("同步人员", "Key");
if (string.IsNullOrEmpty(DomainN) || string.IsNullOrEmpty(Port) || string.IsNullOrEmpty(Timer) || string.IsNullOrEmpty(Deptnumber) || string.IsNullOrEmpty(Key))
{
RefreshLog(DateTime.Now + " 初始化平台失败.\r\n" + "原因:配置文件不全请重新配置.");
MessageBox.Show(" 初始化平台失败.\r\n" + "原因:配置文件不全请重新配置.");
config c = new config();
c.ShowDialog();
Initialization();
}
else
{
NewInitSuccess = true;
RefreshLog(DateTime.Now + " 初始化平台成功.\r\n");
this.timer1.Interval = int.Parse(Timer) * 1000;
this.timer1.Start();
this.timer2.Interval = 100;
this.timer2.Start();
}
}
else
{
MessageBox.Show("此数据库内无'P_EmployeeTab'表!请配置好注册表后重新打开!");
this.Close();
return;
}
}
else
{
RefreshLog(DateTime.Now + " 初始化平台失败.\r\n");
}
}
catch (Exception ex)
{
RefreshLog(DateTime.Now + string.Format(" 初始化失败.原因:{0}.\r\n", ex.Message));
}
}
/// <summary>
/// 后台线程刷新数据
/// </summary>
/// <param name="msg"></param>
public void RefreshLogUIThread(string msg)
{
if (this.IsHandleCreated)
{
Invoke((EventHandler)delegate
{
txtLog.Text += msg;
txtLog.Select(txtLog.TextLength, 0);
txtLog.ScrollToCaret();
});
}
}
/// <summary>
/// 主线程刷新数据
/// </summary>
/// <param name="msg"></param>
public void RefreshLog(string msg)
{
txtLog.Text += msg;
txtLog.Select(txtLog.TextLength, 0);
txtLog.ScrollToCaret();
}
private void txtLog_TextChanged(object sender, EventArgs e)
{
if (this.txtLog.Text.Length > NewTxtMaxLength)
{
if (this.IsHandleCreated)
{
Invoke((EventHandler)delegate
{
txtLog.Text = "";
txtLog.Select(txtLog.TextLength, 0);
txtLog.ScrollToCaret();
});
}
}
}
private void timer1_Tick(object sender, EventArgs e)
{
UpdateEmp();
}
private void timer2_Tick(object sender, EventArgs e)
{
if (flog)
{
this.timer1.Enabled = true;
}
}
}
}