using Microsoft.Reporting.WinForms; using System; using System.Collections.Generic; using System.Data; using System.Drawing; using System.Drawing.Imaging; using System.Drawing.Printing; using System.IO; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using ZXing; using ZXing.Common; using SuperMap.RealEstate.CoreFrameWork; using SuperMap.RealEstate.ServiceModel; using System.Configuration; using SuperMap.RealEstate.HighWay.Storage.Business; using System.Threading; using System.Windows.Threading; using System.Text.RegularExpressions; namespace HighWay.PrintLable { /// /// MainWindow.xaml 的交互逻辑 /// public partial class PrintLableMain : Window { protected string _BUSINESSTYPE = ConfigurationManager.AppSettings["BUSINESSTYPE"].ToString(); private decimal PageLeft = decimal.Parse(ConfigurationManager.AppSettings["PAGELEFT"].ToString()); private decimal PageRight = decimal.Parse(ConfigurationManager.AppSettings["PAGERIGHT"].ToString()); private decimal PageTop = decimal.Parse(ConfigurationManager.AppSettings["PAGETOP"].ToString()); private decimal PageBottom = decimal.Parse(ConfigurationManager.AppSettings["PAGEBOTTOM"].ToString()); private decimal PageLeftI = decimal.Parse(ConfigurationManager.AppSettings["PAGELEFTI"].ToString()); private decimal PageRightI = decimal.Parse(ConfigurationManager.AppSettings["PAGERIGHTI"].ToString()); private decimal PageTopI = decimal.Parse(ConfigurationManager.AppSettings["PAGETOPI"].ToString()); private decimal PageBottomI = decimal.Parse(ConfigurationManager.AppSettings["PAGEBOTTOMI"].ToString()); Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); string ServerPart_ID = ""; Thread steachGoods; public PrintLableMain() { InitializeComponent(); CbPrintLoad(); txtBarCode.Focus(); foreach (SERVERPART _SERVERPART in new SERVERPART(RealEstateHelper.ServiceTransaction).FillCollection("" + "WHERE SERVERPART_NAME IN (" + ConfigurationManager.AppSettings["SERVERPART_NAME"].ToString() + ") ORDER BY SERVERPART_CODE")) { ServerPart_ID += ServerPart_ID == "" ? _SERVERPART.SERVERPART_ID.ToString() : "," + _SERVERPART.SERVERPART_ID; } steachGoods = new Thread(BindingTable) { IsBackground = true }; } private void Button_Click(object sender, RoutedEventArgs e) { Button keybtn = sender as Button; switch (keybtn.Name) { case "btnGoodsAdd": //商品添加 DataView dataView = (DataView)gridGoods.ItemsSource; if (dataView != null) { dataView.RowFilter = "CHOICES = 1"; DataTable _dataTable = dataView.ToTable(); //for (int i = 0; i < _dataTable.Rows.Count; i++) //{ // string _strCommodityGrade = ""; // switch (_dataTable.Rows[i]["COMMODITY_GRADE"].ToString()) // { // case "1000": // _strCommodityGrade = "一等品"; // break; // case "2000": // _strCommodityGrade = "二等品"; // break; // case "2500": // _strCommodityGrade = "三等品"; // break; // case "3000": // _strCommodityGrade = "优等品"; // break; // case "4000": // default: // _strCommodityGrade = "合格品"; // break; // } // //try // //{ // // _strCommodityGrade = DictionaryHelper.GetFieldEnumName("COMMODITYGRADE", _dataTable.Rows[i]["COMMODITY_GRADE"].ToString(), new Transaction()); // //} // //catch // //{ // // _strCommodityGrade = ""; // //} // _dataTable.Rows[i]["COMMODITY_GRADE"] = _strCommodityGrade; //} if (gridPrint.ItemsSource == null) { gridPrint.AutoGenerateColumns = false; gridPrint.ItemsSource = _dataTable.DefaultView; } else { DataTable goodsTable = ((DataView)gridPrint.ItemsSource).Table; goodsTable.Merge(_dataTable); } //txtGoodsName.Text.Trim() + "$" + txtBarCode.Text.Trim() txtBarCode.Focus(); txtBarCode.SelectAll(); DataRow[] dataRow = ((DataView)gridPrint.ItemsSource).Table.Select("CHOICES = 0"); if (dataRow.Length <= 0) { chkAll.IsChecked = true; } GetChoiceCount(); Clear(); } break; case "btnClose": //关闭 CommodityPrompt commodityPrompt = new CommodityPrompt("", "退出后将会丢失临时打印列表,确认退出?"); commodityPrompt.ShowDialog(); if (commodityPrompt.DialogResult == true) { this.Close(); } break; case "btnSetUp": //修改边距参数 if (PrinterSettings.InstalledPrinters.Count == 0) { txtMessage.Text = "未检测到打印机,请先安装打印机!"; return; } using (PrintDocument printDocument = new PrintDocument())//创建对象 { System.Windows.Forms.PageSetupDialog psd = new System.Windows.Forms.PageSetupDialog(); psd.EnableMetric = false; psd.Document = printDocument; //加载默认值 if (cbPrintCol.Text == "双列") { psd.PageSettings.Margins.Left = (int)((double)PageLeft * 0.2546 + 0.5) * 10; psd.PageSettings.Margins.Right = (int)((double)PageRight * 0.2546 + 0.5) * 10; psd.PageSettings.Margins.Top = (int)((double)PageTop * 0.2546 + 0.5) * 10; psd.PageSettings.Margins.Bottom = (int)((double)PageBottom * 0.2546 + 0.5) * 10; } else if (cbPrintCol.Text == "单列") { psd.PageSettings.Margins.Left = (int)((double)PageLeftI * 0.2546 + 0.5) * 10; psd.PageSettings.Margins.Right = (int)((double)PageRightI * 0.2546 + 0.5) * 10; psd.PageSettings.Margins.Top = (int)((double)PageTopI * 0.2546 + 0.5) * 10; psd.PageSettings.Margins.Bottom = (int)((double)PageBottomI * 0.2546 + 0.5) * 10; } //保存默认值 if (System.Windows.Forms.DialogResult.OK == psd.ShowDialog()) { //单双列分开存入 if (cbPrintCol.Text == "双列") { PageRight = printDocument.DefaultPageSettings.Margins.Right; PageTop = printDocument.DefaultPageSettings.Margins.Top; PageLeft = printDocument.DefaultPageSettings.Margins.Left; PageBottom = printDocument.DefaultPageSettings.Margins.Bottom; config.AppSettings.Settings["PAGERIGHT"].Value = PageRight.ToString(); config.AppSettings.Settings["PAGETOP"].Value = PageTop.ToString(); config.AppSettings.Settings["PAGELEFT"].Value = PageLeft.ToString(); config.AppSettings.Settings["PAGEBOTTOM"].Value = PageBottom.ToString(); config.Save(); } else if (cbPrintCol.Text == "单列") { PageRightI = printDocument.DefaultPageSettings.Margins.Right; PageTopI = printDocument.DefaultPageSettings.Margins.Top; PageLeftI = printDocument.DefaultPageSettings.Margins.Left; PageBottomI = printDocument.DefaultPageSettings.Margins.Bottom; config.AppSettings.Settings["PAGERIGHTI"].Value = PageRightI.ToString(); config.AppSettings.Settings["PAGETOPI"].Value = PageTopI.ToString(); config.AppSettings.Settings["PAGELEFTI"].Value = PageLeftI.ToString(); config.AppSettings.Settings["PAGEBOTTOMI"].Value = PageBottomI.ToString(); config.Save(); } } } break; case "btnDelete": //删除数据 DataTable dataTable = ((DataView)gridPrint.ItemsSource).Table; DataRow[] dataRows = dataTable.Select("CHOICES = 1"); if (dataRows.Count() > 0) { foreach (DataRow dataRow in dataRows) { dataTable.Rows.Remove(dataRow); } GetChoiceCount(); } else { commodityPrompt = new CommodityPrompt("", "是否清除打印列表?"); commodityPrompt.ShowDialog(); if (commodityPrompt.DialogResult == true) { dataTable.Clear(); txtMessage.Text = "清除成功!"; chkAll.IsChecked = false; GetChoiceCount(); } } break; case "btnSearch": //条件检索 //BindingTable(); gridMessage.Visibility = Visibility.Visible; if (steachGoods.ThreadState != ThreadState.Background) { steachGoods = new Thread(BindingTable) { IsBackground = true }; steachGoods.Start(txtGoodsName.Text.Trim() + "$" + txtBarCode.Text.Trim() + "$" + dateOperate.Text); } txtBarCode.Focus(); txtBarCode.SelectAll(); break; } } //拖动列表不会导致程序拖动 private void SCManipulationBoundaryFeedback(object sender, ManipulationBoundaryFeedbackEventArgs e) { e.Handled = true; } #region 方法 -> 下拉框加载 private void CbPrintLoad() { //绑定下拉框选项 cbPrintCol.Items.Add("单列"); cbPrintCol.Items.Add("双列"); cbPrintCol.Text = "双列"; } #endregion #region 方法 -> 全选框点击事件 private void chkAll_Click(object sender, RoutedEventArgs e) { //打印全选框点击事件 if (chkAll.IsChecked.Value) { for (int i = 0; i < gridPrint.Items.Count; i++) { DataRowView item = gridPrint.Items[i] as DataRowView; item.Row["CHOICES"] = 1; } } else { for (int i = 0; i < gridPrint.Items.Count; i++) { DataRowView item = gridPrint.Items[i] as DataRowView; item.Row["CHOICES"] = 0; } } GetChoiceCount(); } private void chk_Click(object sender, RoutedEventArgs e) { //商品列表全选点击事件 if (chk.IsChecked.Value) { for (int i = 0; i < gridGoods.Items.Count; i++) { DataRowView item = gridGoods.Items[i] as DataRowView; item.Row["CHOICES"] = 1; } } else { for (int i = 0; i < gridGoods.Items.Count; i++) { DataRowView item = gridGoods.Items[i] as DataRowView; item.Row["CHOICES"] = 0; } } //GetChoiceCount(); } #endregion #region 方法 -> 计算数量 private void GetChoiceCount() { //计算数量 if (gridPrint.ItemsSource != null) { DataTable _goodsTable = ((DataView)gridPrint.ItemsSource).Table; DataRow[] _Row = _goodsTable.Select("CHOICES = 1"); txtGoodsCount.Text = _Row.Length + "件"; } } #endregion #region 方法 -> 列选择框点击事件 private void cbChoices_Click(object sender, RoutedEventArgs e) { //待打印列表行选择 if (gridPrint.SelectedIndex != -1) { DataRowView item = gridPrint.Items[gridPrint.SelectedIndex] as DataRowView; DataRow dr = item.Row; if (decimal.Parse(dr["CHOICES"].ToString()) == 0) { dr["CHOICES"] = 1; DataTable dataTable = ((DataView)gridPrint.ItemsSource).Table; DataRow[] dataRows = dataTable.Select("CHOICES = 1"); if (dataRows.Length == dataTable.Rows.Count) chkAll.IsChecked = true; } else { dr["CHOICES"] = 0; chkAll.IsChecked = false; } } GetChoiceCount(); } private void cbChoice_Click(object sender, RoutedEventArgs e) { //商品列表行选择 if (gridGoods.SelectedIndex != -1) { DataRowView item = gridGoods.Items[gridGoods.SelectedIndex] as DataRowView; DataRow dr = item.Row; if (decimal.Parse(dr["CHOICES"].ToString()) == 0) { dr["CHOICES"] = 1; DataTable dataTable = ((DataView)gridGoods.ItemsSource).Table; DataRow[] dataRows = dataTable.Select("CHOICES = 1"); if (dataRows.Length == dataTable.Rows.Count) chk.IsChecked = true; } else { dr["CHOICES"] = 0; chk.IsChecked = false; } } GetChoiceCount(); } #endregion #region 方法 -> 单行修改数据和单行删除 private void UpdateButton_Click(object sender, RoutedEventArgs e) { //修改数据 DataRowView dataRowView = gridPrint.Items[gridPrint.SelectedIndex] as DataRowView; DataRow dataRow = dataRowView.Row; GoodsUpdate goodsUpdate = new GoodsUpdate(dataRow) { Owner = this }; goodsUpdate.ShowDialog(); } private void DeleteButton_Click(object sender, RoutedEventArgs e) { //删除数据 DataRowView dataRowView = gridPrint.Items[gridPrint.SelectedIndex] as DataRowView; DataRow dataRow = dataRowView.Row; DataTable dataTable = ((DataView)gridPrint.ItemsSource).Table; dataTable.Rows.Remove(dataRow); GetChoiceCount(); } #endregion #region 方法 -> 打印 private void btnPrint_Click(object sender, RoutedEventArgs e) { if (gridPrint.ItemsSource != null) { DataRow[] _DataRow = ((DataView)gridPrint.ItemsSource).Table.Select("CHOICES = 1"); if (_DataRow.Count() > 0) { DataTable goodsTable = _DataRow.CopyToDataTable(); PrintHelper _PrintHelper = new PrintHelper(); if (cbPrintCol.Text == "单列") { _PrintHelper.Print(goodsTable, "HighWay.PrintLable.Report.ReportOne.rdlc", PageLeftI, PageRightI, PageTopI, PageBottomI); } else { _PrintHelper.Print(goodsTable, "HighWay.PrintLable.Report.ReportTwo.rdlc", PageLeft, PageRight, PageTop, PageBottom); } CommodityPrompt commodityPrompt = new CommodityPrompt("", "打印完成是否清除打印列表?") { Owner = this }; commodityPrompt.ShowDialog(); if (commodityPrompt.DialogResult == true) { gridPrint.ItemsSource = null; chkAll.IsChecked = false; } } else { CommodityPrompt commodityPrompt = new CommodityPrompt("1", "请勾选需要打印的商品!") { Owner = this }; commodityPrompt.ShowDialog(); } } } #endregion #region 方法 -> 绑定数据源 private void BindingTable(object Condition) { //单个商品数据源 COMMODITYTYPE _COMMODITYTYPE = new COMMODITYTYPE(RealEstateHelper.ServiceTransaction); DataTable bindingDataSource = this.GetBindingDataSource("-1", Condition.ToString(), ServerPart_ID); this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate { if (bindingDataSource.Rows.Count > 0) { gridGoods.ItemsSource = null; gridGoods.AutoGenerateColumns = false; gridGoods.SelectionMode = DataGridSelectionMode.Single; gridGoods.ItemsSource = bindingDataSource.DefaultView; } gridMessage.Visibility = Visibility.Collapsed; }); } #region GetBindingDataSource public DataTable GetBindingDataSource(string pid, string ServerPart_ID) { return this.GetBindingDataSource(pid, string.Empty, ServerPart_ID); } public DataTable GetBindingDataSource(string pid, string obscureName, string ServerPart_ID) { string arg2 = string.Empty; arg2 = string.IsNullOrEmpty(obscureName.Split('$')[0]) ? "" : (" AND COMMODITY_NAME LIKE '%" + obscureName.Split('$')[0] + "%'"); arg2 += string.IsNullOrEmpty(obscureName.Split('$')[1]) ? "" : (" AND COMMODITY_BARCODE LIKE '%" + obscureName.Split('$')[1] + "%'"); arg2 += string.IsNullOrEmpty(obscureName.Split('$')[2]) ? "" : (" AND TRUNC(OPERATE_DATE) = TRUNC(TO_DATE('" + obscureName.Split('$')[2] + "','YYYY/MM/DD HH24:MI:SS'))"); return new COMMODITY(RealEstateHelper.ServiceTransaction).ExecuteDataTable( @"SELECT 0 AS CHOICES,A.COMMODITY_TYPE,A.COMMODITY_CODE,A.COMMODITY_NAME, A.COMMODITY_BARCODE,A.COMMODITY_UNIT,A.COMMODITY_RULE,A.COMMODITY_ORI, CASE NVL(A.COMMODITY_GRADE,'4000') WHEN '1000' THEN '一等品' WHEN '2000' THEN '二等品' WHEN '2500' THEN '三等品' WHEN '3000' THEN '优等品' ELSE '合格品' END AS COMMODITY_GRADE, A.COMMODITY_CURRPRICE,A.COMMODITY_RETAILPRICE FROM HIGHWAY_STORAGE.T_COMMODITY A WHERE A.COMMODITY_STATE = 1 AND A.BUSINESSTYPE IN (" + _BUSINESSTYPE + ") AND A.SERVERPART_ID IN (" + ServerPart_ID + ") " + (arg2 == "" ? " AND 1 != 1" : arg2) + " ORDER BY A.COMMODITY_CODE,A.COMMODITY_TYPE,A.COMMODITY_NAME"); } #endregion #endregion #region 方法 -> 快捷键检索商品 private void txtGoodsName_KeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Enter) Button_Click(btnSearch, null); } #endregion #region 方法 -> 添加完后重置商品表 private void Clear() { if (gridGoods.ItemsSource != null) { DataTable dataTable = ((DataView)gridGoods.ItemsSource).Table; dataTable.DefaultView.RowFilter = ""; for (int i = 0; i < dataTable.Rows.Count; i++) { dataTable.Rows[i]["CHOICES"] = 0; } } } #endregion private void ButtonGoodsAdd_Click(object sender, RoutedEventArgs e) { DataRowView dataRowView = gridGoods.Items[gridGoods.SelectedIndex] as DataRowView; DataRow dataRow = dataRowView.Row; dataRow["CHOICES"] = 1; DataView dataView = (DataView)gridGoods.ItemsSource; dataView.RowFilter = "COMMODITY_BARCODE = '" + dataRow["COMMODITY_BARCODE"] + "'"; DataTable _dataTable = dataView.ToTable(); if (gridPrint.ItemsSource == null) { gridPrint.AutoGenerateColumns = false; gridPrint.ItemsSource = _dataTable.DefaultView; } else { DataTable goodsTable = ((DataView)gridPrint.ItemsSource).Table; goodsTable.Merge(_dataTable); } dataView.RowFilter = ""; dataRow["CHOICES"] = 0; GetChoiceCount(); } private void TextBox_TextChanged(object sender, TextChangedEventArgs e) { //if(Regex.IsMatch(((TextBox)sender).Text, @"--['%]")) //{ //} } } }