Query_SOP.cs 31 KB

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