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

182 lines
6.5 KiB
C#

using Lserp_UpdateServer;
using Lskj.Core;
using System;
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;
namespace lserp_UpdateDepart
{
public partial class UpdateDepart : Form
{
private string DomainN, Port, Timer, Deptnumber, Key, jsonObj;
private bool NewInitSuccess = false;
private Int64 NewTxtMaxLength = 10000;
bool isUpdDepart = false;
private IniFiles ini = new IniFiles(Application.StartupPath + @"\MyConfig.INI");
public UpdateDepart()
{
InitializeComponent();
}
/// <summary>
/// 请求更新部门地址
/// </summary>
public static string GetPostUpdDepartUrl(string DomainN, string Port, string Key)
{
string url = string.Format("http://" + DomainN + ":" + Port + "/api/v2/department/update/?key={0}", Key);
return url;
}
/// <summary>
/// 同步部门
/// </summary>
public void Insertdept()
{
if (!NewInitSuccess) return;
RefreshLogUIThread(DateTime.Now + " 开始同步.\r\n");
try
{
DBConfig.Instance.CreateConnection();
DataTable DT = SqlHelper.ExecuteDataTable("select SpeciesNo as deptnumber,SpeciesName as deptname,case when len(SpeciesNo)=2 then '1' else SUBSTRING(SpeciesNo,1,len(SpeciesNo)-2) end as parentnumber from P_EmployeeSpecTab");
foreach (DataRow item in DT.Rows)
{
if (string.IsNullOrEmpty(item["parentnumber"].ToString()))
{
continue;
}
string url = GetPostUpdDepartUrl(DomainN, Port, Key);
jsonObj = JsonUtil.ToJsonObject(item);
//string deljson = jsonObj.Substring(1, jsonObj.Length - 2);
ResponseMode mode = NewEmp.PostMethodToObj(url, jsonObj);
if (0 == mode.ret)
{
string updareabak = DateTime.Now + string.Format(" {0} 部门读取成功.\r\n", item["deptname"]);
RefreshLogUIThread(updareabak);
isUpdDepart = true;
}
else
{
string errorMsg = mode != null ? mode.msg : "未知原因.";
RefreshLogUIThread(DateTime.Now + string.Format(" 部门读取失败,错误原因:{0}.\r\n", errorMsg));
isUpdDepart = false;
}
}
}
catch (Exception ex)
{
RefreshLogUIThread(DateTime.Now + string.Format(" 部门读取失败.\r\n" + " 原因:" + ex.Message + Environment.NewLine + ex.InnerException.ToString()));
}
RefreshLogUIThread(DateTime.Now + " 本轮部门更新完成.\r\n");
RefreshLogUIThread("--------------------分割线------------------------\r\n");
if (isUpdDepart)
{
DialogResult TS = MessageBox.Show("是否继续同步人员?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (TS == DialogResult.Yes)
{
Form1 r = new Form1();
r.ShowDialog();
}
else
{
return;
}
}
}
private void Initialization()
{
try
{
RefreshLog(DateTime.Now + " 正在初始化平台.\r\n");
if (DBConfig.Instance.CreateConnection())
{
//获取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(Deptnumber))
{
RefreshLog(DateTime.Now + " 初始化平台失败.\r\n" + "原因:配置文件不全请重新配置.");
MessageBox.Show(" 初始化平台失败.\r\n" + "原因:配置文件不全请重新配置.");
config c = new config();
c.ShowDialog();
Initialization();
}
else
{
NewInitSuccess = true;
RefreshLog(DateTime.Now + " 初始化平台成功.\r\n");
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void UpdateDepart_Load_1(object sender, EventArgs e)
{
Initialization();
Thread backThread = new Thread(Insertdept);
backThread.IsBackground = true;
backThread.Start();
}
/// <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)
{
Invoke((EventHandler)delegate
{
txtLog.Text = "";
txtLog.Select(txtLog.TextLength, 0);
txtLog.ScrollToCaret();
});
}
}
}
}