Query_SN.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. using NPOI.SS.Formula.Functions;
  2. using Seagull.BarTender.Print;
  3. using System;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Drawing.Printing;
  7. using System.Security.Cryptography;
  8. using System.Text.RegularExpressions;
  9. using System.Windows.Forms;
  10. using UAS_MES_NEW.DataOperate;
  11. using UAS_MES_NEW.Entity;
  12. using UAS_MES_NEW.PublicMethod;
  13. namespace UAS_MES_NEW.Query
  14. {
  15. public partial class Query_SN : Form
  16. {
  17. int serialnum = 0;
  18. //客户的流水号
  19. int custserialnum = 0;
  20. Engine engine = new Engine();
  21. LabelFormatDocument format;
  22. DataHelper dh = SystemInf.dh;
  23. //前缀条件
  24. //生成条码的流水号
  25. DataTable Dbfind;
  26. public Query_SN()
  27. {
  28. InitializeComponent();
  29. }
  30. private void PrintTest_Click(object sender, EventArgs e)
  31. {
  32. PrintDocument print = new PrintDocument();
  33. print.PrinterSettings.PrinterName = PrinterList.Text;
  34. print.PrintPage += Print_PrintPage;
  35. print.Print();
  36. }
  37. private void Print_PrintPage(object sender, PrintPageEventArgs e)
  38. {
  39. Graphics g = e.Graphics;
  40. float leftMargin = 10f; //左边距
  41. SolidBrush myBrush = new SolidBrush(Color.Black);//刷子
  42. float yPosition = 5f;//行定位
  43. Font printFont = new Font("微软雅黑", 20f, FontStyle.Bold);//设置字体
  44. yPosition += printFont.GetHeight(g);//另起一行
  45. g.DrawString("成功连接此打印机", printFont, myBrush, leftMargin, yPosition, new StringFormat());
  46. }
  47. private void Setting_Click(object sender, EventArgs e)
  48. {
  49. PrintDialog printd = new PrintDialog();
  50. printd.PrinterSettings.PrinterName = PrinterList.Text;
  51. printd.ShowDialog();
  52. }
  53. private void Export_Click(object sender, EventArgs e)
  54. {
  55. MD5 md5 = MD5.Create();
  56. folderBrowserDialog1.Description = "选择导出的路径";
  57. DialogResult result = folderBrowserDialog1.ShowDialog();
  58. if (result == DialogResult.OK)
  59. {
  60. string FolderPath = folderBrowserDialog1.SelectedPath;
  61. ExcelHandler eh = new ExcelHandler();
  62. DataTable dt = (DataTable)dh.ExecuteSql("select msl_makecode 工单号,msl_sncode 序列号 from makesnlist " +
  63. "where msl_makecode='" + ma_code.Text + "' order by msl_sncode", "select");
  64. eh.ExportExcel(dt, FolderPath);
  65. }
  66. }
  67. public string BarcodeMethod1(string Prefix, string Suffix, int Index, int Length, int radix, string char_out)
  68. {
  69. string str = Prefix;
  70. //如果是流水则需要在前面加0
  71. string serialcode = BaseUtil.DToAny(custserialnum, radix, char_out);
  72. for (int j = serialcode.ToString().Length; j < Length; j++)
  73. {
  74. serialcode = "0" + serialcode;
  75. }
  76. str += serialcode;
  77. str += Suffix;
  78. custserialnum = custserialnum + 1;
  79. return str;
  80. }
  81. private static string lpad(int length, string number)
  82. {
  83. while (number.Length < length)
  84. {
  85. number = "0" + number;
  86. }
  87. number = number.Substring(number.Length - length, length);
  88. return number;
  89. }
  90. private void SystemSetting_PrinterTest_Load(object sender, EventArgs e)
  91. {
  92. engine.Start();
  93. ma_code.TableName = " make left join product on ma_prodcode=pr_code";
  94. ma_code.SelectField = "ma_code # 工单编号,pr_code # 产品编号,pr_spec # 型号";
  95. ma_code.FormName = Name;
  96. ma_code.SetValueField = new string[] { "ma_code" };
  97. //ma_code.Condition = "ma_statuscode='STARTED'";
  98. ma_code.DbChange += pr_code_DbChange;
  99. }
  100. private void pr_code_DbChange(object sender, EventArgs e)
  101. {
  102. Dbfind = ma_code.ReturnData;
  103. BaseUtil.SetFormValue(this.Controls, Dbfind);
  104. 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");
  105. if (dt.Rows.Count > 0)
  106. {
  107. pr_code.Text = dt.Rows[0]["ma_prodcode"].ToString();
  108. ma_qty.Text = dt.Rows[0]["ma_qty"].ToString();
  109. pr_detail.Text = dt.Rows[0]["pr_detail"].ToString();
  110. }
  111. ma_count.Text = ma_qty.Text;
  112. }
  113. private void GenSN_Click(object sender, EventArgs e)
  114. {
  115. string Prefix = "";
  116. string Suffix = "";
  117. //获取编码规则
  118. if (!dh.CheckExist("make", "ma_code='" + ma_code.Text + "'"))
  119. {
  120. MessageBox.Show("工单号不存在");
  121. return;
  122. }
  123. 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");
  124. string pr_exbarcode = "";
  125. string ma_qty = "";
  126. if (dt.Rows.Count > 0)
  127. {
  128. pr_exbarcode = dt.Rows[0]["pr_exbarcode"].ToString();
  129. ma_qty = dt.Rows[0]["ma_qty"].ToString();
  130. }
  131. if (dh.getRowCount("makesnlist", "msl_makecode='" + ma_code.Text + "' and msl_type='" + (msl_type.Text == "转号前" ? "before" : "after") + "'") + int.Parse(ma_count.Text) > int.Parse(ma_qty))
  132. {
  133. MessageBox.Show("工单号" + ma_code.Text + "生产序列号超出工单数");
  134. return;
  135. }
  136. 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");
  137. //如果没有则取公共规则
  138. if (Nr.Rows.Count == 0)
  139. 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");
  140. //用于过滤参数的正则表达式
  141. Regex match = new Regex("{\\w+}");
  142. //用于存放每一项的明细的数据
  143. string[] NrData = new string[Nr.Rows.Count];
  144. //流水号的索引
  145. int SerialNumIndex = 0;
  146. //流水长度
  147. int SerialNumLength = 0;
  148. //存放键值对
  149. int Radix = 10;
  150. string PrefixFixed = "";
  151. string char_out = "";
  152. string nr_changenum = Nr.Rows[0]["nr_changenum"].ToString();
  153. //如果是
  154. if (nr_changenum != "-1")
  155. {
  156. for (int m = 0; m < Nr.Rows.Count; m++)
  157. {
  158. switch (Nr.Rows[m]["nrd_type"].ToString())
  159. {
  160. //常量直接进行拼接
  161. case "常量":
  162. NrData[m] = Nr.Rows[m]["nrd_sql"].ToString();
  163. Prefix += NrData[m];
  164. Suffix += NrData[m];
  165. break;
  166. case "SQL":
  167. string SQL = Nr.Rows[m]["nrd_sql"].ToString();
  168. DataTable Temp;
  169. //如果不包含参数替换
  170. if (SQL.IndexOf("{") == 0)
  171. {
  172. Temp = (DataTable)dh.ExecuteSql(SQL, "select");
  173. }
  174. else
  175. {
  176. //替换参数后重新执行SQL
  177. foreach (System.Text.RegularExpressions.Match mch in match.Matches(SQL))
  178. {
  179. SQL = SQL.Replace(mch.Value.Trim(), "'" + ma_code.Text + "'");
  180. }
  181. Temp = (DataTable)dh.ExecuteSql(SQL, "select");
  182. }
  183. if (Temp.Rows.Count > 0)
  184. {
  185. NrData[m] = Temp.Rows[0][0].ToString();
  186. Prefix += NrData[m];
  187. Suffix += NrData[m];
  188. }
  189. else
  190. {
  191. NrData[m] = "";
  192. Prefix += NrData[m];
  193. Suffix += NrData[m];
  194. }
  195. break;
  196. //流水需要通过MaxNumber去取
  197. case "流水":
  198. NrData[m] = dh.getFieldDataByCondition("RuleMaxNum", "rmn_maxnumber", "rmn_nrcode='" + pr_exbarcode + "'").ToString();
  199. Suffix = "";
  200. PrefixFixed = Prefix;
  201. //设置当前流水
  202. custserialnum = int.Parse(NrData[m] == "" ? "0" : NrData[m]);
  203. SerialNumIndex = m;
  204. SerialNumLength = int.Parse(Nr.Rows[m]["nrd_length"].ToString());
  205. Radix = int.Parse(Nr.Rows[m]["nrd_radix"].ToString());
  206. char_out = Nr.Rows[m]["nrd_content"].ToString();
  207. break;
  208. default:
  209. break;
  210. }
  211. }
  212. //获取最大的流水号
  213. string maxnum = dh.getFieldDataByCondition("RuleMaxNum", "rmn_maxnumber", "rmn_nrcode='" + pr_exbarcode + "' and rmn_prefix='" + Prefix + "'").ToString();
  214. //如果流水号为空则插入一条新记录,从1开始取
  215. if (maxnum == "")
  216. {
  217. dh.ExecuteSql("insert into RuleMaxNum(rmn_id,rmn_nrcode,rmn_prefix,rmn_maxnumber) values(RuleMaxNum_seq.nextval,'" + pr_exbarcode + "','" + Prefix + "','1')", "insert");
  218. custserialnum = 1;
  219. }
  220. //如果流水号不为空则取当前流水
  221. else
  222. {
  223. custserialnum = int.Parse(maxnum);
  224. }
  225. //遍历整个Grid,勾选的项目全部进行条码生成
  226. ArrayList<string> custbarcode = new ArrayList<string>();
  227. for (int i = 0; i < int.Parse(ma_count.Text); i++)
  228. {
  229. string data = BarcodeMethod1(PrefixFixed, Suffix, SerialNumIndex, SerialNumLength, Radix, char_out);
  230. if (!custbarcode.Contains(data))
  231. custbarcode.Add(data);
  232. }
  233. //插入条码
  234. string sql = "insert into makesnlist(MSL_ID, MSL_INDATE, MSL_MAKECODE, MSL_SNCODE,msl_status,MSL_TYPE)" +
  235. "select makesnlist_seq.nextval,sysdate,'" + ma_code.Text + "',:custbarcode,0,'" + (msl_type.Text == "转号前" ? "before" : "after") + "' from dual where :custbarcode not in " +
  236. "(select msl_sncode from makesnlist)";
  237. dh.BatchInsert(sql, new string[] { "custbarcode", "custbarcode" }, custbarcode.ToArray(), custbarcode.ToArray());
  238. //更新最大流水号
  239. dh.UpdateByCondition("RuleMaxNum", "rmn_maxnumber='" + custserialnum + "'", "rmn_nrcode='" + pr_exbarcode + "' and rmn_prefix='" + Prefix + "'");
  240. }
  241. else
  242. {
  243. for (int m = 0; m < Nr.Rows.Count; m++)
  244. {
  245. switch (Nr.Rows[m]["nrd_type"].ToString())
  246. {
  247. //常量直接进行拼接
  248. case "常量":
  249. NrData[m] = Nr.Rows[m]["nrd_sql"].ToString();
  250. Prefix += NrData[m];
  251. Suffix += NrData[m];
  252. break;
  253. case "SQL":
  254. string SQL = Nr.Rows[m]["nrd_sql"].ToString();
  255. DataTable Temp;
  256. //如果不包含参数替换
  257. if (SQL.IndexOf("{") == 0)
  258. {
  259. Temp = (DataTable)dh.ExecuteSql(SQL, "select");
  260. }
  261. else
  262. {
  263. //替换参数后重新执行SQL
  264. foreach (System.Text.RegularExpressions.Match mch in match.Matches(SQL))
  265. {
  266. SQL = SQL.Replace(mch.Value.Trim(), "'" + ma_code.Text + "'");
  267. }
  268. Temp = (DataTable)dh.ExecuteSql(SQL, "select");
  269. }
  270. if (Temp.Rows.Count > 0)
  271. {
  272. NrData[m] = Temp.Rows[0][0].ToString();
  273. Prefix += NrData[m];
  274. Suffix += NrData[m];
  275. }
  276. else
  277. {
  278. NrData[m] = "";
  279. Prefix += NrData[m];
  280. Suffix += NrData[m];
  281. }
  282. break;
  283. //流水需要通过MaxNumber去取
  284. case "流水":
  285. NrData[m] = dh.getFieldDataByCondition("RuleMaxNum", "rmn_maxnumber", "rmn_nrcode='" + pr_exbarcode + "'").ToString();
  286. Suffix = "";
  287. PrefixFixed = Prefix;
  288. //设置当前流水
  289. custserialnum = int.Parse(NrData[m] == "" ? "0" : NrData[m]);
  290. SerialNumIndex = m;
  291. SerialNumLength = int.Parse(Nr.Rows[m]["nrd_length"].ToString());
  292. Radix = int.Parse(Nr.Rows[m]["nrd_radix"].ToString());
  293. char_out = Nr.Rows[m]["nrd_content"].ToString();
  294. break;
  295. default:
  296. break;
  297. }
  298. }
  299. //获取最大的流水号
  300. string maxnum = dh.getFieldDataByCondition("RuleMaxNum", "rmn_maxnumber", "rmn_nrcode='" + pr_exbarcode + "'").ToString();
  301. //如果流水号为空则插入一条新记录,从1开始取
  302. if (maxnum == "")
  303. {
  304. dh.ExecuteSql("insert into RuleMaxNum(rmn_id,rmn_nrcode,rmn_maxnumber) values(RuleMaxNum_seq.nextval,'" + pr_exbarcode + "','1')", "insert");
  305. custserialnum = 1;
  306. }
  307. //如果流水号不为空则取当前流水
  308. else
  309. {
  310. custserialnum = int.Parse(maxnum);
  311. }
  312. //遍历整个Grid,勾选的项目全部进行条码生成
  313. ArrayList<string> custbarcode = new ArrayList<string>();
  314. for (int i = 0; i < int.Parse(ma_count.Text); i++)
  315. {
  316. string data = BarcodeMethod1(PrefixFixed, Suffix, SerialNumIndex, SerialNumLength, Radix, char_out);
  317. if (!custbarcode.Contains(data))
  318. custbarcode.Add(data);
  319. }
  320. string sql = "insert into makesnlist(MSL_ID, MSL_INDATE, MSL_MAKECODE, MSL_SNCODE, MSL_TYPE,msl_status)" +
  321. "select makesnlist_seq.nextval,sysdate,'" + ma_code.Text + "',:custbarcode,'before',0 from dual where :custbarcode not in " +
  322. "(select msl_sncode from makesnlist)";
  323. dh.BatchInsert(sql, new string[] { "custbarcode", "custbarcode" }, custbarcode.ToArray(), custbarcode.ToArray());
  324. //更新最大流水号
  325. dh.UpdateByCondition("RuleMaxNum", "rmn_maxnumber='" + custserialnum + "'", "rmn_nrcode='" + pr_exbarcode + "'");
  326. }
  327. //插入条码
  328. MessageBox.Show("工单" + ma_code.Text + "生成SN清单成功");
  329. }
  330. }
  331. }