cdk
2 天以前 9d36af31f3646a9ece38f17fcde31f1320d767ec
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
using DataexchangeServer.Helper;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;
 
namespace DataexchangeServer
{  
    public  delegate void AppendTextCallBack(string text);
    public partial class frmMain : Form
    {    
        private SQLHelper _sqlHelper;
        public List<Allocation> ListCount = new List<Allocation>();
        public DateTime startTime;
 
        #region 默认起动软件frmMain()
        /// <summary>
        /// 默认起动软件
        /// </summary>
        public frmMain()
        {
            InitializeComponent();
            this.FormClosing += frmMain_FormClosing;
            this.SizeChanged += frmMain_SizeChanged;
            btnStart.Click += btnStart_Click;
            btnStop.Click += btnStop_Click;
            timer.Tick += timer_Tick;
            notifyIcon1.DoubleClick += notifyIcon1_DoubleClick;
            contextMenuStrip1.ItemClicked += contextMenuStrip1_ItemClicked;
            Initialize();
            //btnStart_Click(null, null);
        }
 
        void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("你确定要退出吗?", "广深软件", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
            {
                btnStop_Click(null, null);
                System.Environment.Exit(0);   //退出所有的线程
 
            }
            else
                e.Cancel = true;
        } 
        #endregion
 
        #region 初始化数据 Initialize
        /// <summary>
        /// 初始化数据
        /// </summary>
        void Initialize()
        {
            try
            {
                //BASE64加解密方式
                //_sqlHelper = new SQLHelper(
                //    Encoding.Default.GetString(Convert.FromBase64String(
                //    ConfigurationManager.ConnectionStrings["conn"].ConnectionString)));
                //Int32 TimingMailInterval =Convert.ToInt32(ConfigurationManager.AppSettings["TimingMailInterval"]);
                //Int16 MaxThreads = Convert.ToInt16(ConfigurationManager.AppSettings["MaxThreads"]);
                //Int16 MinThreads = Convert.ToInt16(ConfigurationManager.AppSettings["MinThreads"]);
 
                string conn = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
                conn = SecurityHelper.Decrypt(conn);
                _sqlHelper = new SQLHelper(conn);
                Int32 TimingMailInterval = Convert.ToInt32(ConfigurationManager.AppSettings["TimingMailInterval"]);
                Int16 MaxThreads = Convert.ToInt16(ConfigurationManager.AppSettings["MaxThreads"]);
                Int16 MinThreads = Convert.ToInt16(ConfigurationManager.AppSettings["MinThreads"]);
 
                string conText = ConfigurationManager.AppSettings["thisText"];
                this.Text = conText;
                notifyIcon1.Text = conText;
 
                nudminThreads.Value = MinThreads;
                nudmaxThreads.Value = MaxThreads;
                timeInterval.Value = TimingMailInterval;
                timer.Interval = TimingMailInterval;
 
                //一定要先设定最小线程池,再设置最大线程池,否则设置无效
                ThreadPool.SetMinThreads(MinThreads, MinThreads);
                ThreadPool.SetMaxThreads(MaxThreads, MaxThreads);
                
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
                Application.Exit();
            }
        }
        #endregion
 
        #region 轮询执行void timer_Tick(object sender, EventArgs e)
        /// <summary>
        /// 轮询执行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void timer_Tick(object sender, EventArgs e)
        {
            if (btnStop.Enabled)
            {
                AllocationThread();
                int Sleep = ListCount.Count(a => a.state == State.Sleep);
                int Alive = ListCount.Count(a => a.state == State.Alive);
                int Finish = ListCount.Count(a => a.state == State.Finish);
                AddThreadPoolState(string.Format("排队的队列数量:{0},正在执行的队列:{1},已完成的队列:{2}", Sleep, Alive, Finish));
 
                var _list = ListCount.Where(a => a.state == State.Finish).ToList<Allocation>();
                if (_list.Count > 500)
                    ListCount.RemoveAll(a => a.state == State.Finish);
                TimeSpan ts = DateTime.Now - startTime;
                lblTotalTimes.Text = string.Format(@"Total Times : {0}天{1}时{2}分{3}秒", ts.Days, ts.Hours, ts.Minutes, ts.Seconds);
 
            }
        } 
        #endregion
 
