SOP.cs 14 KB

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