using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; using DevExpress.XtraEditors; using Lskj.Control; using Lskj.Control.Model; using Lskj.Data; using FastReport.Preview; using Lskj.Business.Impl; using Lskj.Util; using System.IO; using Lskj.Model; using FastReport.Data; using FastReport; using Lskj.Business; using System.Text.RegularExpressions; using System.Diagnostics; using Lskj.Core; using System.Net; using FastReport.DevComponents.DotNetBar; namespace Lskj.FastReportDesign { public partial class FormMain : BaseForm { public Report ReportObj; public PreviewControl PreviewControlObj { get { return this.previewControl1; } } public FormMain() { InitializeComponent(); } #region 公有参数 /// /// 调用动态链接库默认传递参数对象 /// public DynamicFastReport Model { get; set; } public List sqls; public string printFile; public string tmpSql; public string tmpSql1; public string tmpSql2; public bool isPrint; public BaseForm form; #endregion #region 私有参数 /// /// 是否直接打印 /// private bool _isExcPrint; /// /// 打印完成后添加数据用到的参数 /// private List _printSaveParams; /// /// 主打印Sql /// private string _mainPrintSql; /// /// 打印主sql中关联打印次数返回条件 /// private string _printModeCond; #endregion /// /// 说明:窗体加载时 /// 创建人:龚宇超 /// 创建日期:2017-09-01 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The instance containing the event data. protected override void OnLoad(EventArgs e) { try { InitializeSetting(); GetPrintOtherParam(); InitializaControl(); } catch (Exception ex) { string Message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show(Message, ex.Message); } finally { Lskj.Control.Model.AutoSizeChange.ControllInitializeSize(this); } } /// /// 说明:打印完成需要参数 /// 创建人:龚宇超 /// 创建日期:2018-07-09 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// private void GetPrintOtherParam() { //取得打印完成后的参数 if (this.tmpSql.Contains("<<")) { string[] strs = this.tmpSql.Split(new[] { ">>" }, 2, StringSplitOptions.None); this.tmpSql = _mainPrintSql = strs[1]; _printModeCond = strs[0].Replace("<<", ""); } if (!string.IsNullOrEmpty(_printModeCond) && _printModeCond.IndexOf(';') > 0) { string[] str = _printModeCond.Split(';'); DataRow item = AttachImpl.GetSystemModule(str[0]); string tableName = item["TableName"] + ""; string modetype = item["modType"] + ""; string comprefix = BaseImpl.GetColumnPrefix(tableName); string primaryKey = modetype == "1" ? BaseImpl.GetBasePrimaryKey(str[0]) : comprefix + "billdocument_id"; //表名,列前缀,主键字段,主键值,模块编号,主打印Sql _printSaveParams = new List() { tableName, comprefix, primaryKey, str[1], str[0], _mainPrintSql }; //判断是否直接打印 modetype 区别单据和基础档案模块 switch (modetype) { case "1": ModuleModel mode = new ModuleModel(MainImpl.GetSystemdllTab(str[0])); if (mode != null) _isExcPrint = mode.PrintType == 3; break; case "2": BaseAccraditationModel model = new BaseAccraditationModel(BillAuditImpl.GetPubAuditProperty(str[0])); if (model != null) _isExcPrint = model.BillPrintType == 3; break; } } } /// /// 说明:窗体加载时 /// 创建人:龚宇超 /// 创建日期:2017-09-01 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The instance containing the event data. public void InitializeSetting() { string ids = string.Empty; this.tmpSql = this.Model.tmpSql; this.tmpSql1 = this.Model.tmpSql1; this.tmpSql2 = this.Model.tmpSql2; this.printFile = this.Model.printFile; //主Sql string mainSql = string.Empty; List tmpSqls = tmpSql2.Split(';').ToList(); form = new BaseForm(); form.Text = "打印程序"; form.IsMdiContainer = true; form.WindowState = FormWindowState.Maximized; } /// /// 说明:设置数据源 /// 创建人:龚宇超 /// 创建日期:2019-09-12 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The arguments. public void SetDataSource(string[] args) { DataSet myDataSet = new DataSet(); myDataSet.DataSetName = "myDataSet"; for (int i = 0; i < args.Length; i++) { string sql = args[i]; if (string.IsNullOrEmpty(sql)) continue; myDataSet.Merge(SetDataSource(sql, "temp" + i), true, MissingSchemaAction.Add); } DataTable newTable = myDataSet.Tables[0].Copy(); foreach (DataColumn dataColumn in newTable.Columns) { if (dataColumn.ColumnName.Contains("AttachString")) { string j = dataColumn.ColumnName.Substring(12); myDataSet.Tables[0].Columns.Add("ImageAttach" + j, typeof(Image)); myDataSet.Tables[0].Rows[0]["ImageAttach" + j] = GetPictureImage(myDataSet.Tables[0].Rows[0]["AttachString" + j] + ""); } } this.ReportObj.RegisterData(myDataSet); foreach (DataSourceBase source in this.ReportObj.Dictionary.DataSources) source.Enabled = true; } /// /// url转成Image /// /// /// public static Image GetPictureImage(string Url) { if (string.IsNullOrEmpty(Url)) { return null; } WebRequest webreq = WebRequest.Create(Url); WebResponse webres = webreq.GetResponse(); using (Stream stream = webres.GetResponseStream()) { return System.Drawing.Image.FromStream(stream); } } /// /// 说明:设置数据源 /// 创建人:龚宇超 /// 创建日期:2019-06-21 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The SQL. /// DataSet. public DataTable SetDataSource(string sql, string tableName) { string sqlValue = BaseImpl.GetDefaultValue(sql); DataTable table = SqlHelper.ExecuteDataTable(sqlValue); table.TableName = tableName; return table; } /// /// 说明:窗体加载时 /// 创建人:龚宇超 /// 创建日期:2017-09-01 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The instance containing the event data. public void InitializaControl() { //主Sql string mainSql = string.Empty; List tmpSqls = tmpSql.Split(';').ToList(); //if (_printSaveParams != null && _printSaveParams.Count > 0) //{ // _printSaveParams[5] = _mainPrintSql; // tmpSqls.Add(tmpSql1); // tmpSqls.Add(_mainPrintSql); //} Report report = new Report(); try { WaitForm.ShowForm(ResourceKeys.Handing); FastReport.Utils.Res.LoadLocale(PubUtil.PrintModeResourcePath); string path = PubUtil.PrintModePath + printFile; if (File.Exists(path) && !string.IsNullOrEmpty(printFile)) report.Load(path); this.Text = Path.GetFileName(printFile); ReportObj = report; //ReportObj.FinishReport += ReportObj_FinishReport; //ReportObj.PrintSettings.SavePrinterWithReport this.SetDataSource(tmpSqls.ToArray()); //设置打印份数 if (!string.IsNullOrWhiteSpace(Model.MultipleCopies.ToString()) && Model.MultipleCopies > 1) { this.ReportObj.PrintSettings.Copies = Model.MultipleCopies; } if (Model.printStyle.Equals(1)) { if (ERPInfo.Instance.IsUserManager) { this.ReportObj.Prepare(); this.ReportObj.Design(form); this.ReportObj.Designer.BorderStyle = BorderStyle.None; this.ReportObj.Designer.cmdPreview.CustomAction += OnCustomAction; form.ShowDialog(); //this.ReportObj.Design(form); //this.ReportObj.Designer.cmdPreview.CustomAction += OnCustomAction; //form.ShowDialog(); } else { this.ReportObj.Prepare(); this.ReportObj.ShowPrepared(form); this.ReportObj.Preview.OnPrint += Preview_OnPrint; this.ReportObj.Preview.ParentForm.WindowState = FormWindowState.Maximized; form.ShowDialog(); } } else if (Model.printStyle.Equals(2)) { this.ReportObj.Prepare(); this.ReportObj.ShowPrepared(form); this.ReportObj.Preview.OnPrint += Preview_OnPrint; this.ReportObj.Preview.ParentForm.WindowState = FormWindowState.Maximized; form.ShowDialog(); } else if (Model.printStyle.Equals(3)) { this.ReportObj.Prepare(); this.ReportObj.PrintSettings.ShowDialog = false; this.ReportObj.Print(); if (_printSaveParams != null && _printSaveParams.Count > 0) { //打印完成后添加数据库 int result = BaseModuleImpl.SavePrintRecord(_printSaveParams[0], _printSaveParams[1], _printSaveParams[2], _printSaveParams[3], _printSaveParams[4]); } } WaitForm.HideForm(); this.Close(); //design.ShowDialog(); } catch (Exception ex) { string Message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show(Message, ex.Message); //MessageUtil.Show(ex); this.ReportObj.Design(); WaitForm.HideForm(); } } private void OnCustomAction(object sender, EventArgs e) { FastReport.Design.StandardDesigner.DesignerControl design = sender as FastReport.Design.StandardDesigner.DesignerControl; FastReport.Report report = design.Report; System.Windows.Forms.Form form = new System.Windows.Forms.Form(); form.IsMdiContainer = true; form.WindowState = System.Windows.Forms.FormWindowState.Maximized; report.Prepare(); report.ShowPrepared(form); report.Preview.OnPrint += Preview_OnPrint; report.Preview.ParentForm.WindowState = FormWindowState.Maximized; form.ShowDialog(); report.Preview = null; form.Dispose(); } private void Preview_OnPrint(object sender, PreviewControl.PrintEventArgs e) { try { if (e.Preview != null && e.Preview.Report != null && e.Preview.Report.Preview != null) { this.ReportObj.PrintSettings.PageRange = PageRange.All; // 设置为全部页面 this.ReportObj.PrintSettings.PageNumbers = ""; // 清空页码范围 bool isPrint = e.Preview.Report.Preview.Print(); if (isPrint) { if (_printSaveParams != null && _printSaveParams.Count > 0) { //打印完成后添加数据库 int result = BaseModuleImpl.SavePrintRecord(_printSaveParams[0], _printSaveParams[1], _printSaveParams[2], _printSaveParams[3], _printSaveParams[4]); } } } else { MessageUtil.Show("Preview, Report or Report Preview is null"); } } catch (Exception ex) { string Message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show(Message, ex.Message); } } } }