Query_SOP.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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.Drawing.Imaging;
  10. using System.Drawing;
  11. using UAS_MES_NEW.Entity;
  12. using UAS_MES_NEW.PublicMethod;
  13. using Aspose.Cells;
  14. using UAS_MES_NEW.DataOperate;
  15. using UAS_MES_NEW.PublicForm;
  16. using Aspose.Cells.Drawing;
  17. using Aspose.Cells.Rendering;
  18. using System.Web.Script.Serialization;
  19. using Seagull.BarTender.Print;
  20. namespace UAS_MES_NEW.Query
  21. {
  22. public partial class Query_SOP : Form
  23. {
  24. DataHelper dh;
  25. string usercode;
  26. string sourcecode;
  27. DataTable Dbfind;
  28. public Query_SOP()
  29. {
  30. usercode = "";
  31. sourcecode = "";
  32. InitializeComponent();
  33. }
  34. private void Form3_Load(object sender, EventArgs e)
  35. {
  36. CheckForIllegalCrossThreadCalls = false;
  37. ps_prodcode.TableName = "productsop";
  38. ps_prodcode.SelectField = "ps_code # 流水号,ps_prodcode # SOP编号";
  39. ps_prodcode.FormName = Name;
  40. ps_prodcode.DBTitle = "SOP查询";
  41. ps_prodcode.SetValueField = new string[] { "ps_code", "ps_prodcode" };
  42. ps_prodcode.Condition = "";
  43. ps_prodcode.DbChange += nr_rule_DBChange;
  44. pr_code.TableName = "product";
  45. pr_code.SelectField = "pr_code # 产品编号,pr_detail # 产品名称,pr_spec # 产品规格";
  46. pr_code.FormName = Name;
  47. pr_code.DBTitle = "物料查询";
  48. pr_code.Condition = "pr_code not like '%LX%'";
  49. pr_code.SetValueField = new string[] { "pr_code" };
  50. DateTime targetMonth = DateCode.Value.AddMonths(2);
  51. DateTime endOfMonth = new DateTime(targetMonth.Year, targetMonth.Month,
  52. DateTime.DaysInMonth(targetMonth.Year, targetMonth.Month));
  53. DateCode.Value = endOfMonth;
  54. dh = SystemInf.dh;
  55. }
  56. private void nr_rule_DBChange(object sender, EventArgs e)
  57. {
  58. Dbfind = ps_prodcode.ReturnData;
  59. BaseUtil.SetFormValue(this.Controls, Dbfind);
  60. }
  61. void uploadfile()
  62. {
  63. if (FilePath.Text == "")
  64. {
  65. MessageBox.Show("文件不能为空!");
  66. return;
  67. }
  68. //if (pr_code.Text == "")
  69. //{
  70. // MessageBox.Show("产品编号不能为空!");
  71. // return;
  72. //}
  73. if (JPG.Checked)
  74. {
  75. ps_prodcode.Text = FilePath.Text.Substring(FilePath.Text.LastIndexOf(@"\") + 1).Replace(".xls", "").Replace(".xlsx", "");
  76. pr_code_Leave(new object(), new EventArgs());
  77. DataTable dt = (DataTable)dh.ExecuteSql("select * from productsop where ps_prodcode='" + ps_prodcode.Text + "' ", "select");
  78. if (dt.Rows.Count == 0)
  79. {
  80. string code = LogicHandler.GetPiInoutCode("ProductSOP", "1");
  81. ps_code.Text = code;
  82. if (VaildDate.Checked)
  83. {
  84. dh.ExecuteSql("insert into productsop(ps_id,ps_code,ps_prodcode,ps_indate,ps_inman,ps_status,ps_statuscode,ps_vailddate,ps_prcode)values(productsop_seq.nextval,'" + code + "','" + ps_prodcode.Text + "',sysdate,'" + User.UserName + "','在录入','ENTERING',to_date('" + DateCode.Value.ToString("yyyy-MM-dd") + "','yyyy-mm-dd'),'" + pr_code.Text + "')", "insert");
  85. }
  86. else
  87. {
  88. dh.ExecuteSql("insert into productsop(ps_id,ps_code,ps_prodcode,ps_indate,ps_inman,ps_status,ps_statuscode,ps_prcode)values(productsop_seq.nextval,'" + code + "','" + ps_prodcode.Text + "',sysdate,'" + User.UserName + "','在录入','ENTERING','" + pr_code.Text + "')", "insert");
  89. }
  90. }
  91. else
  92. {
  93. string ps_id = dt.Rows[0]["ps_id"].ToString();
  94. dh.ExecuteSql("delete from ProductSOPdetail where psd_psid='" + ps_id + "'", "delete");
  95. ps_prodcode.Text = dt.Rows[0]["ps_prodcode"].ToString();
  96. }
  97. Workbook workbook = new Workbook(FilePath.Text);
  98. List<Worksheet> list = new List<Worksheet>();
  99. List<string> filename = new List<string>();
  100. if (VaildDate.Checked)
  101. {
  102. dh.ExecuteSql("update ProductSOP set ps_attachsop='',ps_inman='" + User.UserName + "',ps_vailddate=to_date('" + DateCode.Value.ToString("yyyy-MM-dd") + "','yyyy-mm-dd') where ps_prodcode='" + ps_prodcode.Text + "'", "update");
  103. }
  104. else
  105. {
  106. dh.ExecuteSql("update ProductSOP set ps_attachsop='',ps_inman='" + User.UserName + "' where ps_prodcode='" + ps_prodcode.Text + "'", "update");
  107. }
  108. OperatResult.AppendText("一共Sheet: " + workbook.Worksheets.Count + "\n");
  109. for (int i = 0; i < workbook.Worksheets.Count; i++)
  110. {
  111. OperatResult.AppendText("正在解析Sheet" + workbook.Worksheets[i].Name + "\n");
  112. if (JPG.Checked)
  113. {
  114. //try
  115. //{
  116. // ---------- 输出目录 ----------
  117. if (!Directory.Exists(Application.StartupPath + @"\" + ps_prodcode.Text))
  118. {
  119. Directory.CreateDirectory(Application.StartupPath + @"\" + ps_prodcode.Text);
  120. }
  121. string path = Application.StartupPath + @"\" + ps_prodcode.Text + @"\";
  122. Worksheet she = workbook.Worksheets[i];
  123. // ---------- 基础参数 ----------
  124. int lastColIndex = she.Cells.MaxDataColumn + 3;
  125. string lastColLetter = CellsHelper.ColumnIndexToName(lastColIndex);
  126. const int headerRows = 3;
  127. int dataRowsPerPage = int.Parse(RowCount.Value.ToString());
  128. int contentMaxRow = she.Cells.MaxDataRow;
  129. for (int p = 0; p < she.Pictures.Count; p++)
  130. {
  131. if (she.Pictures[p].LowerRightRow > contentMaxRow)
  132. contentMaxRow = she.Pictures[p].LowerRightRow;
  133. }
  134. for (int s = 0; s < she.Shapes.Count; s++)
  135. {
  136. if (she.Shapes[s].LowerRightRow > contentMaxRow)
  137. contentMaxRow = she.Shapes[s].LowerRightRow;
  138. }
  139. int actualDataRows = Math.Max(contentMaxRow + 1 - headerRows, 0);
  140. int totalPages = (int)Math.Ceiling(actualDataRows / (double)dataRowsPerPage);
  141. if (totalPages == 0) totalPages = 1;
  142. int totalRows = headerRows + totalPages * dataRowsPerPage;
  143. int totalDataRows = Math.Max(totalRows - headerRows, 0);
  144. int wmRowGlobal = int.Parse(Y.Value.ToString());
  145. int wmCol = int.Parse(X.Value.ToString());
  146. int imageIndex = 1;
  147. for (int page = 0; page < totalPages; page++)
  148. {
  149. // ---------- 1. 复制原表到临时工作表 ----------
  150. int newSheetIndex = workbook.Worksheets.Add();
  151. Worksheet newshe = workbook.Worksheets[newSheetIndex];
  152. newshe.Copy(she);
  153. newshe.Name = $"Temp_{page}";
  154. int dataStartRow = headerRows + page * dataRowsPerPage;
  155. int dataEndRow = Math.Min(headerRows + (page + 1) * dataRowsPerPage - 1, totalRows - 1);
  156. // ---------- 2. 按页过滤原表图片(Pictures) ----------
  157. for (int p = newshe.Pictures.Count - 1; p >= 0; p--)
  158. {
  159. Aspose.Cells.Drawing.Picture pic = newshe.Pictures[p];
  160. int topRow = pic.UpperLeftRow;
  161. bool isHeaderPic = topRow < headerRows;
  162. bool belongsToPage = topRow >= dataStartRow && topRow <= dataEndRow;
  163. if (!isHeaderPic && !belongsToPage)
  164. {
  165. newshe.Pictures.RemoveAt(p);
  166. }
  167. }
  168. // ---------- 3. 按页过滤原表形状(Shapes) ----------
  169. for (int s = newshe.Shapes.Count - 1; s >= 0; s--)
  170. {
  171. Aspose.Cells.Drawing.Shape shape;
  172. try { shape = newshe.Shapes[s]; }
  173. catch { continue; }
  174. int topRow = shape.UpperLeftRow;
  175. bool isHeaderShape = topRow < headerRows;
  176. bool belongsToPage = topRow >= dataStartRow && topRow <= dataEndRow;
  177. if (!isHeaderShape && !belongsToPage)
  178. {
  179. try { newshe.Shapes.RemoveAt(s); }
  180. catch { shape.IsHidden = true; }
  181. }
  182. }
  183. // ---------- 4. 清空非本页单元格内容 ----------
  184. for (int r = 0; r < totalRows; r++)
  185. {
  186. if (r < headerRows) continue;
  187. if (r >= dataStartRow && r <= dataEndRow) continue;
  188. newshe.Cells.ClearRange(r, 0, r, lastColIndex);
  189. }
  190. // ---------- 5. 删除非本页整行 ----------
  191. for (int r = totalRows - 1; r >= 0; r--)
  192. {
  193. if (r < headerRows) continue;
  194. if (r >= dataStartRow && r <= dataEndRow) continue;
  195. newshe.Cells.DeleteRow(r);
  196. }
  197. // ---------- 6. 页面设置 ----------
  198. int printMaxRow = newshe.Cells.MaxDataRow;
  199. for (int p = 0; p < newshe.Pictures.Count; p++)
  200. if (newshe.Pictures[p].LowerRightRow > printMaxRow) printMaxRow = newshe.Pictures[p].LowerRightRow;
  201. for (int s = 0; s < newshe.Shapes.Count; s++)
  202. if (newshe.Shapes[s].LowerRightRow > printMaxRow) printMaxRow = newshe.Shapes[s].LowerRightRow;
  203. int newTotalRows = printMaxRow + 1;
  204. int pageHeight = headerRows + dataRowsPerPage;
  205. int printEndRow = Math.Max(newTotalRows, pageHeight);
  206. if (i == 0)
  207. newshe.PageSetup.PrintArea = $"A1:{lastColLetter}{25}";
  208. else
  209. newshe.PageSetup.PrintArea = $"A1:{lastColLetter}{printEndRow}";
  210. newshe.PageSetup.CenterHorizontally = true;
  211. newshe.PageSetup.CenterVertically = true;
  212. newshe.PageSetup.LeftMargin = 2;
  213. newshe.PageSetup.RightMargin = 2;
  214. newshe.PageSetup.TopMargin = 2;
  215. newshe.PageSetup.BottomMargin = 2;
  216. newshe.PageSetup.SetFooter(0, "");
  217. newshe.PageSetup.SetFooter(1, "");
  218. newshe.PageSetup.SetFooter(2, "");
  219. // ---------- 7. 插入水印 ----------
  220. if (File.Exists(PrintPath.Text))
  221. {
  222. int rowOffset = headerRows + int.Parse(Y.Value.ToString());
  223. newshe.Pictures.Add(rowOffset, int.Parse(X.Value.ToString()), PrintPath.Text);
  224. }
  225. // ---------- 8. 页脚 ----------
  226. string pageFooter = $"第 {imageIndex} 页 / 共 {totalPages} 页";
  227. newshe.PageSetup.SetFooter(1, pageFooter);
  228. // ==================★ 优化点:提高渲染分辨率(核心改动) ==================
  229. ImageOrPrintOptions options = new ImageOrPrintOptions();
  230. options.ImageFormat = ImageFormat.Png;
  231. options.OnePagePerSheet = true; // 保留:整页一张图
  232. options.PrintingPage = PrintingPageType.Default;
  233. // ★ 把默认 96 DPI 提高到 300 DPI,文字/表格瞬间变清晰(约放大3倍像素)
  234. options.HorizontalResolution = 300;
  235. options.VerticalResolution = 300;
  236. // ★ 开启抗锯齿,边缘更平滑(18.x 支持,可选)
  237. options.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  238. // ★ 文本渲染更清晰(可选)
  239. options.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
  240. // ==================★ 优化点结束 ==================
  241. SheetRender sr = new SheetRender(newshe, options);
  242. if (sr.PageCount > 0)
  243. {
  244. using (Bitmap bitmap = sr.ToImage(0))
  245. {
  246. string imagename = $"{she.Name}_{imageIndex}.png";
  247. string imagePath = Path.Combine(path, imagename);
  248. bitmap.Save(imagePath, ImageFormat.Png);
  249. OperatResult.AppendText("解析图片【" + imagePath + "】\n");
  250. // ---------- 10. 上传 ----------
  251. Dictionary<string, object> dic = new Dictionary<string, object>();
  252. dic.Add("em_name", "管理员");
  253. dic.Add("em_code", "ADMIN");
  254. dic.Add("caller", "ProductSOP");
  255. OperatResult.AppendText("上传文件【" + imagePath + "】\n");
  256. string fp_id = UploadFilesToRemoteUrl(
  257. "https://mes.lihantech.cn/mes/MEScommon/uploadFiles.action?_noc=1",
  258. imagePath, dic);
  259. // ---------- 11. 入库 ----------
  260. string ps_id = dh.getFieldDataByCondition(
  261. "ProductSOP", "ps_id", "ps_prodcode='" + ps_prodcode.Text + "'").ToString();
  262. dh.ExecuteSql(
  263. "insert into ProductSOPdetail(psd_id,psd_psid,psd_attach) " +
  264. "select ProductSOPdetail_seq.nextval,ps_id,'" + imagename + ";" + fp_id +
  265. "' from ProductSOP where ps_prodcode='" + ps_prodcode.Text + "'", "insert");
  266. }
  267. }
  268. // ---------- 12. 删除临时工作表 ----------
  269. workbook.Worksheets.RemoveAt(newSheetIndex);
  270. imageIndex++;
  271. }
  272. //}
  273. //catch (Exception ex)
  274. //{
  275. // OperatResult.AppendText(ex.Message + ex.StackTrace);
  276. //}
  277. }
  278. }
  279. }
  280. else if (PDF.Checked)
  281. {
  282. ps_prodcode.Text = FilePath.Text.Substring(FilePath.Text.LastIndexOf(@"\") + 1).Replace(".pdf", "");
  283. pr_code_Leave(new object(), new EventArgs());
  284. DataTable dt = (DataTable)dh.ExecuteSql("select * from productsop where ps_prodcode='" + ps_prodcode.Text + "' ", "select");
  285. if (dt.Rows.Count == 0)
  286. {
  287. string code = LogicHandler.GetPiInoutCode("ProductSOP", "1");
  288. ps_code.Text = code;
  289. if (VaildDate.Checked)
  290. {
  291. dh.ExecuteSql("insert into productsop(ps_id,ps_code,ps_prodcode,ps_indate,ps_inman,ps_status,ps_statuscode,ps_vailddate)values(productsop_seq.nextval,'" + code + "','" + ps_prodcode.Text + "',sysdate,'" + User.UserName + "','在录入','ENTERING',to_date('" + DateCode.Value.ToString("yyyy-MM-dd") + "','yyyy-mm-dd'))", "insert");
  292. }
  293. else
  294. {
  295. 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,'" + User.UserName + "','在录入','ENTERING')", "insert");
  296. }
  297. }
  298. else
  299. {
  300. string psid = dt.Rows[0]["ps_id"].ToString();
  301. dh.ExecuteSql("delete from ProductSOPdetail where psd_psid='" + psid + "'", "delete");
  302. ps_prodcode.Text = dt.Rows[0]["ps_prodcode"].ToString();
  303. }
  304. if (VaildDate.Checked)
  305. {
  306. dh.ExecuteSql("update ProductSOP set ps_attachsop='',ps_inman='" + User.UserName + "',ps_vailddate=to_date('" + DateCode.Value.ToString("yyyy-MM-dd") + "','yyyy-mm-dd') where ps_prodcode='" + ps_prodcode.Text + "'", "update");
  307. }
  308. else
  309. {
  310. dh.ExecuteSql("update ProductSOP set ps_attachsop='',ps_inman='" + User.UserName + "' where ps_prodcode='" + ps_prodcode.Text + "'", "update");
  311. }
  312. Dictionary<string, object> dic = new Dictionary<string, object>();
  313. dic.Add("em_name", "管理员");
  314. dic.Add("em_code", "ADMIN");
  315. dic.Add("caller", "ProductSOP");
  316. OperatResult.AppendText("上传文件【" + ps_prodcode.Text + "" + "】\n");
  317. string fp_id = UploadFilesToRemoteUrl("https://mes.lihantech.cn/mes/MEScommon/uploadFiles.action?_noc=1", FilePath.Text, dic);
  318. string ps_id = dh.getFieldDataByCondition("ProductSOP", "ps_id", "ps_prodcode='" + ps_prodcode.Text + "'").ToString();
  319. dh.ExecuteSql("insert into ProductSOPdetail(psd_id,psd_psid,psd_attach) select ProductSOPdetail_seq.nextval,ps_id,'" + ps_prodcode.Text + ";" + fp_id + "' from ProductSOP where ps_prodcode='" + ps_prodcode.Text + "'", "insert");
  320. }
  321. }
  322. /// <summary>
  323. /// 请求上传图片到阿里云
  324. /// </summary>
  325. /// <param name="url">上传地址</param>
  326. /// <param name="filepath">本地文件路径</param>
  327. /// <param name="dic">上传的数据信息</param>
  328. /// <returns></returns>
  329. public string UploadFilesToRemoteUrl(string url1, string filepath, Dictionary<string, object> dic)
  330. {
  331. try
  332. {
  333. ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11;
  334. ServicePointManager.DefaultConnectionLimit = 50;
  335. string boundary = DateTime.Now.Ticks.ToString("x");
  336. byte[] boundarybytes = System.Text.Encoding.UTF8.GetBytes("--" + boundary + "\r\n");
  337. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url1);
  338. request.Method = "POST";
  339. request.Timeout = 10 * 10000;
  340. request.ContentType = "multipart/form-data; boundary=" + boundary;
  341. Stream rs = request.GetRequestStream();
  342. var endBoundaryBytes = Encoding.UTF8.GetBytes("--" + boundary + "--\r\n");
  343. string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n" + "\r\n" + "{1}" + "\r\n";
  344. if (dic != null)
  345. {
  346. foreach (string key in dic.Keys)
  347. {
  348. rs.Write(boundarybytes, 0, boundarybytes.Length);
  349. string formitem = string.Format(formdataTemplate, key, dic[key]);
  350. byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
  351. rs.Write(formitembytes, 0, formitembytes.Length);
  352. }
  353. }
  354. string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n\r\n";
  355. {
  356. rs.Write(boundarybytes, 0, boundarybytes.Length);
  357. var header = string.Format(headerTemplate, "file", Path.GetFileName(filepath));
  358. var headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
  359. rs.Write(headerbytes, 0, headerbytes.Length);
  360. using (var fileStream = new FileStream(filepath, FileMode.Open, FileAccess.Read))
  361. {
  362. var buffer = new byte[1024];
  363. var bytesRead = 0;
  364. while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
  365. {
  366. rs.Write(buffer, 0, bytesRead);
  367. }
  368. }
  369. var cr = Encoding.UTF8.GetBytes("\r\n");
  370. rs.Write(cr, 0, cr.Length);
  371. }
  372. rs.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
  373. var response = request.GetResponse() as HttpWebResponse;
  374. StreamReader newReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
  375. string Content = newReader.ReadToEnd();
  376. Dictionary<string, object> dic1 = new Dictionary<string, object>();
  377. List<Dictionary<string, object>> dic2 = null;
  378. dic1 = BaseUtil.ToDictionary(Content);
  379. dic2 = dic1["data"] as List<Dictionary<string, object>>;
  380. string fp_id = "";
  381. if (dic2[0]["filepath"] != null)
  382. {
  383. fp_id = dic2[0]["filepath"].ToString();
  384. }
  385. if (response.StatusCode == HttpStatusCode.OK)
  386. {
  387. return fp_id;
  388. }
  389. }
  390. catch (Exception e)
  391. {
  392. LogicHandler.DoCommandLog("SOP", usercode, "", "", sourcecode, "上传SOP", "上传失败", ps_prodcode.Text, "");
  393. Console.WriteLine(e.Message + e.StackTrace);
  394. }
  395. return "";
  396. }
  397. private void ChooseFile_Click(object sender, EventArgs e)
  398. {
  399. ChoosePath.Filter = "(*.xlsx)|*.xlsx|(*.xls)|*.xls|(*.pdf)|*.pdf";
  400. DialogResult result = ChoosePath.ShowDialog();
  401. if (result == DialogResult.OK)
  402. {
  403. FilePath.Text = ChoosePath.FileName;
  404. }
  405. }
  406. private void UploadSOP_Click(object sender, EventArgs e)
  407. {
  408. Thread thread = new Thread(uploadfile);
  409. SetLoadingWindow stw = new SetLoadingWindow(thread, "上传文件");
  410. stw.StartPosition = FormStartPosition.CenterScreen;
  411. stw.ShowDialog();
  412. if (JPG.Checked)
  413. {
  414. LogicHandler.DoCommandLog("SOP", usercode, "", "", sourcecode, "上传SOP【图片格式】", "上传成功", ps_prodcode.Text, "");
  415. }
  416. else
  417. {
  418. LogicHandler.DoCommandLog("SOP", usercode, "", "", sourcecode, "上传SOP【PDF格式】", "上传成功", ps_prodcode.Text, "");
  419. }
  420. }
  421. private void OperatResult_TextChanged(object sender, EventArgs e)
  422. {
  423. //OperatResult.SelectionStart = Text.Length;
  424. OperatResult.ScrollToCaret();
  425. }
  426. private void SendSop_Click(object sender, EventArgs e)
  427. {
  428. try
  429. {
  430. if (ps_prodcode.Text == "")
  431. {
  432. MessageBox.Show("线别和SOP文档不能为空");
  433. return;
  434. }
  435. if (SendDGV.Rows.Count > 0)
  436. {
  437. List<Dictionary<string, string>> listr = new List<Dictionary<string, string>>();
  438. JavaScriptSerializer jss = new JavaScriptSerializer();
  439. Dictionary<string, object> map1 = new Dictionary<string, object>();
  440. Dictionary<string, object> map = new Dictionary<string, object>();
  441. string path;
  442. string pathroot = dh.GetConfig("filePathUrl", "sys").ToString();
  443. dh.ExecuteSql("insert into soprelease(SR_ID, sr_pscode, SR_STATUS, SR_MACHINETYPE,sr_releaseman,sr_readman)" +
  444. "select soprelease_seq.nextval,'" + ps_code.Text + "',0,'" + ps_prodcode.Text + "','" + User.UserName + "',em_code from employee ", "insert");
  445. LogicHandler.DoCommandLog("SOP", usercode, "", "", sourcecode, "广播SOP", "广播成功", ps_prodcode.Text, "");
  446. MessageBox.Show("文档[" + ps_prodcode.Text + "]下放成功");
  447. }
  448. else
  449. {
  450. MessageBox.Show("产品编号" + ps_prodcode.Text + "未维护SOP文档");
  451. LogicHandler.DoCommandLog("SOP", usercode, "", "", sourcecode, "广播SOP", "广播失败,未维护SOP文档", ps_prodcode.Text, "");
  452. }
  453. }
  454. catch (Exception ex)
  455. {
  456. LogicHandler.DoCommandLog("SOP", usercode, "", "", sourcecode, "广播SOP", "广播失败", ps_prodcode.Text, "");
  457. Console.WriteLine(ex.Message + ex.StackTrace);
  458. }
  459. }
  460. public string encryptBASE64(string key)
  461. {
  462. Console.WriteLine(key);
  463. byte[] bytes = Encoding.Default.GetBytes(key);
  464. return Convert.ToBase64String(bytes);
  465. }
  466. public static string EncodeBase64(string code_type, string code)
  467. {
  468. string encode = "";
  469. byte[] bytes = Encoding.GetEncoding(code_type).GetBytes(code);
  470. try
  471. {
  472. encode = Convert.ToBase64String(bytes);
  473. }
  474. catch
  475. {
  476. encode = code;
  477. }
  478. return encode;
  479. }
  480. private void pr_code_KeyDown(object sender, KeyEventArgs e)
  481. {
  482. if (e.KeyCode == Keys.Enter)
  483. {
  484. LoadPrCode();
  485. }
  486. }
  487. private BindingSource bs = new BindingSource(); // 类成员变量
  488. private void LoadPrCode()
  489. {
  490. DataTable dt = (DataTable)dh.ExecuteSql("select * from productsop where ps_prodcode='" + ps_prodcode.Text + "'", "select");
  491. if (dt.Rows.Count > 0)
  492. {
  493. string ps_id = dt.Rows[0]["ps_id"].ToString();
  494. dt = (DataTable)dh.ExecuteSql(@"select fp_name,fp_path,'' issend,psd_iskey
  495. from ProductSOPdetail
  496. left join filepath on FP_ID=substr(PSD_ATTACH,instr(PSD_ATTACH,';')+1)
  497. where psd_psid=" + ps_id, "select");
  498. if (dt.Rows.Count > 0)
  499. {
  500. // 在 UI 线程安全执行(如果已经在 UI 线程则直接调用)
  501. if (SendDGV.InvokeRequired)
  502. SendDGV.Invoke(new Action(() => BindData(dt)));
  503. else
  504. BindData(dt);
  505. }
  506. }
  507. }
  508. private void BindData(DataTable dt)
  509. {
  510. // 使用 BindingSource 避免直接置 null
  511. if (SendDGV.DataSource == null)
  512. {
  513. bs.DataSource = dt;
  514. SendDGV.DataSource = bs;
  515. }
  516. else
  517. {
  518. bs.DataSource = dt; // 直接替换内部数据,控件不会经历“无数据”状态
  519. }
  520. // 可选:刷新显示
  521. SendDGV.Refresh();
  522. }
  523. private void pr_code_Leave(object sender, EventArgs e)
  524. {
  525. //LoadPrCode();
  526. }
  527. bool AllChecked = false;
  528. private void ChooseALL_Click(object sender, EventArgs e)
  529. {
  530. if (AllChecked)
  531. {
  532. foreach (DataGridViewRow dr in SendDGV.Rows)
  533. dr.Cells[0].Value = false;
  534. AllChecked = false;
  535. }
  536. else
  537. {
  538. foreach (DataGridViewRow dr in SendDGV.Rows)
  539. dr.Cells[0].Value = true;
  540. AllChecked = true;
  541. }
  542. }
  543. private void ps_prodcode_UserControlTextChanged(object sender, EventArgs e)
  544. {
  545. LoadPrCode();
  546. }
  547. private void GetSOP_Click(object sender, EventArgs e)
  548. {
  549. LoadPrCode();
  550. }
  551. private void ChoosePrint_Click(object sender, EventArgs e)
  552. {
  553. ChoosePath.Filter = "(*.png)|*.png";
  554. DialogResult result = ChoosePath.ShowDialog();
  555. if (result == DialogResult.OK)
  556. {
  557. PrintPath.Text = ChoosePath.FileName;
  558. }
  559. }
  560. private void SendDGV_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
  561. {
  562. if (e.RowIndex < 0 || e.ColumnIndex < 0) return;
  563. }
  564. }
  565. }