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

376 lines
12 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.XtraReports.UserDesigner;
  9. using DevExpress.XtraReports.UI;
  10. using DevExpress.XtraReports.UserDesigner.Native;
  11. using System.IO;
  12. using DevExpress.Data;
  13. using System.Drawing.Design;
  14. using DevExpress.XtraEditors;
  15. using DevExpress.XtraPrinting;
  16. using System.Data.Linq;
  17. using System.Linq;
  18. using System.Text.RegularExpressions;
  19. using ICSSoft.Base.Config.AppConfig;
  20. using ICSSoft.Base.Lable.Entity;
  21. using ICSSoft.Base.Config.DBHelper;
  22. namespace ICSSoft.Base.Lable.PrintTool
  23. {
  24. public partial class FormPrintDesigner : XRDesignFormExBase
  25. {
  26. private DataSet _DtSource { get; set; }
  27. private string _PrintFortmatText { get; set; }
  28. private string _LinkCols = "";
  29. private string _PrintId = "";
  30. private string _UserID = "";
  31. public FormPrintDesigner(string tempId)
  32. {
  33. InitializeComponent();
  34. _PrintId = tempId;
  35. _UserID = AppConfig.UserId;
  36. Sys_Lables obj = GetPrintTemp();
  37. Sys_LableDataSource dsource=GetLableDataSource(obj.LableSourceID);
  38. string tempSql = dsource.SqlTxt;
  39. if (tempSql.Contains("[AppConfig.WorkPointCode]"))
  40. {
  41. tempSql = tempSql.Replace("[AppConfig.WorkPointCode]", AppConfig.WorkPointCode);
  42. }
  43. if (tempSql.Contains("[AppConfig.UserId]"))
  44. {
  45. tempSql = tempSql.Replace("[AppConfig.UserId]", AppConfig.UserId);
  46. }
  47. if (tempSql.Contains("[AppConfig.UserCode]"))
  48. {
  49. tempSql = tempSql.Replace("[AppConfig.UserCode]", AppConfig.UserCode);
  50. }
  51. if (tempSql.Contains("[AppConfig.UserName]"))
  52. {
  53. tempSql = tempSql.Replace("[AppConfig.UserName]", AppConfig.UserName);
  54. }
  55. if (tempSql.Contains("[AppConfig.WorkPointName]"))
  56. {
  57. tempSql = tempSql.Replace("[AppConfig.WorkPointName]", AppConfig.WorkPointName);
  58. }
  59. Dictionary<string, string> dbDic = AppConfig.GetDataBaseNames();
  60. foreach (string key in dbDic.Keys)
  61. {
  62. if (tempSql.Contains(key))
  63. {
  64. tempSql = tempSql.Replace(key, dbDic[key]);
  65. }
  66. }
  67. string[] indexs = dsource.Paras.Split(',');
  68. List<string> indexList = new List<string>();
  69. foreach (string index in indexs)
  70. {
  71. if (!string.IsNullOrEmpty(index))
  72. {
  73. indexList.Add(index);
  74. }
  75. }
  76. if (indexList.Count > 0)
  77. {
  78. foreach (string index in indexList)
  79. {
  80. if (tempSql.Contains("'" + index + "'"))
  81. {
  82. tempSql = tempSql.Replace(index, "");
  83. }
  84. else
  85. {
  86. tempSql = tempSql.Replace(index, "0");
  87. }
  88. }
  89. }
  90. string conn = "";
  91. if (dsource.SysDataFlag)
  92. {
  93. conn = AppConfig.FrameConnectString;
  94. }
  95. else
  96. {
  97. if (dsource.WorkPointDataFlag)
  98. {
  99. conn = AppConfig.AppConnectString;
  100. }
  101. else
  102. {
  103. conn= AppConfig.GetDataBaseConnectString(dsource.OtherDataBaseID);
  104. }
  105. }
  106. _DtSource = DBHelper.ExecuteDataset(conn,CommandType.Text,tempSql);
  107. _PrintFortmatText = obj==null?"":obj.LableXml;
  108. _LinkCols = dsource.LinkCols;
  109. this.xrDesignPanel.AddCommandHandler(new SaveCommandHandler(this.xrDesignPanel,_PrintId,AppConfig.FrameConnectString,_UserID) );
  110. }
  111. /// <summary>
  112. /// ��ȡģ����Ϣ
  113. /// </summary>
  114. /// <returns></returns>
  115. private Sys_Lables GetPrintTemp()
  116. {
  117. using (LableDataContext db = new LableDataContext(AppConfig.FrameConnectString))
  118. {
  119. Sys_Lables obj = db.Sys_Lables.SingleOrDefault(a => a.ID == _PrintId);
  120. return obj;
  121. }
  122. }
  123. private Sys_LableDataSource GetLableDataSource(string ID)
  124. {
  125. using (LableDataContext db = new LableDataContext(AppConfig.FrameConnectString))
  126. {
  127. Sys_LableDataSource obj = db.Sys_LableDataSource.SingleOrDefault(a => a.ID == ID);
  128. return obj;
  129. }
  130. }
  131. /// <summary>
  132. /// Load the report into the designer
  133. /// </summary>
  134. public void BindReport()
  135. {
  136. XtraReport ltdReport = new XtraReport();
  137. string msg = "";
  138. if (_DtSource.Tables.Count == 1)
  139. {
  140. ltdReport.DataSource = _DtSource.Tables[0];
  141. ltdReport.DataMember = _DtSource.Tables[0].TableName;
  142. }
  143. else if (_DtSource.Tables.Count > 1)
  144. {
  145. if (_LinkCols.Trim() == "")
  146. {
  147. msg += "û�й����ֶΣ�\n";
  148. }
  149. else
  150. {
  151. for (int i = 0; i < _DtSource.Tables.Count; i++)
  152. {
  153. if (!_DtSource.Tables[i].Columns.Contains(_LinkCols))
  154. {
  155. msg += "��:" + _DtSource.Tables[i] .TableName+ "û�й����ֶΣ�\n";
  156. }
  157. }
  158. }
  159. DataColumn parentCol = _DtSource.Tables[0].Columns[_LinkCols];
  160. if (msg == "")
  161. {
  162. for (int i = 0; i < _DtSource.Tables.Count; i++)
  163. {
  164. if (i == 0)
  165. continue;
  166. DataColumn sonCol = _DtSource.Tables[i].Columns[_LinkCols];
  167. DataRelation relatioon = new DataRelation("R"+i.ToString(), parentCol, sonCol);
  168. if (!_DtSource.Relations.Contains("R"+i.ToString()))
  169. {
  170. _DtSource.Relations.Add(relatioon);
  171. }
  172. }
  173. ltdReport.DataMember = _DtSource.Tables[0].TableName;
  174. ltdReport.DataSource = _DtSource;
  175. }
  176. }
  177. else
  178. {
  179. msg += "��ӡ����Դ������\n";
  180. }
  181. if (msg != "")
  182. {
  183. MessageBox.Show( msg);
  184. return;
  185. }
  186. if (!string.IsNullOrEmpty(_PrintFortmatText))
  187. {
  188. byte[] bs2 = System.Text.Encoding.UTF8.GetBytes(_PrintFortmatText);
  189. MemoryStream steam = new MemoryStream(bs2);
  190. ltdReport.LoadLayout(steam);
  191. ltdReport.CreateDocument();
  192. }
  193. xrDesignPanel.OpenReport( ltdReport );
  194. commandBarItem32.Enabled = false;
  195. }
  196. ///// <summary>
  197. ///// Hide data source properties
  198. ///// </summary>
  199. ///// <param name="sender"></param>
  200. ///// <param name="e"></param>
  201. //void LTDReport_FilterControlProperties( object sender, FilterControlPropertiesEventArgs e )
  202. //{
  203. // if ( e.Properties.Contains( "DataSource" ))
  204. // e.Properties.Remove( "DataSource" );
  205. // if ( e.Properties.Contains( "DataMember" ) )
  206. // e.Properties.Remove( "DataMember" );
  207. // if ( e.Properties.Contains( "DataAdapter" ) )
  208. // e.Properties.Remove( "DataAdapter" );
  209. //}
  210. private void commandBarItem31_ItemClick( object sender, DevExpress.XtraBars.ItemClickEventArgs e )
  211. {
  212. BindReport();
  213. }
  214. private void xrDesignPanel_DesignerHostLoaded( object sender, DesignerLoadedEventArgs e )
  215. {
  216. IToolboxService ts = ( IToolboxService )e.DesignerHost.GetService( typeof( IToolboxService ) );
  217. ToolboxItem devExpressSubReport = null;
  218. // Add a custom control.
  219. ToolboxItemCollection toolboxItems = ts.GetToolboxItems();
  220. foreach( ToolboxItem toolboxItem in toolboxItems )
  221. {
  222. if ( toolboxItem.TypeName == "DevExpress.XtraReports.UI.XRSubreport" )
  223. {
  224. devExpressSubReport = toolboxItem;
  225. break;
  226. }
  227. }
  228. if ( devExpressSubReport != null )
  229. ts.RemoveToolboxItem( devExpressSubReport );
  230. }
  231. private void FormRepDesign_Load(object sender, EventArgs e)
  232. {
  233. BindReport();
  234. }
  235. private void FormRepDesign_FormClosed(object sender, FormClosedEventArgs e)
  236. {
  237. //MemoryStream layoutStream = new MemoryStream();
  238. //xrDesignPanel.Report.SaveLayout(layoutStream);
  239. //xrDesignPanel.ReportState = ReportState.Saved;
  240. //// StreamReader readStream = new StreamReader(layoutStream, Encoding.UTF8);
  241. //string txt = System.Text.Encoding.UTF8.GetString(layoutStream.ToArray());
  242. ////readStream.Close();
  243. //_PrintFortmatText = txt;
  244. }
  245. }
  246. /// <summary>
  247. /// Command Handler to overwrite the save event
  248. /// </summary>
  249. public class SaveCommandHandler : DevExpress.XtraReports.UserDesigner.ICommandHandler
  250. {
  251. XRDesignPanel panel;
  252. private string _PrintId = "";
  253. private string _ConnectString = "";
  254. private string _UserCode = "";
  255. public SaveCommandHandler( XRDesignPanel panel,string pId,string connectString ,string userCode)
  256. {
  257. this.panel = panel;
  258. _PrintId = pId;
  259. _ConnectString = connectString;
  260. _UserCode = userCode;
  261. }
  262. public void HandleCommand(ReportCommand command, object[] args)
  263. {
  264. bool useNexHadle = true ;
  265. if (!CanHandleCommand(command, ref useNexHadle))
  266. return;
  267. // Save a report.
  268. if ( panel.ReportState == ReportState.Changed && command == ReportCommand.Closing )
  269. {
  270. Save();
  271. }
  272. else if (command != ReportCommand.Closing)
  273. {
  274. if ( panel.ReportState == ReportState.Changed )
  275. Save();
  276. }
  277. // handled = true;
  278. }
  279. public bool CanHandleCommand(ReportCommand command, ref bool useNextHandler)
  280. {
  281. // This handler is used for SaveFile, SaveFileAs and Closing commands.
  282. return command == ReportCommand.SaveFile ||
  283. command == ReportCommand.SaveFileAs ||
  284. command == ReportCommand.Closing;
  285. }
  286. void Save()
  287. {
  288. if (MessageBox.Show( "�Ƿ񱣴��޸ģ�","��ʾ",MessageBoxButtons.YesNo,MessageBoxIcon.Information) == DialogResult.No)
  289. return;
  290. LableDataContext db = new LableDataContext(_ConnectString);
  291. db.Connection.Open();
  292. db.Transaction = db.Connection.BeginTransaction();
  293. try
  294. {
  295. var temp = db.Sys_Lables.SingleOrDefault(a => a.ID == _PrintId);
  296. if (temp == null)
  297. {
  298. MessageBox.Show("ģ�岻�������ݿ��У��޷��������£�");
  299. }
  300. else
  301. {
  302. string txt = "";
  303. using (MemoryStream layoutStream = new MemoryStream())
  304. {
  305. panel.Report.SaveLayout(layoutStream);
  306. txt = System.Text.Encoding.UTF8.GetString(layoutStream.ToArray());
  307. }
  308. temp.LableXml = txt;
  309. temp.CreateDate =DateTime.Now;
  310. temp.CreateUserID =_UserCode;
  311. db.SubmitChanges();
  312. db.Transaction.Commit();
  313. panel.ReportState = ReportState.Saved;
  314. }
  315. }
  316. catch (Exception ex)
  317. {
  318. MessageBox.Show( "�쳣:" + ex.Message);
  319. db.Transaction.Rollback();
  320. }
  321. }
  322. #region ICommandHandler Members
  323. #endregion
  324. }
  325. }