Query_SN.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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_SN : Form
  14. {
  15. int serialnum = 0;
  16. //客户的流水号
  17. int custserialnum = 0;
  18. Engine engine = new Engine();
  19. LabelFormatDocument format;
  20. DataHelper dh = SystemInf.dh;
  21. //前缀条件
  22. //生成条码的流水号
  23. DataTable Dbfind;
  24. public Query_SN()
  25. {
  26. InitializeComponent();
  27. }
  28. private void PrintTest_Click(object sender, EventArgs e)
  29. {
  30. PrintDocument print = new PrintDocument();
  31. print.PrinterSettings.PrinterName = PrinterList.Text;
  32. print.PrintPage += Print_PrintPage;
  33. print.Print();
  34. }
  35. private void Print_PrintPage(object sender, PrintPageEventArgs e)
  36. {
  37. Graphics g = e.Graphics;
  38. float leftMargin = 10f; //左边距
  39. SolidBrush myBrush = new SolidBrush(Color.Black);//刷子
  40. float yPosition = 5f;//行定位
  41. Font printFont = new Font("微软雅黑", 20f, FontStyle.Bold);//设置字体
  42. yPosition += printFont.GetHeight(g);//另起一行
  43. g.DrawString("成功连接此打印机", printFont, myBrush, leftMargin, yPosition, new StringFormat());
  44. }
  45. private void Setting_Click(object sender, EventArgs e)
  46. {
  47. PrintDialog printd = new PrintDialog();
  48. printd.PrinterSettings.PrinterName = PrinterList.Text;
  49. printd.ShowDialog();
  50. }
  51. private void Export_Click(object sender, EventArgs e)
  52. {
  53. if (!dh.CheckExist("make", "ma_code='" + ma_code.Text + "'"))
  54. {
  55. MessageBox.Show("工单号不存在");
  56. return;
  57. }
  58. DataTable dt = (DataTable)dh.ExecuteSql("select msl_sncode from makesnlist where msl_makecode='" + ma_code.Text + "' order by msl_sncode", "select");
  59. if (dt.Rows.Count == 0)
  60. {
  61. MessageBox.Show("工单号未生成SN");
  62. return;
  63. }
  64. if (OneColumn.Checked)
  65. {
  66. format = engine.Documents.Open(Application.StartupPath + "/单排.btw");
  67. for (int i = 0; i < dt.Rows.Count; i = i + 1)
  68. {
  69. int temp = i;
  70. for (int j = 0; j < format.SubStrings.Count; j++)
  71. {
  72. switch (format.SubStrings[j].Name)
  73. {
  74. case "SN":
  75. format.SubStrings[j].Value = dt.Rows[i]["msl_sncode"].ToString();
  76. break;
  77. default:
  78. break;
  79. }
  80. }
  81. format.PrintSetup.PrinterName = PrinterList.Text;
  82. format.PrintSetup.IdenticalCopiesOfLabel = 1;
  83. format.Print();
  84. }
  85. }
  86. if (TwoColumn.Checked)
  87. {
  88. format = engine.Documents.Open(Application.StartupPath + "/双排.btw");
  89. for (int i = 0; i < dt.Rows.Count; i = i + 2)
  90. {
  91. int temp = i;
  92. for (int j = 0; j < format.SubStrings.Count; j++)
  93. {
  94. switch (format.SubStrings[j].Name)
  95. {
  96. case "SN":
  97. format.SubStrings[j].Value = dt.Rows[i]["msl_sncode"].ToString();
  98. break;
  99. case "SN2":
  100. format.SubStrings[j].Value = dt.Rows[i + 1]["msl_sncode"].ToString();
  101. break;
  102. default:
  103. break;
  104. }
  105. }
  106. format.PrintSetup.PrinterName = PrinterList.Text;
  107. format.PrintSetup.IdenticalCopiesOfLabel = 1;
  108. format.Print();
  109. }
  110. }
  111. if (ThreeColumn.Checked)
  112. {
  113. format = engine.Documents.Open(Application.StartupPath + "/Label.btw");
  114. for (int i = 0; i < dt.Rows.Count; i = i + 3)
  115. {
  116. int temp = i;
  117. for (int j = 0; j < format.SubStrings.Count; j++)
  118. {
  119. switch (format.SubStrings[j].Name)
  120. {
  121. case "SN":
  122. format.SubStrings[j].Value = dt.Rows[i]["msl_sncode"].ToString();
  123. break;
  124. case "SN2":
  125. format.SubStrings[j].Value = dt.Rows[i + 1]["msl_sncode"].ToString();
  126. break;
  127. case "SN3":
  128. format.SubStrings[j].Value = dt.Rows[i + 2]["msl_sncode"].ToString();
  129. break;
  130. default:
  131. break;
  132. }
  133. }
  134. format.PrintSetup.PrinterName = PrinterList.Text;
  135. format.PrintSetup.IdenticalCopiesOfLabel = 1;
  136. format.Print();
  137. }
  138. }
  139. if (FourColumn.Checked)
  140. {
  141. format = engine.Documents.Open(Application.StartupPath + "/四排.btw");
  142. for (int i = 0; i < dt.Rows.Count; i = i + 4)
  143. {
  144. int temp = i;
  145. for (int j = 0; j < format.SubStrings.Count; j++)
  146. {
  147. switch (format.SubStrings[j].Name)
  148. {
  149. case "SN":
  150. format.SubStrings[j].Value = dt.Rows[i]["msl_sncode"].ToString();
  151. break;
  152. case "SN2":
  153. format.SubStrings[j].Value = dt.Rows[i + 1]["msl_sncode"].ToString();
  154. break;
  155. case "SN3":
  156. format.SubStrings[j].Value = dt.Rows[i + 2]["msl_sncode"].ToString();
  157. break;
  158. case "SN4":
  159. format.SubStrings[j].Value = dt.Rows[i + 3]["msl_sncode"].ToString();
  160. break;
  161. default:
  162. break;
  163. }
  164. }
  165. format.PrintSetup.PrinterName = PrinterList.Text;
  166. format.PrintSetup.IdenticalCopiesOfLabel = 1;
  167. format.Print();
  168. }
  169. }
  170. }
  171. public string BarcodeMethod1(string Prefix, string Suffix, int Index, int Length, int radix,string char_out)
  172. {
  173. string str = Prefix;
  174. //如果是流水则需要在前面加0
  175. string serialcode = BaseUtil.DToAny(custserialnum, radix, char_out);
  176. for (int j = serialcode.ToString().Length; j < Length; j++)
  177. {
  178. serialcode = "0" + serialcode;
  179. }
  180. str += serialcode;
  181. str += Suffix;
  182. custserialnum = custserialnum + 1;
  183. return str;
  184. }
  185. private static string lpad(int length, string number)
  186. {
  187. while (number.Length < length)
  188. {
  189. number = "0" + number;
  190. }
  191. number = number.Substring(number.Length - length, length);
  192. return number;
  193. }
  194. private void SystemSetting_PrinterTest_Load(object sender, EventArgs e)
  195. {
  196. engine.Start();
  197. ma_code.TableName = " make left join product on ma_prodcode=pr_code";
  198. ma_code.SelectField = "ma_code # 工单编号,pr_code # 产品编号,pr_spec # 型号";
  199. ma_code.FormName = Name;
  200. ma_code.SetValueField = new string[] { "ma_code" };
  201. //ma_code.Condition = "ma_statuscode='STARTED'";
  202. ma_code.DbChange += pr_code_DbChange;
  203. }
  204. private void pr_code_DbChange(object sender, EventArgs e)
  205. {
  206. Dbfind = ma_code.ReturnData;
  207. BaseUtil.SetFormValue(this.Controls, Dbfind);
  208. 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");
  209. if (dt.Rows.Count > 0)
  210. {
  211. pr_code.Text = dt.Rows[0]["ma_prodcode"].ToString();
  212. ma_qty.Text = dt.Rows[0]["ma_qty"].ToString();
  213. pr_detail.Text = dt.Rows[0]["pr_detail"].ToString();
  214. }
  215. ma_count.Text = ma_qty.Text;
  216. }
  217. private void GenSN_Click(object sender, EventArgs e)
  218. {
  219. string Prefix = "";
  220. string Suffix = "";
  221. //获取编码规则
  222. if (!dh.CheckExist("make", "ma_code='" + ma_code.Text + "'"))
  223. {
  224. MessageBox.Show("工单号不存在");
  225. return;
  226. }
  227. 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");
  228. string pr_exbarcode = "";
  229. string ma_qty = "";
  230. if (dt.Rows.Count > 0)
  231. {
  232. pr_exbarcode = dt.Rows[0]["pr_exbarcode"].ToString();
  233. ma_qty = dt.Rows[0]["ma_qty"].ToString();
  234. }
  235. if (dh.getRowCount("makesnlist", "msl_makecode='" + ma_code.Text + "'") + int.Parse(ma_count.Text) > int.Parse(ma_qty))
  236. {
  237. MessageBox.Show("工单号" + ma_code.Text + "生产序列号超出工单数");
  238. return;
  239. }
  240. DataTable Nr = (DataTable)dh.ExecuteSql("select nrd_detno,nrd_name,nrd_type,nrd_radix,nrd_sql,nrd_length,nrd_content,nvl(nr_changenum,0)nr_changenum from NoRuleDetail left join norule on nrd_nrid=nr_id where nr_code='" + pr_exbarcode + "' order by nrd_detno", "select");
  241. //如果没有则取公共规则
  242. if (Nr.Rows.Count == 0)
  243. Nr = (DataTable)dh.ExecuteSql("select nrd_detno,nrd_name,nrd_radix,nrd_type,nrd_sql,nrd_length,nrd_content,nvl(nr_changenum,0)nr_changenum from NoRuleDetail left join norule on nrd_nrid=nr_id where nr_custcode is null and nr_isdefault <> 0 order by nrd_detno", "select");
  244. //用于过滤参数的正则表达式
  245. Regex match = new Regex("{\\w+}");
  246. //用于存放每一项的明细的数据
  247. string[] NrData = new string[Nr.Rows.Count];
  248. //流水号的索引
  249. int SerialNumIndex = 0;
  250. //流水长度
  251. int SerialNumLength = 0;
  252. //存放键值对
  253. int Radix = 10;
  254. string PrefixFixed = "";
  255. string char_out = "";
  256. string nr_changenum = Nr.Rows[0]["nr_changenum"].ToString();
  257. //如果是
  258. if (nr_changenum != "-1")
  259. {
  260. for (int m = 0; m < Nr.Rows.Count; m++)
  261. {
  262. switch (Nr.Rows[m]["nrd_type"].ToString())
  263. {
  264. //常量直接进行拼接
  265. case "常量":
  266. NrData[m] = Nr.Rows[m]["nrd_sql"].ToString();
  267. Prefix += NrData[m];
  268. Suffix += NrData[m];
  269. break;
  270. case "SQL":
  271. string SQL = Nr.Rows[m]["nrd_sql"].ToString();
  272. DataTable Temp;
  273. //如果不包含参数替换
  274. if (SQL.IndexOf("{") == 0)
  275. {
  276. Temp = (DataTable)dh.ExecuteSql(SQL, "select");
  277. }
  278. else
  279. {
  280. //替换参数后重新执行SQL
  281. foreach (Match mch in match.Matches(SQL))
  282. {
  283. SQL = SQL.Replace(mch.Value.Trim(), "'" + ma_code.Text + "'");
  284. }
  285. Temp = (DataTable)dh.ExecuteSql(SQL, "select");
  286. }
  287. if (Temp.Rows.Count > 0)
  288. {
  289. NrData[m] = Temp.Rows[0][0].ToString();
  290. Prefix += NrData[m];
  291. Suffix += NrData[m];
  292. }
  293. else
  294. {
  295. NrData[m] = "";
  296. Prefix += NrData[m];
  297. Suffix += NrData[m];
  298. }
  299. break;
  300. //流水需要通过MaxNumber去取
  301. case "流水":
  302. NrData[m] = dh.getFieldDataByCondition("RuleMaxNum", "rmn_maxnumber", "rmn_nrcode='" + pr_exbarcode + "'").ToString();
  303. Suffix = "";
  304. PrefixFixed = Prefix;
  305. //设置当前流水
  306. custserialnum = int.Parse(NrData[m] == "" ? "0" : NrData[m]);
  307. SerialNumIndex = m;
  308. SerialNumLength = int.Parse(Nr.Rows[m]["nrd_length"].ToString());
  309. Radix = int.Parse(Nr.Rows[m]["nrd_radix"].ToString());
  310. char_out = Nr.Rows[m]["nrd_content"].ToString();
  311. break;
  312. default:
  313. break;
  314. }
  315. }
  316. //获取最大的流水号
  317. string maxnum = dh.getFieldDataByCondition("RuleMaxNum", "rmn_maxnumber", "rmn_nrcode='" + pr_exbarcode + "' and rmn_prefix='" + Prefix + "'").ToString();
  318. //如果流水号为空则插入一条新记录,从1开始取
  319. if (maxnum == "")
  320. {
  321. dh.ExecuteSql("insert into RuleMaxNum(rmn_id,rmn_nrcode,rmn_prefix,rmn_maxnumber) values(RuleMaxNum_seq.nextval,'" + pr_exbarcode + "','" + Prefix + "','1')", "insert");
  322. custserialnum = 1;
  323. }
  324. //如果流水号不为空则取当前流水
  325. else
  326. {
  327. custserialnum = int.Parse(maxnum);
  328. }
  329. //遍历整个Grid,勾选的项目全部进行条码生成
  330. ArrayList<string> custbarcode = new ArrayList<string>();
  331. for (int i = 0; i < int.Parse(ma_count.Text); i++)
  332. {
  333. string data = BarcodeMethod1(PrefixFixed, Suffix, SerialNumIndex, SerialNumLength, Radix, char_out);
  334. if (!custbarcode.Contains(data))
  335. custbarcode.Add(data);
  336. }
  337. string sql = "insert into makesnlist(MSL_ID, MSL_INDATE, MSL_MAKECODE, MSL_SNCODE, MSL_TYPE,msl_status)" +
  338. "select makesnlist_seq.nextval,sysdate,'" + ma_code.Text + "',:custbarcode,'before',0 from dual where :custbarcode not in " +
  339. "(select msl_sncode from makesnlist)";
  340. dh.BatchInsert(sql, new string[] { "custbarcode", "custbarcode" }, custbarcode.ToArray(), custbarcode.ToArray());
  341. //更新最大流水号
  342. dh.UpdateByCondition("RuleMaxNum", "rmn_maxnumber='" + custserialnum + "'", "rmn_nrcode='" + pr_exbarcode + "' and rmn_prefix='" + Prefix + "'");
  343. }
  344. else {
  345. for (int m = 0; m < Nr.Rows.Count; m++)
  346. {
  347. switch (Nr.Rows[m]["nrd_type"].ToString())
  348. {
  349. //常量直接进行拼接
  350. case "常量":
  351. NrData[m] = Nr.Rows[m]["nrd_sql"].ToString();
  352. Prefix += NrData[m];
  353. Suffix += NrData[m];
  354. break;
  355. case "SQL":
  356. string SQL = Nr.Rows[m]["nrd_sql"].ToString();
  357. DataTable Temp;
  358. //如果不包含参数替换
  359. if (SQL.IndexOf("{") == 0)
  360. {
  361. Temp = (DataTable)dh.ExecuteSql(SQL, "select");
  362. }
  363. else
  364. {
  365. //替换参数后重新执行SQL
  366. foreach (Match mch in match.Matches(SQL))
  367. {
  368. SQL = SQL.Replace(mch.Value.Trim(), "'" + ma_code.Text + "'");
  369. }
  370. Temp = (DataTable)dh.ExecuteSql(SQL, "select");
  371. }
  372. if (Temp.Rows.Count > 0)
  373. {
  374. NrData[m] = Temp.Rows[0][0].ToString();
  375. Prefix += NrData[m];
  376. Suffix += NrData[m];
  377. }
  378. else
  379. {
  380. NrData[m] = "";
  381. Prefix += NrData[m];
  382. Suffix += NrData[m];
  383. }
  384. break;
  385. //流水需要通过MaxNumber去取
  386. case "流水":
  387. NrData[m] = dh.getFieldDataByCondition("RuleMaxNum", "rmn_maxnumber", "rmn_nrcode='" + pr_exbarcode + "'").ToString();
  388. Suffix = "";
  389. PrefixFixed = Prefix;
  390. //设置当前流水
  391. custserialnum = int.Parse(NrData[m] == "" ? "0" : NrData[m]);
  392. SerialNumIndex = m;
  393. SerialNumLength = int.Parse(Nr.Rows[m]["nrd_length"].ToString());
  394. Radix = int.Parse(Nr.Rows[m]["nrd_radix"].ToString());
  395. char_out = Nr.Rows[m]["nrd_content"].ToString();
  396. break;
  397. default:
  398. break;
  399. }
  400. }
  401. //获取最大的流水号
  402. string maxnum = dh.getFieldDataByCondition("RuleMaxNum", "rmn_maxnumber", "rmn_nrcode='" + pr_exbarcode + "'").ToString();
  403. //如果流水号为空则插入一条新记录,从1开始取
  404. if (maxnum == "")
  405. {
  406. dh.ExecuteSql("insert into RuleMaxNum(rmn_id,rmn_nrcode,rmn_maxnumber) values(RuleMaxNum_seq.nextval,'" + pr_exbarcode + "','1')", "insert");
  407. custserialnum = 1;
  408. }
  409. //如果流水号不为空则取当前流水
  410. else
  411. {
  412. custserialnum = int.Parse(maxnum);
  413. }
  414. //遍历整个Grid,勾选的项目全部进行条码生成
  415. ArrayList<string> custbarcode = new ArrayList<string>();
  416. for (int i = 0; i < int.Parse(ma_count.Text); i++)
  417. {
  418. string data = BarcodeMethod1(PrefixFixed, Suffix, SerialNumIndex, SerialNumLength, Radix, char_out);
  419. if (!custbarcode.Contains(data))
  420. custbarcode.Add(data);
  421. }
  422. string sql = "insert into makesnlist(MSL_ID, MSL_INDATE, MSL_MAKECODE, MSL_SNCODE, MSL_TYPE,msl_status)" +
  423. "select makesnlist_seq.nextval,sysdate,'" + ma_code.Text + "',:custbarcode,'before',0 from dual where :custbarcode not in " +
  424. "(select msl_sncode from makesnlist)";
  425. dh.BatchInsert(sql, new string[] { "custbarcode", "custbarcode" }, custbarcode.ToArray(), custbarcode.ToArray());
  426. //更新最大流水号
  427. dh.UpdateByCondition("RuleMaxNum", "rmn_maxnumber='" + custserialnum + "'", "rmn_nrcode='" + pr_exbarcode + "'");
  428. }
  429. //插入条码
  430. MessageBox.Show("工单" + ma_code.Text + "生成SN清单成功");
  431. }
  432. }
  433. }