SOP.cs 17 KB

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