SOP_PNE.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. using Spire.Xls;
  6. using System.Net;
  7. using System.Collections.Generic;
  8. using System.Threading;
  9. using System.Net.Sockets;
  10. using System.Data;
  11. using System.Web.Script.Serialization;
  12. namespace FileWatcher
  13. {
  14. public partial class SOP_PNE : Form
  15. {
  16. DataHelper dh = new DataHelper();
  17. string usercode;
  18. string sourcecode;
  19. public SOP_PNE(string iUserCode, string iSource)
  20. {
  21. usercode = iUserCode;
  22. sourcecode = iSource;
  23. InitializeComponent();
  24. }
  25. private void Form3_Load(object sender, EventArgs e)
  26. {
  27. CheckForIllegalCrossThreadCalls = false;
  28. }
  29. void uploadfile()
  30. {
  31. pr_code.Text = FilePath.Text.Substring(FilePath.Text.LastIndexOf(@"\") + 1).Replace(".xls", "").Replace(".xlsx", "");
  32. pr_code_Leave(new object(), new EventArgs());
  33. DataTable dt = (DataTable)dh.ExecuteSql("select * from productsop where instr('" + pr_code.Text + "',ps_prodcode)>0 ", "select");
  34. if (dt.Rows.Count == 0)
  35. {
  36. DataTable dt1 = (DataTable)dh.ExecuteSql("select pr_code from product where instr('" + pr_code.Text + "',pr_code)>0", "select");
  37. if (dt1.Rows.Count > 0)
  38. {
  39. string code = LogicHandler.GetPiInoutCode("ProductSOP", "1");
  40. pr_code.Text = dt1.Rows[0]["pr_code"].ToString();
  41. dh.ExecuteSql("insert into productsop(ps_id,ps_code,ps_prodcode,ps_indate,ps_inman)values(productsop_seq.nextval,'" + code + "','" + pr_code.Text + "',sysdate,'" + usercode + "')", "insert");
  42. }
  43. else
  44. {
  45. MessageBox.Show("产品编号" + pr_code.Text + "不存在");
  46. return;
  47. }
  48. }
  49. else
  50. {
  51. pr_code.Text = dt.Rows[0]["ps_prodcode"].ToString();
  52. }
  53. Workbook workbook = new Workbook();
  54. workbook.LoadFromFile(FilePath.Text);
  55. workbook.ConverterSetting.JPEGQuality = 100;
  56. workbook.ConverterSetting.XDpi = 600;
  57. workbook.ConverterSetting.YDpi = 600;
  58. List<Worksheet> list = new List<Worksheet>();
  59. List<string> filename = new List<string>();
  60. dh.ExecuteSql("update ProductSOP set ps_attachsop='' where ps_prodcode='" + pr_code.Text + "'", "update");
  61. OperatResult.AppendText("一共Sheet: " + workbook.Worksheets.Count + "\n");
  62. try
  63. {
  64. for (int i = 0; i < workbook.Worksheets.Count; i++)
  65. {
  66. for (int k = 0; k < workbook.Worksheets[i].TextBoxes.Count; k++)
  67. {
  68. if (workbook.Worksheets[i].TextBoxes[k].RichText.Text.Length > 1)
  69. {
  70. //OperatResult.AppendText(workbook.Worksheets[i].TextBoxes[k].ID + " " + workbook.Worksheets[i].TextBoxes[k].Name + " " + workbook.Worksheets[i].TextBoxes[k].RichText.GetFont(0).FontName + " " + workbook.Worksheets[i].TextBoxes[k].RichText.GetFont(0).Size);
  71. workbook.Worksheets[i].TextBoxes[k].RichText.Text = workbook.Worksheets[i].TextBoxes[k].RichText.Text;
  72. workbook.Worksheets[i].TextBoxes[k].Width = workbook.Worksheets[i].TextBoxes[k].Width * 2;
  73. workbook.Worksheets[i].TextBoxes[k].Height = workbook.Worksheets[i].TextBoxes[k].Height * 2;
  74. workbook.Worksheets[i].TextBoxes[k].HAlignment = CommentHAlignType.Center;
  75. }
  76. }
  77. Workbook bw = new Workbook();
  78. Worksheet she = bw.CreateEmptySheet();
  79. she.Name = workbook.Worksheets[i].Name;
  80. she.CopyFrom(workbook.Worksheets[i]);
  81. if (JPG.Checked)
  82. {
  83. var myThread = new Thread(() => SaveFileToJPG(she));
  84. myThread.Start();
  85. filename.Add(she.Name + ".jpg");
  86. }
  87. else
  88. {
  89. var myThread = new Thread(() => SaveFileToPDF(she));
  90. myThread.Start();
  91. filename.Add(she.Name + ".pdf");
  92. }
  93. }
  94. }
  95. catch (Exception ex)
  96. {
  97. OperatResult.AppendText(ex.Message + "\n");
  98. }
  99. }
  100. private void SaveFileToPDF(Worksheet sheet)
  101. {
  102. if (!Directory.Exists(Application.StartupPath + @"\" + pr_code.Text))
  103. {
  104. Directory.CreateDirectory(Application.StartupPath + @"\" + pr_code.Text);
  105. }
  106. sheet.SaveToPdf(Application.StartupPath + @"\" + pr_code.Text + @"\" + sheet.Name + ".pdf");
  107. OperatResult.AppendText("解析PDF【" + sheet.Name + ".pdf】" + "\n");
  108. Dictionary<string, object> dic = new Dictionary<string, object>();
  109. dic.Add("em_name", "管理员");
  110. dic.Add("em_code", "ADMIN");
  111. dic.Add("caller", "ProductSOP");
  112. OperatResult.AppendText("上传文件【" + sheet.Name + ".pdf" + "】\n");
  113. string fp_id = UploadFilesToRemoteUrl("http://erp.ubtob.net:11773/mes/MEScommon/uploadFiles.action?_noc=1", Application.StartupPath + @"\" + pr_code.Text + @"\" + sheet.Name + ".pdf", dic);
  114. if (fp_id != "")
  115. {
  116. dh.ExecuteSql("update ProductSOP set ps_attachsop=ps_attachsop||" + fp_id + "||';' where ps_prodcode='" + pr_code.Text + "'", "update");
  117. }
  118. }
  119. private void SaveFileToJPG(Worksheet sheet)
  120. {
  121. try
  122. {
  123. if (!Directory.Exists(Application.StartupPath + @"\" + pr_code.Text))
  124. {
  125. Directory.CreateDirectory(Application.StartupPath + @"\" + pr_code.Text);
  126. }
  127. ExcelPicture picture = sheet.Pictures.Add(@"图片\电子受控章.png");
  128. picture.Width = 113;
  129. picture.Height = 38;
  130. picture.LeftColumn = 20;
  131. picture.TopRowOffset = 20;
  132. ExcelPicture picture1 = sheet.Pictures.Add(@"图片\签名.png");
  133. picture1.Width = 100;
  134. picture1.Height = 38;
  135. picture1.LeftColumn = 15;
  136. picture1.TopRowOffset = 600;
  137. sheet.SaveToImage(Application.StartupPath + @"\" + pr_code.Text + @"\" + sheet.Name + ".jpg");
  138. OperatResult.AppendText("解析图片【" + sheet.Name + ".jpg】" + "\n");
  139. Dictionary<string, object> dic = new Dictionary<string, object>();
  140. dic.Add("em_name", "管理员");
  141. dic.Add("em_code", "ADMIN");
  142. dic.Add("caller", "ProductSOP");
  143. OperatResult.AppendText("上传文件【" + sheet.Name + ".jpg" + "】\n");
  144. string fp_id = UploadFilesToRemoteUrl("http://erp.ubtob.net:11773/mes/MEScommon/uploadFiles.action?_noc=1", Application.StartupPath + @"\" + pr_code.Text + @"\" + sheet.Name + ".jpg", dic);
  145. if (fp_id != "")
  146. {
  147. dh.ExecuteSql("update ProductSOP set ps_attachsop=ps_attachsop||" + fp_id + "||';' where ps_prodcode='" + pr_code.Text + "'", "update");
  148. }
  149. }
  150. catch (Exception ex)
  151. {
  152. Console.WriteLine(ex.Message);
  153. }
  154. }
  155. /// <summary>
  156. /// 请求上传图片到阿里云
  157. /// </summary>
  158. /// <param name="url">上传地址</param>
  159. /// <param name="filepath">本地文件路径</param>
  160. /// <param name="dic">上传的数据信息</param>
  161. /// <returns></returns>
  162. public string UploadFilesToRemoteUrl(string url1, string filepath, Dictionary<string, object> dic)
  163. {
  164. try
  165. {
  166. ServicePointManager.DefaultConnectionLimit = 50;
  167. string boundary = DateTime.Now.Ticks.ToString("x");
  168. byte[] boundarybytes = System.Text.Encoding.UTF8.GetBytes("--" + boundary + "\r\n");
  169. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url1);
  170. request.Method = "POST";
  171. request.Timeout = 10 * 10000;
  172. request.ContentType = "multipart/form-data; boundary=" + boundary;
  173. Stream rs = request.GetRequestStream();
  174. var endBoundaryBytes = Encoding.UTF8.GetBytes("--" + boundary + "--\r\n");
  175. string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n" + "\r\n" + "{1}" + "\r\n";
  176. if (dic != null)
  177. {
  178. foreach (string key in dic.Keys)
  179. {
  180. rs.Write(boundarybytes, 0, boundarybytes.Length);
  181. string formitem = string.Format(formdataTemplate, key, dic[key]);
  182. byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
  183. rs.Write(formitembytes, 0, formitembytes.Length);
  184. }
  185. }
  186. string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n\r\n";
  187. {
  188. rs.Write(boundarybytes, 0, boundarybytes.Length);
  189. var header = string.Format(headerTemplate, "file", Path.GetFileName(filepath));
  190. var headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
  191. rs.Write(headerbytes, 0, headerbytes.Length);
  192. using (var fileStream = new FileStream(filepath, FileMode.Open, FileAccess.Read))
  193. {
  194. var buffer = new byte[1024];
  195. var bytesRead = 0;
  196. while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
  197. {
  198. rs.Write(buffer, 0, bytesRead);
  199. }
  200. }
  201. var cr = Encoding.UTF8.GetBytes("\r\n");
  202. rs.Write(cr, 0, cr.Length);
  203. }
  204. rs.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
  205. var response = request.GetResponse() as HttpWebResponse;
  206. StreamReader newReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
  207. string Content = newReader.ReadToEnd();
  208. Dictionary<string, object> dic1 = new Dictionary<string, object>();
  209. List<Dictionary<string, object>> dic2 = null;
  210. dic1 = BaseUtil.ToDictionary(Content);
  211. dic2 = dic1["data"] as List<Dictionary<string, object>>;
  212. string fp_id = "";
  213. if (dic2[0]["filepath"] != null)
  214. {
  215. fp_id = dic2[0]["filepath"].ToString();
  216. }
  217. if (response.StatusCode == HttpStatusCode.OK)
  218. {
  219. return fp_id;
  220. }
  221. }
  222. catch (Exception e)
  223. {
  224. LogicHandler.DoCommandLog("SOP", usercode, "", "", sourcecode, "上传SOP", "上传失败", pr_code.Text, "");
  225. Console.WriteLine(e.Message + e.StackTrace);
  226. }
  227. return "";
  228. }
  229. private void ChooseFile_Click(object sender, EventArgs e)
  230. {
  231. ChoosePath.Filter = "(*.xls)|*.xls|(*.xlsx)|*.xlsx";
  232. DialogResult result = ChoosePath.ShowDialog();
  233. if (result == DialogResult.OK)
  234. {
  235. FilePath.Text = ChoosePath.FileName;
  236. }
  237. }
  238. private void UploadSOP_Click(object sender, EventArgs e)
  239. {
  240. Thread thread = new Thread(uploadfile);
  241. SetLoadingWindow stw = new SetLoadingWindow(thread, "上传文件");
  242. stw.StartPosition = FormStartPosition.CenterScreen;
  243. stw.ShowDialog();
  244. if (JPG.Checked)
  245. {
  246. LogicHandler.DoCommandLog("SOP", usercode, "", "", sourcecode, "上传SOP【图片格式】", "上传成功", pr_code.Text, "");
  247. }
  248. else
  249. {
  250. LogicHandler.DoCommandLog("SOP", usercode, "", "", sourcecode, "上传SOP【PDF格式】", "上传成功", pr_code.Text, "");
  251. }
  252. }
  253. private void OperatResult_TextChanged(object sender, EventArgs e)
  254. {
  255. //OperatResult.SelectionStart = Text.Length;
  256. OperatResult.ScrollToCaret();
  257. }
  258. TcpServer tcpserver = new TcpServer();
  259. private void SendSop_Click(object sender, EventArgs e)
  260. {
  261. try
  262. {
  263. IPHostEntry IpEntry = Dns.GetHostEntry(Dns.GetHostName());
  264. string IPAddress = "";
  265. for (int i = 0; i < IpEntry.AddressList.Length; i++)
  266. {
  267. if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
  268. {
  269. IPAddress = IpEntry.AddressList[i].ToString();
  270. }
  271. }
  272. dh.ExecuteSql("update SOPSOURCE set SS_BRDIP='" + IPAddress + "'", "update");
  273. DataTable dt = (DataTable)dh.ExecuteSql("select * from productsop where ps_prodcode='" + pr_code.Text + "'", "select");
  274. if (dt.Rows.Count > 0)
  275. {
  276. tcpserver.Start();
  277. Thread.Sleep(5000);
  278. List<Dictionary<string, string>> listr = new List<Dictionary<string, string>>();
  279. string PS_ATTACHSOP = dt.Rows[0]["PS_ATTACHSOP"].ToString();
  280. PS_ATTACHSOP = PS_ATTACHSOP.Substring(0, PS_ATTACHSOP.Length - 1).Replace(";", ",");
  281. dt = (DataTable)dh.ExecuteSql("select fp_path, fp_name from filepath where fp_id in (" + PS_ATTACHSOP + ") and nvl(fp_path, ' ') <> ' '", "select");
  282. JavaScriptSerializer jss = new JavaScriptSerializer();
  283. Dictionary<string, object> map1 = new Dictionary<string, object>();
  284. Dictionary<string, object> map = new Dictionary<string, object>();
  285. string path;
  286. string pathroot = dh.GetConfig("filePathUrl", "sys").ToString();
  287. for (int i = 0; i < dt.Rows.Count; i++)
  288. {
  289. Dictionary<string, string> dic1 = new Dictionary<string, string>();
  290. path = dt.Rows[i]["fp_path"].ToString();
  291. path = encryptBASE64(path.Replace("/app/uas/webapps/postattach", pathroot)).Replace("\\s*|\r|\n|\t", "");
  292. dic1.Add("path", path);
  293. dic1.Add("filename", dt.Rows[i]["fp_name"].ToString().Replace(" ", "").Replace("(", "").Replace(")", ""));
  294. dic1.Add("ps_prodcode", pr_code.Text);
  295. listr.Add(dic1);
  296. }
  297. map1.Add("ps_code", "");
  298. map1.Add("url", listr);
  299. map.Add("success", true);
  300. map.Add("data", map1);
  301. tcpserver.Send(jss.Serialize(map));
  302. tcpserver.Stop();
  303. LogicHandler.DoCommandLog("SOP", usercode, "", "", sourcecode, "广播SOP", "广播成功", pr_code.Text, "");
  304. MessageBox.Show("产品编号" + pr_code.Text + "广播成功");
  305. }
  306. else
  307. {
  308. MessageBox.Show("产品编号" + pr_code.Text + "未维护SOP文档");
  309. LogicHandler.DoCommandLog("SOP", usercode, "", "", sourcecode, "广播SOP", "广播失败,未维护SOP文档", pr_code.Text, "");
  310. }
  311. }
  312. catch (Exception ex)
  313. {
  314. LogicHandler.DoCommandLog("SOP", usercode, "", "", sourcecode, "广播SOP", "广播失败", pr_code.Text, "");
  315. Console.WriteLine(ex.Message + ex.StackTrace);
  316. }
  317. }
  318. public string encryptBASE64(string key)
  319. {
  320. byte[] bytes = Encoding.Default.GetBytes(key);
  321. return Convert.ToBase64String(bytes);
  322. }
  323. public static string EncodeBase64(string code_type, string code)
  324. {
  325. string encode = "";
  326. byte[] bytes = Encoding.GetEncoding(code_type).GetBytes(code);
  327. try
  328. {
  329. encode = Convert.ToBase64String(bytes);
  330. }
  331. catch
  332. {
  333. encode = code;
  334. }
  335. return encode;
  336. }
  337. private void pr_code_KeyDown(object sender, KeyEventArgs e)
  338. {
  339. if (e.KeyCode == Keys.Enter)
  340. {
  341. LoadPrCode();
  342. }
  343. }
  344. private void LoadPrCode()
  345. {
  346. DataTable dt = (DataTable)dh.ExecuteSql("select pr_code,pr_spec,pr_detail from product where instr('" + pr_code.Text + "',pr_code)>0", "select");
  347. if (dt.Rows.Count > 0)
  348. {
  349. pr_spec.Clear();
  350. pr_spec.AppendText(dt.Rows[0]["pr_spec"].ToString());
  351. pr_detail.Text = dt.Rows[0]["pr_detail"].ToString();
  352. pr_code.Text = dt.Rows[0]["pr_code"].ToString();
  353. }
  354. else
  355. {
  356. MessageBox.Show("产品编号" + pr_code.Text + "不存在");
  357. }
  358. }
  359. private void pr_code_Leave(object sender, EventArgs e)
  360. {
  361. LoadPrCode();
  362. }
  363. private void pr_spec_Click(object sender, EventArgs e)
  364. {
  365. }
  366. }
  367. }