Query_RadiumSN.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. using Seagull.BarTender.Print;
  2. using System;
  3. using System.Data;
  4. using System.Drawing;
  5. using System.Drawing.Printing;
  6. using System.Text.RegularExpressions;
  7. using System.Windows.Forms;
  8. using UAS_MES_NEW.DataOperate;
  9. using UAS_MES_NEW.Entity;
  10. using UAS_MES_NEW.PublicMethod;
  11. namespace UAS_MES_NEW.Query
  12. {
  13. public partial class Query_RadiumSN : Form
  14. {
  15. int serialnum = 0;
  16. //客户的流水号
  17. int custserialnum = 0;
  18. LabelFormatDocument format;
  19. DataHelper dh = SystemInf.dh;
  20. //前缀条件
  21. //生成条码的流水号
  22. DataTable Dbfind;
  23. public Query_RadiumSN()
  24. {
  25. InitializeComponent();
  26. }
  27. private void PrintTest_Click(object sender, EventArgs e)
  28. {
  29. }
  30. private void Print_PrintPage(object sender, PrintPageEventArgs e)
  31. {
  32. Graphics g = e.Graphics;
  33. float leftMargin = 10f; //左边距
  34. SolidBrush myBrush = new SolidBrush(Color.Black);//刷子
  35. float yPosition = 5f;//行定位
  36. Font printFont = new Font("微软雅黑", 20f, FontStyle.Bold);//设置字体
  37. yPosition += printFont.GetHeight(g);//另起一行
  38. g.DrawString("成功连接此打印机", printFont, myBrush, leftMargin, yPosition, new StringFormat());
  39. }
  40. private void Setting_Click(object sender, EventArgs e)
  41. {
  42. }
  43. private void Export_Click(object sender, EventArgs e)
  44. {
  45. if (!dh.CheckExist("make", "ma_code='" + ma_code.Text + "'"))
  46. {
  47. MessageBox.Show("工单号不存在");
  48. return;
  49. }
  50. DataTable dt = (DataTable)dh.ExecuteSql("select msl_sncode from makesnlist where msl_makecode='" + ma_code.Text + "' order by msl_sncode", "select");
  51. if (dt.Rows.Count == 0)
  52. {
  53. MessageBox.Show("工单号未生成SN");
  54. return;
  55. }
  56. }
  57. public string BarcodeMethod1(string Prefix, string Suffix, int Index, int Length, int radix)
  58. {
  59. string str = Prefix;
  60. //如果是流水则需要在前面加0
  61. string serialcode = BaseUtil.DToAny(custserialnum, radix);
  62. for (int j = serialcode.ToString().Length; j < Length; j++)
  63. {
  64. serialcode = "0" + serialcode;
  65. }
  66. str += serialcode;
  67. str += Suffix;
  68. custserialnum = custserialnum + 1;
  69. return str;
  70. }
  71. private static string lpad(int length, string number)
  72. {
  73. while (number.Length < length)
  74. {
  75. number = "0" + number;
  76. }
  77. number = number.Substring(number.Length - length, length);
  78. return number;
  79. }
  80. private void SystemSetting_PrinterTest_Load(object sender, EventArgs e)
  81. {
  82. ma_code.TableName = " make left join product on ma_prodcode=pr_code";
  83. ma_code.SelectField = "ma_code # 工单编号,pr_code # 产品编号,pr_spec # 型号";
  84. ma_code.FormName = Name;
  85. ma_code.SetValueField = new string[] { "ma_code" };
  86. //ma_code.Condition = "ma_statuscode='STARTED'";
  87. ma_code.DbChange += pr_code_DbChange;
  88. }
  89. private void pr_code_DbChange(object sender, EventArgs e)
  90. {
  91. Dbfind = ma_code.ReturnData;
  92. BaseUtil.SetFormValue(this.Controls, Dbfind);
  93. DataTable dt = (DataTable)dh.ExecuteSql("select ma_qty,ma_prodcode,pr_detail from make left join product on ma_prodcode=pr_code where ma_code='" + ma_code.Text + "'", "select");
  94. if (dt.Rows.Count > 0)
  95. {
  96. pr_code.Text = dt.Rows[0]["ma_prodcode"].ToString();
  97. ma_qty.Text = dt.Rows[0]["ma_qty"].ToString();
  98. pr_detail.Text = dt.Rows[0]["pr_detail"].ToString();
  99. }
  100. ma_count.Text = ma_qty.Text;
  101. }
  102. private void GenSN_Click(object sender, EventArgs e)
  103. {
  104. string Prefix = "";
  105. string Suffix = "";
  106. //获取编码规则
  107. if (!dh.CheckExist("make", "ma_code='" + ma_code.Text + "'"))
  108. {
  109. MessageBox.Show("工单号不存在");
  110. return;
  111. }
  112. DataTable dt = (DataTable)dh.ExecuteSql("select pr_exbarcode,ma_qty from make left join product on ma_prodcode=pr_code where ma_code='" + ma_code.Text + "'", "select");
  113. string pr_exbarcode = "";
  114. string ma_qty = "";
  115. if (dt.Rows.Count > 0)
  116. {
  117. pr_exbarcode = dt.Rows[0]["pr_exbarcode"].ToString();
  118. ma_qty = dt.Rows[0]["ma_qty"].ToString();
  119. }
  120. if (dh.getRowCount("makesnlist", "msl_makecode='" + ma_code.Text + "'") + int.Parse(ma_count.Text) > int.Parse(ma_qty))
  121. {
  122. MessageBox.Show("工单号" + ma_code.Text + "生产序列号超出工单数");
  123. return;
  124. }
  125. DataTable Nr = (DataTable)dh.ExecuteSql("select nrd_detno,nrd_name,nrd_type,nrd_radix,nrd_sql,nrd_length from NoRuleDetail left join norule on nrd_nrid=nr_id where nr_code='" + pr_exbarcode + "' order by nrd_detno", "select");
  126. //如果没有则取公共规则
  127. if (Nr.Rows.Count == 0)
  128. Nr = (DataTable)dh.ExecuteSql("select nrd_detno,nrd_name,nrd_radix,nrd_type,nrd_sql,nrd_length from NoRuleDetail left join norule on nrd_nrid=nr_id where nr_custcode is null and nr_isdefault <> 0 order by nrd_detno", "select");
  129. //用于过滤参数的正则表达式
  130. Regex match = new Regex("{\\w+}");
  131. //用于存放每一项的明细的数据
  132. string[] NrData = new string[Nr.Rows.Count];
  133. //流水号的索引
  134. int SerialNumIndex = 0;
  135. //流水长度
  136. int SerialNumLength = 0;
  137. //存放键值对
  138. int Radix = 10;
  139. string PrefixFixed = "";
  140. for (int m = 0; m < Nr.Rows.Count; m++)
  141. {
  142. switch (Nr.Rows[m]["nrd_type"].ToString())
  143. {
  144. //常量直接进行拼接
  145. case "常量":
  146. NrData[m] = Nr.Rows[m]["nrd_sql"].ToString();
  147. Prefix += NrData[m];
  148. Suffix += NrData[m];
  149. break;
  150. case "SQL":
  151. string SQL = Nr.Rows[m]["nrd_sql"].ToString();
  152. DataTable Temp;
  153. //如果不包含参数替换
  154. if (SQL.IndexOf("{") == 0)
  155. {
  156. Temp = (DataTable)dh.ExecuteSql(SQL, "select");
  157. }
  158. else
  159. {
  160. //替换参数后重新执行SQL
  161. foreach (Match mch in match.Matches(SQL))
  162. {
  163. SQL = SQL.Replace(mch.Value.Trim(), "'" + ma_code.Text + "'");
  164. }
  165. Temp = (DataTable)dh.ExecuteSql(SQL, "select");
  166. }
  167. if (Temp.Rows.Count > 0)
  168. {
  169. NrData[m] = Temp.Rows[0][0].ToString();
  170. Prefix += NrData[m];
  171. Suffix += NrData[m];
  172. }
  173. else
  174. {
  175. NrData[m] = "";
  176. Prefix += NrData[m];
  177. Suffix += NrData[m];
  178. }
  179. break;
  180. //流水需要通过MaxNumber去取
  181. case "流水":
  182. NrData[m] = dh.getFieldDataByCondition("RuleMaxNum", "rmn_maxnumber", "rmn_nrcode='" + pr_exbarcode + "'").ToString();
  183. Suffix = "";
  184. PrefixFixed = Prefix;
  185. //设置当前流水
  186. custserialnum = int.Parse(NrData[m] == "" ? "0" : NrData[m]);
  187. SerialNumIndex = m;
  188. SerialNumLength = int.Parse(Nr.Rows[m]["nrd_length"].ToString());
  189. Radix = int.Parse(Nr.Rows[m]["nrd_radix"].ToString());
  190. break;
  191. default:
  192. break;
  193. }
  194. }
  195. //获取最大的流水号
  196. string maxnum = dh.getFieldDataByCondition("RuleMaxNum", "rmn_maxnumber", "rmn_nrcode='" + pr_exbarcode + "' and rmn_prefix='" + Prefix + "'").ToString();
  197. //如果流水号为空则插入一条新记录,从1开始取
  198. if (maxnum == "")
  199. {
  200. dh.ExecuteSql("insert into RuleMaxNum(rmn_id,rmn_nrcode,rmn_prefix,rmn_maxnumber) values(RuleMaxNum_seq.nextval,'" + pr_exbarcode + "','" + Prefix + "','1')", "insert");
  201. custserialnum = 1;
  202. }
  203. //如果流水号不为空则取当前流水
  204. else
  205. {
  206. custserialnum = int.Parse(maxnum);
  207. }
  208. //遍历整个Grid,勾选的项目全部进行条码生成
  209. ArrayList<string> custbarcode = new ArrayList<string>();
  210. for (int i = 0; i < int.Parse(ma_count.Text); i++)
  211. {
  212. string data = BarcodeMethod1(PrefixFixed, Suffix, SerialNumIndex, SerialNumLength, Radix);
  213. if (!custbarcode.Contains(data))
  214. custbarcode.Add(data);
  215. }
  216. //插入条码
  217. string sql = "insert into MAKESNLIST_RADIUM(MSL_ID, MSL_INDATE, MSL_MAKECODE, MSL_SNCODE, MSL_TYPE,msl_status)" +
  218. "select MAKESNLIST_RADIUM_seq.nextval,sysdate,'" + ma_code.Text + "',:custbarcode,'before',0 from dual where :custbarcode not in " +
  219. "(select msl_sncode from MAKESNLIST_RADIUM)";
  220. dh.BatchInsert(sql, new string[] { "custbarcode", "custbarcode" }, custbarcode.ToArray(), custbarcode.ToArray());
  221. //更新最大流水号
  222. dh.UpdateByCondition("RuleMaxNum", "rmn_maxnumber='" + custserialnum + "'", "rmn_nrcode='" + pr_exbarcode + "' and rmn_prefix='" + Prefix + "'");
  223. MessageBox.Show("工单" + ma_code.Text + "生成SN清单成功");
  224. }
  225. }
  226. }