SOP.cs 16 KB

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