Query_SOP.cs 29 KB

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