using InSituLaboratory.Entities; using InSituLaboratory.IService; using InSituLaboratory.Models; using InSituLaboratory.Service; using Microsoft.IdentityModel.Tokens; using Microsoft.VisualBasic.ApplicationServices; using Prism.Services.Dialogs; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Forms; using System.Windows.Media.Animation; namespace InSituLaboratory.ViewModels.Pages.Dialogs { /// /// 新增或者增加时序 /// public class ModifySequentialViewModel : DialogViewModelBase { #region Model /// /// 传感器设备集合 /// public List DeviceNodes { get; set; } = new List(); public SysSequentialDetails SysSequentialdata { get; set; } public string? workTime { get; set; } public long? workInt { get; set; } /// /// 开始时间 /// public DateTime? startTime { get; set; } /// /// 结束时间 /// public DateTime? endTime { get; set; } /// /// 序号 /// public int Number { get; set; } /// /// 时序类别 /// public string kind { get; set; } private string _sequenceGroup; /// /// 配置 /// public string SequenceGroup { get { return _sequenceGroup; } set { _sequenceGroup = value; //SysSequentialdata = _sequentialService.GetSequentialSequenceGroup(SequenceGroup, kind); //if (SysSequentialdata != null) //{ // ErrorList.Add("DurationTime", new List { "当前配置存在循环时长:" + SysSequentialdata.DurationTime + "s" }); //} //else //{ // ErrorList.Remove("DurationTime"); //} //this.RaiseErrorsChanged("DurationTime"); } } private string? _sensorID; /// /// 传感器设备 /// public string? SensorID { get { return _sensorID; } set { _sensorID = value; //if (SequenceGroup == null) //{ // System.Windows.Forms.MessageBox.Show("配置为空!!! 请选择配置后,必须重新选择传感器设备!!!", "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation); //} //else //{ // ///查询当前设备是否已存在数据库中 // var select = _sequentialService.Query(m => m.SensorID == value && m.SequenceGroup == SequenceGroup && m.Sequential == kind && m.IsDelete == 0).ToList(); // if (select.Count() == 0) // { // //通过设备信息查询设备工作时长 // workTime = _deviceSvice.GetDeviceByDeviceName(_sensorID); // this.ErrorList.Clear(); // if (_sensorID == "质谱仪") // { // workInt = Convert.ToInt64(workTime) * 60 * 60; // if (Convert.ToInt64(WorkTime) > workInt) // { // ErrorList.Add("WorkTime", new List { "质谱仪已超出最大工作时长" }); // } // else // { // ErrorList.Remove("WorkTime"); // } // } // else if (_sensorID == "二氧化碳同位素分析仪") // { // workInt = Convert.ToInt64(workTime) * 60 * 60; // if (Convert.ToInt64(WorkTime) > workInt) // { // ErrorList.Add("WorkTime", new List { "二氧化碳同位素分析仪已超出最大工作时长" }); // } // else // { // ErrorList.Remove("WorkTime"); // } // } // else if (_sensorID == "甲烷同位素分析仪") // { // workInt = Convert.ToInt64(workTime) * 60 * 60; // if (Convert.ToInt64(WorkTime) > workInt) // { // ErrorList.Add("WorkTime", new List { "甲烷同位素分析仪已超出最大工作时长" }); // } // else // { // ErrorList.Remove("WorkTime"); // } // } // else if (_sensorID == "气相色谱仪") // { // workInt = Convert.ToInt64(workTime) * 60 * 60; // if (Convert.ToInt64(WorkTime) > workInt) // { // ErrorList.Add("WorkTime", new List { "气相色谱仪已超出最大工作时长" }); // } // else // { // ErrorList.Remove("WorkTime"); // } // } // else if (_sensorID == "基因测序仪") // { // workInt = Convert.ToInt64(workTime) * 60 * 60; // if (Convert.ToInt64(WorkTime) > workInt) // { // ErrorList.Add("WorkTime", new List { "基因测序仪已超出最大工作时长" }); // } // else // { // ErrorList.Remove("WorkTime"); // } // } // else // { // ErrorList.Remove("WorkTime"); // } // this.RaiseErrorsChanged("WorkTime"); // } // else // { // System.Windows.MessageBox.Show(kind + "中 配置" + SequenceGroup + " 已存在" + SensorID, "提示"); // workTime = _deviceSvice.GetDeviceByDeviceName(_sensorID); // } //} } } private string _workTime; /// /// 工作时长 /// public string WorkTime { get { return _workTime; } set { _workTime = value; //this.ErrorList.Clear(); //if (string.IsNullOrEmpty(value)) //{ // ErrorList.Add("WorkTime", new List { "工作时长不能为空" }); //} //else if (_sensorID == "质谱仪") //{ // workInt = Convert.ToInt64(workTime) * 60 * 60; // if (Convert.ToInt64(value) > workInt) // { // ErrorList.Add("WorkTime", new List { "质谱仪已超出最大工作时长" }); // } // else // { // ErrorList.Remove("WorkTime"); // } //} //else if (_sensorID == "二氧化碳同位素分析仪") //{ // workInt = Convert.ToInt64(workTime) * 60 * 60; // if (Convert.ToInt64(value) > workInt) // { // ErrorList.Add("WorkTime", new List { "二氧化碳同位素分析仪已超出最大工作时长" }); // } // else // { // ErrorList.Remove("WorkTime"); // } //} //else if (_sensorID == "甲烷同位素分析仪") //{ // workInt = Convert.ToInt64(workTime) * 60 * 60; // if (Convert.ToInt64(value) > workInt) // { // ErrorList.Add("WorkTime", new List { "甲烷同位素分析仪已超出最大工作时长" }); // } // else // { // ErrorList.Remove("WorkTime"); // } //} //else if (_sensorID == "气相色谱仪") //{ // workInt = Convert.ToInt64(workTime) * 60 * 60; // if (Convert.ToInt64(value) > workInt) // { // ErrorList.Add("WorkTime", new List { "气相色谱仪已超出最大工作时长" }); // } // else // { // ErrorList.Remove("WorkTime"); // } //} //else if (_sensorID == "基因测序仪") //{ // workInt = Convert.ToInt64(workTime) * 60 * 60; // if (Convert.ToInt64(value) > workInt) // { // ErrorList.Add("WorkTime", new List { "基因测序仪已超出最大工作时长" }); // } // else // { // ErrorList.Remove("WorkTime"); // } //} //else //{ // ErrorList.Remove("WorkTime"); //} //this.RaiseErrorsChanged("WorkTime"); } } private string _durationTime; /// /// 循环时长 /// public string DurationTime { get { return _durationTime; } set { _durationTime = value; //ErrorList.Remove("DurationTime"); //if (SequenceGroup != null) //{ // if (SysSequentialdata != null) // { // if (_durationTime != SysSequentialdata.DurationTime) // { // ErrorList.Add("DurationTime", new List { "当前配置已存在循环时长:" + SysSequentialdata.DurationTime + "s" + ",无法修改" }); // } // else // { // ErrorList.Remove("DurationTime"); // } // this.RaiseErrorsChanged("DurationTime"); // } //} } } #endregion ISysSequentialService _sequentialService; IDeviceSvice _deviceSvice; public ModifySequentialViewModel(ISysSequentialService sequentialService, IDeviceSvice deviceSvice) { _sequentialService = sequentialService; _deviceSvice = deviceSvice; } public override void OnDialogOpened(IDialogParameters parameters) { var SequentialDetail = parameters.GetValue("model"); startTime = parameters.GetValue("sequentialStartTime"); endTime = parameters.GetValue("sequentialEndTime"); kind = parameters.GetValue("kind"); if (SequentialDetail == null) { this.Title = "新增时序配置"; } else { this.Title = "编辑时序配置"; var su = _sequentialService.Find(SequentialDetail.Number); Number = su.Number; SequenceGroup = su.SequenceGroup; SensorID = su.SensorID; WorkTime = su.WorkTime; DurationTime = su.DurationTime; } DeviceNodes = _deviceSvice.GetDevices().ToList(); } public override void DoSave() { try { //非空校验 if (string.IsNullOrEmpty(SequenceGroup)) { System.Windows.MessageBox.Show("配置不能为空", "提示"); return; } else if (string.IsNullOrEmpty(SensorID)) { System.Windows.MessageBox.Show("传感器设备不能为空", "提示"); return; } else if (string.IsNullOrEmpty(WorkTime)) { System.Windows.MessageBox.Show("工作时长不能为空", "提示"); return; } else if (string.IsNullOrEmpty(DurationTime)) { System.Windows.MessageBox.Show("循环时长不能为空", "提示"); return; } if (Number != 0) { //编辑情况下 } //传感器是否已存在校验 var select = _sequentialService.Query(m => m.SensorID == SensorID && m.SequenceGroup == SequenceGroup && m.Sequential == kind && m.IsDelete == 0).ToList(); if (select.Count != 0) { System.Windows.MessageBox.Show(kind + "- 配置" + SequenceGroup + "中已存在" + SensorID, "提示"); return; } ///工作时长是否已超过传感器预设时间 workTime = _deviceSvice.GetDeviceByDeviceName(SensorID); if (workTime == "无") { workTime = "0"; } workInt = Convert.ToInt64(workTime) * 60 * 60; switch (SensorID) { case "气相色谱仪": if (Convert.ToInt64(WorkTime) > workInt) { System.Windows.MessageBox.Show(SensorID + "设置的工作时长超过传感器本身最大工作时长,为:" + workTime +"h/ " + workInt +"s", "提示"); return; } break; case "质谱仪": if (Convert.ToInt64(WorkTime) > workInt) { System.Windows.MessageBox.Show(SensorID + "设置的工作时长超过传感器本身最大工作时长,为:" + workTime + "h/ " + workInt + "s", "提示"); return; } break; case "二氧化碳同位素分析仪": if (Convert.ToInt64(WorkTime) > workInt) { System.Windows.MessageBox.Show(SensorID + "设置的工作时长超过传感器本身最大工作时长,为:" + workTime + "h/ " + workInt + "s", "提示"); return; } break; case "基因测序仪": if (Convert.ToInt64(WorkTime) > workInt) { System.Windows.MessageBox.Show(SensorID + "设置的工作时长超过传感器本身最大工作时长,为:" + workTime + "h/ " + workInt + "s", "提示"); return; } break; case "甲烷同位素分析仪": if (Convert.ToInt64(WorkTime) > workInt) { System.Windows.MessageBox.Show(SensorID + "设置的工作时长超过传感器本身最大工作时长,为:" + workTime + "h/ " + workInt + "s", "提示"); return; } break; default: break; } ///循环时长校验 var SysSequentialdata1 = _sequentialService.GetSequentialSequenceGroup(SequenceGroup, kind); if (SysSequentialdata1 != null) { if (DurationTime != SysSequentialdata1.DurationTime) { System.Windows.MessageBox.Show(kind + "- 配置"+ SequenceGroup + " 已存在循环时长:" + SysSequentialdata1.DurationTime + "s" + ",无法修改", "提示"); return; } } var data = _sequentialService.GetSequentials(kind).ToList(); //保存时 首次去查询 data == null 说明数据库中无数据 为第一次插入 if (data.Count() == 0) { _sequentialService.Insert(new SysSequential { StartTime = startTime, EndTime = endTime, CreateTime = DateTime.Now, Sequential = kind, IsDelete = 0 }); var Squential = _sequentialService.Query(m => m.Sequential == kind).OrderByDescending(n => n.CreateTime).First(); _sequentialService.Insert(new SysSequentialDetails { SysSquentialID = Squential.Number, SensorID = SensorID, DurationTime = DurationTime, WorkTime = WorkTime, SequenceGroup = SequenceGroup, CreateTime = DateTime.Now, Sequential = kind, IsDelete = 0 }); } else { //判断查询的当前时序开始和结束时间与打开的窗口传值是否有变化 //无变化说明是在当前时序下新增传感器运行时间配置 var data1 = _sequentialService.GetSequentials(kind).First(); if (data1.StartTime == startTime && data1.EndTime == endTime) { //新增传感器配置 if (Number == 0) { _sequentialService.Insert(new SysSequentialDetails { SysSquentialID = data1.Number, SensorID = SensorID, DurationTime = DurationTime, WorkTime = WorkTime, SequenceGroup = SequenceGroup, CreateTime = DateTime.Now, Sequential = kind, IsDelete = 0 }); } else { //修改传感器配置 var ssd = _sequentialService.Find(Number); ssd.SensorID = SensorID; ssd.DurationTime = DurationTime; ssd.WorkTime = WorkTime; ssd.SequenceGroup = SequenceGroup; _sequentialService.Update(ssd); } } else { //新的时序 _sequentialService.Insert(new SysSequential { StartTime = startTime, EndTime = endTime, CreateTime = DateTime.Now, Sequential = kind, IsDelete = 0 }); var Squential = _sequentialService.Query(m => m.Sequential == kind).OrderByDescending(n => n.CreateTime).First(); _sequentialService.Insert(new SysSequentialDetails { SysSquentialID = Squential.Number, SensorID = SensorID, DurationTime = DurationTime, WorkTime = WorkTime, SequenceGroup = SequenceGroup, CreateTime = DateTime.Now, Sequential = kind, IsDelete = 0 }); } } base.DoSave(); } catch (Exception ex) { System.Windows.MessageBox.Show(ex.Message, "提示"); } } } }