Files
lserp_cs_6.0/插件库/Lskj.FastReport/FormMain.cs
2026-07-16 16:05:38 +08:00

452 lines
17 KiB
C#

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 公有参数
/// <summary>
/// 调用动态链接库默认传递参数对象
/// </summary>
public DynamicFastReport Model { get; set; }
public List<string> sqls;
public string printFile;
public string tmpSql;
public string tmpSql1;
public string tmpSql2;
public bool isPrint;
public BaseForm form;
#endregion
#region 私有参数
/// <summary>
/// 是否直接打印
/// </summary>
private bool _isExcPrint;
/// <summary>
/// 打印完成后添加数据用到的参数
/// </summary>
private List<string> _printSaveParams;
/// <summary>
/// 主打印Sql
/// </summary>
private string _mainPrintSql;
/// <summary>
/// 打印主sql中关联打印次数返回条件
/// </summary>
private string _printModeCond;
#endregion
/// <summary>
/// <para>说明:窗体加载时</para>
/// <para>创建人:龚宇超</para>
/// <para>创建日期:2017-09-01 </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
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);
}
}
/// <summary>
/// <para>说明:打印完成需要参数</para>
/// <para>创建人:龚宇超</para>
/// <para>创建日期:2018-07-09 </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
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<string>() { 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;
}
}
}
/// <summary>
/// <para>说明:窗体加载时</para>
/// <para>创建人:龚宇超</para>
/// <para>创建日期:2017-09-01 </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
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<string> tmpSqls = tmpSql2.Split(';').ToList();
form = new BaseForm();
form.Text = "打印程序";
form.IsMdiContainer = true;
form.WindowState = FormWindowState.Maximized;
}
/// <summary>
/// <para>说明:设置数据源</para>
/// <para>创建人:龚宇超</para>
/// <para>创建日期:2019-09-12 </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
/// <param name="args">The arguments.</param>
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;
}
/// <summary>
/// url转成Image
/// </summary>
/// <param name="Url"></param>
/// <returns></returns>
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);
}
}
/// <summary>
/// <para>说明:设置数据源</para>
/// <para>创建人:龚宇超</para>
/// <para>创建日期:2019-06-21 </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
/// <param name="sql">The SQL.</param>
/// <returns>DataSet.</returns>
public DataTable SetDataSource(string sql, string tableName)
{
string sqlValue = BaseImpl.GetDefaultValue(sql);
DataTable table = SqlHelper.ExecuteDataTable(sqlValue);
table.TableName = tableName;
return table;
}
/// <summary>
/// <para>说明:窗体加载时</para>
/// <para>创建人:龚宇超</para>
/// <para>创建日期:2017-09-01 </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
public void InitializaControl()
{
//主Sql
string mainSql = string.Empty;
List<string> tmpSqls = tmpSql.Split(';').ToList();
//if (_printSaveParams != null && _printSaveParams.Count > 0)
//{
// _printSaveParams[5] = _mainPrintSql;
// tmpSqls.Add(tmpSql1);
// tmpSqls.Add(_mainPrintSql);
//}
Report report = new Report();
//设置边距
//if(report.Pages.Count > 0 && report.Pages[0] is ReportPage page)
//{
// // 设置左边距(单位:缇,1英寸=1440缇)
// page.LeftMargin = 100; // 约等于0.07英寸或1.76毫米
//}
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 || SystemInfo.Instance.PrintTemplate.Split(',').Contains(ERPInfo.Instance.UserName))
{
try
{
this.ReportObj.Prepare();
this.ReportObj.Design(form);
this.ReportObj.Designer.BorderStyle = BorderStyle.None;
this.ReportObj.Designer.cmdPreview.CustomAction += OnCustomAction;
form.ShowDialog();
}
finally
{
ReleaseDesignerResource();
}
}
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);
if (this.ReportObj != null)
{
this.ReportObj.Design();
}
WaitForm.HideForm();
}
}
private void ReleaseDesignerResource()
{
try
{
if (this.ReportObj != null && this.ReportObj.Designer != null)
{
try
{
this.ReportObj.Designer.StopAutoSave();
}
catch
{
}
try
{
this.ReportObj.Designer.cmdPreview.CustomAction -= OnCustomAction;
}
catch
{
}
}
if (form != null && !form.IsDisposed)
{
form.Dispose();
}
if (this.ReportObj != null)
{
this.ReportObj.Dispose();
this.ReportObj = null;
}
}
catch
{
}
}
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);
}
}
}
}