华恒Mes鼎捷代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

377 lines
12 KiB

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraReports.UserDesigner;
using DevExpress.XtraReports.UI;
using DevExpress.XtraReports.UserDesigner.Native;
using System.IO;
using DevExpress.Data;
using System.Drawing.Design;
using DevExpress.XtraEditors;
using DevExpress.XtraPrinting;
using System.Data.Linq;
using System.Linq;
using System.Text.RegularExpressions;
using ICSSoft.Base.Config.AppConfig;
using ICSSoft.Base.Lable.Entity;
using ICSSoft.Base.Config.DBHelper;
namespace ICSSoft.Base.Lable.PrintTool
{
public partial class FormPrintDesigner : XRDesignFormExBase
{
private DataSet _DtSource { get; set; }
private string _PrintFortmatText { get; set; }
private string _LinkCols = "";
private string _PrintId = "";
private string _UserID = "";
public FormPrintDesigner(string tempId)
{
InitializeComponent();
_PrintId = tempId;
_UserID = AppConfig.UserId;
Sys_Lables obj = GetPrintTemp();
Sys_LableDataSource dsource=GetLableDataSource(obj.LableSourceID);
string tempSql = dsource.SqlTxt;
if (tempSql.Contains("[AppConfig.WorkPointCode]"))
{
tempSql = tempSql.Replace("[AppConfig.WorkPointCode]", AppConfig.WorkPointCode);
}
if (tempSql.Contains("[AppConfig.UserId]"))
{
tempSql = tempSql.Replace("[AppConfig.UserId]", AppConfig.UserId);
}
if (tempSql.Contains("[AppConfig.UserCode]"))
{
tempSql = tempSql.Replace("[AppConfig.UserCode]", AppConfig.UserCode);
}
if (tempSql.Contains("[AppConfig.UserName]"))
{
tempSql = tempSql.Replace("[AppConfig.UserName]", AppConfig.UserName);
}
if (tempSql.Contains("[AppConfig.WorkPointName]"))
{
tempSql = tempSql.Replace("[AppConfig.WorkPointName]", AppConfig.WorkPointName);
}
Dictionary<string, string> dbDic = AppConfig.GetDataBaseNames();
foreach (string key in dbDic.Keys)
{
if (tempSql.Contains(key))
{
tempSql = tempSql.Replace(key, dbDic[key]);
}
}
string[] indexs = dsource.Paras.Split(',');
List<string> indexList = new List<string>();
foreach (string index in indexs)
{
if (!string.IsNullOrEmpty(index))
{
indexList.Add(index);
}
}
if (indexList.Count > 0)
{
foreach (string index in indexList)
{
if (tempSql.Contains("'" + index + "'"))
{
tempSql = tempSql.Replace(index, "");
}
else
{
tempSql = tempSql.Replace(index, "0");
}
}
}
string conn = "";
if (dsource.SysDataFlag)
{
conn = AppConfig.FrameConnectString;
}
else
{
if (dsource.WorkPointDataFlag)
{
conn = AppConfig.AppConnectString;
}
else
{
conn= AppConfig.GetDataBaseConnectString(dsource.OtherDataBaseID);
}
}
_DtSource = DBHelper.ExecuteDataset(conn,CommandType.Text,tempSql);
_PrintFortmatText = obj==null?"":obj.LableXml;
_LinkCols = dsource.LinkCols;
this.xrDesignPanel.AddCommandHandler(new SaveCommandHandler(this.xrDesignPanel,_PrintId,AppConfig.FrameConnectString,_UserID) );
}
/// <summary>
/// ��ȡģ����Ϣ
/// </summary>
/// <returns></returns>
private Sys_Lables GetPrintTemp()
{
using (LableDataContext db = new LableDataContext(AppConfig.FrameConnectString))
{
Sys_Lables obj = db.Sys_Lables.SingleOrDefault(a => a.ID == _PrintId);
return obj;
}
}
private Sys_LableDataSource GetLableDataSource(string ID)
{
using (LableDataContext db = new LableDataContext(AppConfig.FrameConnectString))
{
Sys_LableDataSource obj = db.Sys_LableDataSource.SingleOrDefault(a => a.ID == ID);
return obj;
}
}
/// <summary>
/// Load the report into the designer
/// </summary>
public void BindReport()
{
XtraReport ltdReport = new XtraReport();
string msg = "";
if (_DtSource.Tables.Count == 1)
{
ltdReport.DataSource = _DtSource.Tables[0];
ltdReport.DataMember = _DtSource.Tables[0].TableName;
}
else if (_DtSource.Tables.Count > 1)
{
if (_LinkCols.Trim() == "")
{
msg += "û�й����ֶΣ�\n";
}
else
{
for (int i = 0; i < _DtSource.Tables.Count; i++)
{
if (!_DtSource.Tables[i].Columns.Contains(_LinkCols))
{
msg += "��:" + _DtSource.Tables[i] .TableName+ "û�й����ֶΣ�\n";
}
}
}
DataColumn parentCol = _DtSource.Tables[0].Columns[_LinkCols];
if (msg == "")
{
for (int i = 0; i < _DtSource.Tables.Count; i++)
{
if (i == 0)
continue;
DataColumn sonCol = _DtSource.Tables[i].Columns[_LinkCols];
DataRelation relatioon = new DataRelation("R"+i.ToString(), parentCol, sonCol);
if (!_DtSource.Relations.Contains("R"+i.ToString()))
{
_DtSource.Relations.Add(relatioon);
}
}
ltdReport.DataMember = _DtSource.Tables[0].TableName;
ltdReport.DataSource = _DtSource;
}
}
else
{
msg += "��ӡ����Դ������\n";
}
if (msg != "")
{
MessageBox.Show( msg);
return;
}
if (!string.IsNullOrEmpty(_PrintFortmatText))
{
byte[] bs2 = System.Text.Encoding.UTF8.GetBytes(_PrintFortmatText);
MemoryStream steam = new MemoryStream(bs2);
ltdReport.LoadLayout(steam);
ltdReport.CreateDocument();
}
xrDesignPanel.OpenReport( ltdReport );
commandBarItem32.Enabled = false;
}
///// <summary>
///// Hide data source properties
///// </summary>
///// <param name="sender"></param>
///// <param name="e"></param>
//void LTDReport_FilterControlProperties( object sender, FilterControlPropertiesEventArgs e )
//{
// if ( e.Properties.Contains( "DataSource" ))
// e.Properties.Remove( "DataSource" );
// if ( e.Properties.Contains( "DataMember" ) )
// e.Properties.Remove( "DataMember" );
// if ( e.Properties.Contains( "DataAdapter" ) )
// e.Properties.Remove( "DataAdapter" );
//}
private void commandBarItem31_ItemClick( object sender, DevExpress.XtraBars.ItemClickEventArgs e )
{
BindReport();
}
private void xrDesignPanel_DesignerHostLoaded( object sender, DesignerLoadedEventArgs e )
{
IToolboxService ts = ( IToolboxService )e.DesignerHost.GetService( typeof( IToolboxService ) );
ToolboxItem devExpressSubReport = null;
// Add a custom control.
ToolboxItemCollection toolboxItems = ts.GetToolboxItems();
foreach( ToolboxItem toolboxItem in toolboxItems )
{
if ( toolboxItem.TypeName == "DevExpress.XtraReports.UI.XRSubreport" )
{
devExpressSubReport = toolboxItem;
break;
}
}
if ( devExpressSubReport != null )
ts.RemoveToolboxItem( devExpressSubReport );
}
private void FormRepDesign_Load(object sender, EventArgs e)
{
BindReport();
}
private void FormRepDesign_FormClosed(object sender, FormClosedEventArgs e)
{
//MemoryStream layoutStream = new MemoryStream();
//xrDesignPanel.Report.SaveLayout(layoutStream);
//xrDesignPanel.ReportState = ReportState.Saved;
//// StreamReader readStream = new StreamReader(layoutStream, Encoding.UTF8);
//string txt = System.Text.Encoding.UTF8.GetString(layoutStream.ToArray());
////readStream.Close();
//_PrintFortmatText = txt;
}
}
/// <summary>
/// Command Handler to overwrite the save event
/// </summary>
public class SaveCommandHandler : DevExpress.XtraReports.UserDesigner.ICommandHandler
{
XRDesignPanel panel;
private string _PrintId = "";
private string _ConnectString = "";
private string _UserCode = "";
public SaveCommandHandler( XRDesignPanel panel,string pId,string connectString ,string userCode)
{
this.panel = panel;
_PrintId = pId;
_ConnectString = connectString;
_UserCode = userCode;
}
public void HandleCommand(ReportCommand command, object[] args)
{
bool useNexHadle = true ;
if (!CanHandleCommand(command, ref useNexHadle))
return;
// Save a report.
if ( panel.ReportState == ReportState.Changed && command == ReportCommand.Closing )
{
Save();
}
else if (command != ReportCommand.Closing)
{
if ( panel.ReportState == ReportState.Changed )
Save();
}
// handled = true;
}
public bool CanHandleCommand(ReportCommand command, ref bool useNextHandler)
{
// This handler is used for SaveFile, SaveFileAs and Closing commands.
return command == ReportCommand.SaveFile ||
command == ReportCommand.SaveFileAs ||
command == ReportCommand.Closing;
}
void Save()
{
if (MessageBox.Show( "�Ƿ񱣴��޸ģ�","��ʾ",MessageBoxButtons.YesNo,MessageBoxIcon.Information) == DialogResult.No)
return;
LableDataContext db = new LableDataContext(_ConnectString);
db.Connection.Open();
db.Transaction = db.Connection.BeginTransaction();
try
{
var temp = db.Sys_Lables.SingleOrDefault(a => a.ID == _PrintId);
if (temp == null)
{
MessageBox.Show("ģ�岻�������ݿ��У��޷��������£�");
}
else
{
string txt = "";
using (MemoryStream layoutStream = new MemoryStream())
{
panel.Report.SaveLayout(layoutStream);
txt = System.Text.Encoding.UTF8.GetString(layoutStream.ToArray());
}
temp.LableXml = txt;
temp.CreateDate =DateTime.Now;
temp.CreateUserID =_UserCode;
db.SubmitChanges();
db.Transaction.Commit();
panel.ReportState = ReportState.Saved;
}
}
catch (Exception ex)
{
MessageBox.Show( "�쳣:" + ex.Message);
db.Transaction.Rollback();
}
}
#region ICommandHandler Members
#endregion
}
}