SOP.cs 17 KB

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