using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using DevComponents.DotNetBar; using Newtonsoft.Json; using System.Drawing.Printing; using HH.WMS.Entitys.Basic; using System.Linq; using System.IO; using HH.WMS.Client.Common; using HH.WMS.Client.frm; using HH.WMS.Common; using DevComponents.DotNetBar.Controls; using HH.WMS.Client.CustomControl; namespace HH.WMS.Client { public partial class frmTempletSet : DevComponents.DotNetBar.Office2007Form { public frmTempletSet() { InitializeComponent(); } private void frmTempletSet_Load(object sender, EventArgs e) { this.EnableGlass = false; LoadDS(); } private void btnClose_Click(object sender, EventArgs e) { this.Close(); } private void btnAdd_Click(object sender, EventArgs e) { frmAddTemplet from = new frmAddTemplet(); if (from.ShowDialog() == System.Windows.Forms.DialogResult.OK) { LoadDS(); } } private void LoadDS() { dgvlist.Rows.Clear(); try { List PrintDataSource = new List(); PrintDataSource.Add(""); foreach (string sPrint in PrinterSettings.InstalledPrinters)//»ñÈ¡ËùÓдòÓ¡»úÃû³Æ { PrintDataSource.Add(sPrint); } string str = WebApiManager.HttpWMS_Get("/api/PrintTemplet/GetPrintTemplet?mac=" + "0000");//StaticUtil.Mac); OperateResult results = JsonConvert.DeserializeObject(str); if (!results.Success) { MessageBox.Show(results.Msg); return; } List v = JsonConvert.DeserializeObject>(results.Data.ToString()); foreach (TN_WM_PRINT_TEMPLETEntity m in v) { DataGridViewRow row = new DataGridViewRow(); row.Tag = m.CN_GUID; DataGridViewTextBoxCell cell0 = new DataGridViewTextBoxCell(); cell0.Value = m.CN_GUID; row.Cells.Add(cell0); DataGridViewTextBoxCell cell1 = new DataGridViewTextBoxCell(); cell1.Value = m.CN_S_TEMPLET_TYPE; row.Cells.Add(cell1); DataGridViewTextBoxCell cell2 = new DataGridViewTextBoxCell(); cell2.Value = m.CN_S_TEMPLET_NAME; cell2.Tag = m.CN_S_TEMPLET_NAME; row.Cells.Add(cell2); DataGridViewComboBoxCell cell3 = new DataGridViewComboBoxCell(); cell3.DataSource = PrintDataSource; m.CN_S_PRINTER = m.CN_S_PRINTER.Replace('!', '\\'); cell3.Tag = m.CN_S_PRINTER; if (PrintDataSource.IndexOf(m.CN_S_PRINTER) >= 0) cell3.Value = m.CN_S_PRINTER; else { // if (!m.CN_S_MAC.Equals(StaticUtil.Mac)) // cell3.Value = m.CN_S_PRINTER; } row.Cells.Add(cell3); DataGridViewTextBoxCell cell5 = new DataGridViewTextBoxCell(); cell5.Value = string.IsNullOrEmpty(m.CN_S_MAC) ? StaticUtil.Mac : m.CN_S_MAC; if (string.IsNullOrEmpty(m.CN_S_MAC)) cell5.Style.ForeColor = Color.Red; row.Cells.Add(cell5); DataGridViewTextBoxCell cell16 = new DataGridViewTextBoxCell(); cell16.Value = m.CN_T_UPLOAD_TIME; row.Cells.Add(cell16); DataGridViewTextBoxCell cell17 = new DataGridViewTextBoxCell(); cell17.Value = m.CN_S_UPLOADOR == null ? "" : m.CN_S_UPLOADOR.Trim(); row.Cells.Add(cell17); DataGridViewTextBoxCell cell171 = new DataGridViewTextBoxCell(); cell171.Value = m.CN_C_IS_FIRST; row.Cells.Add(cell171); DataGridViewTextBoxCell cell172 = new DataGridViewTextBoxCell(); cell172.Value = m.CN_C_IS_TOOL_PRINT; row.Cells.Add(cell172); dgvlist.Rows.Add(row); if (m.CN_C_IS_UPLOADED != "Y") { //δÉÏ´« ((DataGridViewDisableButtonXCell)row.Cells["colDown"]).Enabled = false; } if (IsExist(m.CN_S_TEMPLET_NAME)) { //±¾µØ´æÔÚÄ£°å row.DefaultCellStyle.BackColor = Color.FromArgb(154, 255, 154); } else { row.DefaultCellStyle.BackColor = Color.FromArgb(255, 0, 0); ((DataGridViewDisableButtonXCell)row.Cells["colUp"]).Enabled = false; } } } catch (Exception ex) { MessageBoxEx.Show(ex.Message); } } private bool IsExist(string fileName) { string path = Application.StartupPath+@"\ActionReport\Template\" + fileName; if (!File.Exists(path)) { return false; } return true; } public delegate void DesignDelegate(string path); public event DesignDelegate DesignEvent; private void btnEdit_Click(object sender, EventArgs e) { if (1 != dgvlist.SelectedRows.Count) { MessageBoxEx.Show("ÇëÑ¡ÔñÒ»¸ö±à¼­µÄÄ£°å"); return; } string name = dgvlist.SelectedRows[0].Cells["CN_S_TEMPLET_NAME"].Value.ToString().Trim(); string path =Application.StartupPath+"\\ActionReport\\Template\\" + name; if (!File.Exists(path)) { MessageBoxEx.Show("¸ÃÄ£°å²»´æÔÚ£¡"); return; } EndUserDesigner designer = new EndUserDesigner(); DesignEvent += designer.OnDesign; DesignEvent(path); designer.ShowDialog(); } #region ÖØÃüÃû /// /// ÖØÃüÃû /// /// Îļþ¼Ð /// ÎļþÃû /// ÐÂÎļþÃû /// private OperateResult ReName(string dirPath, string oldName,string newName) { try { DirectoryInfo directoryinfo = new DirectoryInfo(dirPath); string newPath = dirPath + "\\" + newName; if (File.Exists(newPath)) { return OperateResult.Error("ÒÑ´æÔÚ¸ÃÎļþÃû£¡"); } FileInfo[] files = directoryinfo.GetFiles(); List fi = files.Where(x => x.Name.Equals(oldName)).ToList(); if (fi.Count>0) { fi[0].MoveTo(newPath); return OperateResult.Succeed(); } else return OperateResult.Error("Ô­Îļþ²»´æÔÚ£¡"); } catch (Exception ex) { return OperateResult.Error(ex.Message); } } #endregion private void btnDeleteClick(object sender, EventArgs e) { if (1 != dgvlist.SelectedRows.Count) { MessageBoxEx.Show("ÇëÑ¡ÔñÒ»¸ö±à¼­µÄÄ£°å"); return; } if (dgvlist.SelectedRows[0].Cells["CN_S_MAC"].Style.ForeColor == Color.Red) { MessageBoxEx.Show("µ±Ç°Ä£°å²»ÊôÓÚ±¾»úÄ£°å£¬²»ÔÊÐíɾ³ý£¡"); return; } DialogResult dr = MessageBoxEx.Show("È·ÈÏɾ³ý¸ÃÄ£°åô£¡", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (dr != System.Windows.Forms.DialogResult.Yes) { return; } string name = dgvlist.SelectedRows[0].Cells["CN_S_TEMPLET_NAME"].Value.ToString().Trim(); string path =Application.StartupPath+"\\ActionReport\\Template\\" + name; if (File.Exists(path)) { File.Delete(path); } string guid = dgvlist.SelectedRows[0].Tag.ToString(); string str = WebApiManager.HttpWMS_Get("/api/PrintTemplet/DeleteTemplet?guid=" + guid); OperateResult or = JsonConvert.DeserializeObject(str); if (or.Status != ResultStatus.Success) MessageBoxEx.Show(or.Msg); else dgvlist.Rows.Remove(dgvlist.SelectedRows[0]); } #region ÉÏ´«Ä£°å /// /// ÉÏ´«Ä£°å /// /// /// /// private bool Upload(string guid,string fileName) { //if (1 != dgvlist.SelectedRows.Count) //{ // MessageBoxEx.Show("ÇëÑ¡ÔñÒ»¸öÐèÒªÉÏ´«µÄÄ£°å"); return; //} //if (dgvlist.SelectedRows[0].Cells["CN_S_MAC"].Style.ForeColor == Color.Red) //{ // MessageBoxEx.Show("µ±Ç°Ä£°å²»ÊôÓÚ±¾»úÄ£°å£¬ÎÞ·¨ÉÏ´«£¡"); return; //} //string name = dgvlist.SelectedRows[0].Cells["CN_S_TEMPLET_NAME"].Value.ToString().Trim(); string path =Application.StartupPath+"\\ActionReport\\Template\\" + fileName; StringBuilder sb = new StringBuilder(); FileStream fs = new FileStream(path, FileMode.Open); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); var requstData = new { guid=guid, file = buffer, tokenId = StaticUtil.TokenId, //path = "resultPath", fileName = fileName }; fs.Close(); fs.Dispose(); var result = HttpPostRequst(StaticUtil.ServiceUrl + "/api/PrintTemplet/UploadTemplet", JsonConvert.SerializeObject(requstData), ""); OperateResult or = JsonConvert.DeserializeObject(result); if (or.Status == ResultStatus.Success) { return true; } else { MessageBoxEx.Show(or.Msg); return false; } } #endregion public static string HttpPostRequst(string url, string postData, string ActionUrl) { try { byte[] bs = Encoding.UTF8.GetBytes(postData); System.Net.HttpWebRequest result = (System.Net.HttpWebRequest)System.Net.WebRequest.Create("http://" + url + ActionUrl); result.ContentType = "application/json"; result.ContentLength = bs.Length; result.Method = "POST"; using (Stream reqStream = result.GetRequestStream()) { reqStream.Write(bs, 0, bs.Length); } using (System.Net.WebResponse wr = result.GetResponse()) { string reader = new StreamReader(wr.GetResponseStream(), Encoding.UTF8).ReadToEnd(); if (!string.IsNullOrEmpty(reader)) { reader = reader.Replace("\\", ""); reader = reader.TrimEnd('\"'); reader = reader.TrimStart('\"'); } return reader; } } catch (Exception ex) { return JsonConvert.SerializeObject(OperateResult.Error(ex.Message)); } } #region ÏÂÔØÄ£°åµ½±¾µØ /// /// ÏÂÔØÄ£°åµ½±¾µØ /// /// Ä£°åÎļþÃû /// private bool Down(string fileName) { try { System.Windows.Forms.ProgressBar prog = new ProgressBar(); System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create("http://" + StaticUtil.ServiceUrl + "/api/PrintTemplet/DownLoadTemplet?fileName=" + fileName); System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse(); long totalBytes = myrp.ContentLength; if (prog != null) { prog.Maximum = (int)totalBytes; } System.IO.Stream st = myrp.GetResponseStream(); System.IO.Stream so = new System.IO.FileStream(Application.StartupPath+"\\ActionReport\\Template\\" + fileName, System.IO.FileMode.Create); byte[] by = new byte[20480]; int osize = st.Read(by, 0, (int)by.Length); while (osize > 0) { //System.Windows.Forms.Application.DoEvents(); so.Write(by, 0, osize); osize = st.Read(by, 0, (int)by.Length); } so.Close(); st.Close(); return true; } catch (Exception ex) { MessageBoxEx.Show(ex.Message); return false; } } #endregion private void dgvlist_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex >= 0 && e.RowIndex >= 0) { if (this.dgvlist.Columns[e.ColumnIndex].HeaderText == "ÉÏ´«") { string fileName = dgvlist.Rows[e.RowIndex].Cells["CN_S_TEMPLET_NAME"].Value.ToString().Trim(); string guid = dgvlist.Rows[e.RowIndex].Cells["CN_GUID"].Value.ToString().Trim(); if (Upload(guid, fileName)) { DataGridViewRow row = dgvlist.Rows[e.RowIndex]; ((DataGridViewDisableButtonXCell)row.Cells["colDown"]).Enabled = true; MessageBoxEx.Show("ÉÏ´«³É¹¦£¡"); } } else if (this.dgvlist.Columns[e.ColumnIndex].HeaderText == "ÏÂÔØ") { string fileName = dgvlist.Rows[e.RowIndex].Cells["CN_S_TEMPLET_NAME"].Value.ToString().Trim(); if (Down(fileName)) { DataGridViewRow row = dgvlist.Rows[e.RowIndex]; row.DefaultCellStyle.BackColor = Color.FromArgb(154, 255, 154); ((DataGridViewDisableButtonXCell)row.Cells["colUp"]).Enabled = true; MessageBoxEx.Show("ÏÂÔØ³É¹¦£¡"); } } } } private void dgvlist_CellEndEdit(object sender, DataGridViewCellEventArgs e) { try { if (e.ColumnIndex == 2) { #region ÐÞ¸ÄÄ£°åÃû³Æ DataGridViewCell cell = dgvlist.Rows[e.RowIndex].Cells[e.ColumnIndex]; string guid = dgvlist.Rows[e.RowIndex].Tag.ToString(); string oldValue = cell.Tag.ToString(); string value = cell.Value.ToString(); if (oldValue == value) return; if (dgvlist.Rows[e.RowIndex].Cells["CN_S_MAC"].Style.ForeColor == Color.Red) { MessageBoxEx.Show("µ±Ç°Ä£°å²»ÊôÓÚ±¾»úÄ£°å£¬ÎÞ·¨ÐÞ¸ÄÃû³Æ£¡"); dgvlist.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = dgvlist.Rows[e.RowIndex].Cells[e.ColumnIndex].Tag.ToString(); return; } if (!value.EndsWith(".rdlx")) { value += ".rdlx"; } string path = Application.StartupPath + @"\ActionReport\Template\"; OperateResult or = ReName(path, oldValue, value); if (or.Status == ResultStatus.Success) { string str = WebApiManager.HttpWMS_Get("/api/PrintTemplet/UpdateTempletName?guid=" + guid + "&fileName=" + value); or = JsonConvert.DeserializeObject(str); if (or.Status != ResultStatus.Success) MessageBox.Show(or.Msg); else dgvlist.Rows[e.RowIndex].Cells[e.ColumnIndex].Tag = value; } else { MessageBoxEx.Show(or.Msg); } #endregion } else if (e.ColumnIndex == 3) { #region ¸ü¸Ä´òÓ¡»ú DataGridViewCell cell = dgvlist.Rows[e.RowIndex].Cells[e.ColumnIndex]; string guid = dgvlist.Rows[e.RowIndex].Tag.ToString(); string oldValue = cell.Tag.ToString(); string value = cell.Value.ToString(); if (oldValue == value) return; //if (dgvlist.Rows[e.RowIndex].Cells["CN_S_MAC"].Style.ForeColor == Color.Red) //{ // MessageBox.Show("µ±Ç°Ä£°å²»ÊôÓÚ±¾»úÄ£°å£¬ÎÞ·¨Ð޸ĴòÓ¡»ú£¡"); // dgvlist.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = dgvlist.Rows[e.RowIndex].Cells[e.ColumnIndex].Tag.ToString(); // return; //} string isFirst = dgvlist.Rows[e.RowIndex].Cells["CN_C_IS_FIRST"].Value.ToString().Trim(); string toolPrint = dgvlist.Rows[e.RowIndex].Cells["CN_C_IS_TOOL_PRINT"].Value.ToString().Trim(); string mac = dgvlist.Rows[e.RowIndex].Cells["CN_S_MAC"].Value.ToString().Trim(); string str = ""; TN_WM_PRINT_TEMPLETEntity m = new TN_WM_PRINT_TEMPLETEntity() {TokenId =StaticUtil.TokenId,CN_GUID = guid, CN_S_PRINTER = value }; if (toolPrint.Equals("N") || StaticUtil.Mac == mac) { //²»Êǹ¤¾ß´òÓ¡µÄÖ»ÁôÒ»·ÝÄ£°å str = WebApiManager.HttpWMS_Post("/api/PrintTemplet/UpdateTempletPrinter",JsonConvert.SerializeObject(m)); } else { m.CN_S_MAC = StaticUtil.Mac; str = WebApiManager.HttpWMS_Post("/api/PrintTemplet/CreateTempletPrinter", JsonConvert.SerializeObject(m)); } OperateResult or = JsonConvert.DeserializeObject(str); if (or.Status != ResultStatus.Success) MessageBox.Show(or.Msg); else dgvlist.Rows[e.RowIndex].Cells[e.ColumnIndex].Tag = value; #endregion } } catch (Exception ex) { MessageBoxEx.Show(ex.Message); } } private void dgvlist_CellContentClick(object sender, DataGridViewCellEventArgs e) { } } }