CodeSoftPrintParam.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using System;
  2. using System.Windows.Forms;
  3. using 贴标机标签打印.CustomControl;
  4. using System.Drawing;
  5. using System.Text;
  6. using System.Data;
  7. namespace 贴标机标签打印
  8. {
  9. public partial class CodeSoftPrintParam : Form
  10. {
  11. public CodeSoftPrintParam()
  12. {
  13. InitializeComponent();
  14. }
  15. //模板参数的名称集合
  16. private string[] ParamName;
  17. //标签的客户编号ID
  18. private string Cl_ID;
  19. //Label表的主键,关联LabelParameter查询SQL
  20. private string LA_ID;
  21. private int LabelHeight;
  22. private int LabelWidth;
  23. //打开的标签文件
  24. private Document Doc;
  25. DataTable dt;
  26. DataHelper dh;
  27. StringBuilder sql = new StringBuilder();
  28. public CodeSoftPrintParam(string cl_id, string la_id, string[] paramname, Document doc)
  29. {
  30. InitializeComponent();
  31. ParamName = paramname;
  32. Cl_ID = cl_id;
  33. LA_ID = la_id;
  34. Doc = doc;
  35. LabelHeight = doc.Format.LabelHeight;
  36. LabelWidth = doc.Format.LabelWidth;
  37. }
  38. private void CodeSoftPrintParam_Load(object sender, EventArgs e)
  39. {
  40. dh = new DataHelper();
  41. dt = dh.getFieldsDatasByCondition("labelparameter", new string[] { "lp_id", "lp_name", "lp_sql", "lp_laid" }, "lp_laid=" + (LA_ID == "" ? "0" : LA_ID));
  42. int y = 100;
  43. for (int i = 0; i < ParamName.Length; i++)
  44. {
  45. //变量的名称Label
  46. Label Param = new Label();
  47. Param.Text = ParamName[i];
  48. Param.Anchor = AnchorStyles.Left;
  49. Param.Anchor = AnchorStyles.Top;
  50. Param.Location = new Point(200, y + 5);
  51. this.Controls.Add(Param);
  52. //维护变量的打印测试参数
  53. EnterTextBox TempParam = new EnterTextBox();
  54. TempParam.Anchor = AnchorStyles.Left;
  55. TempParam.Anchor = AnchorStyles.Top;
  56. TempParam.Name = "Temp" + ParamName[i];
  57. TempParam.Text = Doc.Variables.FreeVariables.Item(ParamName[i]).Value;
  58. TempParam.Location = new Point(300, y);
  59. y = y + 30;
  60. TempParam.Size = new Size(150, 22);
  61. this.Controls.Add(TempParam);
  62. //变量的SQL标签
  63. Label ParamSQL = new Label();
  64. ParamSQL.Text = ParamName[i] + "取值SQL";
  65. ParamSQL.Anchor = AnchorStyles.Left;
  66. ParamSQL.Anchor = AnchorStyles.Top;
  67. ParamSQL.AutoSize = false;
  68. ParamSQL.Location = new Point(200, y + 5);
  69. this.Controls.Add(ParamSQL);
  70. //维护SQL输入框
  71. EnterTextBox SQL = new EnterTextBox();
  72. SQL.Anchor = AnchorStyles.Left;
  73. SQL.Anchor = AnchorStyles.Top;
  74. SQL.Name = ParamName[i];
  75. for (int j = 0; j < dt.Rows.Count; j++)
  76. {
  77. if (dt.Rows[j]["lp_name"].ToString() == ParamName[i])
  78. {
  79. SQL.Text = dt.Rows[j]["lp_sql"].ToString();
  80. //只要维护个这个参数就会有lp_laid这个属性,判断该SQL是否需要插入
  81. SQL.Tag = dt.Rows[j]["lp_id"].ToString();
  82. }
  83. }
  84. SQL.Location = new Point(300, y);
  85. this.Controls.Add(SQL);
  86. SQL.Size = new Size(250, 22);
  87. NormalButton Test = new NormalButton();
  88. Test.Text = "检测";
  89. Test.Tag = ParamName[i];
  90. Test.Click += CheckSQL;
  91. Test.Location = new Point(570, y);
  92. Test.Size = new Size(60, 22);
  93. this.Controls.Add(Test);
  94. y = y + 30;
  95. }
  96. NormalButton nb = new NormalButton();
  97. nb.Text = "保存";
  98. nb.Click += Save_Click;
  99. nb.Location = new Point(330, y + 10);
  100. nb.Size = new Size(80, 22);
  101. this.Controls.Add(nb);
  102. Height = y + 150;
  103. }
  104. private void Save_Click(object sender, EventArgs e)
  105. {
  106. //将标签信息插入Label表,将SQL和标签其他信息插入LabelParameter
  107. //如果此标签没有维护过则LA_ID字段的值为空
  108. string[] psql = new string[ParamName.Length];
  109. for (int i = 0; i < ParamName.Length; i++)
  110. {
  111. Doc.Variables.FreeVariables.Item(ParamName[i]).Value = "Temp" + Controls[ParamName[i]].Text;
  112. psql[i] = Controls[ParamName[i]].Text;
  113. }
  114. //未维护参数的时候从序列取一个新的ID
  115. if (LA_ID == "" || LA_ID == null)
  116. {
  117. string la_id = dh.GetSEQ("label_seq");
  118. dh.ExecuteSql("insert into label(la_id,la_code,la_name,la_indate,la_inman) select '" + la_id + "',cl_custcode,cu_name,sysdate,'管理员' from customerlabel left join customer on cu_code=cl_custcode where cl_id=" + Cl_ID, "insert");
  119. sql.Clear();
  120. sql.Append("insert into labelparameter (lp_id,lp_laid,lp_name,lp_software,lp_height,lp_width,lp_sql) ");
  121. sql.Append("values(labelparameter_seq.nextval,'" + la_id + "',:lp_name,'CodeSoft','" + LabelHeight + "','" + LabelWidth + "',:lp_sql)");
  122. //保存标签参数的值和获取需要插入的SQL的数组
  123. dh.BatchInsert(sql.ToString(), new string[] { "lp_name", "lp_sql" }, ParamName, psql);
  124. }
  125. //如果维护过变标签的时候就使用传进来的LA_ID作为参数
  126. if (LA_ID != "")
  127. {
  128. //存在ID直接更新的数据
  129. ArrayList<string> UpdateName = new ArrayList<string>();
  130. ArrayList<string> UpdateSQL = new ArrayList<string>();
  131. ArrayList<string> UpdateID = new ArrayList<string>();
  132. //不存在ID需要新插入的数据
  133. ArrayList<string> InsertName = new ArrayList<string>();
  134. ArrayList<string> InsertSQL = new ArrayList<string>();
  135. for (int i = 0; i < ParamName.Length; i++)
  136. {
  137. //ID为空需要插入的数据
  138. if (Controls[ParamName[i]].Tag == null || Controls[ParamName[i]].Tag.ToString() == "")
  139. {
  140. InsertName.Add(ParamName[i]);
  141. InsertSQL.Add(Controls[ParamName[i]].Text);
  142. }
  143. //存在ID需要更新的数据
  144. else {
  145. UpdateName.Add(ParamName[i]);
  146. UpdateSQL.Add(Controls[ParamName[i]].Text);
  147. UpdateID.Add(Controls[ParamName[i]].Tag.ToString());
  148. }
  149. }
  150. //保存标签参数的值和获取需要插入的SQL的数组
  151. sql.Clear();
  152. sql.Append("insert into labelparameter (lp_id,lp_laid,lp_name,lp_software,lp_height,lp_width,lp_sql) ");
  153. sql.Append("values(labelparameter_seq.nextval,'" + LA_ID + "',:lp_name,'CodeSoft','" + LabelHeight + "','" + LabelWidth + "',:lp_sql)");
  154. dh.BatchInsert(sql.ToString(), new string[] { "lp_name", "lp_sql" }, InsertName.ToArray(), InsertSQL.ToArray());
  155. //更新存在ID的参数
  156. sql.Clear();
  157. sql.Append("update labelparameter set lp_sql=:lp_sql , lp_name=:lp_name where lp_laid=:lp_laid");
  158. dh.BatchInsert(sql.ToString(), new string[] { "lp_sql", "lp_name", "lp_laid" }, UpdateSQL.ToArray(), UpdateName.ToArray(), UpdateID.ToArray());
  159. }
  160. Doc.Save();
  161. MessageBox.Show("保存成功");
  162. }
  163. //通过Tag获取到输入框的SQL,执行测试的方法
  164. private void CheckSQL(object sender, EventArgs e)
  165. {
  166. NormalButton nbt = sender as NormalButton;
  167. string ErrorMessage = "";
  168. if (dh.CheckSQL(this.Controls[nbt.Tag.ToString()].Text, out ErrorMessage))
  169. {
  170. MessageBox.Show("检测通过");
  171. }
  172. else
  173. {
  174. MessageBox.Show(ErrorMessage);
  175. }
  176. }
  177. }
  178. }