Query_SOP.cs 30 KB

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