SOP.cs 17 KB

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