        #region 调用线程方法 void AllocationThread()
        /// <summary>
        /// 调用线程方法
        /// </summary>
        public void AllocationThread()
        {
            //long s =(DateTime.Now - preTime).Seconds;
            var _list = ListCount.Where(a => a.state == State.Alive || a.state == State.Sleep).ToList<Allocation>();
 
            if (_list.Count > 0) return;
 
            string msg = string.Empty;
            try
            {
                msg = "query data 1 ;";
                //preTime = DateTime.Now;
                string sqlstr = string.Format(@";with cte as (
                                                        select top 1000
                                                        case when task_name in ('TB_ERPTOMES_BM',
                                                                                'TB_ERPTOMES_GW',
                                                                                'TB_ERPTOMES_RY',
                                                                                'TB_ERPTOMES_WF',
                                                                                'TB_ERPTOMES_WL',
                                                                                'TB_ERPTOMES_KH',
                                                                                'TB_ERPTOMES_GYS',
                                                                                'TB_ERPTOMES_CK',
                                                                                'TB_ERPTOMES_GX',
                                                                                'TB_ERPTOMES_BOM',
                                                                                'TB_ERPTOMES_TDL') then 1
                                                             when task_name in ('TB_ERPTOMES_XCK',
                                                                                'TB_ERPTOMES_QCK',
                                                                                'TB_ERPTOMES_TH',
                                                                                'TB_ERPTOMES_CG',
                                                                                'TB_ERPTOMES_FHTZ',
                                                                                'TB_ERPTOMES_WW',
                                                                                'TB_ERPTOMES_TL',
                                                                                'TB_ERPTOMES_DW',
                                                                                'TB_ERPTOMES_TH_CONFIRM',
                                                                                'TB_ERPTOMES_GDRK',
                                                                                'TB_ERPTOMES_WCK',
                                                                                'TB_ERPTOMES_CGDH',
                                                                                'TB_ERPTOME_KHWL',
                                                                                'TB_ERPTOMES_XSTH',
                                                                                'TB_ERPTOMES_CGTH') then 2
                                                             when task_name in ('TB_MESTOERP_WGRK',
                                                                                'TB_MESTOERP_WWRK',
                                                                                'TB_MESTOERP_QTRK',
                                                                                'TB_MESTOERP_CPRK',
                                                                                'TB_MESTOERP_SLTZ',
                                                                                'TB_MESTOERP_CGRK',
                                                                                'TB_MESTOERP_XSCK',
                                                                                'TB_MESTOERP_DD',
                                                                                'TB_MESTOERP_PYPK',
                                                                                'TB_MESTOERP_LL',
                                                                                'TB_MESTOERP_SCTL',
                                                                                'TB_MESTOERP_WGTL',
                                                                                'TB_MESTOERP_WGTL',
                                                                                'TB_MESTOERP_GDRK',
                                                                                'TB_MESTOERP_ICMO',
                                                                                'TB_MESTOERP_SLSH',
                                                                                'TB_MESTOERP_SCDDXD',
                                                                                'TB_MESTOERP_WLQDBB',
                                                                                'TB_MESTOERP_TransferIn',
                                                                                'TB_MESTOERP_WLQDBBV2',
                                                                                'TB_MESTOERP_WWDDXD',
                                                                                'TB_MESTOERP_WWCG',
                                                                                'TB_MESTOERP_WWLL',
                                                                                'TB_MESTOERP_XSTH',
                                                                                'TB_MESTOERP_CGTL'
                                                                                ) then 3
                                                                else 4 end gp,* 
                                                        from SYSDEC  WITH(nolock) where states='NEW'  AND   descript IN ('用料清单变更','高冲物料变更','物料信息表','仓库信息表','部门信息表','员工信息表','部门信息表','领料单','委外领料单','销售退货单','采购退货单',
                                                                                  '成品入库单','采购入库单','收料通知单','生产订单下达','物料客户对应表','采购订单','发货通知单','销售退货','采购退货','销售出库单','生产领料单','分步调入单','生产退料单','直接调拨单','生产补料单','委外订单','其他入库单','其他出库单','供应商信息表','委外订单下达','委外下推采购')
                                                                --and task_name='TB_ERPTOMES_CG' 
                                                        order by create_time
                                                        union all
                                                          select top 10
                                                       3 gp,* 
                                                        from SYSDEC  WITH(nolock) where task_id IN  ( SELECT task_ID
FROM (
    SELECT task_ID, data, task_name, create_time,
    ROW_NUMBER() OVER (PARTITION BY data, task_name ORDER BY create_time DESC) AS rn
    FROM sysdec where  states='NEW' AND   descript IN ('生产投料单')
) AS SubQuery
WHERE rn = 1)
                                                        union all
                                                        select top 1000
                                                        case when task_name in ('TB_ERPTOMES_BM',
                                                                                'TB_ERPTOMES_GW',
                                                                                'TB_ERPTOMES_RY',
                                                                                'TB_ERPTOMES_WF',
                                                                                'TB_ERPTOMES_WL',
                                                                                'TB_ERPTOMES_KH',
                                                                                'TB_ERPTOMES_GYS',
                                                                                'TB_ERPTOMES_CK',
                                                                                'TB_ERPTOMES_GX',
                                                                                'TB_ERPTOMES_BOM',
                                                                                'TB_ERPTOMES_TDL') then 1
                                                             when task_name in ('TB_ERPTOMES_XCK',
                                                                                'TB_ERPTOMES_QCK',
                                                                                'TB_ERPTOMES_TH',
                                                                                'TB_ERPTOMES_CG',
                                                                                'TB_ERPTOMES_FHTZ',
                                                                                'TB_ERPTOMES_WW',
                                                                                'TB_ERPTOMES_TL',
                                                                                'TB_ERPTOMES_DW',
                                                                                'TB_ERPTOMES_TH_CONFIRM',
                                                                                'TB_ERPTOMES_GDRK',
                                                                                'TB_ERPTOMES_WCK',
                                                                                'TB_ERPTOMES_CGDH',
                                                                                'TB_ERPTOME_KHWL',
                                                                                'TB_ERPTOMES_XSTH',
                                                                                'TB_ERPTOMES_CGTH') then 2
                                                             when task_name in ('TB_MESTOERP_WGRK',
                                                                                'TB_MESTOERP_WWRK',
                                                                                'TB_MESTOERP_QTRK',
                                                                                'TB_MESTOERP_CPRK',
                                                                                'TB_MESTOERP_SLTZ',
                                                                                'TB_MESTOERP_CGRK',
                                                                                'TB_MESTOERP_XSCK',
                                                                                'TB_MESTOERP_DD',
                                                                                'TB_MESTOERP_PYPK',
                                                                                'TB_MESTOERP_LL',
                                                                                'TB_MESTOERP_SCTL',
                                                                                'TB_MESTOERP_GDRK',
                                                                                'TB_MESTOERP_ICMO',
                                                                                'TB_MESTOERP_SLSH',
                                                                                'TB_MESTOERP_SCDDXD',
                                                                                'TB_MESTOERP_WLQDBB',
                                                                                'TB_MESTOERP_WLQDBBV2',
                                                                                'TB_MESTOERP_TransferIn',
                                                                                'TB_MESTOERP_WWDDXD',
                                                                                'TB_MESTOERP_WWCG',
                                                                                'TB_MESTOERP_XSTH',
                                                                                'TB_MESTOERP_CGTL') then 3
                                                                else 4 end gp,* 
                                                        from SYSDEC  WITH(nolock) where states='FAIL' and retry_times<1 and CREATE_TIME>'2024-06-14 13:13:34'  AND descript IN ('用料清单变更','高冲物料变更','物料信息表','仓库信息表','部门信息表','员工信息表','生产投料单','部门信息表','领料单','成品入库单','采购入库单','生产订单下达','物料客户对应表','采购订单','发货通知单','销售出库单','分步调入单','生产领料单','生产退料单','生产补料单','供应商信息表','委外订单下达','委外下推采购')
                                                                            and task_name not in ('TB_MESTOERP_WGRK',
                                                                                                    'TB_MESTOERP_WWRK',
                                                                                                    'TB_MESTOERP_QTRK',
                                                                                                    'TB_MESTOERP_CPRK',
                                                                                                    'TB_MESTOERP_SLTZ',
                                                                                                    'TB_MESTOERP_CGRK',
                                                                                                    'TB_MESTOERP_XSCK',
                                                                                                    'TB_MESTOERP_DD',
                                                                                                    'TB_MESTOERP_PYPK',
                                                                                                    'TB_MESTOERP_LL',
                                                                                                    'TB_MESTOERP_SCTL',
                                                                                                    'TB_MESTOERP_ICMO',
                                                                                                    'TB_MESTOERP_GDRK',
                                                                                                    'TB_MESTOERP_SLSH',
                                                                                                    'TB_MESTOERP_WLQDBB',
                                                                                                    'TB_MESTOERP_WLQDBBV2',
                                                                                                    'TB_MESTOERP_TransferIn',
                                                                                                     'TB_MESTOERP_WWDDXD',
                                                                                                      'TB_MESTOERP_WWCG')
                                                    )
 
                                                    select rtrim(gp)+'-'+rtrim(rank() over(partition by gp order by data)) gcp,* from cte
                                                    order by 1,create_time");
 
 
                DataTable tbThread = _sqlHelper.ExecuteDataTable(sqlstr);
                DataTable tbgroup = tbThread.DefaultView.ToTable(true, new string[1] { "gcp" }); //对线程ID进行分组 
                msg = msg + "begin to allocation thread 2 ; ";
                foreach (DataRow r in tbgroup.Rows)
                {
                    string gcp = r["gcp"].ToString();
                    Allocation EQ = new Allocation();
                    //EQ.task_id = r["task_id"].ToString();
                    //EQ.ID = r["data"].ToString();
                    EQ.strStartTime = DateTime.Now;
                    EQ.Name = gcp;
                    EQ.WriteLog += AddStatusText;
                    EQ.WriteErrorLog += AddErrorText;
                    SetTableByThreadID(gcp, tbThread, ref EQ.tbQueue);
                    EQ.tbQueue.AcceptChanges();
                    ListCount.Add(EQ);
                    ThreadPool.QueueUserWorkItem(EQ.Execute);
                }                
            }
            catch (Exception ex)
            {
                JSONHelper.WriteToFile(msg + " 调用线程时异常:" + ex.Message);
                AddErrorText(msg + " 调用线程时异常:" + ex.Message);
            }
        } 
        #endregion
 
