SOP.cs 15 KB

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