华恒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.

407 lines
15 KiB

5 months ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using DevExpress.XtraEditors;
  9. using DevExpress.XtraReports.UI;
  10. using System.IO;
  11. using System.Data.Linq;
  12. using System.Linq;
  13. using DevExpress.XtraPrinting;
  14. using ICSSoft.Base.Lable.Entity;
  15. using ICSSoft.Base.Config.AppConfig;
  16. using ICSSoft.Base.Config.DBHelper;
  17. using ICSSoft.Frame.Common;
  18. namespace ICSSoft.Base.Lable.PrintTool
  19. {
  20. public partial class FormPrint : DevExpress.XtraEditors.XtraForm
  21. {
  22. private DataSet _DtSource = null;//数据源
  23. private string _LinkCol = "";//关系字段
  24. private string _PrintFortmatText = "";//打印模板XML文件
  25. private string _PrintName = ""; //打印机
  26. private bool _ShowDialogFlag = false;//是否显示预览
  27. private List<string> SqlList = new List<string>();//sql语句集合
  28. private string _PrintTableName = "";
  29. //private string _PrintKey = "";
  30. private int _PrintNum = 1;
  31. private string _PrintPointID = "";
  32. private string _PrintPointName = "";
  33. bool _LocalFlag = true;
  34. string _LabelCode = "";
  35. string _LabelType = "";
  36. public FormPrint(string LabelID, string Printer, bool localFlag,bool ShowDialogFlag, string PrintTableName, int PrintNum, string PrintPointID, string PrintPointName, List<PrintPara> PrintParas)
  37. {
  38. InitializeComponent();
  39. _PrintTableName = PrintTableName;
  40. _LocalFlag = localFlag;
  41. // _PrintKey = PrintKey;
  42. _PrintNum = PrintNum;
  43. _PrintPointID = PrintPointID;
  44. _PrintPointName = PrintPointName;
  45. Dictionary<int, DataTable> tableDic = GetTablesCount(LabelID);
  46. int tablecount = tableDic.Keys.Count;
  47. Sys_Lables label = GetLable(LabelID);
  48. Sys_LableDataSource source = GetSource(label.LableSourceID);
  49. _LabelCode = label.LableCode;//模板编码
  50. _LabelType = label.LableType;//模板类型
  51. foreach (PrintPara p in PrintParas)
  52. {
  53. string sqltxt = source.SqlTxt;
  54. if (source.Paras.Trim() != "")
  55. {
  56. string[] ps = source.Paras.Split(',');
  57. for (int i = 0; i < ps.Length; i++)
  58. {
  59. if (!string.IsNullOrEmpty(ps[i]))
  60. {
  61. sqltxt = sqltxt.Replace(ps[i], p.PrintValues[i].ToString());
  62. }
  63. }
  64. }
  65. if (sqltxt.Contains("[AppConfig.WorkPointCode]"))
  66. {
  67. sqltxt = sqltxt.Replace("[AppConfig.WorkPointCode]", AppConfig.WorkPointCode);
  68. }
  69. if (sqltxt.Contains("[AppConfig.UserId]"))
  70. {
  71. sqltxt = sqltxt.Replace("[AppConfig.UserId]", AppConfig.UserId);
  72. }
  73. if (sqltxt.Contains("[AppConfig.UserCode]"))
  74. {
  75. sqltxt = sqltxt.Replace("[AppConfig.UserCode]", AppConfig.UserCode);
  76. }
  77. if (sqltxt.Contains("[AppConfig.UserName]"))
  78. {
  79. sqltxt = sqltxt.Replace("[AppConfig.UserName]", AppConfig.UserName);
  80. }
  81. if (sqltxt.Contains("[AppConfig.WorkPointName]"))
  82. {
  83. sqltxt = sqltxt.Replace("[AppConfig.WorkPointName]", AppConfig.WorkPointName);
  84. }
  85. Dictionary<string, string> dbDic = AppConfig.GetDataBaseNames();
  86. foreach (string key in dbDic.Keys)
  87. {
  88. if (sqltxt.Contains(key))
  89. {
  90. sqltxt = sqltxt.Replace(key, dbDic[key]);
  91. }
  92. }
  93. SqlList.Add(sqltxt);
  94. }
  95. string conn = "";
  96. if (source.SysDataFlag)
  97. {
  98. conn = AppConfig.FrameConnectString;
  99. }
  100. else
  101. {
  102. if (source.WorkPointDataFlag)
  103. {
  104. conn = AppConfig.AppConnectString;
  105. }
  106. else
  107. {
  108. conn = AppConfig.GetDataBaseConnectString(source.OtherDataBaseID);
  109. }
  110. }
  111. foreach (string sql in SqlList)
  112. {
  113. DataSet ds = DBHelper.ExecuteDataset(conn, CommandType.Text, sql);
  114. int i = 0;
  115. foreach (DataTable data in ds.Tables)
  116. {
  117. DataTable sData = tableDic[i];
  118. foreach (DataRow dr in data.Rows)
  119. {
  120. DataRow ndr = sData.NewRow();
  121. foreach (DataColumn dc in data.Columns)
  122. {
  123. ndr[dc.ColumnName] = dr[dc.ColumnName];
  124. }
  125. sData.Rows.Add(ndr);
  126. }
  127. sData.AcceptChanges();
  128. i++;
  129. }
  130. }
  131. _LinkCol = source.LinkCols;
  132. _PrintName = Printer;
  133. _ShowDialogFlag = ShowDialogFlag;
  134. _PrintFortmatText = label.LableXml;
  135. _DtSource = new DataSet();
  136. for (int k = 0; k <= tablecount-1; k++)
  137. {
  138. _DtSource.Tables.Add(tableDic[k]);
  139. }
  140. BindReport();
  141. UpdateTask(label.ID, PrintParas);
  142. }
  143. public FormPrint(string LabelID, string Printer, bool ShowDialogFlag, string PrintTableName, int PrintNum, string PrintPointID, string PrintPointName, DataSet printData)
  144. {
  145. InitializeComponent();
  146. _PrintTableName = PrintTableName;
  147. _PrintNum = PrintNum;
  148. _PrintPointID = PrintPointID;
  149. _PrintPointName = PrintPointName;
  150. Dictionary<int, DataTable> tableDic = GetTablesCount(LabelID);
  151. int tablecount = tableDic.Keys.Count;
  152. Sys_Lables label = GetLable(LabelID);
  153. Sys_LableDataSource source = GetSource(label.LableSourceID);
  154. _LinkCol = source.LinkCols;
  155. _PrintName = Printer;
  156. _ShowDialogFlag = ShowDialogFlag;
  157. _PrintFortmatText = label.LableXml;
  158. _DtSource = new DataSet();
  159. _DtSource = printData;
  160. BindReport();
  161. // UpdatePrintFlag(taskId);
  162. }
  163. private void UpdateTask(string LableID,List<PrintPara> pList)
  164. {
  165. LableDataContext db = new LableDataContext(AppConfig.FrameConnectString);
  166. db.Connection.Open();
  167. db.Transaction = db.Connection.BeginTransaction();
  168. try
  169. {
  170. foreach (PrintPara pp in pList)
  171. {
  172. for (int i = 0; i < _PrintNum; i++)
  173. {
  174. Sys_LableTask task = new Sys_LableTask();
  175. task.ID = AppConfig.GetGuid();
  176. task.LableID = LableID;
  177. if (_LocalFlag == true)
  178. {
  179. task.PrintDate = AppConfig.GetSeverDateTime("yyyy-MM-dd HH:mm:ss");
  180. task.PrintFlag = true;
  181. }
  182. task.PrintKey = pp.PrintKey;
  183. task.PrintName = _PrintName;
  184. task.PrintNum = 1;
  185. task.PrintPointID = _PrintPointID;
  186. task.PrintPointName = _PrintPointName;
  187. task.PrintSeq = 0;
  188. task.PrintTableName = _PrintTableName;
  189. task.PrintUserID = AppConfig.UserId;
  190. string values = "";
  191. if (pp.PrintValues.Length > 0)
  192. {
  193. foreach (string v in pp.PrintValues)
  194. {
  195. if (!string.IsNullOrEmpty(v))
  196. {
  197. values = values + v + ",";
  198. }
  199. }
  200. if (values != "")
  201. {
  202. values = values.Substring(0, values.Length - 1);
  203. }
  204. }
  205. task.ValueParameters = values;
  206. task.WorkPointCode = AppConfig.WorkPointCode;
  207. db.Sys_LableTask.InsertOnSubmit(task);
  208. Sys_LableContext lcontext = new Sys_LableContext();
  209. lcontext.AppConfig_UserCode = AppConfig.UserCode;
  210. lcontext.AppConfig_UserId = AppConfig.UserId;
  211. lcontext.AppConfig_UserName = AppConfig.UserName;
  212. lcontext.AppConfig_WorkPointCode = AppConfig.WorkPointCode;
  213. lcontext.AppConfig_WorkPointName = AppConfig.WorkPointName;
  214. lcontext.ID = AppConfig.GetGuid();
  215. lcontext.TaskID = task.ID;
  216. db.Sys_LableContext.InsertOnSubmit(lcontext);
  217. }
  218. }
  219. db.SubmitChanges();
  220. db.Transaction.Commit();
  221. }
  222. catch (Exception ex)
  223. {
  224. db.Transaction.Rollback();
  225. }
  226. }
  227. private Sys_Lables GetLable(string ID)
  228. {
  229. using (LableDataContext db = new LableDataContext(AppConfig.FrameConnectString))
  230. {
  231. return db.Sys_Lables.SingleOrDefault(a => a.ID == ID);
  232. }
  233. }
  234. private Sys_LableDataSource GetSource(string ID)
  235. {
  236. using (LableDataContext db = new LableDataContext(AppConfig.FrameConnectString))
  237. {
  238. return db.Sys_LableDataSource.SingleOrDefault(a => a.ID == ID);
  239. }
  240. }
  241. private Dictionary<int, DataTable> GetTablesCount(string ID)
  242. {
  243. Dictionary<int, DataTable> dic = new Dictionary<int, DataTable>();
  244. Sys_Lables obj = GetLable(ID);
  245. Sys_LableDataSource dsource = GetSource(obj.LableSourceID);
  246. string tempSql = dsource.SqlTxt;
  247. string[] indexs = dsource.Paras.Split(',');
  248. List<string> indexList = new List<string>();
  249. foreach (string index in indexs)
  250. {
  251. if (!string.IsNullOrEmpty(index))
  252. {
  253. indexList.Add(index);
  254. }
  255. }
  256. if (indexList.Count > 0)
  257. {
  258. foreach (string index in indexList)
  259. {
  260. if (tempSql.Contains("'" + index + "'"))
  261. {
  262. tempSql = tempSql.Replace(index, "");
  263. }
  264. else
  265. {
  266. tempSql = tempSql.Replace(index, "0");
  267. }
  268. }
  269. }
  270. string conn = "";
  271. if (dsource.SysDataFlag)
  272. {
  273. conn = AppConfig.FrameConnectString;
  274. }
  275. else
  276. {
  277. if (dsource.WorkPointDataFlag)
  278. {
  279. conn = AppConfig.AppConnectString;
  280. }
  281. else
  282. {
  283. conn = AppConfig.GetDataBaseConnectString(dsource.OtherDataBaseID);
  284. }
  285. }
  286. DataSet ds = DBHelper.ExecuteDataset(conn, CommandType.Text, tempSql);
  287. int i=0;
  288. foreach (DataTable data in ds.Tables)
  289. {
  290. DataTable newData = data.Clone();
  291. newData.TableName = "table" + i.ToString();
  292. dic.Add(i, newData);
  293. i++;
  294. }
  295. return dic;
  296. }
  297. void PrintingSystem_StartPrint(object sender, PrintDocumentEventArgs e)
  298. {
  299. }
  300. private void BindReport()
  301. {
  302. XtraReport ltdReport = new XtraReport();
  303. string msg = "";
  304. if (_DtSource.Tables.Count == 1)
  305. {
  306. if (_LabelCode == "0005" && _LabelType == "014")
  307. {
  308. string mo = _DtSource.Tables[0].Rows[0]["MoCode"].ToString();
  309. DataTable tableMo = TranOutClass.TranOut(mo);
  310. tableMo.TableName = "Table";
  311. ltdReport.DataSource = tableMo;
  312. ltdReport.DataMember = tableMo.TableName;
  313. }
  314. else if (_LabelCode == "0010" && _LabelType == "016")
  315. {
  316. string mo = _DtSource.Tables[0].Rows[0]["MoCode"].ToString();
  317. DataTable tableMo = TranOutClass.TranOutX(mo);
  318. tableMo.TableName = "Table";
  319. ltdReport.DataSource = tableMo;
  320. ltdReport.DataMember = tableMo.TableName;
  321. }
  322. else
  323. {
  324. ltdReport.DataSource = _DtSource.Tables[0];
  325. ltdReport.DataMember = _DtSource.Tables[0].TableName;
  326. }
  327. }
  328. else if (_DtSource.Tables.Count == 2)
  329. {
  330. if (_LinkCol.Trim() == "")
  331. {
  332. msg += "没有关联字段!\n";
  333. }
  334. else
  335. {
  336. if (!_DtSource.Tables[0].Columns.Contains(_LinkCol))
  337. {
  338. msg += "主表中没有关联字段!\n";
  339. }
  340. else if (!_DtSource.Tables[1].Columns.Contains(_LinkCol))
  341. {
  342. msg += "子表中没有关联字段!\n";
  343. }
  344. else
  345. {
  346. DataColumn parentCol = _DtSource.Tables[0].Columns[_LinkCol];
  347. DataColumn sonCol = _DtSource.Tables[1].Columns[_LinkCol];
  348. DataRelation relatioon = new DataRelation("R1", parentCol, sonCol);
  349. if (!_DtSource.Relations.Contains("R1"))
  350. {
  351. _DtSource.Relations.Add(relatioon);
  352. }
  353. ltdReport.DataMember = _DtSource.Tables[0].TableName;
  354. ltdReport.DataSource = _DtSource;
  355. }
  356. }
  357. }
  358. else
  359. {
  360. msg += "列印数据源错误!\n";
  361. }
  362. if (msg != "")
  363. {
  364. MessageBox.Show( msg);
  365. return;
  366. }
  367. if (!string.IsNullOrEmpty(_PrintFortmatText))
  368. {
  369. byte[] bs2 = System.Text.Encoding.UTF8.GetBytes(_PrintFortmatText);
  370. if (!string.IsNullOrEmpty(_PrintName))
  371. {
  372. ltdReport.PrinterName = _PrintName;
  373. }
  374. MemoryStream steam = new MemoryStream(bs2);
  375. ltdReport.LoadLayout(steam);
  376. printControl1.PrintingSystem = ltdReport.PrintingSystem;
  377. ltdReport.CreateDocument();
  378. }
  379. ltdReport.PrintingSystem.ShowMarginsWarning = printControl1.PrintingSystem.ShowMarginsWarning = false;
  380. ltdReport.PrintingSystem.StartPrint += new PrintDocumentEventHandler(PrintingSystem_StartPrint);
  381. }
  382. }
  383. }