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.
408 lines
15 KiB
408 lines
15 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.XtraEditors;
|
|
using DevExpress.XtraReports.UI;
|
|
using System.IO;
|
|
|
|
using System.Data.Linq;
|
|
using System.Linq;
|
|
using DevExpress.XtraPrinting;
|
|
using ICSSoft.Base.Lable.Entity;
|
|
using ICSSoft.Base.Config.AppConfig;
|
|
using ICSSoft.Base.Config.DBHelper;
|
|
using ICSSoft.Frame.Common;
|
|
|
|
namespace ICSSoft.Base.Lable.PrintTool
|
|
{
|
|
public partial class FormPrint : DevExpress.XtraEditors.XtraForm
|
|
{
|
|
private DataSet _DtSource = null;//数据源
|
|
private string _LinkCol = "";//关系字段
|
|
private string _PrintFortmatText = "";//打印模板XML文件
|
|
private string _PrintName = ""; //打印机
|
|
private bool _ShowDialogFlag = false;//是否显示预览
|
|
private List<string> SqlList = new List<string>();//sql语句集合
|
|
private string _PrintTableName = "";
|
|
//private string _PrintKey = "";
|
|
private int _PrintNum = 1;
|
|
private string _PrintPointID = "";
|
|
private string _PrintPointName = "";
|
|
bool _LocalFlag = true;
|
|
string _LabelCode = "";
|
|
string _LabelType = "";
|
|
|
|
public FormPrint(string LabelID, string Printer, bool localFlag,bool ShowDialogFlag, string PrintTableName, int PrintNum, string PrintPointID, string PrintPointName, List<PrintPara> PrintParas)
|
|
{
|
|
InitializeComponent();
|
|
_PrintTableName = PrintTableName;
|
|
_LocalFlag = localFlag;
|
|
// _PrintKey = PrintKey;
|
|
_PrintNum = PrintNum;
|
|
_PrintPointID = PrintPointID;
|
|
_PrintPointName = PrintPointName;
|
|
|
|
Dictionary<int, DataTable> tableDic = GetTablesCount(LabelID);
|
|
int tablecount = tableDic.Keys.Count;
|
|
Sys_Lables label = GetLable(LabelID);
|
|
Sys_LableDataSource source = GetSource(label.LableSourceID);
|
|
_LabelCode = label.LableCode;//模板编码
|
|
_LabelType = label.LableType;//模板类型
|
|
foreach (PrintPara p in PrintParas)
|
|
{
|
|
string sqltxt = source.SqlTxt;
|
|
if (source.Paras.Trim() != "")
|
|
{
|
|
string[] ps = source.Paras.Split(',');
|
|
for (int i = 0; i < ps.Length; i++)
|
|
{
|
|
if (!string.IsNullOrEmpty(ps[i]))
|
|
{
|
|
sqltxt = sqltxt.Replace(ps[i], p.PrintValues[i].ToString());
|
|
}
|
|
}
|
|
}
|
|
if (sqltxt.Contains("[AppConfig.WorkPointCode]"))
|
|
{
|
|
sqltxt = sqltxt.Replace("[AppConfig.WorkPointCode]", AppConfig.WorkPointCode);
|
|
}
|
|
if (sqltxt.Contains("[AppConfig.UserId]"))
|
|
{
|
|
sqltxt = sqltxt.Replace("[AppConfig.UserId]", AppConfig.UserId);
|
|
}
|
|
if (sqltxt.Contains("[AppConfig.UserCode]"))
|
|
{
|
|
sqltxt = sqltxt.Replace("[AppConfig.UserCode]", AppConfig.UserCode);
|
|
}
|
|
if (sqltxt.Contains("[AppConfig.UserName]"))
|
|
{
|
|
sqltxt = sqltxt.Replace("[AppConfig.UserName]", AppConfig.UserName);
|
|
}
|
|
if (sqltxt.Contains("[AppConfig.WorkPointName]"))
|
|
{
|
|
sqltxt = sqltxt.Replace("[AppConfig.WorkPointName]", AppConfig.WorkPointName);
|
|
}
|
|
Dictionary<string, string> dbDic = AppConfig.GetDataBaseNames();
|
|
foreach (string key in dbDic.Keys)
|
|
{
|
|
if (sqltxt.Contains(key))
|
|
{
|
|
sqltxt = sqltxt.Replace(key, dbDic[key]);
|
|
}
|
|
}
|
|
SqlList.Add(sqltxt);
|
|
}
|
|
string conn = "";
|
|
if (source.SysDataFlag)
|
|
{
|
|
conn = AppConfig.FrameConnectString;
|
|
}
|
|
else
|
|
{
|
|
if (source.WorkPointDataFlag)
|
|
{
|
|
conn = AppConfig.AppConnectString;
|
|
}
|
|
else
|
|
{
|
|
conn = AppConfig.GetDataBaseConnectString(source.OtherDataBaseID);
|
|
}
|
|
}
|
|
foreach (string sql in SqlList)
|
|
{
|
|
DataSet ds = DBHelper.ExecuteDataset(conn, CommandType.Text, sql);
|
|
int i = 0;
|
|
foreach (DataTable data in ds.Tables)
|
|
{
|
|
DataTable sData = tableDic[i];
|
|
foreach (DataRow dr in data.Rows)
|
|
{
|
|
DataRow ndr = sData.NewRow();
|
|
foreach (DataColumn dc in data.Columns)
|
|
{
|
|
ndr[dc.ColumnName] = dr[dc.ColumnName];
|
|
}
|
|
sData.Rows.Add(ndr);
|
|
}
|
|
sData.AcceptChanges();
|
|
i++;
|
|
}
|
|
}
|
|
_LinkCol = source.LinkCols;
|
|
|
|
_PrintName = Printer;
|
|
_ShowDialogFlag = ShowDialogFlag;
|
|
_PrintFortmatText = label.LableXml;
|
|
_DtSource = new DataSet();
|
|
for (int k = 0; k <= tablecount-1; k++)
|
|
{
|
|
_DtSource.Tables.Add(tableDic[k]);
|
|
|
|
}
|
|
BindReport();
|
|
UpdateTask(label.ID, PrintParas);
|
|
}
|
|
public FormPrint(string LabelID, string Printer, bool ShowDialogFlag, string PrintTableName, int PrintNum, string PrintPointID, string PrintPointName, DataSet printData)
|
|
{
|
|
InitializeComponent();
|
|
_PrintTableName = PrintTableName;
|
|
|
|
_PrintNum = PrintNum;
|
|
_PrintPointID = PrintPointID;
|
|
_PrintPointName = PrintPointName;
|
|
|
|
Dictionary<int, DataTable> tableDic = GetTablesCount(LabelID);
|
|
int tablecount = tableDic.Keys.Count;
|
|
Sys_Lables label = GetLable(LabelID);
|
|
Sys_LableDataSource source = GetSource(label.LableSourceID);
|
|
|
|
_LinkCol = source.LinkCols;
|
|
|
|
_PrintName = Printer;
|
|
_ShowDialogFlag = ShowDialogFlag;
|
|
_PrintFortmatText = label.LableXml;
|
|
_DtSource = new DataSet();
|
|
_DtSource = printData;
|
|
BindReport();
|
|
// UpdatePrintFlag(taskId);
|
|
}
|
|
private void UpdateTask(string LableID,List<PrintPara> pList)
|
|
{
|
|
LableDataContext db = new LableDataContext(AppConfig.FrameConnectString);
|
|
db.Connection.Open();
|
|
db.Transaction = db.Connection.BeginTransaction();
|
|
try
|
|
{
|
|
foreach (PrintPara pp in pList)
|
|
{
|
|
for (int i = 0; i < _PrintNum; i++)
|
|
{
|
|
Sys_LableTask task = new Sys_LableTask();
|
|
task.ID = AppConfig.GetGuid();
|
|
task.LableID = LableID;
|
|
if (_LocalFlag == true)
|
|
{
|
|
task.PrintDate = AppConfig.GetSeverDateTime("yyyy-MM-dd HH:mm:ss");
|
|
task.PrintFlag = true;
|
|
}
|
|
task.PrintKey = pp.PrintKey;
|
|
task.PrintName = _PrintName;
|
|
task.PrintNum = 1;
|
|
task.PrintPointID = _PrintPointID;
|
|
task.PrintPointName = _PrintPointName;
|
|
task.PrintSeq = 0;
|
|
task.PrintTableName = _PrintTableName;
|
|
task.PrintUserID = AppConfig.UserId;
|
|
string values = "";
|
|
if (pp.PrintValues.Length > 0)
|
|
{
|
|
foreach (string v in pp.PrintValues)
|
|
{
|
|
if (!string.IsNullOrEmpty(v))
|
|
{
|
|
values = values + v + ",";
|
|
}
|
|
}
|
|
if (values != "")
|
|
{
|
|
values = values.Substring(0, values.Length - 1);
|
|
}
|
|
}
|
|
task.ValueParameters = values;
|
|
task.WorkPointCode = AppConfig.WorkPointCode;
|
|
db.Sys_LableTask.InsertOnSubmit(task);
|
|
Sys_LableContext lcontext = new Sys_LableContext();
|
|
lcontext.AppConfig_UserCode = AppConfig.UserCode;
|
|
lcontext.AppConfig_UserId = AppConfig.UserId;
|
|
lcontext.AppConfig_UserName = AppConfig.UserName;
|
|
lcontext.AppConfig_WorkPointCode = AppConfig.WorkPointCode;
|
|
lcontext.AppConfig_WorkPointName = AppConfig.WorkPointName;
|
|
lcontext.ID = AppConfig.GetGuid();
|
|
lcontext.TaskID = task.ID;
|
|
db.Sys_LableContext.InsertOnSubmit(lcontext);
|
|
}
|
|
}
|
|
db.SubmitChanges();
|
|
db.Transaction.Commit();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
db.Transaction.Rollback();
|
|
}
|
|
}
|
|
private Sys_Lables GetLable(string ID)
|
|
{
|
|
using (LableDataContext db = new LableDataContext(AppConfig.FrameConnectString))
|
|
{
|
|
return db.Sys_Lables.SingleOrDefault(a => a.ID == ID);
|
|
}
|
|
}
|
|
private Sys_LableDataSource GetSource(string ID)
|
|
{
|
|
using (LableDataContext db = new LableDataContext(AppConfig.FrameConnectString))
|
|
{
|
|
return db.Sys_LableDataSource.SingleOrDefault(a => a.ID == ID);
|
|
}
|
|
}
|
|
private Dictionary<int, DataTable> GetTablesCount(string ID)
|
|
{
|
|
Dictionary<int, DataTable> dic = new Dictionary<int, DataTable>();
|
|
Sys_Lables obj = GetLable(ID);
|
|
Sys_LableDataSource dsource = GetSource(obj.LableSourceID);
|
|
string tempSql = dsource.SqlTxt;
|
|
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);
|
|
}
|
|
}
|
|
DataSet ds = DBHelper.ExecuteDataset(conn, CommandType.Text, tempSql);
|
|
int i=0;
|
|
foreach (DataTable data in ds.Tables)
|
|
{
|
|
DataTable newData = data.Clone();
|
|
newData.TableName = "table" + i.ToString();
|
|
dic.Add(i, newData);
|
|
i++;
|
|
}
|
|
return dic;
|
|
}
|
|
void PrintingSystem_StartPrint(object sender, PrintDocumentEventArgs e)
|
|
{
|
|
|
|
|
|
}
|
|
|
|
private void BindReport()
|
|
{
|
|
XtraReport ltdReport = new XtraReport();
|
|
|
|
string msg = "";
|
|
if (_DtSource.Tables.Count == 1)
|
|
{
|
|
if (_LabelCode == "0005" && _LabelType == "014")
|
|
{
|
|
string mo = _DtSource.Tables[0].Rows[0]["MoCode"].ToString();
|
|
DataTable tableMo = TranOutClass.TranOut(mo);
|
|
tableMo.TableName = "Table";
|
|
ltdReport.DataSource = tableMo;
|
|
ltdReport.DataMember = tableMo.TableName;
|
|
}
|
|
else if (_LabelCode == "0010" && _LabelType == "016")
|
|
{
|
|
string mo = _DtSource.Tables[0].Rows[0]["MoCode"].ToString();
|
|
DataTable tableMo = TranOutClass.TranOutX(mo);
|
|
tableMo.TableName = "Table";
|
|
ltdReport.DataSource = tableMo;
|
|
ltdReport.DataMember = tableMo.TableName;
|
|
}
|
|
else
|
|
{
|
|
ltdReport.DataSource = _DtSource.Tables[0];
|
|
ltdReport.DataMember = _DtSource.Tables[0].TableName;
|
|
}
|
|
}
|
|
else if (_DtSource.Tables.Count == 2)
|
|
{
|
|
if (_LinkCol.Trim() == "")
|
|
{
|
|
msg += "没有关联字段!\n";
|
|
}
|
|
else
|
|
{
|
|
|
|
if (!_DtSource.Tables[0].Columns.Contains(_LinkCol))
|
|
{
|
|
msg += "主表中没有关联字段!\n";
|
|
}
|
|
else if (!_DtSource.Tables[1].Columns.Contains(_LinkCol))
|
|
{
|
|
msg += "子表中没有关联字段!\n";
|
|
}
|
|
else
|
|
{
|
|
DataColumn parentCol = _DtSource.Tables[0].Columns[_LinkCol];
|
|
DataColumn sonCol = _DtSource.Tables[1].Columns[_LinkCol];
|
|
DataRelation relatioon = new DataRelation("R1", parentCol, sonCol);
|
|
if (!_DtSource.Relations.Contains("R1"))
|
|
{
|
|
_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);
|
|
if (!string.IsNullOrEmpty(_PrintName))
|
|
{
|
|
ltdReport.PrinterName = _PrintName;
|
|
}
|
|
MemoryStream steam = new MemoryStream(bs2);
|
|
ltdReport.LoadLayout(steam);
|
|
printControl1.PrintingSystem = ltdReport.PrintingSystem;
|
|
|
|
ltdReport.CreateDocument();
|
|
}
|
|
|
|
ltdReport.PrintingSystem.ShowMarginsWarning = printControl1.PrintingSystem.ShowMarginsWarning = false;
|
|
ltdReport.PrintingSystem.StartPrint += new PrintDocumentEventHandler(PrintingSystem_StartPrint);
|
|
|
|
|
|
}
|
|
|
|
}
|
|
}
|