Query_SOP.cs 32 KB

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