using DevExpress.XtraEditors; using System; using System.Collections.Generic; using System.Windows.Forms; using ZJ_BYD.Common; using ZJ_BYD.DB; using ZJ_BYD.Model; using ZJ_BYD.Untils; namespace ZJ_BYD.UserControls.Station { public partial class EditStation : XtraForm { public EditStation(int id = 0) { InitializeComponent(); labelControl4.Visible = false; txtDatabaseIp.Visible = false; if (id > 0) { GetStationById(id); } else { var maxSortIndex = StationHelper.GetMaxSortIndex(); txtSortIndex.Text = (maxSortIndex + 1).ToString(); } } private void EditStation_Load(object sender, EventArgs e) { } private void GetStationById(int id) { var model = StationHelper.QueryStations().First(m => m.Id == id); if (model != null) { lblId.Text = model.Id.ToString(); txtIpcCode.Text = model.IpcId; txtCategory.Text = model.Category; txtstationcode.Text = model.StationCode; txtstationname.Text = model.StationName; txtSortIndex.Text = model.SortIndex.ToString(); ckassemblesearch.Checked = model.IsSearchAssemble; ckbindassemblecode.Checked = model.IsBindAssembleCode; ckorder.Checked = model.IsShowOrderBtn; ckShowPrint.Checked = model.IsShowPrintBtn; ck_IsBranch.Checked=model.IsBranch; ck_IsRepeat.Checked = model.IsSfcCodeRepeat; cksearchlaststation.Checked = model.IsSearchLastStation; if (cksearchlaststation.Checked) { labelControl4.Visible = true; txtDatabaseIp.Visible = true; txtDatabaseIp.Text=model.DatabaseIp; } } } private void cksearchlaststation_CheckedChanged(object sender, EventArgs e) { if (cksearchlaststation.Checked) { labelControl4.Visible = true; txtDatabaseIp.Visible = true; } else { labelControl4.Visible = false; txtDatabaseIp.Visible = false; } } private void btnCancel_Click(object sender, EventArgs e) { this.Close(); } private void btnSave_Click(object sender, EventArgs e) { int row; var t_Station = new T_Station(); var flag = this.RegexSave(new Control[] { txtCategory, txtstationcode, txtstationname }, out _); if (!flag) { return; } splashScreenManager1.ShowWaitForm(); //t_Station.IpcId = Program.CurrentIpcId; t_Station.LineCode = Program.CurrentLineCode; t_Station.Category = txtCategory.Text.Trim(); t_Station.IpcId = txtIpcCode.Text.Trim(); t_Station.StationCode = txtstationcode.Text.Trim(); t_Station.StationName = txtstationname.Text.Trim(); t_Station.IsSearchLastStation = cksearchlaststation.Checked; t_Station.IsBindAssembleCode = ckbindassemblecode.Checked; t_Station.IsSearchAssemble = ckassemblesearch.Checked; t_Station.IsShowOrderBtn = ckorder.Checked; t_Station.IsShowPrintBtn = ckShowPrint.Checked; t_Station.IsBranch=ck_IsBranch.Checked; t_Station.IsSfcCodeRepeat=ck_IsRepeat.Checked; t_Station.SortIndex = int.Parse(txtSortIndex.Text.Trim()); t_Station.DatabaseIp = txtDatabaseIp.Text?.Trim(); if (string.IsNullOrWhiteSpace(lblId.Text)) { t_Station.CreatedBy = CurrentUser.UserName; if (CheckIsExistStationCode(t_Station.IpcId, Program.CurrentLineCode, t_Station.Category, t_Station.StationCode)) { splashScreenManager1.CloseWaitForm(); XtraMessageBox.Show("工位编码已存在!"); return; } t_Station.CreatedTime = DateTime.Now; row = StationHelper.AddStation(t_Station); #region 初始化PLC指令性点位 var pointCount = PlcPointHelper.QueryPlcPoints().Where(m => m.StationCode == t_Station.StationCode).ToList().Count; if (pointCount <= 0) { var systemPoints = XmlHelper.GetSystemPoints(); if (systemPoints.Count > 0) { List plcPoints = new List(); foreach (var item in systemPoints) { var t_PlcPoint = new T_PlcPoint { IpcId = Program.CurrentIpcId, LineCode = Program.CurrentLineCode, StationCode = t_Station.StationCode, PointCode = item.PointCode, PointStartAddress = item.Address, PointDataType = item.DataType, PointLength = item.Length, PointName = item.PointName, IsSystem = item.IsSystem, IsSaveDb = item.IsSaveDb, IsActive = item.IsActive, Remark = item.Remark, SortIndex = item.SortIndex, CreatedTime = DateTime.Now, CreatedBy = "系统生成" }; plcPoints.Add(t_PlcPoint); } PlcPointHelper.AddPlcPoints(plcPoints); } } #endregion } else { t_Station.Id = int.Parse(lblId.Text); t_Station.UpdatedBy = CurrentUser.UserName; t_Station.UpdatedTime = DateTime.Now; if (CheckIsExistStationCode(t_Station.IpcId, t_Station.LineCode, t_Station.Category, t_Station.StationCode, t_Station.Id)) { splashScreenManager1.CloseWaitForm(); XtraMessageBox.Show("工位编码已存在!"); return; } row = StationHelper.UpdateStation(t_Station); } if (row <= 0) { splashScreenManager1.CloseWaitForm(); XtraMessageBox.Show("操作失败!"); return; } splashScreenManager1.CloseWaitForm(); XtraMessageBox.Show("操作成功,请重新启动程序!"); DialogResult = DialogResult.OK; this.Close(); } /// /// 校验工位编码是否已存在 /// /// /// /// /// /// /// private bool CheckIsExistStationCode(string ipcId, string lineCode,string category, string stationCode, int id = 0) { var model = StationHelper.QueryActiveStations() .Where(m => m.IpcId == ipcId && m.LineCode == lineCode && m.Category== category && m.StationCode == stationCode) .WhereIF(id > 0, m => m.Id != id) .First(); return model != null; } } }