        #region 数据行复制到数据表中void SetTableByThreadID(string tid, DataTable dataTb, ref DataTable target)
        /// <summary>
        /// 数据行复制到数据表中
        /// </summary>
        /// <param name="tid"></param>
        /// <param name="dataTb"></param>
        /// <param name="target"></param>
        private void SetTableByThreadID(string tid, DataTable dataTb, ref DataTable target)
        {
            DataRow[] rowsArray = dataTb.Select("gcp='" + tid + "'", "create_time");
 
            if (rowsArray.Length == 0)
                return;
            target.Rows.Clear();
            foreach (DataRow row in rowsArray)
            {
                DataRow newrow = target.NewRow();
                foreach (DataColumn dc in dataTb.Columns)
                {
                    if ("gcp;gp".Contains(dc.ColumnName))
                        continue;
                    newrow[dc.ColumnName] = row[dc.ColumnName];
                }
                target.Rows.Add(newrow);
            }
        } 
        #endregion
 
        #region 启动服务void btnStart_Click(object sender, EventArgs e)
        /// <summary>
        /// 启动服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStart_Click(object sender, EventArgs e)
        {
            ApiGlobal.Init();
            AllocationThread();
            btnStop.Enabled = true;
            btnStart.Enabled = false;
            timer.Enabled = false;
            timer.Start();
            startTime = DateTime.Now;
        } 
        #endregion
 
