SOP.cs 18 KB

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