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.Entity; using POSV.Utils; namespace POSV.Component { public partial class AddResourceFormEx : BaseForm { private List _dataSource; public AddResourceFormEx() { InitializeComponent(); this.ImeMode = ImeMode.On; this.controlBox.Text = "注册功能项"; } protected override void OnLoad(EventArgs e) { base.OnLoad(e); this.comboBoxGroup.Items.Clear(); this.comboBoxGroup.Items.AddRange(Enum.GetNames(typeof(ResourcesGroup))); this.comboBoxGroup.SelectedIndex = 0; lock (Global.Instance.SyncLock) { //获取数据库的资源数据 using (var db = Global.Instance.OpenDataBase) { this._dataSource = db.Query().ToList(); } } //如果数据库数据为空 if (this._dataSource == null) { this._dataSource = new List(); } } 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; } if (this._dataSource.Exists(x => x.Name.Equals(txtName.Text.Trim()))) { this.ShowMessage(this.lblInfo , "资源名称已经存在" , true); txtName.Focus(); return; } if (string.IsNullOrEmpty(txtKeyCode.Text.Trim())) { this.ShowMessage(this.lblInfo , "资源标识不能为空" , true); return; } if (this._dataSource.Exists(x => x.KeyCode.Equals(txtKeyCode.Text.Trim()))) { this.ShowMessage(this.lblInfo , "资源标识已经存在" , true); txtName.Focus(); return; } Resources entity = new Resources(); entity.Id = IdWorkerUtils.Instance.NextId(); entity.TenantId = Global.Instance.Authc.TenantId; entity.Group = this.comboBoxGroup.SelectedItem.ToString(); entity.Name = txtName.Text.Trim(); entity.KeyCode = txtKeyCode.Text.Trim(); entity.KeyData = txtKeyData.Text.Trim(); entity.Enable = 1; entity.PermissionCode = txtPermission.Text.Trim(); lock (Global.Instance.SyncLock) { using (var db = Global.Instance.OpenDataBase) { using (var transaction = db.GetTransaction()) { db.Save(entity); transaction.Complete(); } } } isException = false; } catch (Exception ex) { isException = true; this.ShowMessage(this.lblInfo , "保存资源信息异常" , true); LOGGER.Error(ex,"保存资源信息异常"); } finally { if (!isException) { this.DialogResult = DialogResult.OK; this.Close(); } } } private void OnCloseClick(object sender , EventArgs e) { this.DialogResult = DialogResult.Cancel; this.Close(); } } }