using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; using System.Threading.Tasks; using POSV.Entity; using POSV.Utils; using POSV.Bean; using DevComponents.DotNetBar.Controls; using DevComponents.Editors; namespace POSV.Component { [ToolboxItem(true)] public partial class LabelPrinterParameter : AbstractParameter { /// /// 修改前的数据 /// private Dictionary _oldValue = new Dictionary(); /// /// 修改后的数据 /// private Dictionary _newValue = new Dictionary(); /// /// 是否初始化 /// private bool _inited = false; private string[] dateFormats = { "年-月-日 时:分:秒", "月-日 时:分:秒", "月-日 时:分", "时:分:秒", "时:分"}; /// /// 下拉数字选择列表 /// private string[] _emptys = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }; /// /// 下拉数字选择列表 /// private string[] _items = { "0", "1", "2", "3", "4", "5" }; /// /// 打印机分辨率 /// private string[] _dpis = { "200", "203", "300", "600" }; /// /// 端口 /// private string[] _port = { "None", "串口", "并口", "网口", "驱动", "USB" }; public LabelPrinterParameter() { InitializeComponent(); } protected override void OnLoad(EventArgs e) { base.OnLoad(e); if (this.DesignMode) return; this.BackColor = Color.Transparent; //时间格式 this.dateFormatCom.DataSource = dateFormats; this.dateFormatCom.SelectedIndex = 2; //打印延迟 this.comboBoxEx4.DataSource = (string[])_items.Clone(); this.comboBoxEx4.SelectedIndex = 1; //小数位数 this.comboBoxEx6.DataSource = (string[])_items.Clone(); this.comboBoxEx6.SelectedIndex = 0; InitUi(); } /// /// 初始化界面 /// private void InitUi() { //界面加载开始 this._inited = false; Task.Factory.StartNew(() => { Dictionary data = null; lock (Global.Instance.SyncLock) { //获取品类分组下的全部参数 using (var db = Global.Instance.OpenDataBase) { data = db.Dictionary(string.Format(SqlConstant.ConfigQueryByGroupToDictionary, ConfigConstant.PERIPHERAL_GROUP)); } } //将新增加或者变更的参数重新更新到数据库 var defaultValue = ConfigConstant.PeripheralGroupDefaultValue(); //数据库有配置参数 if (data != null) { var diff = defaultValue.Where(kvp => !data.ContainsKey(kvp.Key)).ToDictionary(kvp => kvp.Key, kvp => kvp.Value); if (diff != null && diff.Keys.Count > 0) { var list = new List(); foreach (var v in diff) { var config = new Config(); config.Id = IdWorkerUtils.Instance.NextId(); config.Group = ConfigConstant.PERIPHERAL_GROUP; config.TenantId = Global.Instance.Authc.TenantId; config.Keys = v.Key; config.Values = v.Value; list.Add(config); } //如果数据表中没有配置group相关的信息,更新差异 this.SaveChanged(list); //将差异合并到data,避免再次访问数据库 data = data.Concat(diff).ToDictionary(x => x.Key, x => x.Value); } } else { //加载系统默认参数 data = defaultValue; var list = new List(); foreach (var v in defaultValue) { var config = new Config(); config.Id = IdWorkerUtils.Instance.NextId(); config.Group = ConfigConstant.PERIPHERAL_GROUP; config.TenantId = Global.Instance.Authc.TenantId; config.Keys = v.Key; config.Values = v.Value; list.Add(config); } //如果数据表中没有配置group相关的信息,保存 this.SaveChanged(list); } return data; }).ContinueWith(task => { if (task.IsFaulted) { LOGGER.Error(task.Exception.GetBaseException(), "加载标签打印参数设置异常"); } else { this.Invoke(new Action(() => { BindUi(task.Result); })); } }); } private void BindUi(Dictionary data) { //变更前的数据单独存储 _oldValue.Clear(); _oldValue = ObjectUtils.Copy>(data); //清理历史变更数据 _newValue.Clear(); //串口参数初始值 this.cmbSerialPort.Items.Clear(); this.cmbSerialPort.Items.AddRange(Constant.COM_PORT_NAME_VALUE); this.cmbSerialPortBaud.Items.Clear(); this.cmbSerialPortBaud.Items.AddRange(Constant.COM_PORT_BAUD_VALUE); //并口默认参数 this.cmbParallelPort.Items.Clear(); this.cmbParallelPort.Items.AddRange(Constant.LPT_NAME_VALUE); //打印机驱动默认参数 this.cmbDriverPrinter.Items.Clear(); this.cmbDriverPrinter.Items.AddRange(Global.Instance.AvailablePrinters.ToArray()); //打印机分辨率 this.comboBoxEx1.Items.Clear(); this.comboBoxEx1.Items.AddRange(Constant.DPI_NAME_VALUE); //打印机端口 this.cmbCashierTicketPort.Items.Clear(); this.cmbCashierTicketPort.Items.AddRange(_port); var lists = new List(); //取系统默认的配置 var defaultPrinterItem = Global.Instance.GetDefaultLabelPrinterItem(); //将默认None打印机压入 lists.Add(defaultPrinterItem); var normalPrinterItem = Global.Instance.GetNormalDefaultLabelPrinterItem(); //将通用标签打印机压入 lists.Add(normalPrinterItem); //将默认参数绑定到Tag,支持更换事件的操作 this.cmbCashierTicketPrinter.Tag = defaultPrinterItem; //打印机列表 this.cmbCashierTicketPrinter.Items.Clear(); this.cmbCashierTicketPrinter.DisplayMember = "Name"; this.cmbCashierTicketPrinter.Items.AddRange(lists.ToArray()); //标签打印机参数 if (data.ContainsKey(ConfigConstant.PERIPHERAL_LABEL_PRINTER)) { string json = data[ConfigConstant.PERIPHERAL_LABEL_PRINTER]; var item = JsonUtils.Deserialize(json); //将变更前的参数存入Tag,等待端口及配置参数的使用 this.cmbCashierTicketPrinter.Tag = item; //当前选中的 var _selectedIndex = lists.FindIndex(x => x.Name == item.Name); this.cmbCashierTicketPrinter.SelectedIndex = _selectedIndex < 0 ? 0 : _selectedIndex; } else { this.cmbCashierTicketPrinter.SelectedIndex = 0; } //打印延迟 if (data.ContainsKey(ConfigConstant.PERIPHERAL_LABEL_PRINT_DELAY)) { this.comboBoxEx4.SelectedIndex = this.comboBoxEx4.FindString(data[ConfigConstant.PERIPHERAL_LABEL_PRINT_DELAY].ToString()); } else { this.comboBoxEx4.SelectedIndex = 1; } //小数位数 if (data.ContainsKey(ConfigConstant.PERIPHERAL_LABEL_DIGIT)) { this.comboBoxEx6.SelectedIndex = this.comboBoxEx6.FindString(data[ConfigConstant.PERIPHERAL_LABEL_DIGIT].ToString()); } else { this.comboBoxEx6.SelectedIndex = 0; } //时间显示格式 if (data.ContainsKey(ConfigConstant.PERIPHERAL_LABEL_DATEFORMAT)) { this.dateFormatCom.SelectedIndex = this.dateFormatCom.FindString(TransDateCode2Show(data[ConfigConstant.PERIPHERAL_LABEL_DATEFORMAT].ToString())); } else { this.dateFormatCom.SelectedIndex = 2; } //CheckBoxX处理 foreach (Control control in this.groupPanel1.Controls) { if (control is CheckBoxX) { var item = control as CheckBoxX; var key = item.Tag.ToString(); if (data.ContainsKey(key)) { string _value = data[key].ToString(); item.Checked = _value.Equals("1"); } else { item.Checked = false; } } } //界面加载完成 this._inited = true; } public List NewChanged() { var result = new List(); //尚未初始化完成 if (!this._inited) return result; //当前选择的打印机 var printerItem = this.cmbCashierTicketPrinter.SelectedItem as PrinterItem; var newValue = new PrinterItem(); newValue.Id = printerItem.Id; newValue.Name = this.cmbCashierTicketPrinter.Text; newValue.Port = this.cmbCashierTicketPort.Text; var data = new Dictionary(); data.Add(Constant.COM_PORT_NAME, this.cmbSerialPort.Text); data.Add(Constant.COM_PORT_BAUD, this.cmbSerialPortBaud.Text); data.Add(Constant.LPT_NAME, this.cmbParallelPort.Text); data.Add(Constant.USB_PID, "0"); data.Add(Constant.USB_VID, "0"); data.Add(Constant.NET_IP_ADDRESS, this.txtIpAddressInput.Text); data.Add(Constant.DRIVE_NAME, this.cmbDriverPrinter.Text); newValue.Data = data; newValue.AlignCenterCommand = printerItem.AlignCenterCommand; newValue.AlignLeftCommand = printerItem.AlignLeftCommand; newValue.AlignRightCommand = printerItem.AlignRightCommand; newValue.DynamicLibrary = printerItem.DynamicLibrary; newValue.Type = printerItem.Type; newValue.UserDefined = printerItem.UserDefined; newValue.PageWidth = printerItem.PageWidth; newValue.Dpi = StringUtils.GetInt(comboBoxEx1.Text); //变更前的数据 PrinterItem oldValue = this.cmbCashierTicketPrinter.Tag as PrinterItem; //判断是否更改,如果过更改压入到 NewValue bool isChanged = !(newValue.Equals(oldValue)); if (isChanged) { var config = new Config(); config.Id = IdWorkerUtils.Instance.NextId(); config.Group = ConfigConstant.PERIPHERAL_GROUP; config.TenantId = Global.Instance.Authc.TenantId; config.Keys = ConfigConstant.PERIPHERAL_LABEL_PRINTER; config.Values = JsonUtils.Serialize(newValue); result.Add(config); } var _value = "1"; //标签打印延迟 _value = this.comboBoxEx4.SelectedItem.ToString(); isChanged = !(this._oldValue.ContainsKey(ConfigConstant.PERIPHERAL_LABEL_PRINT_DELAY) && this._oldValue[ConfigConstant.PERIPHERAL_LABEL_PRINT_DELAY].Equals(_value)); if (isChanged) { this._newValue[ConfigConstant.PERIPHERAL_LABEL_PRINT_DELAY] = _value; } _value = "0"; //标签小数位数 _value = this.comboBoxEx6.SelectedItem.ToString(); isChanged = !(this._oldValue.ContainsKey(ConfigConstant.PERIPHERAL_LABEL_DIGIT) && this._oldValue[ConfigConstant.PERIPHERAL_LABEL_DIGIT].Equals(_value)); if (isChanged) { this._newValue[ConfigConstant.PERIPHERAL_LABEL_DIGIT] = _value; } _value = "月-日 时:分"; //时间格式 _value = this.dateFormatCom.SelectedItem.ToString(); isChanged = !(this._oldValue.ContainsKey(ConfigConstant.PERIPHERAL_LABEL_DATEFORMAT) && this._oldValue[ConfigConstant.PERIPHERAL_LABEL_DATEFORMAT].Equals(TransDateShow2Code(_value))); if (isChanged) { this._newValue[ConfigConstant.PERIPHERAL_LABEL_DATEFORMAT] = TransDateShow2Code(_value); } //CheckBoxX处理 foreach (Control control in groupPanel1.Controls) { if (control is CheckBoxX) { var item = control as CheckBoxX; var key = item.Tag.ToString(); //忽略为空的 if (string.IsNullOrEmpty(key)) continue; var value = item.Checked ? "1" : "0"; //判断是否更改,如果过更改压入到 NewValue bool changed = !(this._oldValue.ContainsKey(key) && this._oldValue[key].Equals(value)); if (changed) { this._newValue[key] = value; } } } //转换为Config对象,便于存储 foreach (var v in this._newValue) { var config = new Config(); config.Id = IdWorkerUtils.Instance.NextId(); config.Group = ConfigConstant.PERIPHERAL_GROUP; config.TenantId = Global.Instance.Authc.TenantId; config.Keys = v.Key; config.Values = v.Value; result.Add(config); } return result; } public Tuple SaveChanged(List data) { bool isSuccess = true; string message = "参数更新成功"; try { lock (Global.Instance.SyncLock) { using (var db = Global.Instance.OpenDataBase) { using (var trans = db.GetTransaction()) { foreach (var config in data) { db.Save(config); } trans.Complete(); } } } } catch (Exception ex) { isSuccess = false; message = "参数更新异常"; LOGGER.Error(ex, message); } finally { if (isSuccess) { //新修改后的参数应用后,重置OldValue,避免用户在没有切换Tab情况下,直接修改当前Tab内容 foreach (var config in data) { string key = config.Keys; string value = config.Values; this._oldValue[key] = value; if (ConfigConstant.PERIPHERAL_LABEL_PRINTER.Equals(key)) { this.cmbCashierTicketPrinter.Tag = JsonUtils.Deserialize(value); } } this._newValue.Clear(); //更新控件UI UpdatePrinterUi(); } } return new Tuple(isSuccess, message); } private void UpdatePrinterUi() { Global.Instance.ReloadConfig(ConfigConstant.PERIPHERAL_GROUP); } private void OnLabelPrinterPortChanged(object sender, EventArgs e) { ComboBoxEx item = (ComboBoxEx)sender; //变更前的数据 PrinterItem oldValue = this.cmbCashierTicketPrinter.Tag as PrinterItem; //if(oldValue == null) //{ // this.cmbSerialPort.Enabled = false;//串口 // this.cmbSerialPort.SelectedIndex = 0; // this.cmbSerialPortBaud.Enabled = false;//波特率 // this.cmbSerialPortBaud.SelectedIndex = 0; // this.cmbParallelPort.Enabled = false;//并口 // this.cmbParallelPort.SelectedIndex = 0; // this.cmbDriverPrinter.Enabled = false;//驱动 // this.cmbDriverPrinter.SelectedIndex = 0; // this.txtIpAddressInput.Enabled = false;//网口 // return; //} //默认值 int inx = Array.IndexOf(Global.Instance.AvailablePrinters.ToArray(), oldValue.Data[Constant.DRIVE_NAME]); this.cmbDriverPrinter.SelectedIndex = inx < 0 ? 0 : inx; inx = Array.IndexOf(Constant.COM_PORT_NAME_VALUE, oldValue.Data[Constant.COM_PORT_NAME]); this.cmbSerialPort.SelectedIndex = inx < 0 ? 0 : inx; inx = Array.IndexOf(Constant.COM_PORT_BAUD_VALUE, oldValue.Data[Constant.COM_PORT_BAUD]); this.cmbSerialPortBaud.SelectedIndex = inx < 0 ? 0 : inx; inx = Array.IndexOf(Constant.LPT_NAME_VALUE, oldValue.Data[Constant.LPT_NAME]); this.cmbParallelPort.SelectedIndex = inx < 0 ? 0 : inx; this.txtIpAddressInput.Value = oldValue.Data[Constant.NET_IP_ADDRESS]; switch (item.Text) { case "串口": this.cmbSerialPort.Enabled = true;//串口 this.cmbSerialPortBaud.Enabled = true;//波特率 this.cmbParallelPort.Enabled = false;//并口 this.cmbDriverPrinter.Enabled = false;//驱动 this.txtIpAddressInput.Enabled = false;//网口 break; case "并口": this.cmbSerialPort.Enabled = false;//串口 this.cmbSerialPortBaud.Enabled = false;//波特率 this.cmbParallelPort.Enabled = true;//并口 this.cmbDriverPrinter.Enabled = false;//驱动 this.txtIpAddressInput.Enabled = false;//网口 break; case "网口": this.cmbSerialPort.Enabled = false;//串口 this.cmbSerialPortBaud.Enabled = false;//波特率 this.cmbParallelPort.Enabled = false;//并口 this.cmbDriverPrinter.Enabled = false;//驱动 this.txtIpAddressInput.Enabled = true;//网口 break; case "USB": this.cmbSerialPort.Enabled = false;//串口 this.cmbSerialPortBaud.Enabled = false;//波特率 this.cmbParallelPort.Enabled = false;//并口 this.cmbDriverPrinter.Enabled = false;//驱动 this.txtIpAddressInput.Enabled = false;//网口 break; case "驱动": this.cmbSerialPort.Enabled = false;//串口 this.cmbSerialPortBaud.Enabled = false;//波特率 this.cmbParallelPort.Enabled = false;//并口 this.cmbDriverPrinter.Enabled = true;//驱动 this.txtIpAddressInput.Enabled = false;//网口 break; default: this.cmbSerialPort.Enabled = false;//串口 this.cmbSerialPortBaud.Enabled = false;//波特率 this.cmbParallelPort.Enabled = false;//并口 this.cmbDriverPrinter.Enabled = false;//驱动 this.txtIpAddressInput.Enabled = false;//网口 break; } } /// /// 时间 呈现转代码 /// /// /// private string TransDateShow2Code(string show) { string result = "MM-dd HH:mm"; switch (show) { case "年-月-日 时:分:秒": result = "yyyy-MM-dd HH:mm:ss"; break; case "月-日 时:分": result = "MM-dd HH:mm"; break; case "时:分:秒": result = "HH:mm:ss"; break; case "时:分": result = "HH:mm"; break; } return result; } /// /// 时间 代码转呈现 /// /// /// private string TransDateCode2Show(string code) { string result = "月-日 时:分"; switch (code) { case "yyyy-MM-dd HH:mm:ss": result = "年-月-日 时:分:秒"; break; case "MM-dd HH:mm": result = "月-日 时:分"; break; case "HH:mm:ss": result = "时:分:秒"; break; case "HH:mm": result = "时:分"; break; } return result; } /// /// 切换打印机 /// /// /// private void OnLabelPrinterChanged(object sender, EventArgs e) { //变更前的数据 PrinterItem oldValue = this.cmbCashierTicketPrinter.Tag as PrinterItem; //更新打印机dpi int inx = Array.IndexOf(Constant.DPI_NAME_VALUE, oldValue.Dpi); this.comboBoxEx1.SelectedIndex = inx < 0 ? 1 : inx; inx = Array.IndexOf(_port, oldValue.Port); this.cmbCashierTicketPort.SelectedIndex = inx < 0 ? 0 : inx; } private void OnIpAddressCustomClick(object sender, EventArgs e) { var item = sender as IpAddressInput; NumericKeyboard.ShowKeyboard(this, item); } } }