huawei
2025-12-04 0bd6795c9962fcbc3b332a4fe0f42276bd484c4f
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
using DevExpress.Utils.DirectXPaint;
using DevExpress.Utils.VisualEffects;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Views.Base.ViewInfo;
using FastReport;
using Gs.DevApp.DevFrm.Rpt;
using Gs.DevApp.Entity;
using Gs.DevApp.ToolBox;
using Newtonsoft.Json;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlTypes;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace Gs.DevApp.DevFrm.WOM
{
    public partial class Frm_MesDbck_Print_Select : DevExpress.XtraEditors.XtraForm
    {
        public string rowGuid = "";
        private List<string> _selectedBlyIds = new List<string>(); // 存储选中的备料员ID
 
        public Frm_MesDbck_Print_Select(string _rowGuid)
        {
            rowGuid = _rowGuid;
            InitializeComponent();
            //getBly();
 
            //// 绑定选择事件
            //this.checkedListBoxControl1.ItemCheck += CheckedListBoxControl1_ItemCheck;
        }
 
        ///// <summary>
        ///// 选项勾选/取消事件
        ///// </summary>
        //private void CheckedListBoxControl1_ItemCheck(object sender, DevExpress.XtraEditors.Controls.ItemCheckEventArgs e)
        //{
        //    if (e.Index >= 0 && e.Index < checkedListBoxControl1.Items.Count)
        //    {
        //        var item = checkedListBoxControl1.Items[e.Index] as CheckedListBoxItem;
        //        if (item != null)
        //        {
        //            string blyId = item.Value?.ToString();
 
        //            if (e.State == CheckState.Checked)
        //            {
        //                // 添加选中的ID
        //                if (!string.IsNullOrEmpty(blyId) && !_selectedBlyIds.Contains(blyId))
        //                {
        //                    _selectedBlyIds.Add(blyId);
        //                }
        //            }
        //            else if (e.State == CheckState.Unchecked)
        //            {
        //                // 移除取消的ID
        //                if (!string.IsNullOrEmpty(blyId))
        //                {
        //                    _selectedBlyIds.Remove(blyId);
        //                }
        //            }
        //        }
        //    }
        //}
 
        ///// <summary>
        ///// 读取备料员
        ///// </summary>
        //private void getBly()
        //{
        //    this.checkedListBoxControl1.Items.Clear();
        //    _selectedBlyIds.Clear(); // 清空选中的ID列表
 
        //    //string userGuid = LoginInfoModel.CurrentUser.LoginUserGuid;
        //    //var pgq = new PageQueryModel(1, 999999, "FID", "asc", userGuid, " and IS_STATUS=1");
        //    //var json = JsonConvert.SerializeObject(pgq);
 
        //    var _obj = new
        //    {
        //        currentPage = 1,
        //        everyPageSize = 999999,
        //        guid = this.rowGuid,//主建
        //        sortName = "",
        //        keyWhere = ""
        //    };
 
        //    try
        //    {
        //        var strReturn = UtilityHelper.HttpPost("", "WomdaaManager/Getbly", JsonConvert.SerializeObject(_obj));
        //        var _rtn = UtilityHelper.ReturnToTablePage(strReturn);
        //        DataTable tb = _rtn.rtnData.list;
 
        //        // 方法1:使用构造函数直接设置Description和Value
        //        foreach (DataRow row in tb.Rows)
        //        {
        //            string blyId = row["blyId"]?.ToString(); // 假设ID字段名为blyId
        //            string blyName = row["blyName"]?.ToString();
 
        //            if (!string.IsNullOrEmpty(blyId) && !string.IsNullOrEmpty(blyName))
        //            {
        //                // 创建CheckedListBoxItem,将名称存储在Description,ID存储在Value
        //                var item = new CheckedListBoxItem(blyName, blyId)
        //                {
        //                    Description = blyName,  // 显示名称
        //                    Value = blyId           // 存储ID
        //                };
 
        //                this.checkedListBoxControl1.Items.Add(item);
        //            }
        //        }
 
        //        // 方法2:或者使用AddRange的方式(如果数据量较大)
        //        /*
        //        var items = new List<CheckedListBoxItem>();
        //        foreach (DataRow row in tb.Rows)
        //        {
        //            string blyId = row["blyId"]?.ToString();
        //            string blyName = row["blyName"]?.ToString();
                    
        //            if (!string.IsNullOrEmpty(blyId) && !string.IsNullOrEmpty(blyName))
        //            {
        //                var item = new CheckedListBoxItem(blyName, blyId);
        //                items.Add(item);
        //            }
        //        }
        //        this.checkedListBoxControl1.Items.AddRange(items.ToArray());
        //        */
 
        //        // 可选:设置显示格式,使显示更清晰
        //        this.checkedListBoxControl1.DisplayMember = "Description"; // 显示Description属性
        //        this.checkedListBoxControl1.ValueMember = "Value";         // 值使用Value属性
 
        //        // 可选:打印调试信息
        //        DebugPrintLoadedItems();
        //    }
        //    catch (Exception ex)
        //    {
        //        MsgHelper.Warning("提示:" + ex.Message);
        //    }
        //}
 
        ///// <summary>
        ///// 调试:打印已加载的项目信息
        ///// </summary>
        //private void DebugPrintLoadedItems()
        //{
        //    StringBuilder sb = new StringBuilder();
        //    sb.AppendLine("已加载的备料员列表:");
 
        //    foreach (var itemObj in this.checkedListBoxControl1.Items)
        //    {
        //        if (itemObj is CheckedListBoxItem item)
        //        {
        //            sb.AppendLine($"名称:{item.Description}, ID:{item.Value}, 选中状态:{item.CheckState}");
        //        }
        //    }
 
        //    // 可以写入日志或显示在调试窗口
        //    // Console.WriteLine(sb.ToString());
        //}
 
        ///// <summary>
        ///// 获取所有选中的备料员ID
        ///// </summary>
        //private List<string> GetSelectedBlyIds()
        //{
        //    var selectedIds = new List<string>();
 
        //    // 方法1:使用存储的_selectedBlyIds列表
        //    // return _selectedBlyIds.ToList();
 
        //    // 方法2:直接从控件获取
        //    foreach (var itemObj in this.checkedListBoxControl1.CheckedItems)
        //    {
        //        if (itemObj is CheckedListBoxItem item && item.Value != null)
        //        {
        //            selectedIds.Add(item.Value.ToString());
        //        }
        //    }
 
        //    return selectedIds;
        //}
 
        ///// <summary>
        ///// 获取所有选中的备料员名称
        ///// </summary>
        //private List<string> GetSelectedBlyNames()
        //{
        //    var selectedNames = new List<string>();
 
        //    foreach (var itemObj in this.checkedListBoxControl1.CheckedItems)
        //    {
        //        if (itemObj is CheckedListBoxItem item && item.Description != null)
        //        {
        //            selectedNames.Add(item.Description.ToString());
        //        }
        //    }
 
        //    return selectedNames;
        //}
 
        /// <summary>
        /// 关闭
        /// </summary>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;  // 先设置DialogResult
            this.Close();
        }
 
        /// <summary>
        /// 直接打印
        /// </summary>
        private void btnSave_Click(object sender, EventArgs e)
        {
            int dylx = radioGroup1.SelectedIndex; // 打印类型
 
            if (dylx == 0) // 逐个备料员打印
            {
 
                printDj("", 1, "0");
 
                //// 获取选中的备料员ID列表
                //var selectedBlyIds = GetSelectedBlyIds();
 
                //// 提示1:检查是否选择了备料员
                //if (selectedBlyIds.Count == 0)
                //{
                //    MsgHelper.ShowInformation("请至少选择一个备料员!");
                //    return;
                //}
 
                //// 获取选中的备料员名称(用于显示)
                //var selectedBlyNames = GetSelectedBlyNames();
 
                //// 提示2:确认对话框
                //if (!MsgHelper.AskQuestion($"您选择了 {selectedBlyNames.Count} 个备料员:\n{string.Join(", ", selectedBlyNames)}\n\n确定要打印吗?"))
                //    return;
 
                //// 逐个打印
                //for (int i = 0; i < selectedBlyIds.Count; i++)
                //{
                //    string blyId = selectedBlyIds[i];
                //    string blyName = selectedBlyNames[i];
 
                //    // 打印,传递备料员ID
                //    printDj(blyId, 1,blyName);
 
                //    Thread.Sleep(50); // 防止打印过快
                //}
            }
            else if(dylx == -1)
            {
                MsgHelper.ShowInformation("请至少选择一种打印方式!");
                return;
            }
            else // 批量打印(不指定具体备料员)
            {
                // 批量打印,传递空ID
                printDj("",1, "1");
            }
        }
 
        /// <summary>
        /// 预览打印
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            int dylx = radioGroup1.SelectedIndex; // 打印类型
 
            if (dylx == 0) // 逐个备料员打印
            {
                printDj("", 0, "0");
 
                //// 获取选中的备料员ID列表
                //var selectedBlyIds = GetSelectedBlyIds();
 
                //// 提示1:检查是否选择了备料员
                //if (selectedBlyIds.Count == 0)
                //{
                //    MsgHelper.ShowInformation("请至少选择一个备料员!");
                //    return;
                //}
 
                //// 获取选中的备料员名称(用于显示)
                //var selectedBlyNames = GetSelectedBlyNames();
 
                //// 提示2:确认对话框
                //if (!MsgHelper.AskQuestion($"您选择了 {selectedBlyNames.Count} 个备料员:\n{string.Join(", ", selectedBlyNames)}\n\n确定要打印吗?"))
                //    return;
 
                //// 逐个打印
                //for (int i = 0; i < selectedBlyIds.Count; i++)
                //{
                //    string blyId = selectedBlyIds[i];
                //    string blyName = selectedBlyNames[i];
 
                //    // 打印,传递备料员ID
                //    printDj(blyId, 0, "0");
 
                //    Thread.Sleep(50); // 防止打印过快
                //}
            }
            else if (dylx == -1)
            {
                MsgHelper.ShowInformation("请至少选择一种打印方式!");
                return;
            }
            else // 批量打印(不指定具体备料员)
            {
                // 批量打印,传递空ID
                printDj("", 0,"1");
            }
        }
 
 
 
        /// <summary>
        /// 调用打印接口
        /// </summary>
        /// <param name="blyId">备料员ID</param>
        /// <param name="blyName">备料员名称(用于日志)></param>
        /// <param name="printType">1直接打印,0预览打印</param>
        private void printDj(string blyId, int printType, string blyName = "")
        {
            try
            {
                // 构建打印参数,传递备料员ID
                string rptParameter = "";
 
                if(blyName=="0")
                {
                    rptParameter = "rpt_sdzjdb{"
                                               + "100"
                                               + "," + ""
                                               + "," + ""
                                               + "," + ""
                                               + "," + ""
                                               + "}";
                }
                else
                {
                    rptParameter = "rpt_dbylqd{"
                                               + "100"
                                               + "," + ""
                                               + "," + ""
                                               + "," + ""
                                               + "," + ""
                                               + "}";
                }
 
                    var _obj = new
                    {
                        rptParameter = rptParameter,
                        guid = this.rowGuid,
                        isDesign = 0
                    };
 
                var strJson = UtilityHelper.HttpPost("", "Report/GetRptData",
                    JsonConvert.SerializeObject(_obj));
                var _job = JObject.Parse(strJson);
                var rtnCode = _job["rtnCode"].ToString();
 
                if (int.Parse(rtnCode) > 0)
                {
                    // 模板
                    var report = new FastReport.Report();
                    EnvironmentSettings eSet = new EnvironmentSettings();
                    eSet.ReportSettings.ShowProgress = false;
                    report = new FastReport.Report();
                    var buffer = (byte[])_job["rtnData"]["reportTemplateData"];
                    using (var Stream = new MemoryStream(buffer))
                    {
                        report.Load(Stream);
                    }
                    // 主表
                    var array = new JArray();
                    foreach (var a in _job["rtnData"]["zb"]) array.Add(a);
                    var _zb = JsonConvert.DeserializeObject<DataTable>(array.ToString());
                    _zb.TableName = "zb";
                    // 明细表1
                    var array1 = new JArray();
                    foreach (var a in _job["rtnData"]["mx1"]) array1.Add(a);
                    var _mx1 = JsonConvert.DeserializeObject<DataTable>(array1.ToString());
                    _mx1.TableName = "mx1";
                    // 明细表2
                    var array2 = new JArray();
                    foreach (var a in _job["rtnData"]["mx2"]) array2.Add(a);
                    var _mx2 = JsonConvert.DeserializeObject<DataTable>(array2.ToString());
                    _mx2.TableName = "mx2";
                    // 加到数据集中
                    var ds = new DataSet();
                    ds.Tables.Add(_zb);
                    ds.Tables.Add(_mx1);
                    ds.Tables.Add(_mx2);
                    // 可选:添加备料员信息到报表数据源
                    if (!string.IsNullOrEmpty(blyName))
                    {
                        // 可以在报表数据源中添加备料员信息
                        DataTable blyTable = new DataTable("BlyInfo");
                        blyTable.Columns.Add("BlyId", typeof(string));
                        blyTable.Columns.Add("BlyName", typeof(string));
                        blyTable.Rows.Add(blyId, blyName);
                        ds.Tables.Add(blyTable);
                    }
 
                    report.RegisterData(ds);
                    report.PrintSettings.ShowDialog = false;
                    if (printType == 1)
                        report.Print();
                    else
                    {
                        try
                        {
                            using (Form frm = new RptPreview(this.rowGuid, rptParameter))
                            {
                                frm.ShowDialog();
                            }
                        }
                        catch (Exception)
                        {
                        }
                    }
                    report.Dispose();
                    // 记录打印日志
                    LogPrintOperation(blyId, blyName, true, "打印成功");
                }
                else
                {
                    string errorMsg = _job["rtnMsg"]?.ToString() ?? "打印失败";
                    LogPrintOperation(blyId, blyName, false, errorMsg);
                    MsgHelper.ShowError($"打印失败:{errorMsg}");
                }
            }
            catch (Exception ex)
            {
                LogPrintOperation(blyId, blyName, false, ex.Message);
                MsgHelper.ShowError($"打印异常:{ex.Message}");
            }
        }
 
        /// <summary>
        /// 记录打印操作日志
        /// </summary>
        private void LogPrintOperation(string blyId, string blyName, bool isSuccess, string message)
        {
            // 这里可以记录打印日志到数据库或文件
            string logMessage = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - " +
                               $"单据GUID: {rowGuid}, " +
                               $"备料员ID: {blyId}, " +
                               $"备料员名称: {blyName}, " +
                               $"结果: {(isSuccess ? "成功" : "失败")}, " +
                               $"消息: {message}";
 
            // 写入日志文件或数据库
            // File.AppendAllText("PrintLog.txt", logMessage + Environment.NewLine);
 
            // 或者输出到调试窗口
            Console.WriteLine(logMessage);
        }
 
      
    }
}