using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using POSV.Bean; using DevComponents.DotNetBar.Controls; using POSV.Utils; namespace POSV.Component { public partial class EditPrinterForm : BusinessForm { private readonly PrinterItem _printerItem = null; private Entity.Printer entity = null; public EditPrinterForm(PrinterItem printerItem) { InitializeComponent(); this._printerItem = printerItem; if (this._printerItem != null) { this.controlBox.Text = "修改门店打印机"; } else { this.controlBox.Text = "添加门店打印机"; } } protected override void OnLoad(EventArgs e) { base.OnLoad(e); if (this.DesignMode) return; //打印机品牌 this.cmbMode.DataSource = System.Enum.GetNames(typeof(PrinterBrand)); //打印机纸张 this.cmbPager.DataSource = System.Enum.GetNames(typeof(PrinterPager)); if (this._printerItem != null) { //打印机名称 this.txtName.Text = this._printerItem.Name; //打印类型 if(this._printerItem.Type == 1) { this.checkBoxX1.Checked = true; } else { this.checkBoxX3.Checked = true; } //品牌 PrinterBrand mode = PrinterBrand.通用打印模式; if("北洋打印专用".Equals(this._printerItem.DynamicLibrary)) { mode = PrinterBrand.北洋打印机专用; } this.cmbMode.SelectedIndex = this.cmbMode.FindString(mode.ToString()); //页宽 PrinterPager pager = PrinterPager.页宽80mm; if(this._printerItem.PageWidth == 58) { pager = PrinterPager.页宽58mm; } this.cmbPager.SelectedIndex = this.cmbPager.FindString(pager.ToString()); //端口 foreach (Control ctrl in this.panelPort.Controls) { var chk = ctrl as CheckBoxX; chk.Checked = !string.IsNullOrEmpty(this._printerItem.Port) && this._printerItem.Port.Contains(ctrl.Tag.ToString()); } //初始化指令 this.txtInitCommand.Text = this._printerItem.InitCommand; //普通字体 this.txtNormalCommand.Text = this._printerItem.NormalCommand; //倍宽字体 this.txtDoubleWidthCommand.Text = this._printerItem.DoubleWidthCommand; //倍高字体 this.txtDoubleHeightCommand.Text = this._printerItem.DoubleHeightCommand; //倍宽倍高 this.txtDoubleWidthHeightCommand.Text = this._printerItem.DoubleWidthHeightCommand; //切纸 this.txtCutPageCommand.Text = this._printerItem.CutPageCommand; //钱箱 this.txtCashboxCommand.Text = this._printerItem.CashboxCommand; } else { this.cmbMode.SelectedIndex = 1; this.cmbPager.SelectedIndex = 0; } } private void ButtonSaveClick(object sender , EventArgs e) { bool isException = true; try { if (string.IsNullOrEmpty(txtName.Text.Trim())) { this.ShowMessage(this.lblInfo, "打印机名称不能为空", true); txtName.Focus(); return; } var port = new List(); foreach(Control ctrl in this.panelPort.Controls) { var chk = ctrl as CheckBoxX; if (chk.Checked) { port.Add(chk.Tag.ToString()); } } var portString = string.Join(",", port.ToArray()); if (string.IsNullOrEmpty(portString)) { this.ShowMessage(this.lblInfo, "请选择打印机端口", true); return; } this.entity = new POSV.Entity.Printer(); if(this._printerItem == null) { this.entity.Id = IdWorkerUtils.Instance.NextId(); } else { this.entity.Id = this._printerItem.Id; } this.entity.TenantId = Global.Instance.Authc.TenantId; this.entity.Name = this.txtName.Text.Trim(); this.entity.DynamicLibrary = this.cmbMode.SelectedItem.ToString(); this.entity.Port = portString; var pagerWidth = PrinterPager.页宽80mm; Enum.TryParse(this.cmbPager.SelectedItem.ToString(), out pagerWidth); this.entity.PageWidth = (int)pagerWidth; if (checkBoxX1.Checked) { this.entity.Type = 1; } else { this.entity.Type = 2; } this.entity.UserDefined = 1; this.entity.InitCommand = this.txtInitCommand.Text.Trim(); this.entity.NormalCommand = this.txtNormalCommand.Text.Trim(); this.entity.DoubleWidthCommand = this.txtDoubleWidthCommand.Text.Trim(); this.entity.DoubleHeightCommand = this.txtDoubleHeightCommand.Text.Trim(); this.entity.DoubleWidthHeightCommand = this.txtDoubleWidthHeightCommand.Text.Trim(); this.entity.CutPageCommand = this.txtCutPageCommand.Text.Trim(); this.entity.CashboxCommand = this.txtCashboxCommand.Text.Trim(); lock (Global.Instance.SyncLock) { using (var db = Global.Instance.OpenDataBase) { using (var transaction = db.GetTransaction()) { db.Save(this.entity); transaction.Complete(); } } } isException = false; } catch (Exception ex) { isException = true; this.ShowMessage(this.lblInfo , "添加打印机发生异常" , true); LOGGER.Error(ex , "添加打印机发生异常"); } finally { if (!isException) { this.OnAcceptButtonClick(new TransparentEventArgs(TransparentAction.Accept , string.Empty , this.entity)); this.OnCloseClick(this.ButtonClose , EventArgs.Empty); } } } private void OnCloseClick(object sender , EventArgs e) { if (this.Owner != null) { this.Owner.Close(); } this.Close(); } } }