Query_SN.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. using LabelManager2;
  2. using System;
  3. using System.Data;
  4. using System.Security.Cryptography;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Windows.Forms;
  8. using UAS_MES_NEW.DataOperate;
  9. using UAS_MES_NEW.Entity;
  10. using UAS_MES_NEW.PublicForm;
  11. using UAS_MES_NEW.PublicMethod;
  12. namespace UAS_MES_NEW.Query
  13. {
  14. public partial class Query_SN : Form
  15. {
  16. DataHelper dh = SystemInf.dh;
  17. ApplicationClass lbl;
  18. Thread InitPrint;
  19. Document doc;
  20. public Query_SN()
  21. {
  22. InitializeComponent();
  23. }
  24. private void Export_Click(object sender, EventArgs e)
  25. {
  26. ExcelExport("");
  27. }
  28. //选择导出Excel时是选择导出数据的还是模板
  29. private void ExcelExport(string DataOrTemplet)
  30. {
  31. //Data表示导出数据
  32. //Templet表示导出模板
  33. MD5 md5 = MD5.Create();
  34. folderBrowserDialog1.Description = "选择导出的路径";
  35. DialogResult result = folderBrowserDialog1.ShowDialog();
  36. if (result == DialogResult.OK)
  37. {
  38. string FolderPath = folderBrowserDialog1.SelectedPath;
  39. ExcelHandler eh = new ExcelHandler();
  40. DataTable dt = new DataTable();
  41. dt.Columns.Add("工单号");
  42. dt.Columns.Add("序列号");
  43. dt.Columns.Add("MAC");
  44. dt.Columns.Add("BT");
  45. dt.Columns.Add("拓展栏位1");
  46. dt.Columns.Add("拓展栏位2");
  47. dt.Columns.Add("拓展栏位3");
  48. dt.Columns.Add("备注");
  49. string prefix = BeginMac.Text.Substring(0, 8);
  50. string suffix = BeginMac.Text.Substring(8, 4);
  51. int Number = Int32.Parse(suffix, System.Globalization.NumberStyles.HexNumber);
  52. for (decimal i = currentSerial.Value; i < currentSerial.Value + int.Parse(num.Text); i++)
  53. {
  54. DataRow dr = dt.NewRow();
  55. dr["工单号"] = ma_code.Text;
  56. dr["MAC"] = prefix + Number.ToString("X");
  57. Number = Number + 1;
  58. dr["BT"] = prefix + Number.ToString("X");
  59. Number = Number + 1;
  60. string serial = pr_code.Text
  61. + color.Text
  62. + GetDate(date.Value.ToString("yyyy"), date.Value.ToString("MM"), date.Value.ToString("dd"))
  63. + factory.Text
  64. + status.Text
  65. + lpad(int.Parse(seriallength.Text), (i + 1).ToString());
  66. string encode = MD5Encoding(serial + ")G$d");
  67. dr["序列号"] = serial + encode.Substring(encode.Length - 2);
  68. dt.Rows.Add(dr);
  69. }
  70. eh.ExportExcel(dt, FolderPath);
  71. }
  72. }
  73. private void Query_SpecialReport_Load(object sender, EventArgs e)
  74. {
  75. InitPrint = new Thread(InPrint);
  76. SetLoadingWindow stw = new SetLoadingWindow(InitPrint, "初始化打印程序");
  77. BaseUtil.SetFormCenter(stw);
  78. stw.ShowDialog();
  79. doc = lbl.Documents.Open(System.Windows.Forms.Application.StartupPath + @"\SN.lab");
  80. }
  81. private void InPrint()
  82. {
  83. try
  84. {
  85. lbl = new ApplicationClass();
  86. BaseUtil.WriteLbl();
  87. }
  88. catch (Exception)
  89. {
  90. MessageBox.Show("未正确安装CodeSoft软件");
  91. }
  92. }
  93. private static string MD5Encoding(string rawPass)
  94. {
  95. // 创建MD5类的默认实例:MD5CryptoServiceProvider
  96. MD5 md5 = MD5.Create();
  97. byte[] bs = Encoding.UTF8.GetBytes(rawPass);
  98. byte[] hs = md5.ComputeHash(bs);
  99. StringBuilder stb = new StringBuilder();
  100. foreach (byte b in hs)
  101. {
  102. // 以十六进制格式格式化
  103. stb.Append(b.ToString("x2"));
  104. }
  105. return stb.ToString().ToUpper();
  106. }
  107. private static string lpad(int length, string number)
  108. {
  109. while (number.Length < length)
  110. {
  111. number = "0" + number;
  112. }
  113. number = number.Substring(number.Length - length, length);
  114. return number;
  115. }
  116. private string GetDate(string year, string month, string day)
  117. {
  118. string returndate = "";
  119. switch (year)
  120. {
  121. case "2020":
  122. returndate = "C";
  123. break;
  124. case "2021":
  125. returndate = "D";
  126. break;
  127. case "2022":
  128. returndate = "E";
  129. break;
  130. case "2023":
  131. returndate = "F";
  132. break;
  133. case "2024":
  134. returndate = "G";
  135. break;
  136. case "2025":
  137. returndate = "H";
  138. break;
  139. case "2026":
  140. returndate = "I";
  141. break;
  142. case "2027":
  143. returndate = "J";
  144. break;
  145. case "2028":
  146. returndate = "K";
  147. break;
  148. case "2029":
  149. returndate = "L";
  150. break;
  151. case "2030":
  152. returndate = "M";
  153. break;
  154. case "2031":
  155. returndate = "N";
  156. break;
  157. case "2032":
  158. returndate = "O";
  159. break;
  160. case "2033":
  161. returndate = "P";
  162. break;
  163. case "2034":
  164. returndate = "Q";
  165. break;
  166. default:
  167. break;
  168. }
  169. switch (month)
  170. {
  171. case "01":
  172. returndate += "1";
  173. break;
  174. case "02":
  175. returndate += "2";
  176. break;
  177. case "03":
  178. returndate += "3";
  179. break;
  180. case "04":
  181. returndate += "4";
  182. break;
  183. case "05":
  184. returndate += "5";
  185. break;
  186. case "06":
  187. returndate += "6";
  188. break;
  189. case "07":
  190. returndate += "7";
  191. break;
  192. case "08":
  193. returndate += "8";
  194. break;
  195. case "09":
  196. returndate += "9";
  197. break;
  198. case "10":
  199. returndate += "A";
  200. break;
  201. case "11":
  202. returndate += "B";
  203. break;
  204. case "12":
  205. returndate += "C";
  206. break;
  207. default:
  208. break;
  209. }
  210. switch (day)
  211. {
  212. case "01":
  213. returndate += "1";
  214. break;
  215. case "02":
  216. returndate += "2";
  217. break;
  218. case "03":
  219. returndate += "3";
  220. break;
  221. case "04":
  222. returndate += "4";
  223. break;
  224. case "05":
  225. returndate += "5";
  226. break;
  227. case "06":
  228. returndate += "6";
  229. break;
  230. case "07":
  231. returndate += "7";
  232. break;
  233. case "08":
  234. returndate += "8";
  235. break;
  236. case "09":
  237. returndate += "9";
  238. break;
  239. case "10":
  240. returndate += "A";
  241. break;
  242. case "11":
  243. returndate += "B";
  244. break;
  245. case "12":
  246. returndate += "C";
  247. break;
  248. case "13":
  249. returndate += "D";
  250. break;
  251. case "14":
  252. returndate += "E";
  253. break;
  254. case "15":
  255. returndate += "F";
  256. break;
  257. case "16":
  258. returndate += "G";
  259. break;
  260. case "17":
  261. returndate += "H";
  262. break;
  263. case "18":
  264. returndate += "I";
  265. break;
  266. case "19":
  267. returndate += "J";
  268. break;
  269. case "20":
  270. returndate += "K";
  271. break;
  272. case "21":
  273. returndate += "L";
  274. break;
  275. case "22":
  276. returndate += "M";
  277. break;
  278. case "23":
  279. returndate += "N";
  280. break;
  281. case "24":
  282. returndate += "O";
  283. break;
  284. case "25":
  285. returndate += "P";
  286. break;
  287. case "26":
  288. returndate += "Q";
  289. break;
  290. case "27":
  291. returndate += "R";
  292. break;
  293. case "28":
  294. returndate += "S";
  295. break;
  296. case "29":
  297. returndate += "T";
  298. break;
  299. case "30":
  300. returndate += "U";
  301. break;
  302. case "31":
  303. returndate += "V";
  304. break;
  305. default:
  306. break;
  307. }
  308. return returndate;
  309. }
  310. private void Print_Click(object sender, EventArgs e)
  311. {
  312. DataTable dt = new DataTable();
  313. dt.Columns.Add("工单号");
  314. dt.Columns.Add("序列号");
  315. dt.Columns.Add("MAC");
  316. dt.Columns.Add("BT");
  317. dt.Columns.Add("拓展栏位1");
  318. dt.Columns.Add("拓展栏位2");
  319. dt.Columns.Add("拓展栏位3");
  320. dt.Columns.Add("备注");
  321. string prefix = BeginMac.Text.Substring(0, 8);
  322. string suffix = BeginMac.Text.Substring(8, 4);
  323. int Number = Int32.Parse(suffix, System.Globalization.NumberStyles.HexNumber);
  324. for (decimal i = currentSerial.Value; i < currentSerial.Value + int.Parse(num.Text); i++)
  325. {
  326. DataRow dr = dt.NewRow();
  327. dr["工单号"] = ma_code.Text;
  328. dr["MAC"] = prefix + Number.ToString("X");
  329. Number = Number + 1;
  330. dr["BT"] = prefix + Number.ToString("X");
  331. Number = Number + 1;
  332. string serial = pr_code.Text
  333. + color.Text
  334. + GetDate(date.Value.ToString("yyyy"), date.Value.ToString("MM"), date.Value.ToString("dd"))
  335. + factory.Text
  336. + status.Text
  337. + lpad(int.Parse(seriallength.Text), (i + 1).ToString());
  338. string encode = MD5Encoding(serial + ")G$d");
  339. dr["序列号"] = serial + encode.Substring(encode.Length - 2);
  340. dt.Rows.Add(dr);
  341. for (int j = 0; j < doc.Variables.FormVariables.Count; j++)
  342. {
  343. if (doc.Variables.FormVariables.Item(j + 1).Name.ToUpper() == "SN")
  344. {
  345. doc.Variables.FormVariables.Item(j + 1).Value = (serial + encode.Substring(encode.Length - 2));
  346. }
  347. }
  348. doc.Printer.SwitchTo(Printer.Text);
  349. doc.PrintDocument(1);
  350. }
  351. }
  352. }
  353. }