        #region 停止服务void btnStop_Click(object sender, EventArgs e)
        /// <summary>
        /// 停止服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStop_Click(object sender, EventArgs e)
        {
            btnStop.Enabled = false;
            btnStart.Enabled = true;
            timer.Stop();
        } 
        #endregion
 
        #region 增加线程池信息void AddThreadPoolState(string message)
        /// <summary>
        /// 增加线程池信息
        /// </summary>
        /// <param name="message"></param>
        private void AddThreadPoolState(string message)
        {
            if (lsbPoolState.InvokeRequired)
            {
                AppendTextCallBack method = AddThreadPoolState;
                base.Invoke(method, new object[] { message });
            }
            else
            {
                if (lsbPoolState.Items.Count > 10)
                {
                    lsbPoolState.Items.Clear();
                }
                //lsbPoolState.Items.Add(string.Format(message));
 
                lsbPoolState.Items.Insert(0, string.Format(message));
                lsbPoolState.Width = 350;
                lsbPoolState.ItemHeight = 10;
            }
        }
        #endregion
 
        #region 增加线程信息void AddStatusText(string message)
        /// <summary>
        /// 增加线程信息
        /// </summary>
        /// <param name="message"></param>
        private void AddStatusText(string message)
        {
            if (rtbStatus.InvokeRequired)
            {
                AppendTextCallBack method = AddStatusText;
                base.Invoke(method, new object[] { message });
            }
            else
            {
                if (rtbStatus.Items.Count > 30)
                {
                    rtbStatus.Items.Clear();
                }
                //rtbStatus.Items.Add(string.Format(message));
                rtbStatus.Items.Insert(0, string.Format(message));
                rtbStatus.Width = 600;
                rtbStatus.ItemHeight = 100;
            }
        } 
        #endregion
 
