using DevExpress.XtraEditors; using System; using System.Web.UI.WebControls; using System.Windows.Forms; using ZJ_BYD.Common; using ZJ_BYD.DB; using ZJ_BYD.Model; using ZJ_BYD.Untils; namespace ZJ_BYD.UserControls.PlcPoint { public partial class EditPlcPoint : XtraForm { public EditPlcPoint(int id = 0) { InitializeComponent(); if (id > 0) { GetPlcPointById(id); } else { lkStation.EditValue = ""; txtSortIndex.Text = GetSortIndex(); } } private void EditPlcPoint_Load(object sender, EventArgs e) { BindCombox.BindDataType(cmbDataType); BindCombox.BindStationForLookUp(lkStation); SetResultFieldVisible(cksavedb.Checked); } private void GetPlcPointById(int id) { var model = PlcPointHelper.QueryPlcPoints().First(m => m.Id == id); if (model != null) { lkStation.EditValue = model.StationCode; BindCombox.BindResultColumn(cmbField, lkStation.EditValue.ToString()); cmbField.Properties.Items.Add(new ListItem { Text = model.ResultField, Value = model.ResultField }); lblId.Text = model.Id.ToString(); txtPointCode.Text = model.PointCode; txtPointName.Text = model.PointName; txtPonitStartAddress.Text = model.PointStartAddress; txtPointLength.Text = model.PointLength; cmbDataType.EditValue = model.PointDataType; cmbField.SelectedItem = new ListItem { Text = model.ResultField, Value = model.ResultField }; ckshowmain.Checked = model.IsShowMain; cksavedb.Checked = model.IsSaveDb; ckIsTestItem.Checked = model.IsTestItem; ckIsActive.Checked = model.IsActive; ckIsResponse.Checked = model.IsResponseData; ckIsSub.Checked = model.IsSub; ckIsSystem.Checked = model.IsSystem; txtSortIndex.Text = model.SortIndex.ToString(); // cmbField.SelectedText = model.ResultField; if (model.IsSystem) { txtPointCode.ReadOnly = true; } else { txtPointCode.ReadOnly = false; } } } private void lkStation_EditValueChanged(object sender, EventArgs e) { BindCombox.BindResultColumn(cmbField, lkStation.EditValue.ToString()); } private void cksavedb_CheckedChanged(object sender, EventArgs e) { SetResultFieldVisible(cksavedb.Checked); } private void SetResultFieldVisible(bool isChecked) { if (isChecked) { lblResultField.Visible = true; cmbField.Visible = true; } else { lblResultField.Visible = false; cmbField.Visible = false; } } private void cmbField_SelectedValueChanged(object sender, EventArgs e) { var plcPoints = PlcPointHelper.QueryActivePlcPoints() .Where(m => m.IpcId == Program.CurrentIpcId && m.LineCode == Program.CurrentLineCode && m.StationCode == lkStation.EditValue.ToString() && !string.IsNullOrWhiteSpace(m.ResultField)) .Select(m => new { m.Id, m.ResultField }); var selectedItem = cmbField.SelectedItem as ListItem; var isUsed = plcPoints.WhereIF(!string.IsNullOrWhiteSpace(lblId.Text), m => m.Id != int.Parse(lblId.Text)) .First(m => m.ResultField == selectedItem.Value) != null; if (isUsed) { XtraMessageBox.Show("此字段已使用,请选择其他字段!", "系统提示"); return; } } private void btnSave_Click(object sender, EventArgs e) { int row; var flag = this.RegexSave(new Control[] { txtPointName, txtPonitStartAddress, txtPointLength, cmbDataType , txtSortIndex }, out _); if (!flag) { return; } var t_PlcPoint = new T_PlcPoint(); var selectedDataType = cmbDataType.EditValue.ToString(); var selectedField = cmbField.SelectedItem as ListItem; if (cksavedb.Checked && selectedField == null) { XtraMessageBox.Show("请选择匹配字段!"); return; } splashScreenManager1.ShowWaitForm(); t_PlcPoint.IpcId = Program.CurrentIpcId; t_PlcPoint.LineCode = Program.CurrentLineCode; t_PlcPoint.StationCode = lkStation.EditValue.ToString(); t_PlcPoint.PointName = txtPointName.Text.Trim(); t_PlcPoint.PointStartAddress = txtPonitStartAddress.Text.Trim(); t_PlcPoint.PointLength = txtPointLength.Text.Trim(); t_PlcPoint.PointDataType = selectedDataType; t_PlcPoint.IsShowMain = ckshowmain.Checked; t_PlcPoint.IsSaveDb = cksavedb.Checked; t_PlcPoint.ResultField = t_PlcPoint.IsSaveDb ? selectedField.Value : null; t_PlcPoint.IsTestItem = ckIsTestItem.Checked; t_PlcPoint.IsSub = ckIsSub.Checked; t_PlcPoint.IsResponseData = ckIsResponse.Checked; t_PlcPoint.IsActive = ckIsActive.Checked; t_PlcPoint.IsSystem = ckIsSystem.Checked; t_PlcPoint.SortIndex = int.Parse(txtSortIndex.Text); if (string.IsNullOrWhiteSpace(txtPointCode.Text)) { t_PlcPoint.PointCode = t_PlcPoint.PointStartAddress; } else { t_PlcPoint.PointCode = txtPointCode.Text.Trim(); } if (string.IsNullOrWhiteSpace(lblId.Text)) { //if ( CheckIsExistPointCode(Program.CurrentIpcId,Program.CurrentLineCode,t_PlcPoint.StationCode,t_PlcPoint.PointCode)) //{ // XtraMessageBox.Show("点位编码已存在!"); // splashScreenManager1.CloseWaitForm(); // return; //} t_PlcPoint.CreatedBy = CurrentUser.UserName; t_PlcPoint.CreatedTime = DateTime.Now; row = PlcPointHelper.AddPlcPoint(t_PlcPoint); } else { t_PlcPoint.Id = int.Parse(lblId.Text); //if (CheckIsExistPointCode(Program.CurrentIpcId, Program.CurrentLineCode, t_PlcPoint.StationCode, t_PlcPoint.PointCode, t_PlcPoint.Id)) //{ // XtraMessageBox.Show("点位编码已存在!"); // splashScreenManager1.CloseWaitForm(); // return; //} t_PlcPoint.UpdatedBy = CurrentUser.UserName; t_PlcPoint.UpdatedTime = DateTime.Now; row = PlcPointHelper.UpdatePlcPoint(t_PlcPoint); } if (row <= 0) { splashScreenManager1.CloseWaitForm(); XtraMessageBox.Show("操作失败!"); return; } splashScreenManager1.CloseWaitForm(); DialogResult = DialogResult.OK; XtraMessageBox.Show("操作成功,请重启系统!", "系统提示", MessageBoxButtons.OK); this.Close(); } private void btnCancel_Click(object sender, EventArgs e) { this.Close(); } /// /// 设置默认排序索引 /// /// private string GetSortIndex() { var maxSortIndex = PlcPointHelper.QueryActivePlcPoints().Max(m => m.SortIndex); return (maxSortIndex + 1).ToString(); } /// /// 校验点位编码是否已存在 /// /// /// /// /// /// /// private bool CheckIsExistPointCode(string ipcId, string lineCode, string stationCode, string pointCode, int id = 0) { var model = PlcPointHelper.QueryActivePlcPoints() .Where(m => m.IpcId == ipcId && m.LineCode == lineCode && m.StationCode == stationCode && m.PointCode == pointCode) .WhereIF(id > 0, m => m.Id != id) .First(); return model != null; } } }