using System; using System.Data; using System.Text; using System.IO; using System.Windows.Forms; using UAS_LabelMachine.Properties; using UAS_LabelMachine.PublicMethod; using UAS_LabelMachine.Entity; namespace UAS_LabelMachine { public partial class 客户标签维护 : Form { DataHelper dh; DataTable dt; //设置Grid中Combox的静态数据,这个格式是必须的,#前面是展示值,#后面是实际值 string[] labeltype = { "单盘#单盘", "中盒#中盒", "外箱#外箱", "全部#" }; //记录上次筛选时的条件,如果本次和上次相同则不进行查询 string LastCondition = ""; //用于拼接条件查询 StringBuilder condition = new StringBuilder(); StringBuilder sql = new StringBuilder(); /*CodeSoft新建打印机引擎*/ /*BarTender新建打印机引擎*/ //private Engine engine = null; //private LabelFormatDocument format ; //点击行的LabelCode string LabelCode = ""; //模板的路径 string LabelPath = ""; //模板编号的ID string CL_ID1; string CL_ID2; //Label表的主键,用于弹窗的传参 string La_id1; //添加在grid末列的操作列 DataGridViewImageColumn PreviewColumn = new DataGridViewImageColumn { Image = Resources.preview_16px_25980_easyicon_net, Width = 40, Name = "预览", ToolTipText = "预览模板参数", Tag = "Read", AutoSizeMode = DataGridViewAutoSizeColumnMode.None }; DataGridViewImageColumn OperateColumn = new DataGridViewImageColumn { Image = Resources.write_15_893687707641px_1181413_easyicon_net, Width = 40, Name = "编辑", ToolTipText = "编辑模板", Tag = "Read", AutoSizeMode = DataGridViewAutoSizeColumnMode.None }; AutoSizeFormClass asc = new AutoSizeFormClass(); public 客户标签维护() { InitializeComponent(); cu_code.FormName = Name; cu_code.SetValueField = new string[] { "cu_code", "cu_name" }; cu_code.TableName = "(select distinct cu_code,cu_name from customer left join customerlabel on cu_code=cl_custcode)"; cu_code.SelectField = "cu_code # 客户编号,cu_name # 客户名称"; 客户编号.FormName = Name; 客户编号.SetValueField = new string[] { "客户编号" }; 客户编号.TableName = "(select distinct cu_code,cu_name from customer left join customerlabel on cu_code=cl_custcode)"; 客户编号.SelectField = "cu_code # 客户编号,cu_name # 客户名称"; la_code.FormName = Name; la_code.SetValueField = new string[] { "la_code" }; la_code.TableName = "label"; la_code.SelectField = "la_code # 模板编号,la_name # 模板名称"; NetURL.Text = Settings.Default.ShareUrl; } private void 条码维护_Load(object sender, EventArgs e) { dh = SystemInf.dh; if (Settings.Default.FTPAddress == "") { FTPShare.Visible = false; } if (dh.GetConfig("PrintMethod", "CodeOrBar").ToString() == "") { CodeSoft.Checked = true; } else { BarTender.Checked = true; } condition.Append(""); ChooseAll.ChooseAll(LabelDataGridView); PortModel.Checked = Settings.Default.FTPModel; BaseUtil.SetComboxData(la_type, "display", "value", labeltype); //加载下拉框的静态值 LoadData(); Width = Width + 1; asc.controllInitializeSize(this); Width = Width + 1; } private void LabelDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e) { if (LabelDataGridView.Columns[e.ColumnIndex].HeaderText == "预览") { //获取路径和标签名称 LabelPath = LabelDataGridView.Rows[e.RowIndex].Cells["cl_labelurl"].Value.ToString(); LabelCode = LabelDataGridView.Rows[e.RowIndex].Cells["cl_labelcode"].Value.ToString(); CL_ID1 = LabelDataGridView.Rows[e.RowIndex].Cells["cl_id"].Value.ToString(); La_id1 = LabelDataGridView.Rows[e.RowIndex].Cells["la_id"].Value.ToString(); System.DateTime time; try { time = Convert.ToDateTime(LabelDataGridView.Rows[e.RowIndex].Cells["cl_date"].Value.ToString()); } catch (Exception) { time = System.DateTime.Now; } if (CodeSoft.Checked) { string LabelName = LabelDataGridView.Rows[e.RowIndex].Cells["cl_labelname"].Value.ToString(); //如果文件已不存在则进行提示 } } if (LabelDataGridView.Columns[e.ColumnIndex].HeaderText == "编辑") { if (e.RowIndex >= 0 && e.ColumnIndex >= 0) { CL_ID2 = LabelDataGridView.Rows[e.RowIndex].Cells["cl_id"].Value.ToString(); LabelPath = LabelDataGridView.Rows[e.RowIndex].Cells["cl_labelurl"].Value.ToString(); string LabelName = LabelDataGridView.Rows[e.RowIndex].Cells["cl_labelname"].Value.ToString(); System.DateTime time; try { time = Convert.ToDateTime(LabelDataGridView.Rows[e.RowIndex].Cells["cl_date"].Value.ToString()); } catch (Exception) { time = System.DateTime.Now; } BaseUtil.GetLabelUrl(LabelPath, LabelName, time); System.Diagnostics.Process.Start(ftpOperater.DownLoadTo + LabelName); 客户编号.Text = LabelDataGridView.Rows[e.RowIndex].Cells["CL_CUSTCODE2"].Value.ToString(); la_type1.Text = LabelDataGridView.Rows[e.RowIndex].Cells["cl_labeltype"].Value.ToString(); la_code.Text = LabelDataGridView.Rows[e.RowIndex].Cells["cl_labelcode"].Value.ToString(); la_name.Text = LabelDataGridView.Rows[e.RowIndex].Cells["cl_labelname"].Value.ToString(); FolderPath.Text = ftpOperater.DownLoadTo; } } //asc.controllInitializeSize(this); } private void Screen_Click(object sender, EventArgs e) { sql.Clear(); condition.Clear(); //如果条件全部为空的时候 if (cu_code.Text == "" && cu_name.Text == "" && la_type.Text == "") { LoadData(); } else { sql.Append("select to_char(nvl(cl_date,sysdate),'YYYY-MM-DD HH24:Mi:SS') cl_date,cu_name,cl_labelurl,cl_labeltype,cl_isdefault,cl_custcode,cl_labelname,cl_id,cl_labelcode,cl_detno,cl_labelsoft,la_id from customerlabel left join customer on cl_custcode=cu_code left join label on la_code=cl_custcode order by cl_custcode "); //设置筛选的条件 condition.Append(BaseUtil.GetScreenSqlCondition(cu_code, la_type)); //最终刷选条件拼接成的sql LoadData(); } } private void Update_Click(object sender, EventArgs e) { string LabelSoft = ""; //需要处理新增的行和原有数据然后更新的行 if (CodeSoft.Checked) { LabelSoft = "CodeSoft"; } if (BarTender.Checked) { LabelSoft = "BarTender"; } DataTable dt = (DataTable)LabelDataGridView.DataSource; //如果有改变的行才传递到后台 if (dt.GetChanges() != null) { //如果是新增的默认加上Radio当前选中的打印软件 for (int i = 0; i < LabelDataGridView.Rows.Count; i++) { if (LabelDataGridView.Rows[i].Cells["cl_labelsoft"].Value.ToString() == "") { LabelDataGridView.Rows[i].Cells["cl_labelsoft"].Value = LabelSoft; } } string sql = "Insert into customerlabel (cl_ID,cl_custCODE,cl_LABELTYPE,cl_LABELCODE,cl_LABELURL,cl_DETNO,cl_LABELSOFT,cl_ISDEFAULT, cl_date) values(customerlabel_seq.nextval,'" + cu_code.Text + "',:cl_LABELTYPE,:cl_LABELCODE,:cl_LABELURL,:cl_DETNO,:cl_LABELSOFT,:cl_ISDEFAULT,sysdate)"; dh.UpDateTableByCondition(dt.GetChanges(), "customerlabel", "cl_id", sql.ToLower()); LoadData(); } } private void DeleteDetail_Click(object sender, EventArgs e) { string[] deleteid = new string[LabelDataGridView.RowCount]; for (int i = 0; i < LabelDataGridView.RowCount; i++) { deleteid[i] = LabelDataGridView.Rows[i].Cells["cl_id"].Value.ToString(); } dh.DeleteDataByID("customerlabel", "cl_id", deleteid); LoadData(); } private void OpenFolder_Click(object sender, EventArgs e) { //根据勾选的不同设置不同的文件后缀过滤 if (CodeSoft.Checked) { openFileDialog1.Filter = "(*.lab)|*.lab"; } if (BarTender.Checked) { openFileDialog1.Filter = "(*.btw)|*.btw"; } DialogResult result = openFileDialog1.ShowDialog(); if (result == DialogResult.OK) { FolderPath.Text = openFileDialog1.FileName; la_name.Text = FolderPath.Text.Substring(FolderPath.Text.LastIndexOf("\\") + 1); for (int i = 0; i < LabelDataGridView.Rows.Count; i++) { if (LabelDataGridView.Rows[i].Cells["Choose"].Value != null) { if ((bool)LabelDataGridView.Rows[i].Cells["Choose"].Value == true) { LabelDataGridView.Rows[i].Cells["cl_labelurl"].Value = FolderPath.Text; } } } } } private void LabelDataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if (LabelDataGridView.Columns[e.ColumnIndex].Name == "cl_isdefault") { if (e.Value != null) { switch (e.Value.ToString()) { case "0": e.Value = "否"; break; case "1": e.Value = "是"; break; default: break; } } } } private void LabelDataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e) { //覆盖参数错误的事件,使得不会报错 } private void LoadData() { //如果条件和上次一样则不进行数据读取 //if (LastCondition != condition.ToString()) { //如果没有任何筛选条件直接选取customerlabel的数据 if (sql.Length == 0) { dt = (DataTable)dh.ExecuteSql("select to_char(nvl(cl_date,sysdate),'YYYY-MM-DD HH24:Mi:SS') cl_date,cu_name,cl_labelurl,cl_labeltype,cl_isdefault,cl_labelname,cl_custcode,cl_id,cl_labelcode,cl_detno,cl_labelsoft,la_id from customerlabel left join customer on cl_custcode=cu_code left join label on la_code=CL_LABELCODE order by cl_custcode", "select"); } else { dt = (DataTable)dh.ExecuteSql(sql.ToString(), "select"); } BaseUtil.FillDgvWithDataTable(LabelDataGridView, dt); string Field = BaseUtil.GetGridViewSelectContent(LabelDataGridView); pagination1.BindDataToNavigator(LabelDataGridView, "CustomerLabel left join customer on cl_custcode=cu_code left join label on la_code=CL_LABELCODE", Field, "cl_id", "", condition.Replace("where", "").ToString(), PreviewColumn, OperateColumn); LastCondition = condition.ToString(); } } private void PrintTest_Click(object sender, EventArgs e) { if (LabelCode != "") { } } private void 产品标签维护_FormClosing(object sender, FormClosingEventArgs e) { try { } catch (Exception) { } } private void Save_Click(object sender, EventArgs e) { if (la_type1.Text != "" && FolderPath.Text != "" && la_code.Text != "") { //文件名 string fileName; //源路径 string sourcePath; //未选择文件的之后默认是这个,从Text输入框中取 if (openFileDialog1.FileName == "openFileDialog1") { fileName = la_name.Text; sourcePath = FolderPath.Text; } else { sourcePath = openFileDialog1.FileName.Substring(0, openFileDialog1.FileName.LastIndexOf(@"\")); fileName = openFileDialog1.FileName.Substring(openFileDialog1.FileName.LastIndexOf(@"\") + 1); } if (FTPShare.Checked) { ftpOperater ftp = new ftpOperater(PortModel.Checked); ftp.UpLoadFile(sourcePath, fileName); string SoftWare = CodeSoft.Checked ? "CodeSoft" : "BarTender"; if (CL_ID2 != null) { sql.Clear(); sql.Append("update customerlabel set cl_labelcode='" + la_code.Text + "',cl_custcode='" + 客户编号.Text + "',"); sql.Append("cl_labeltype='" + la_type1.Text + "',cl_labelname='" + la_name.Text + "',cl_date=sysdate where cl_id=" + CL_ID2); } else { DataTable dt = (DataTable)dh.ExecuteSql("select cl_custcode,cl_labeltype from customerlabel where cl_labelname='" + la_name.Text + "' and cl_custcode='" + 客户编号.Text + "' and cl_labeltype<>'" + la_type1.Text + "'", "select"); if (dt.Rows.Count > 0) { MessageBox.Show("已有名称为" + la_name.Text + "的标签用作" + dt.Rows[0]["cl_custcode"].ToString() + dt.Rows[0]["cl_labeltype"].ToString()); return; } sql.Clear(); sql.Append("insert into customerlabel (cl_id,cl_labelcode,cl_custcode,cl_labeltype,cl_labelurl,cl_labelname,cl_labelsoft,cl_date) values( customerlabel_seq.nextval, "); sql.Append("'" + la_code.Text + "','" + (客户编号.Text == "" ? "" : 客户编号.Text) + "','" + la_type1.Text + "','" + ftpOperater.FTPAddress + "','" + la_name.Text + "','" + SoftWare + "',sysdate)"); } dh.ExecuteSql(sql.ToString(), "insert"); sql.Clear(); } else { //目标路径 string targetPath = NetURL.Text; //var file = Directory.GetFiles(targetPath); string sourceFile = Path.Combine(sourcePath, fileName); string destFile = Path.Combine(targetPath, fileName); //获取指定路径下的全部文件名 var file = Directory.GetFiles(targetPath); string overwrite = ""; for (int i = 0; i < file.Length; i++) { if (file[i].Substring(file[i].LastIndexOf(@"\") + 1) == fileName) { overwrite = MessageBox.Show(this.ParentForm, "已存在名为" + fileName + "的文件,是否覆盖", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString(); break; } } if (overwrite == "Yes" || overwrite == "") { //不存在文件家的话进行创建 if (!Directory.Exists(targetPath)) Directory.CreateDirectory(targetPath); //将文件复制到指定位置 File.Copy(sourceFile, destFile, true); string SoftWare = "CodeSoft"; sql.Clear(); sql.Append("insert into customerlabel (cl_id,cl_labelcode,cl_custcode,cl_labeltype,cl_labelurl,cl_labelname,cl_labelsoft,cl_date) values( customerlabel_seq.nextval, "); sql.Append("'" + la_code.Text + "','" + (客户编号.Text == "" ? "共用" : 客户编号.Text) + "','" + la_type1.Text + "','" + destFile + "','" + la_name.Text + "','" + SoftWare + "',sysdate)"); dh.ExecuteSql(sql.ToString(), "insert"); sql.Clear(); } } MessageBox.Show("保存成功!"); LoadData(); } else { MessageBox.Show("请填写完整参数!"); } } private void 产品标签维护_SizeChanged(object sender, EventArgs e) { asc.controlAutoSize(this); } private void NetURL_TextChanged(object sender, EventArgs e) { Settings.Default.ShareUrl = NetURL.Text; Settings.Default.Save(); } private void NewLabel_Click(object sender, EventArgs e) { 客户编号.Text = ""; la_type1.Text = ""; la_code.Text = ""; la_name.Text = ""; FolderPath.Text = ""; CL_ID2 = null; } private void PortModel_CheckedChanged(object sender, EventArgs e) { Settings.Default.FTPModel = PortModel.Checked; Settings.Default.Save(); } } }