SOP.cs 17 KB

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