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 _selectedBlyIds = new List(); // 存储选中的备料员ID public Frm_MesDbck_Print_Select(string _rowGuid) { rowGuid = _rowGuid; InitializeComponent(); //getBly(); //// 绑定选择事件 //this.checkedListBoxControl1.ItemCheck += CheckedListBoxControl1_ItemCheck; } ///// ///// 选项勾选/取消事件 ///// //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); // } // } // } // } //} ///// ///// 读取备料员 ///// //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(); // 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); // } //} ///// ///// 调试:打印已加载的项目信息 ///// //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()); //} ///// ///// 获取所有选中的备料员ID ///// //private List GetSelectedBlyIds() //{ // var selectedIds = new List(); // // 方法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; //} ///// ///// 获取所有选中的备料员名称 ///// //private List GetSelectedBlyNames() //{ // var selectedNames = new List(); // foreach (var itemObj in this.checkedListBoxControl1.CheckedItems) // { // if (itemObj is CheckedListBoxItem item && item.Description != null) // { // selectedNames.Add(item.Description.ToString()); // } // } // return selectedNames; //} /// /// 关闭 /// private void btnCancel_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.Cancel; // 先设置DialogResult this.Close(); } /// /// 直接打印 /// 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"); } } /// /// 预览打印 /// /// /// 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"); } } /// /// 调用打印接口 /// /// 备料员ID /// 备料员名称(用于日志)> /// 1直接打印,0预览打印 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(array.ToString()); _zb.TableName = "zb"; // 明细表1 var array1 = new JArray(); foreach (var a in _job["rtnData"]["mx1"]) array1.Add(a); var _mx1 = JsonConvert.DeserializeObject(array1.ToString()); _mx1.TableName = "mx1"; // 明细表2 var array2 = new JArray(); foreach (var a in _job["rtnData"]["mx2"]) array2.Add(a); var _mx2 = JsonConvert.DeserializeObject(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}"); } } /// /// 记录打印操作日志 /// 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); } } }