SOP.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. tcpserver.Start();
  33. }
  34. void uploadfile()
  35. {
  36. pr_code.Text = FilePath.Text.Substring(FilePath.Text.LastIndexOf(@"\") + 1).Replace(".xls", "").Replace(".xlsx", "");
  37. pr_code_Leave(new object(), new EventArgs());
  38. DataTable dt = (DataTable)dh.ExecuteSql("select * from productsop where ps_prodcode='" + pr_code.Text + "' ", "select");
  39. if (dt.Rows.Count == 0)
  40. {
  41. string code = LogicHandler.GetPiInoutCode("ProductSOP", "1");
  42. 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");
  43. }
  44. else
  45. {
  46. string ps_id = dt.Rows[0]["ps_id"].ToString();
  47. dh.ExecuteSql("delete from ProductSOPdetail where psd_psid='" + ps_id + "'", "delete");
  48. pr_code.Text = dt.Rows[0]["ps_prodcode"].ToString();
  49. }
  50. Workbook workbook = new Workbook(FilePath.Text);
  51. List<Worksheet> list = new List<Worksheet>();
  52. List<string> filename = new List<string>();
  53. dh.ExecuteSql("update ProductSOP set ps_attachsop='' where ps_prodcode='" + pr_code.Text + "'", "update");
  54. OperatResult.AppendText("一共Sheet: " + workbook.Worksheets.Count + "\n");
  55. try
  56. {
  57. for (int i = 0; i < workbook.Worksheets.Count; i++)
  58. {
  59. OperatResult.AppendText("正在解析Sheet" + workbook.Worksheets[i].Name + "\n");
  60. Worksheet she = workbook.Worksheets[i];
  61. if (JPG.Checked)
  62. {
  63. var myThread = new Thread(() => SaveFileToJPG(she));
  64. myThread.Start();
  65. filename.Add(she.Name + ".jpg");
  66. }
  67. }
  68. }
  69. catch (Exception ex)
  70. {
  71. OperatResult.AppendText(ex.Message + "\n");
  72. }
  73. }
  74. private void SaveFileToJPG(Worksheet sheet)
  75. {
  76. try
  77. {
  78. DataHelper dh = new DataHelper();
  79. OperatResult.AppendText("解析图片【" + sheet.Name + ".jpg】添加水印" + "\n");
  80. if (!Directory.Exists(Application.StartupPath + @"\" + pr_code.Text))
  81. {
  82. Directory.CreateDirectory(Application.StartupPath + @"\" + pr_code.Text);
  83. }
  84. int width = 0;
  85. int height = 0;
  86. //Console.WriteLine(sheet.Name);
  87. //for (int i = 0; i < sheet.Cells.Columns.Count; i++)
  88. //{
  89. // width +=(int) sheet.Cells.Columns[i].Width;
  90. //}
  91. //for (int i = 0; i < sheet.Cells.Rows.Count; i++)
  92. //{
  93. // height += (int)sheet.Cells.Rows[i].Height;
  94. //}
  95. //Console.WriteLine("width:"+width);
  96. //Console.WriteLine("height:"+height);
  97. sheet.Pictures.Add(0, 18, @"图片\电子受控章.png");
  98. sheet.Pictures.Add(0, 15, @"图片\签名.png");
  99. sheet.PageSetup.LeftMargin = 0;
  100. sheet.PageSetup.RightMargin = 0;
  101. sheet.PageSetup.BottomMargin = 0;
  102. sheet.PageSetup.TopMargin = 0;
  103. ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
  104. imgOptions.ImageFormat = ImageFormat.Png;
  105. imgOptions.OnePagePerSheet = true;
  106. imgOptions.PrintingPage = PrintingPageType.IgnoreBlank;
  107. imgOptions.SetDesiredSize(1565, 1043);
  108. SheetRender sr = new SheetRender((Aspose.Cells.Worksheet)sheet, imgOptions);
  109. sr.ToImage(0, Application.StartupPath + @"\" + pr_code.Text + @"\" + sheet.Name + ".jpg");
  110. //// 创建一个图片
  111. OperatResult.AppendText("解析图片【" + sheet.Name + ".jpg】" + "\n");
  112. Dictionary<string, object> dic = new Dictionary<string, object>();
  113. dic.Add("em_name", "管理员");
  114. dic.Add("em_code", "ADMIN");
  115. dic.Add("caller", "ProductSOP");
  116. OperatResult.AppendText("上传文件【" + sheet.Name + ".jpg" + "】\n");
  117. 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);
  118. string ps_id = dh.getFieldDataByCondition("ProductSOP", "ps_id", "ps_prodcode='" + pr_code.Text + "'").ToString();
  119. dh.ExecuteSql("insert into ProductSOPdetail(psd_id,psd_psid,psd_attach) select ProductSOPdetail_seq.nextval,ps_id,'" + sheet.Name + ".jpg" + ";" + fp_id + "' from ProductSOP where ps_prodcode='" + pr_code.Text + "'", "insert");
  120. //if (fp_id != "")
  121. //{
  122. // dh.ExecuteSql("update ProductSOP set ps_attachsop=ps_attachsop||" + fp_id + "||';' where ps_prodcode='" + pr_code.Text + "'", "update");
  123. //}
  124. }
  125. catch (Exception ex)
  126. {
  127. OperatResult.AppendText(ex.Message + ex.StackTrace);
  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 && !IpEntry.AddressList[i].ToString().Contains("10.10"))
  243. {
  244. IPAddress = IpEntry.AddressList[i].ToString();
  245. }
  246. }
  247. dh.ExecuteSql("update SOPSOURCE set SS_BRDIP='" + IPAddress + "'", "update");
  248. if (SendDGV.Rows.Count > 0)
  249. {
  250. Thread.Sleep(8000);
  251. List<Dictionary<string, string>> listr = new List<Dictionary<string, string>>();
  252. JavaScriptSerializer jss = new JavaScriptSerializer();
  253. Dictionary<string, object> map1 = new Dictionary<string, object>();
  254. Dictionary<string, object> map = new Dictionary<string, object>();
  255. string path;
  256. string pathroot = dh.GetConfig("filePathUrl", "sys").ToString();
  257. for (int i = 0; i < SendDGV.Rows.Count; i++)
  258. {
  259. if (SendDGV.Rows[i].Cells["CheckColumn"].FormattedValue.ToString().ToUpper() == "TRUE")
  260. {
  261. Dictionary<string, string> dic1 = new Dictionary<string, string>();
  262. path = SendDGV.Rows[i].Cells["fp_path"].Value.ToString();
  263. path = encryptBASE64(path.Replace("/app/uas/webapps/postattach", pathroot)).Replace("\\s*|\r|\n|\t", "");
  264. dic1.Add("path", path);
  265. dic1.Add("filename", SendDGV.Rows[i].Cells["fp_name"].Value.ToString().Replace(" ", "").Replace("(", "").Replace(")", ""));
  266. dic1.Add("ps_prodcode", pr_code.Text);
  267. listr.Add(dic1);
  268. }
  269. }
  270. map1.Add("ps_code", "");
  271. map1.Add("li_code", li_code.Text);
  272. map1.Add("url", listr);
  273. map.Add("success", true);
  274. map.Add("data", map1);
  275. tcpserver.Send(jss.Serialize(map));
  276. //Thread.Sleep(2000);
  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. Console.WriteLine(key);
  296. byte[] bytes = Encoding.Default.GetBytes(key);
  297. return Convert.ToBase64String(bytes);
  298. }
  299. public static string EncodeBase64(string code_type, string code)
  300. {
  301. string encode = "";
  302. byte[] bytes = Encoding.GetEncoding(code_type).GetBytes(code);
  303. try
  304. {
  305. encode = Convert.ToBase64String(bytes);
  306. }
  307. catch
  308. {
  309. encode = code;
  310. }
  311. return encode;
  312. }
  313. private void pr_code_KeyDown(object sender, KeyEventArgs e)
  314. {
  315. if (e.KeyCode == Keys.Enter)
  316. {
  317. LoadPrCode();
  318. }
  319. }
  320. private void LoadPrCode()
  321. {
  322. DataTable dt = (DataTable)dh.ExecuteSql("select * from productsop where ps_prodcode='" + pr_code.Text + "'", "select");
  323. if (dt.Rows.Count > 0)
  324. {
  325. string ps_id = dt.Rows[0]["ps_id"].ToString();
  326. dt = (DataTable)dh.ExecuteSql("select fp_name,fp_path,'' issend from ProductSOPdetail left join " +
  327. "filepath on FP_ID=substr(PSD_ATTACH,instr(PSD_ATTACH,';')+1) where psd_psid=" + ps_id, "select");
  328. if (dt.Rows.Count > 0)
  329. {
  330. SendDGV.DataSource = dt;
  331. }
  332. }
  333. }
  334. private void pr_code_Leave(object sender, EventArgs e)
  335. {
  336. LoadPrCode();
  337. }
  338. bool AllChecked = false;
  339. private void ChooseALL_Click(object sender, EventArgs e)
  340. {
  341. if (AllChecked)
  342. {
  343. foreach (DataGridViewRow dr in SendDGV.Rows)
  344. dr.Cells[0].Value = false;
  345. AllChecked = false;
  346. }
  347. else
  348. {
  349. foreach (DataGridViewRow dr in SendDGV.Rows)
  350. dr.Cells[0].Value = true;
  351. AllChecked = true;
  352. }
  353. }
  354. }
  355. }