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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
using DevExpress.Utils.DirectXPaint;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Columns;
using Gs.DevApp.DevFrm.QC;
using Gs.DevApp.DevFrm.Rpt;
using Gs.DevApp.Entity;
using Gs.DevApp.ToolBox;
using Gs.DevApp.UserControl;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace Gs.DevApp.DevFrm.JJGZ
{
    public partial class Frm_MesAttanCon : DevExpress.XtraEditors.XtraForm
    {
        string _webServiceName = "MesAttanCon/";
        List<FilterEntity> _filterList = new List<FilterEntity>();
        string date;
        string lineNo;
        
        // 用于跟踪原始值的字典
        private Dictionary<string, Dictionary<string, object>> _originalValues = new Dictionary<string, Dictionary<string, object>>();
 
        public Frm_MesAttanCon()
        {
            InitializeComponent();
            //this.toolBarMenu1.btnAddClick += ToolBarMenu1_btnAddClick;
            this.toolBarMenu1.btnEdtClick += ToolBarMenu1_btnEdtClick;
            this.toolBarMenu1.btnSaveClick += ToolBarMenu1_btnSaveClick;
            this.toolBarMenu1.btnLoadClick += ToolBarMenu1_btnLoadClick;
            //this.toolBarMenu1.btnDelClick += ToolBarMenu1_btnDelClick1;
            this.toolBarMenu1.btnEscClick += ToolBarMenu1_btnEscClick;
            this.toolBarMenu1.btnQueryClick += ToolBarMenu1_btnQueryClick;
            this.toolBarMenu1.btnGongZiClick += ToolBarMenu1_btnGongZiClick;
            this.toolBarMenu1.btnLianBanClick += ToolBarMenu1_btnLianBanClick;
            toolBarMenu1.btnChkClick += ToolBarMenu1_btnChkClick;
            toolBarMenu1.btnFChkClick += ToolBarMenu1_btnFChkClick;
            this.toolBarMenu1.getXmlConfig();
            Gs.DevApp.ToolBox.UtilityHelper.SetGridViewParameter(gridView1, picCheckBox, this, "checkStatus", "", (value) =>
            {
                Gs.DevApp.ToolBox.UtilityHelper.JumpTab(xtraTabControl1, 0);
            }, tips);
            //getPageList(1);
            //_setIno();
 
        }
 
        /// <summary>
        ///     查询事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ToolBarMenu1_btnQueryClick(object sender, EventArgs e)
        {
            var frm = new KQCX(lineNo, date);
            //赋值给明细表
            frm.UpdateParent += (ss, ee) =>
            {
                var lst = new List<string>();
                lst = ee.StringList;
                lineNo = string.IsNullOrEmpty(lst[0]) ? "0" : lst[0];
                date = string.IsNullOrEmpty(lst[1]) ? DateTime.Now.AddMonths(-1).ToString() : lst[1];
                getPageList();
            };
            frm.ShowDialog();
        }
 
        /// <summary>
        ///     刷新事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ToolBarMenu1_btnLoadClick(object sender, EventArgs e)
        {
 
            //getPageList(1);
            getPageList();
 
        }
 
        /// <summary>
        /// 连班设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ToolBarMenu1_btnLianBanClick(object sender, EventArgs e)
        {
 
            var frm = new SetLB();
            //赋值给明细表
            frm.UpdateParent += (ss, ee) =>
            {
                var lst = new List<string>();
                lst = ee.StringList;
                var time = string.IsNullOrEmpty(lst[0]) ? "0" : lst[0];
                SetLB(time);
            };
            frm.ShowDialog();
 
        }
 
        /// <summary>
        /// </summary>
        /// <param name="curPage">第几页</param>
        /// <param name="pageSize">每页几条</param>
        private void getPageList()
        {
            var _obj = new
            {
                date = date,
                lineNo = lineNo
            };
           // gcMain1.DataSource = null; 
            var _sbSqlWhere = UtilityHelper.GetSearchWhere(_filterList);
 
            try
            {
                var strReturn = UtilityHelper.HttpPost("",
                    _webServiceName + "GetListPage", JsonConvert.SerializeObject(_obj));
                ReturnModel<dynamic> _rtn = ToolBox.UtilityHelper.ReturnToDynamic(strReturn);
                if (_rtn.rtnCode > 0)
                {
                    dynamic dy = _rtn.rtnData;
                    JObject _job = JObject.Parse(strReturn);
 
 
                    JArray array = new JArray();
                    foreach (var a in _job["rtnData"]["list"])
                    {
                        array.Add(a);
                    }
                    DataTable dt = JsonConvert.DeserializeObject<DataTable>(array.ToString());
                    if (dt.Rows.Count > 0)
                    {
                        gridControl1.BindingContext = new BindingContext();
                        gridControl1.DataSource = dt;
                        gridControl1.ForceInitialize();
                        gridView1.BestFitColumns();
                        Gs.DevApp.ToolBox.UtilityHelper.SetGridLayout(gridView1);
                    }
                    else
                    {
                        Gs.DevApp.ToolBox.UtilityHelper.SetDefaultTable(gridControl1, gridView1);
                    }
 
                    JArray array1 = new JArray();
                    foreach (var a in _job["rtnData"]["list1"])
                    {
                        array1.Add(a);
                    }
                    DataTable dt1 = JsonConvert.DeserializeObject<DataTable>(array1.ToString());
                    if (dt1.Rows.Count > 0)
                    {
                        gridControl2.BindingContext = new BindingContext();
                        gridControl2.DataSource = dt1;
                        gridControl2.ForceInitialize();
                        gridView2.BestFitColumns();
                        Gs.DevApp.ToolBox.UtilityHelper.SetGridLayout(gridView2);
                        // 保存原始值
                        SaveOriginalValues();
                    }
                    else
                    {
                        Gs.DevApp.ToolBox.UtilityHelper.SetDefaultTable(gridControl2, gridView2);
                        _originalValues.Clear();
                    }
 
                    JArray array2 = new JArray();
                    foreach (var a in _job["rtnData"]["list2"])
                    {
                        array2.Add(a);
                    }
                    DataTable dt2 = JsonConvert.DeserializeObject<DataTable>(array2.ToString());
                    if (dt2.Rows.Count > 0)
                    {
                        gridControl3.BindingContext = new BindingContext();
                        gridControl3.DataSource = dt2;
                        gridControl3.ForceInitialize();
                        gridView3.BestFitColumns();
                        Gs.DevApp.ToolBox.UtilityHelper.SetGridLayout(gridView3);
                    }
                    else
                    {
                        Gs.DevApp.ToolBox.UtilityHelper.SetDefaultTable(gridControl3, gridView3);
                    }
 
                    JArray array3 = new JArray();
                    foreach (var a in _job["rtnData"]["list3"])
                    {
                        array3.Add(a);
                    }
                    DataTable dt3 = JsonConvert.DeserializeObject<DataTable>(array3.ToString());
                    if (dt3.Rows.Count > 0)
                    {
                        gridControl4.BindingContext = new BindingContext();
                        gridControl4.DataSource = dt3;
                        gridControl4.ForceInitialize();
                        gridView4.BestFitColumns();
                        Gs.DevApp.ToolBox.UtilityHelper.SetGridLayout(gridView4);
                    }
                    else
                    {
                        Gs.DevApp.ToolBox.UtilityHelper.SetDefaultTable(gridControl4, gridView4);
                    }
                }
                else
                    ToolBox.MsgHelper.Warning("提示:" + _rtn.rtnMsg);
            }
            catch (Exception ex)
            {
                MsgHelper.Warning("提示:" + ex.Message);
            }
        }
 
        /// <summary>
        /// 取消事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ToolBarMenu1_btnEscClick(object sender, EventArgs e)
        {
           // Gs.DevApp.ToolBox.UtilityHelper.JumpTab(xtraTabControl1, 1);
        }
 
        /// <summary>
        ///     计算工资
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ToolBarMenu1_btnGongZiClick(object sender, EventArgs e)
        {
 
            var _obj = new
            {
                date = date,
                lineNo = lineNo
            };
            try
            {
                string strJson = UtilityHelper.HttpPost("", _webServiceName + "CountModel", JsonConvert.SerializeObject(_obj));
                ReturnModel<dynamic> _rtn = ToolBox.UtilityHelper.ReturnToDynamic(strJson);
 
                if (_rtn.rtnCode > 0)
                {
                    ToolBox.MsgHelper.ShowInformation("提示:" + _rtn.rtnMsg);
                    getPageList();
                }
                else
                    ToolBox.MsgHelper.ShowError("提示:" + _rtn.rtnMsg);
            }
            catch (Exception ex)
            {
                ToolBox.MsgHelper.ShowError("提示:" + ex.Message);
            }
        }
 
        /// <summary>
        ///     设置连班
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SetLB(string time)
        {
            // 验证参数
            if (string.IsNullOrEmpty(time) || !decimal.TryParse(time, out decimal addTime))
            {
                ToolBox.MsgHelper.ShowError("连班时长格式不正确!");
                return;
            }
 
            // 获取当前 gridControl2 的数据
            DataTable dt = gridControl2.DataSource as DataTable;
            if (dt == null || dt.Rows.Count == 0)
            {
                ToolBox.MsgHelper.ShowError("没有可设置连班的数据!");
                return;
            }
 
            var list = new List<object>();
            
            // 遍历所有行,设置连班
            foreach (DataRow row in dt.Rows)
            {
                if (row["GUID"] != null)
                {
                    string guid = row["GUID"].ToString();
                    
                    // 获取原始的 Hour 值并加上连班时长
                    decimal originalHour = 0;
                    if (row["Hour"] != null && decimal.TryParse(row["Hour"].ToString(), out originalHour))
                    {
                        decimal newHour = originalHour + addTime;
                        
                        list.Add(new
                        {
                            GUID = guid,
                            Hour = newHour,
                            Reason = "连班"
                        });
                    }
                }
            }
 
            if (list.Count == 0)
            {
                ToolBox.MsgHelper.ShowError("没有有效的数据可以设置连班!");
                return;
            }
 
            var postObj = new
            {
                lineNo = lineNo,
                date = date,
                list = list
            };
 
            try
            {
                string strJson = UtilityHelper.HttpPost("", _webServiceName + "EditModel", JsonConvert.SerializeObject(postObj));
                ReturnModel<dynamic> _rtn = ToolBox.UtilityHelper.ReturnToDynamic(strJson);
 
                if (_rtn.rtnCode > 0)
                {
                    ToolBox.MsgHelper.ShowInformation($"连班设置成功!共设置 {list.Count} 条记录,每条增加 {addTime} 小时。");
                    getPageList();
                    
                }
                else
                    ToolBox.MsgHelper.ShowError("连班设置失败:" + _rtn.rtnMsg);
            }
            catch (Exception ex)
            {
                ToolBox.MsgHelper.ShowError("连班设置异常:" + ex.Message);
            }
        }
 
        /// <summary>
        /// 保存原始值
        /// </summary>
        private void SaveOriginalValues()
        {
            _originalValues.Clear();
            DataTable dt = gridControl2.DataSource as DataTable;
            if (dt == null) return;
 
            foreach (DataRow row in dt.Rows)
            {
                if (row["GUID"] != null)
                {
                    string guid = row["GUID"].ToString();
                    _originalValues[guid] = new Dictionary<string, object>
                    {
                        ["Hour"] = row["Hour"]
                    };
                }
            }
        }
 
        /// <summary>
        ///     修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ToolBarMenu1_btnEdtClick(object sender, EventArgs e)
        {
            // 其它需要的UI状态切换
            //Gs.DevApp.ToolBox.UtilityHelper.JumpTab(xtraTabControl1, 3);
        }
 
        /// <summary>
        ///     保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
 
        private void ToolBarMenu1_btnSaveClick(object sender, EventArgs e)
        {
            // 结束编辑,确保数据已提交到DataTable
            gridView2.CloseEditor();
            gridView2.UpdateCurrentRow();
            gridView2.PostEditor();
            BindingContext[gridControl2.DataSource].EndCurrentEdit();
 
            DataTable dt = gridControl2.DataSource as DataTable;
            if (dt == null) return;
 
            var list = new List<object>();
            foreach (DataRow row in dt.Rows)
            {
                if (row["GUID"] != null)
                {
                    string guid = row["GUID"].ToString();
                    
                    // 检查是否有原始值记录
                    if (_originalValues.ContainsKey(guid))
                    {
                        var originalRow = _originalValues[guid];
                        object currentHour = row["Hour"];
                        object originalHour = originalRow["Hour"];
 
                        // 只比较 Hour 字段的当前值与原始值
                        bool hourChanged = !Equals(currentHour, originalHour);
 
                        if (hourChanged)
                        {
                            list.Add(new
                            {
                                GUID = guid,
                                Hour = currentHour,
                                Reason = row["Reason"]
                            });
                        }
                    }
                }
            }
 
            if (list.Count == 0)
            {
                ToolBox.MsgHelper.ShowInformation("没有需要保存的更改。");
                return;
            }
 
            var postObj = new
            {
                lineNo = lineNo,
                date = date,
                list = list
            };
 
            try
            {
                string strJson = UtilityHelper.HttpPost("", _webServiceName + "EditModel", JsonConvert.SerializeObject(postObj));
                ReturnModel<dynamic> _rtn = ToolBox.UtilityHelper.ReturnToDynamic(strJson);
                if (_rtn.rtnCode > 0)
                {
                    ToolBox.MsgHelper.ShowInformation("保存成功!");
                    getPageList();
                    toolBarMenu1.isSetBtn = true;
                }
                else
                {
                    ToolBox.MsgHelper.ShowError("保存失败:" + _rtn.rtnMsg);
                }
            }
            catch (Exception ex)
            {
                ToolBox.MsgHelper.ShowError("异常:" + ex.Message);
            }
        }
 
        /// <summary>
        ///     审核事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ToolBarMenu1_btnChkClick(object sender, EventArgs e)
        {
            _toolCk(1);
        }
 
        /// <summary>
        ///     反审核
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void ToolBarMenu1_btnFChkClick(object sender, EventArgs e)
        {
            _toolCk(0);
        }
 
        private void _toolCk(int _inFieldValue)
        {
            string strMsg = "";
            switch (_inFieldValue)
            {
                case 1:
                    strMsg = "审核";
                    break;
                case 0:
                    strMsg = "反审核";
                    break;
            }
    ;
            toolBarMenu1.guidKey = "";
            var _obj = new
            {
                lineNo = lineNo,
                attenDate = date,
                inFieldValue = _inFieldValue
            };
            try
            {
                var strJson = UtilityHelper.HttpPost("", _webServiceName + "EditModelSubmit",
                    JsonConvert.SerializeObject(_obj));
                var _rtn = UtilityHelper.ReturnToDynamic(strJson);
                if (_rtn.rtnCode > 0)
                {
                    MsgHelper.ShowInformation(_rtn.rtnData.outMsg.ToString());
                    UtilityHelper.SetCheckIco(gridView1, "checkStatus", "checkBy", "checkDate", picCheckBox, this, _inFieldValue.ToString());
                }
                else
                    MsgHelper.ShowError(_rtn.rtnData.outMsg.ToString());
            }
            catch (Exception ex)
            {
                MsgHelper.ShowError("提示:" + ex.Message);
            }
        }
    }
}