You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

370 lines
13 KiB

2 years ago
  1. using Entity.Sugar;
  2. using Euro.Transfer.Base;
  3. using SqlSugar.Base;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Drawing;
  8. using System.Drawing.Drawing2D;
  9. using System.Linq;
  10. using System.Reflection;
  11. using System.Threading;
  12. using System.Windows.Forms;
  13. using System.Xml;
  14. namespace Euro.Transfer
  15. {
  16. public partial class WordTransfer : Form
  17. {
  18. //用於存放任务项
  19. private Hashtable hashTasks = new Hashtable();
  20. //是否停止運行
  21. private bool bRun = true;
  22. public WordTransfer()
  23. {
  24. InitializeComponent();
  25. InitAsync();
  26. InitialTray();
  27. }
  28. #region 自定义方法
  29. public void RunTasks()
  30. {
  31. try
  32. {
  33. //加载工作项
  34. if (this.hashTasks.Count == 0)
  35. {
  36. //获取configSections节点
  37. var configSections = ServiceTools.GetConfigSections();
  38. foreach (XmlNode section in configSections)
  39. {
  40. //过滤注释节点(如section中还包含其它节点需过滤)
  41. if (section.Name.ToLower() == nameof(section))
  42. {
  43. //创建每个节点的配置对象
  44. var sectionName = section.Attributes["name"].Value.Trim();
  45. var sectionType = section.Attributes["type"].Value.Trim();
  46. //程序集名称
  47. var assemblyName = sectionType.Split(',')[1];
  48. //完整类名
  49. var classFullName = assemblyName + ".Jobs." + sectionName + ".Config";
  50. //创建配置对象
  51. var config = (ServiceConfig)Assembly.Load(assemblyName).CreateInstance(classFullName);
  52. //创建工作对象
  53. var job = (ServiceTask)Assembly.Load(config.Assembly.Split(',')[1]).CreateInstance(config.Assembly.Split(',')[0]);
  54. job.ConfigObject = config;
  55. //将工作对象加载进HashTable
  56. this.hashTasks.Add(sectionName, job);
  57. }
  58. }
  59. }
  60. //执行工作项
  61. if (this.hashTasks.Keys.Count > 0)
  62. {
  63. foreach (ServiceTask task in hashTasks.Values)
  64. {
  65. //插入一个新的请求到线程池
  66. if (ThreadPool.QueueUserWorkItem(ThreadCallBack, task))
  67. {
  68. //方法成功排入队列
  69. }
  70. else
  71. {
  72. //失败
  73. }
  74. }
  75. }
  76. }
  77. catch (Exception error)
  78. {
  79. ServiceTools.WriteLog(ServiceBase.Errorlog_Path, error.ToString(), true);
  80. }
  81. }
  82. private void StopTasks()
  83. {
  84. //停止
  85. if (this.hashTasks != null)
  86. {
  87. this.hashTasks.Clear();
  88. }
  89. }
  90. /// <summary>
  91. /// 线程池回调方法
  92. /// </summary>
  93. /// <param name="state"></param>
  94. private void ThreadCallBack(Object state)
  95. {
  96. while (bRun)
  97. {
  98. ((ServiceTask)state).StartJob();
  99. //休眠1秒
  100. Thread.Sleep(1000);
  101. }
  102. }
  103. #endregion 自定义方法
  104. private void BtnStart_Click(object sender, EventArgs e)
  105. {
  106. var user = ServiceTask.hubClient.OnlineUsers.FirstOrDefault(u => (u.OrgId == ServiceTask.hubClient.clientOrgId && u.UserId == ServiceTask.hubClient.clientId));
  107. if (user != null)
  108. {
  109. MessageBox.Show("小助手已經在別的機器上開啟並運行,請勿再次運行(OrgId:" + user.OrgId + ";UserId:" + user.UserId + ")");
  110. return;
  111. }
  112. if (ServiceTask.hubClient.connection.State == Microsoft.AspNet.SignalR.Client.ConnectionState.Connected)
  113. {
  114. ServiceTask.hubClient.msgProxy.Invoke("Register", ServiceTask.hubClient.clientOrgId, ServiceTask.hubClient.clientId, ServiceTask.hubClient.clientName, true);
  115. }
  116. else
  117. {
  118. ServiceTask.hubClient.connection.Start().ContinueWith(t =>
  119. {
  120. if (!t.IsFaulted)
  121. {
  122. //连接成功,调用Register方法
  123. ServiceTask.hubClient.msgProxy.Invoke("Register", ServiceTask.hubClient.clientOrgId, ServiceTask.hubClient.clientId, ServiceTask.hubClient.clientName, true);
  124. }
  125. else
  126. {
  127. //MessageBox.Show("通訊連接失敗!! 請檢查Tracking後臺系統是否正常運行");
  128. ServiceTools.WriteLog(ServiceBase.Errorlog_Path, "Euro.Transfer.WordTransfer:通訊連接失敗!! 請檢查Tracking後臺系統是否正常運行", true);
  129. }
  130. });
  131. }
  132. btnStart.Enabled = false;
  133. btnEnd.Enabled = true;
  134. bRun = true;
  135. Thread.Sleep(2000); //延时两秒
  136. this.RunTasks();
  137. scrollingText1.BackgroundBrush =
  138. new LinearGradientBrush(this.scrollingText1.ClientRectangle,
  139. Color.Red, Color.Blue,
  140. LinearGradientMode.Horizontal);
  141. scrollingText1.ForeColor = Color.Yellow;
  142. scrollingText1.ScrollText = "運行中...";
  143. scrollingText1.ScrollDirection = ScrollingTextControl.ScrollDirection.LeftToRight;
  144. scrollingText1.Enabled = true;
  145. }
  146. private void BtnEnd_Click(object sender, EventArgs e)
  147. {
  148. if (ServiceTask.hubClient.connection.State == Microsoft.AspNet.SignalR.Client.ConnectionState.Connected)
  149. {
  150. ServiceTask.hubClient.msgProxy.Invoke("offline");
  151. }
  152. else
  153. {
  154. ServiceTask.hubClient.connection.Start().ContinueWith(t =>
  155. {
  156. if (!t.IsFaulted)
  157. {
  158. ServiceTask.hubClient.msgProxy.Invoke("offline");
  159. }
  160. else
  161. {
  162. //MessageBox.Show("通訊連接失敗!! 請檢查Tracking後臺系統是否正常運行");
  163. ServiceTools.WriteLog(ServiceBase.Errorlog_Path, "Euro.Transfer.WordTransfer:通訊連接失敗!! 請檢查Tracking後臺系統是否正常運行", true);
  164. }
  165. });
  166. }
  167. btnStart.Enabled = true;
  168. btnEnd.Enabled = false;
  169. bRun = false;
  170. this.StopTasks();
  171. scrollingText1.BackgroundBrush =
  172. new LinearGradientBrush(this.scrollingText1.ClientRectangle,
  173. Color.LightGray, Color.LightGray,
  174. LinearGradientMode.Horizontal);
  175. scrollingText1.ForeColor = Color.LightSlateGray;
  176. scrollingText1.ScrollText = "已停止";
  177. scrollingText1.Enabled = false;
  178. }
  179. private async void InitAsync()
  180. {
  181. var sWriteWordPath = Common.GetAppSetting("WriteWordPath");
  182. txtPath.Text = sWriteWordPath;
  183. scrollingText1.BackgroundBrush =
  184. new LinearGradientBrush(this.scrollingText1.ClientRectangle,
  185. Color.LightGray, Color.LightGray,
  186. LinearGradientMode.Horizontal);
  187. scrollingText1.ForeColor = Color.LightSlateGray;
  188. scrollingText1.ScrollText = "請點擊“啟動”運行小助手";
  189. scrollingText1.Enabled = false;
  190. var url = Common.GetAppSetting("EURO_MsgServerUrl");
  191. await ServiceTask.hubClient.RunAsync(url);
  192. ServiceTask.hubClient.writeOrLogs += new HubTransfer.WriteOrLogsHandler(this.WriteOrLogs);
  193. }
  194. /// <summary>
  195. /// 显示记录并写log
  196. /// </summary>
  197. /// <param name="msg"></param>
  198. private void WriteOrLogs(string text, int count)
  199. {
  200. try
  201. {
  202. this.BeginInvoke(new Action<string, int>(WriteMsg), text, count);
  203. }
  204. catch (Exception ex)
  205. {
  206. ServiceTools.WriteLog(ServiceBase.Errorlog_Path, ex.ToString(), true);
  207. }
  208. }
  209. private void WriteMsg(string text, int count = 0)
  210. {
  211. if (!string.IsNullOrEmpty(text))
  212. {
  213. if (this.txtSyntax.Lines.Length > 1 * 1000)
  214. {
  215. this.txtSyntax.Text = "";
  216. }
  217. this.txtSyntax.AppendText(text);
  218. lbCount.Text = (count + int.Parse(lbCount.Text)).ToString();
  219. }
  220. var db = SugarBase.GetIntance();
  221. var iCount_Cus = db.Queryable<OTB_CRM_CustomersTransferBak>().Count();
  222. var iCount_Bills = db.Queryable<OTB_OPM_BillsBak>().Count();
  223. var iCount_Exh = db.Queryable<OTB_OPM_ExhibitionsTransferBak>().Count();
  224. lbTotalCount.Text = (iCount_Cus + iCount_Bills + iCount_Exh).ToString();
  225. }
  226. /// <summary>
  227. /// 窗体关闭的单击事件
  228. /// </summary>
  229. /// <param name="sender"></param>
  230. /// <param name="e"></param>
  231. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  232. {
  233. e.Cancel = true;
  234. //通过这里可以看出,这里的关闭其实不是真正意义上的“关闭”,而是将窗体隐藏,实现一个“伪关闭”
  235. this.Hide();
  236. }
  237. private void InitialTray()
  238. {
  239. //隐藏主窗体
  240. this.Hide();
  241. //true表示在托盘区可见,false表示在托盘区不可见
  242. notifyIcon1.Visible = true;
  243. //气泡显示的时间(单位是毫秒)
  244. notifyIcon1.ShowBalloonTip(2000);
  245. //notifyIcon.MouseClick += new MouseEventHandler(notifyIcon_MouseClick);
  246. notifyIcon1.MouseDoubleClick += new MouseEventHandler(NotifyIcon_MouseClick);
  247. ////设置一级级菜单
  248. MenuItem home = new MenuItem("主頁面");
  249. home.Click += new EventHandler(ShowHome_Click);
  250. MenuItem exit = new MenuItem("退出");
  251. exit.Click += new EventHandler(Exit_Click);
  252. //退出菜单项
  253. var childen = new MenuItem[] { home, exit };
  254. notifyIcon1.ContextMenu = new ContextMenu(childen);
  255. //窗体关闭时触发
  256. this.FormClosing += new FormClosingEventHandler(this.Form1_FormClosing);
  257. WriteMsg("");
  258. }
  259. /// <summary>
  260. /// 单击菜单"主页面"
  261. /// </summary>
  262. /// <param name="sender"></param>
  263. /// <param name="e"></param>
  264. private void ShowHome_Click(object sender, EventArgs e)
  265. {
  266. this.Visible = true;
  267. this.Activate();
  268. }
  269. /// <summary>
  270. /// 鼠标单击
  271. /// </summary>
  272. /// <param name="sender"></param>
  273. /// <param name="e"></param>
  274. private void NotifyIcon_MouseClick(object sender, MouseEventArgs e)
  275. {
  276. //鼠标左键单击
  277. if (e.Button == MouseButtons.Left)
  278. {
  279. //如果窗体是可见的,那么鼠标左击托盘区图标后,窗体为不可见
  280. //if (this.Visible == true)
  281. //{
  282. // this.Visible = false;
  283. //}
  284. //else
  285. //{
  286. // this.Visible = true;
  287. // this.Activate();
  288. //}
  289. this.Visible = true;
  290. this.Activate();
  291. }
  292. }
  293. /// <summary>
  294. /// 退出选项
  295. /// </summary>
  296. /// <param name="sender"></param>
  297. /// <param name="e"></param>
  298. private void Exit_Click(object sender, EventArgs e)
  299. {
  300. //退出程序
  301. Environment.Exit(0);
  302. }
  303. private void BtnSure_Click(object sender, EventArgs e)
  304. {
  305. try
  306. {
  307. var bError = true;
  308. do
  309. {
  310. var dicUpdKeys = new Dictionary<string, string>
  311. {
  312. { "WriteWordPath", txtPath.Text }
  313. };
  314. foreach (string key in dicUpdKeys.Keys)
  315. {
  316. var bOk = Common.UpdateAppSettings(key, dicUpdKeys[key]);
  317. if (!bOk)
  318. {
  319. bError = false;
  320. break;
  321. }
  322. }
  323. }
  324. while (false);
  325. if (bError)
  326. {
  327. MessageBox.Show("修改成功");
  328. }
  329. else
  330. {
  331. MessageBox.Show("修改失敗");
  332. }
  333. }
  334. catch (Exception error)
  335. {
  336. MessageBox.Show("修改失敗");
  337. ServiceTools.WriteLog(ServiceBase.Errorlog_Path, error.ToString(), true);
  338. }
  339. }
  340. }
  341. }