SOP.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. string code = LogicHandler.GetPiInoutCode("ProductSOP", "1");
  41. 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");
  42. }
  43. else
  44. {
  45. string ps_id = dt.Rows[0]["ps_id"].ToString();
  46. dh.ExecuteSql("delete from ProductSOPdetail where psd_psid='" + ps_id + "'", "delete");
  47. pr_code.Text = dt.Rows[0]["ps_prodcode"].ToString();
  48. }
  49. Workbook workbook = new Workbook(FilePath.Text);
  50. List<Worksheet> list = new List<Worksheet>();
  51. List<string> filename = new List<string>();
  52. dh.ExecuteSql("update ProductSOP set ps_attachsop='' where ps_prodcode='" + pr_code.Text + "'", "update");
  53. OperatResult.AppendText("一共Sheet: " + workbook.Worksheets.Count + "\n");
  54. try
  55. {
  56. for (int i = 0; i < workbook.Worksheets.Count; i++)
  57. {
  58. OperatResult.AppendText("正在解析Sheet" + workbook.Worksheets[i].Name + "\n");
  59. Worksheet she = workbook.Worksheets[i];
  60. if (JPG.Checked)
  61. {
  62. var myThread = new Thread(() => SaveFileToJPG(she));
  63. myThread.Start();
  64. filename.Add(she.Name + ".jpg");
  65. }
  66. }
  67. }
  68. catch (Exception ex)
  69. {
  70. OperatResult.AppendText(ex.Message + "\n");
  71. }
  72. }
  73. private void SaveFileToJPG(Worksheet sheet)
  74. {
  75. try
  76. {
  77. DataHelper dh = new DataHelper();
  78. //OperatResult.AppendText("解析图片【" + sheet.Name + ".jpg】添加水印" + "\n");
  79. if (!Directory.Exists(Application.StartupPath + @"\" + pr_code.Text))
  80. {
  81. Directory.CreateDirectory(Application.StartupPath + @"\" + pr_code.Text);
  82. }
  83. int width = 0;
  84. int height = 0;
  85. //Console.WriteLine(sheet.Name);
  86. //for (int i = 0; i < sheet.Cells.Columns.Count; i++)
  87. //{
  88. // width +=(int) sheet.Cells.Columns[i].Width;
  89. //}
  90. //for (int i = 0; i < sheet.Cells.Rows.Count; i++)
  91. //{
  92. // height += (int)sheet.Cells.Rows[i].Height;
  93. //}
  94. //Console.WriteLine("width:"+width);
  95. //Console.WriteLine("height:"+height);
  96. //sheet.Pictures.Add(0, 18, @"图片\电子受控章.png");
  97. //sheet.Pictures.Add(0, 15, @"图片\签名.png");
  98. //sheet.PageSetup.LeftMargin = 0;
  99. //sheet.PageSetup.RightMargin = 0;
  100. //sheet.PageSetup.BottomMargin = 0;
  101. //sheet.PageSetup.TopMargin = 0;
  102. ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
  103. imgOptions.ImageFormat = ImageFormat.Png;
  104. imgOptions.OnePagePerSheet = true;
  105. imgOptions.PrintingPage = PrintingPageType.IgnoreBlank;
  106. imgOptions.SetDesiredSize(1565, 1043);
  107. SheetRender sr = new SheetRender((Aspose.Cells.Worksheet)sheet, imgOptions);
  108. sr.ToImage(0, Application.StartupPath + @"\" + pr_code.Text + @"\" + sheet.Name + ".jpg");
  109. //// 创建一个图片
  110. OperatResult.AppendText("解析图片【" + sheet.Name + ".jpg】" + "\n");
  111. Dictionary<string, object> dic = new Dictionary<string, object>();
  112. dic.Add("em_name", "管理员");
  113. dic.Add("em_code", "ADMIN");
  114. dic.Add("caller", "ProductSOP");
  115. OperatResult.AppendText("上传文件【" + sheet.Name + ".jpg" + "】\n");
  116. 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);
  117. string ps_id = dh.getFieldDataByCondition("ProductSOP", "ps_id", "ps_prodcode='" + pr_code.Text + "'").ToString();
  118. 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");
  119. //if (fp_id != "")
  120. //{
  121. // dh.ExecuteSql("update ProductSOP set ps_attachsop=ps_attachsop||" + fp_id + "||';' where ps_prodcode='" + pr_code.Text + "'", "update");
  122. //}
  123. }
  124. catch (Exception ex)
  125. {
  126. OperatResult.AppendText(ex.Message + ex.StackTrace);
  127. }
  128. }
  129. /// <summary>
  130. /// 请求上传图片到阿里云
  131. /// </summary>
  132. /// <param name="url">上传地址</param>
  133. /// <param name="filepath">本地文件路径</param>
  134. /// <param name="dic">上传的数据信息</param>
  135. /// <returns></returns>
  136. public string UploadFilesToRemoteUrl(string url1, string filepath, Dictionary<string, object> dic)
  137. {
  138. try
  139. {
  140. ServicePointManager.DefaultConnectionLimit = 50;
  141. string boundary = DateTime.Now.Ticks.ToString("x");
  142. byte[] boundarybytes = System.Text.Encoding.UTF8.GetBytes("--" + boundary + "\r\n");
  143. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url1);
  144. request.Method = "POST";
  145. request.Timeout = 10 * 10000;
  146. request.ContentType = "multipart/form-data; boundary=" + boundary;
  147. Stream rs = request.GetRequestStream();
  148. var endBoundaryBytes = Encoding.UTF8.GetBytes("--" + boundary + "--\r\n");
  149. string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n" + "\r\n" + "{1}" + "\r\n";
  150. if (dic != null)
  151. {
  152. foreach (string key in dic.Keys)
  153. {
  154. rs.Write(boundarybytes, 0, boundarybytes.Length);
  155. string formitem = string.Format(formdataTemplate, key, dic[key]);
  156. byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
  157. rs.Write(formitembytes, 0, formitembytes.Length);
  158. }
  159. }
  160. string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n\r\n";
  161. {
  162. rs.Write(boundarybytes, 0, boundarybytes.Length);
  163. var header = string.Format(headerTemplate, "file", Path.GetFileName(filepath));
  164. var headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
  165. rs.Write(headerbytes, 0, headerbytes.Length);
  166. using (var fileStream = new FileStream(filepath, FileMode.Open, FileAccess.Read))
  167. {
  168. var buffer = new byte[1024];
  169. var bytesRead = 0;
  170. while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
  171. {
  172. rs.Write(buffer, 0, bytesRead);
  173. }
  174. }
  175. var cr = Encoding.UTF8.GetBytes("\r\n");
  176. rs.Write(cr, 0, cr.Length);
  177. }
  178. rs.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
  179. var response = request.GetResponse() as HttpWebResponse;
  180. StreamReader newReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
  181. string Content = newReader.ReadToEnd();
  182. Dictionary<string, object> dic1 = new Dictionary<string, object>();
  183. List<Dictionary<string, object>> dic2 = null;
  184. dic1 = BaseUtil.ToDictionary(Content);
  185. dic2 = dic1["data"] as List<Dictionary<string, object>>;
  186. string fp_id = "";
  187. if (dic2[0]["filepath"] != null)
  188. {
  189. fp_id = dic2[0]["filepath"].ToString();
  190. }
  191. if (response.StatusCode == HttpStatusCode.OK)
  192. {
  193. return fp_id;
  194. }
  195. }
  196. catch (Exception e)
  197. {
  198. LogicHandler.DoCommandLog("SOP", usercode, "", "", sourcecode, "上传SOP", "上传失败", pr_code.Text, "");
  199. Console.WriteLine(e.Message + e.StackTrace);
  200. }
  201. return "";
  202. }
  203. private void ChooseFile_Click(object sender, EventArgs e)
  204. {
  205. ChoosePath.Filter = "(*.xls)|*.xls|(*.xlsx)|*.xlsx";
  206. DialogResult result = ChoosePath.ShowDialog();
  207. if (result == DialogResult.OK)
  208. {
  209. FilePath.Text = ChoosePath.FileName;
  210. }
  211. }
  212. private void UploadSOP_Click(object sender, EventArgs e)
  213. {
  214. Thread thread = new Thread(uploadfile);
  215. SetLoadingWindow stw = new SetLoadingWindow(thread, "上传文件");
  216. stw.StartPosition = FormStartPosition.CenterScreen;
  217. stw.ShowDialog();
  218. if (JPG.Checked)
  219. {
  220. LogicHandler.DoCommandLog("SOP", usercode, "", "", sourcecode, "上传SOP【图片格式】", "上传成功", pr_code.Text, "");
  221. }
  222. else
  223. {
  224. LogicHandler.DoCommandLog("SOP", usercode, "", "", sourcecode, "上传SOP【PDF格式】", "上传成功", pr_code.Text, "");
  225. }
  226. }
  227. private void OperatResult_TextChanged(object sender, EventArgs e)
  228. {
  229. //OperatResult.SelectionStart = Text.Length;
  230. OperatResult.ScrollToCaret();
  231. }
  232. private void SendSop_Click(object sender, EventArgs e)
  233. {
  234. try
  235. {
  236. TcpServer tcpserver = new TcpServer();
  237. tcpserver.Start();
  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(5000);
  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. }