        #region 增加错误信息void AddErrorText(string message)
        /// <summary>
        /// 增加错误信息
        /// </summary>
        /// <param name="message"></param>
        private void AddErrorText(string message)
        {
            if (lsbErrorMsg.InvokeRequired)
            {
                AppendTextCallBack method = AddErrorText;
                base.Invoke(method, new object[] { message });
            }
            else
            {
                if (lsbErrorMsg.Items.Count > 6)
                {
                    lsbErrorMsg.Items.Clear();
                }
                //lsbErrorMsg.Items.Add(string.Format(message));
                lsbErrorMsg.Items.Insert(0, message);//string.Format(message)
                lsbErrorMsg.Width = 600;
                lsbErrorMsg.ItemHeight = 30;
            }
        } 
        #endregion
 
        #region        托盘程序
        void frmMain_SizeChanged(object sender, EventArgs e)
        {
            if(WindowState == FormWindowState.Minimized)
            {
                this.ShowInTaskbar = false;
                notifyIcon1.Visible = true;
            }
        }
 
        void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            if(WindowState == FormWindowState.Minimized)
            {
                WindowState = FormWindowState.Normal;
                this.Activate();
                this.ShowInTaskbar = true;
                notifyIcon1.Visible = false;
            }
        }
 
        void contextMenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if(e.ClickedItem == showInTaskbar)
            {
                if (WindowState == FormWindowState.Minimized)
                {
                    WindowState = FormWindowState.Normal;
                    this.Activate();
                    this.ShowInTaskbar = true;
                    notifyIcon1.Visible = false;
                }
            }
            else if (e.ClickedItem == exitWindow)
            {
                btnStop_Click(null, null);
                System.Environment.Exit(0);   //退出所有的线程
            }
        }
        #endregion
    }
}