Query_SOP.cs 30 KB

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