Query_SOP_CY.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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.Web.Script.Serialization;
  10. using Aspose.Cells.Rendering;
  11. using Aspose.Cells;
  12. using System.Drawing.Imaging;
  13. using UAS_MES_NEW.DataOperate;
  14. using UAS_MES_NEW.Entity;
  15. using UAS_MES_NEW.PublicMethod;
  16. using UAS_MES_NEW.PublicForm;
  17. namespace UAS_MES_NEW.Query
  18. {
  19. public partial class Query_SOP_CY : Form
  20. {
  21. DataHelper dh = new DataHelper();
  22. string usercode;
  23. string sourcecode;
  24. DataTable Dbfind;
  25. public Query_SOP_CY(string iUserCode, string iSource)
  26. {
  27. usercode = iUserCode;
  28. sourcecode = iSource;
  29. InitializeComponent();
  30. }
  31. private void Form3_Load(object sender, EventArgs e)
  32. {
  33. CheckForIllegalCrossThreadCalls = false;
  34. ps_prodcode.TableName = "productsop";
  35. ps_prodcode.SelectField = "ps_prodcode # SOP编号";
  36. ps_prodcode.FormName = Name;
  37. ps_prodcode.DBTitle = "SOP查询";
  38. ps_prodcode.SetValueField = new string[] { "ps_prodcode" };
  39. ps_prodcode.Condition = "";
  40. ps_prodcode.DbChange += nr_rule_DBChange;
  41. li_code.TableName = "line";
  42. li_code.SelectField = "li_code # 线别编号,li_name # 线别名称";
  43. li_code.FormName = Name;
  44. li_code.DBTitle = "线别查询";
  45. li_code.SetValueField = new string[] { "li_code" };
  46. li_code.Condition = "";
  47. li_code.DbChange += nr_rule_DBChange;
  48. SystemInf.dh = dh;
  49. }
  50. private void nr_rule_DBChange(object sender, EventArgs e)
  51. {
  52. Dbfind = ps_prodcode.ReturnData;
  53. BaseUtil.SetFormValue(this.Controls, Dbfind);
  54. }
  55. void uploadfile()
  56. {
  57. if (FilePath.Text == "")
  58. {
  59. MessageBox.Show("文件不能为空!");
  60. return;
  61. }
  62. ps_prodcode.Text = FilePath.Text.Substring(FilePath.Text.LastIndexOf(@"\") + 1).Replace(".xls", "").Replace(".xlsx", "");
  63. pr_code_Leave(new object(), new EventArgs());
  64. DataTable dt = (DataTable)dh.ExecuteSql("select * from productsop where ps_prodcode='" + ps_prodcode.Text + "' ", "select");
  65. if (dt.Rows.Count == 0)
  66. {
  67. string code = LogicHandler.GetPiInoutCode("ProductSOP", "1");
  68. 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,'" + usercode + "','在录入','ENTERING')", "insert");
  69. }
  70. else
  71. {
  72. string ps_id = dt.Rows[0]["ps_id"].ToString();
  73. dh.ExecuteSql("delete from ProductSOPdetail where psd_psid='" + ps_id + "'", "delete");
  74. ps_prodcode.Text = dt.Rows[0]["ps_prodcode"].ToString();
  75. }
  76. Workbook workbook = new Workbook(FilePath.Text);
  77. List<Worksheet> list = new List<Worksheet>();
  78. List<string> filename = new List<string>();
  79. dh.ExecuteSql("update ProductSOP set ps_attachsop='' where ps_prodcode='" + ps_prodcode.Text + "'", "update");
  80. OperatResult.AppendText("一共Sheet: " + workbook.Worksheets.Count + "\n");
  81. try
  82. {
  83. for (int i = 0; i < workbook.Worksheets.Count; i++)
  84. {
  85. OperatResult.AppendText("正在解析Sheet" + workbook.Worksheets[i].Name + "\n");
  86. Worksheet she = workbook.Worksheets[i];
  87. if (JPG.Checked)
  88. {
  89. var myThread = new Thread(() => SaveFileToJPG(she));
  90. myThread.Start();
  91. filename.Add(she.Name + ".jpg");
  92. }
  93. }
  94. }
  95. catch (Exception ex)
  96. {
  97. OperatResult.AppendText(ex.Message + "\n");
  98. }
  99. }
  100. private void SaveFileToJPG(Worksheet sheet)
  101. {
  102. try
  103. {
  104. DataHelper dh = new DataHelper();
  105. //OperatResult.AppendText("解析图片【" + sheet.Name + ".jpg】添加水印" + "\n");
  106. if (!Directory.Exists(Application.StartupPath + @"\" + ps_prodcode.Text))
  107. {
  108. Directory.CreateDirectory(Application.StartupPath + @"\" + ps_prodcode.Text);
  109. }
  110. string sheetname = sheet.Name;
  111. sheetname = sheetname.Replace("01", "1");
  112. sheetname = sheetname.Replace("02", "2");
  113. sheetname = sheetname.Replace("03", "3");
  114. sheetname = sheetname.Replace("04", "4");
  115. sheetname = sheetname.Replace("05", "5");
  116. sheetname = sheetname.Replace("06", "6");
  117. sheetname = sheetname.Replace("07", "7");
  118. sheetname = sheetname.Replace("08", "8");
  119. sheetname = sheetname.Replace("09", "9");
  120. //if (sheetname.Contains("-"))
  121. //{
  122. // sheetname = sheetname.Split('-')[0];
  123. //}
  124. int width = 0;
  125. int height = 0;
  126. //Console.WriteLine(sheet.Name);
  127. //for (int i = 0; i < sheet.Cells.Columns.Count; i++)
  128. //{
  129. // width +=(int) sheet.Cells.Columns[i].Width;
  130. //}
  131. //for (int i = 0; i < sheet.Cells.Rows.Count; i++)
  132. //{
  133. // height += (int)sheet.Cells.Rows[i].Height;
  134. //}
  135. //Console.WriteLine("width:"+width);
  136. //Console.WriteLine("height:"+height);
  137. //sheet.Pictures.Add(0, 18, @"图片\电子受控章.png");
  138. //sheet.Pictures.Add(0, 15, @"图片\签名.png");
  139. //sheet.PageSetup.LeftMargin = 0;
  140. //sheet.PageSetup.RightMargin = 0;
  141. //sheet.PageSetup.BottomMargin = 0;
  142. //sheet.PageSetup.TopMargin = 0;
  143. ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
  144. imgOptions.ImageFormat = ImageFormat.Png;
  145. imgOptions.OnePagePerSheet = true;
  146. imgOptions.CheckWorkbookDefaultFont = false;
  147. imgOptions.DefaultFont = "宋体";
  148. imgOptions.PrintingPage = PrintingPageType.IgnoreBlank;
  149. imgOptions.SetDesiredSize(1565, 1043);
  150. SheetRender sr = new SheetRender((Aspose.Cells.Worksheet)sheet, imgOptions);
  151. sr.ToImage(0, Application.StartupPath + @"\" + ps_prodcode.Text + @"\" + sheetname + ".jpg");
  152. //// 创建一个图片
  153. OperatResult.AppendText("解析图片【" + sheetname + ".jpg】" + "\n");
  154. Dictionary<string, object> dic = new Dictionary<string, object>();
  155. dic.Add("em_name", "管理员");
  156. dic.Add("em_code", "ADMIN");
  157. dic.Add("caller", "ProductSOP");
  158. OperatResult.AppendText("上传文件【" + sheetname + ".jpg" + "】\n");
  159. string fp_id = UploadFilesToRemoteUrl("http://192.168.0.85:8099/mes/MEScommon/uploadFiles.action?_noc=1", Application.StartupPath + @"\" + ps_prodcode.Text + @"\" + sheetname + ".jpg", dic);
  160. string ps_id = dh.getFieldDataByCondition("ProductSOP", "ps_id", "ps_prodcode='" + ps_prodcode.Text + "'").ToString();
  161. dh.ExecuteSql("insert into ProductSOPdetail(psd_id,psd_psid,psd_attach) select ProductSOPdetail_seq.nextval,ps_id,'" + sheetname + ".jpg" + ";" + fp_id + "' from ProductSOP where ps_prodcode='" + ps_prodcode.Text + "'", "insert");
  162. //if (fp_id != "")
  163. //{
  164. // dh.ExecuteSql("update ProductSOP set ps_attachsop=ps_attachsop||" + fp_id + "||';' where ps_prodcode='" + pr_code.Text + "'", "update");
  165. //}
  166. }
  167. catch (Exception ex)
  168. {
  169. OperatResult.AppendText(ex.Message + ex.StackTrace);
  170. }
  171. }
  172. /// <summary>
  173. /// 请求上传图片到阿里云
  174. /// </summary>
  175. /// <param name="url">上传地址</param>
  176. /// <param name="filepath">本地文件路径</param>
  177. /// <param name="dic">上传的数据信息</param>
  178. /// <returns></returns>
  179. public string UploadFilesToRemoteUrl(string url1, string filepath, Dictionary<string, object> dic)
  180. {
  181. try
  182. {
  183. ServicePointManager.DefaultConnectionLimit = 50;
  184. string boundary = DateTime.Now.Ticks.ToString("x");
  185. byte[] boundarybytes = System.Text.Encoding.UTF8.GetBytes("--" + boundary + "\r\n");
  186. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url1);
  187. request.Method = "POST";
  188. request.Timeout = 10 * 10000;
  189. request.ContentType = "multipart/form-data; boundary=" + boundary;
  190. Stream rs = request.GetRequestStream();
  191. var endBoundaryBytes = Encoding.UTF8.GetBytes("--" + boundary + "--\r\n");
  192. string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n" + "\r\n" + "{1}" + "\r\n";
  193. if (dic != null)
  194. {
  195. foreach (string key in dic.Keys)
  196. {
  197. rs.Write(boundarybytes, 0, boundarybytes.Length);
  198. string formitem = string.Format(formdataTemplate, key, dic[key]);
  199. byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
  200. rs.Write(formitembytes, 0, formitembytes.Length);
  201. }
  202. }
  203. string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n\r\n";
  204. {
  205. rs.Write(boundarybytes, 0, boundarybytes.Length);
  206. var header = string.Format(headerTemplate, "file", Path.GetFileName(filepath));
  207. var headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
  208. rs.Write(headerbytes, 0, headerbytes.Length);
  209. using (var fileStream = new FileStream(filepath, FileMode.Open, FileAccess.Read))
  210. {
  211. var buffer = new byte[1024];
  212. var bytesRead = 0;
  213. while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
  214. {
  215. rs.Write(buffer, 0, bytesRead);
  216. }
  217. }
  218. var cr = Encoding.UTF8.GetBytes("\r\n");
  219. rs.Write(cr, 0, cr.Length);
  220. }
  221. rs.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
  222. var response = request.GetResponse() as HttpWebResponse;
  223. StreamReader newReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
  224. string Content = newReader.ReadToEnd();
  225. Dictionary<string, object> dic1 = new Dictionary<string, object>();
  226. List<Dictionary<string, object>> dic2 = null;
  227. dic1 = BaseUtil.ToDictionary(Content);
  228. dic2 = dic1["data"] as List<Dictionary<string, object>>;
  229. string fp_id = "";
  230. if (dic2[0]["filepath"] != null)
  231. {
  232. fp_id = dic2[0]["filepath"].ToString();
  233. }
  234. if (response.StatusCode == HttpStatusCode.OK)
  235. {
  236. return fp_id;
  237. }
  238. }
  239. catch (Exception e)
  240. {
  241. LogicHandler.DoCommandLog("SOP", usercode, "", "", sourcecode, "上传SOP", "上传失败", ps_prodcode.Text, "");
  242. Console.WriteLine(e.Message + e.StackTrace);
  243. }
  244. return "";
  245. }
  246. private void ChooseFile_Click(object sender, EventArgs e)
  247. {
  248. ChoosePath.Filter = "(*.xls)|*.xls|(*.xlsx)|*.xlsx";
  249. DialogResult result = ChoosePath.ShowDialog();
  250. if (result == DialogResult.OK)
  251. {
  252. FilePath.Text = ChoosePath.FileName;
  253. }
  254. }
  255. private void UploadSOP_Click(object sender, EventArgs e)
  256. {
  257. Thread thread = new Thread(uploadfile);
  258. SetLoadingWindow stw = new SetLoadingWindow(thread, "上传文件");
  259. stw.StartPosition = FormStartPosition.CenterScreen;
  260. stw.ShowDialog();
  261. if (JPG.Checked)
  262. {
  263. LogicHandler.DoCommandLog("SOP", usercode, "", "", sourcecode, "上传SOP【图片格式】", "上传成功", ps_prodcode.Text, "");
  264. }
  265. else
  266. {
  267. LogicHandler.DoCommandLog("SOP", usercode, "", "", sourcecode, "上传SOP【PDF格式】", "上传成功", ps_prodcode.Text, "");
  268. }
  269. }
  270. private void OperatResult_TextChanged(object sender, EventArgs e)
  271. {
  272. //OperatResult.SelectionStart = Text.Length;
  273. OperatResult.ScrollToCaret();
  274. }
  275. private void SendSop_Click(object sender, EventArgs e)
  276. {
  277. try
  278. {
  279. if (li_code.Text == "" || ps_prodcode.Text == "")
  280. {
  281. MessageBox.Show("线别和SOP文档不能为空");
  282. return;
  283. }
  284. if (SendDGV.Rows.Count > 0)
  285. {
  286. List<Dictionary<string, string>> listr = new List<Dictionary<string, string>>();
  287. JavaScriptSerializer jss = new JavaScriptSerializer();
  288. Dictionary<string, object> map1 = new Dictionary<string, object>();
  289. Dictionary<string, object> map = new Dictionary<string, object>();
  290. string path;
  291. string pathroot = dh.GetConfig("filePathUrl", "sys").ToString();
  292. for (int i = 0; i < SendDGV.Rows.Count; i++)
  293. {
  294. if (SendDGV.Rows[i].Cells["CheckColumn"].FormattedValue.ToString().ToUpper() == "TRUE")
  295. {
  296. Dictionary<string, string> dic1 = new Dictionary<string, string>();
  297. path = SendDGV.Rows[i].Cells["fp_path"].Value.ToString();
  298. path = encryptBASE64(path.Replace("/app/uas/webapps/postattach", pathroot)).Replace("\\s*|\r|\n|\t", "");
  299. dic1.Add("path", path);
  300. dic1.Add("filename", SendDGV.Rows[i].Cells["fp_name"].Value.ToString().Replace(" ", "").Replace("(", "").Replace(")", ""));
  301. dic1.Add("ps_prodcode", ps_prodcode.Text);
  302. listr.Add(dic1);
  303. dh.ExecuteSql("insert into soprelease(SR_ID, SR_LINECODE, SR_PATH, SR_STATUS, SR_MACHINETYPE, SR_FILENAME,sr_ifkeyset)" +
  304. "values(soprelease_seq.nextval,'" + li_code.Text + "','" + path + "',0,'" + ps_prodcode.Text + "','" + SendDGV.Rows[i].Cells["fp_name"].Value.ToString().Replace(" ", "").Replace("(", "").Replace(")", "") + "','" + SendDGV.Rows[i].Cells["psd_iskey"].Value + "') ", "insert");
  305. }
  306. }
  307. LogicHandler.DoCommandLog("SOP", usercode, "", "", sourcecode, "广播SOP", "广播成功", ps_prodcode.Text, "");
  308. MessageBox.Show("文档[" + ps_prodcode.Text + "]下放成功");
  309. }
  310. else
  311. {
  312. MessageBox.Show("产品编号" + ps_prodcode.Text + "未维护SOP文档");
  313. LogicHandler.DoCommandLog("SOP", usercode, "", "", sourcecode, "广播SOP", "广播失败,未维护SOP文档", ps_prodcode.Text, "");
  314. }
  315. }
  316. catch (Exception ex)
  317. {
  318. LogicHandler.DoCommandLog("SOP", usercode, "", "", sourcecode, "广播SOP", "广播失败", ps_prodcode.Text, "");
  319. Console.WriteLine(ex.Message + ex.StackTrace);
  320. }
  321. }
  322. public string encryptBASE64(string key)
  323. {
  324. Console.WriteLine(key);
  325. byte[] bytes = Encoding.Default.GetBytes(key);
  326. return Convert.ToBase64String(bytes);
  327. }
  328. public static string EncodeBase64(string code_type, string code)
  329. {
  330. string encode = "";
  331. byte[] bytes = Encoding.GetEncoding(code_type).GetBytes(code);
  332. try
  333. {
  334. encode = Convert.ToBase64String(bytes);
  335. }
  336. catch
  337. {
  338. encode = code;
  339. }
  340. return encode;
  341. }
  342. private void pr_code_KeyDown(object sender, KeyEventArgs e)
  343. {
  344. if (e.KeyCode == Keys.Enter)
  345. {
  346. LoadPrCode();
  347. }
  348. }
  349. private void LoadPrCode()
  350. {
  351. DataTable dt = (DataTable)dh.ExecuteSql("select * from productsop where ps_prodcode='" + ps_prodcode.Text + "'", "select");
  352. if (dt.Rows.Count > 0)
  353. {
  354. string ps_id = dt.Rows[0]["ps_id"].ToString();
  355. dt = (DataTable)dh.ExecuteSql("select fp_name,fp_path,'' issend,psd_iskey from ProductSOPdetail left join " +
  356. "filepath on FP_ID=substr(PSD_ATTACH,instr(PSD_ATTACH,';')+1) where psd_psid=" + ps_id, "select");
  357. if (dt.Rows.Count > 0)
  358. {
  359. SendDGV.DataSource = dt;
  360. }
  361. }
  362. }
  363. private void pr_code_Leave(object sender, EventArgs e)
  364. {
  365. //LoadPrCode();
  366. }
  367. bool AllChecked = false;
  368. private void ChooseALL_Click(object sender, EventArgs e)
  369. {
  370. if (AllChecked)
  371. {
  372. foreach (DataGridViewRow dr in SendDGV.Rows)
  373. dr.Cells[0].Value = false;
  374. AllChecked = false;
  375. }
  376. else
  377. {
  378. foreach (DataGridViewRow dr in SendDGV.Rows)
  379. dr.Cells[0].Value = true;
  380. AllChecked = true;
  381. }
  382. }
  383. private void ps_prodcode_UserControlTextChanged(object sender, EventArgs e)
  384. {
  385. LoadPrCode();
  386. }
  387. private void GetSOP_Click(object sender, EventArgs e)
  388. {
  389. LoadPrCode();
  390. }
  391. }
  392. }