20230201_145_upperpc/InSituLaboratory/ViewModels/Pages/Dialogs/ModifySequentialViewModel.cs

428 lines
15 KiB
C#
Raw Normal View History

2024-04-09 08:17:44 +00:00
using InSituLaboratory.Entities;
using InSituLaboratory.IService;
using InSituLaboratory.Models;
using InSituLaboratory.Service;
using Microsoft.VisualBasic.ApplicationServices;
2024-03-27 13:49:16 +00:00
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2024-04-09 08:17:44 +00:00
using System.Windows;
2024-04-10 09:50:54 +00:00
using System.Windows.Media.Animation;
2024-03-27 13:49:16 +00:00
namespace InSituLaboratory.ViewModels.Pages.Dialogs
{
2024-04-09 08:17:44 +00:00
/// <summary>
/// 新增或者增加时序
/// </summary>
public class ModifySequentialViewModel : DialogViewModelBase
2024-03-27 13:49:16 +00:00
{
2024-04-10 09:50:54 +00:00
#region Model
/// <summary>
/// 传感器设备集合
/// </summary>
2024-04-09 08:17:44 +00:00
public List<SysDevice> DeviceNodes { get; set; } = new List<SysDevice>();
2024-04-11 09:49:43 +00:00
public SysSequentialDetails SysSequentialdata { get; set; }
2024-04-10 09:50:54 +00:00
public string? workTime { get; set; }
public long? workInt { get; set; }
/// <summary>
/// 开始时间
/// </summary>
2024-04-09 08:17:44 +00:00
public DateTime? startTime { get; set; }
2024-04-10 09:50:54 +00:00
/// <summary>
/// 结束时间
/// </summary>
2024-04-09 08:17:44 +00:00
public DateTime? endTime { get; set; }
2024-04-12 05:30:03 +00:00
/// <summary>
/// 序号
/// </summary>
public int Number { get; set; }
/// <summary>
/// 时序类别
/// </summary>
public string kind { get; set; }
2024-04-10 09:50:54 +00:00
/// <summary>
/// 配置
/// </summary>
2024-04-11 09:49:43 +00:00
private string _sequenceGroup;
public string SequenceGroup
{
get { return _sequenceGroup; }
set
{
_sequenceGroup = value;
2024-04-12 05:30:03 +00:00
2024-04-11 09:49:43 +00:00
SysSequentialdata = _sequentialService.GetSequentialSequenceGroup(SequenceGroup, kind);
if (SysSequentialdata != null)
{
ErrorList.Add("DurationTime", new List<string> { "当前配置存在循环时长:" + SysSequentialdata.DurationTime +"s"});
}
else
{
ErrorList.Remove("DurationTime");
}
this.RaiseErrorsChanged("DurationTime");
}
}
2024-04-10 09:50:54 +00:00
/// <summary>
/// 传感器设备
/// </summary>
private string _sensorID;
public string SensorID
{
2024-04-11 09:49:43 +00:00
get { return _sensorID; }
set
{
2024-04-10 09:50:54 +00:00
_sensorID = value;
//通过设备信息查询设备工作时长
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<string> { "质谱仪已超出最大工作时长" });
}
else
{
ErrorList.Remove("WorkTime");
}
}
else if (_sensorID == "二氧化碳同位素分析仪")
{
workInt = Convert.ToInt64(workTime) * 60 * 60;
if (Convert.ToInt64(WorkTime) > workInt)
{
ErrorList.Add("WorkTime", new List<string> { "二氧化碳同位素分析仪已超出最大工作时长" });
}
else
{
ErrorList.Remove("WorkTime");
}
}
else if (_sensorID == "甲烷同位素分析仪")
{
workInt = Convert.ToInt64(workTime) * 60 * 60;
if (Convert.ToInt64(WorkTime) > workInt)
{
ErrorList.Add("WorkTime", new List<string> { "甲烷同位素分析仪已超出最大工作时长" });
2024-04-11 09:49:43 +00:00
}
else
{
ErrorList.Remove("WorkTime");
2024-04-10 09:50:54 +00:00
}
}
else if (_sensorID == "气相色谱仪")
{
workInt = Convert.ToInt64(workTime) * 60 * 60;
if (Convert.ToInt64(WorkTime) > workInt)
{
ErrorList.Add("WorkTime", new List<string> { "气相色谱仪已超出最大工作时长" });
2024-04-11 09:49:43 +00:00
}
else
{
ErrorList.Remove("WorkTime");
2024-04-10 09:50:54 +00:00
}
}
else if (_sensorID == "基因测序仪")
{
workInt = Convert.ToInt64(workTime) * 60 * 60;
if (Convert.ToInt64(WorkTime) > workInt)
{
ErrorList.Add("WorkTime", new List<string> { "基因测序仪已超出最大工作时长" });
2024-04-11 09:49:43 +00:00
}
else
{
ErrorList.Remove("WorkTime");
2024-04-10 09:50:54 +00:00
}
}
else
{
ErrorList.Remove("WorkTime");
}
2024-04-11 09:49:43 +00:00
this.RaiseErrorsChanged("WorkTime");
2024-04-10 09:50:54 +00:00
}
}
2024-04-09 08:17:44 +00:00
2024-04-10 09:50:54 +00:00
private string _workTime;
/// <summary>
/// 工作时长
/// </summary>
public string WorkTime
{
get { return _workTime; }
2024-04-11 09:49:43 +00:00
set
2024-04-10 09:50:54 +00:00
{
_workTime = value;
this.ErrorList.Clear();
if (string.IsNullOrEmpty(value))
{
ErrorList.Add("WorkTime", new List<string> { "工作时长不能为空" });
}
else if (_sensorID == "质谱仪")
{
workInt = Convert.ToInt64(workTime) * 60 * 60;
if (Convert.ToInt64(value) > workInt)
{
ErrorList.Add("WorkTime", new List<string> { "质谱仪已超出最大工作时长" });
2024-04-11 09:49:43 +00:00
}
else
{
ErrorList.Remove("WorkTime");
2024-04-10 09:50:54 +00:00
}
}
else if (_sensorID == "二氧化碳同位素分析仪")
{
workInt = Convert.ToInt64(workTime) * 60 * 60;
if (Convert.ToInt64(value) > workInt)
{
ErrorList.Add("WorkTime", new List<string> { "二氧化碳同位素分析仪已超出最大工作时长" });
2024-04-11 09:49:43 +00:00
}
else
{
ErrorList.Remove("WorkTime");
2024-04-10 09:50:54 +00:00
}
}
else if (_sensorID == "甲烷同位素分析仪")
{
workInt = Convert.ToInt64(workTime) * 60 * 60;
if (Convert.ToInt64(value) > workInt)
{
ErrorList.Add("WorkTime", new List<string> { "甲烷同位素分析仪已超出最大工作时长" });
2024-04-11 09:49:43 +00:00
}
else
{
ErrorList.Remove("WorkTime");
2024-04-10 09:50:54 +00:00
}
}
else if (_sensorID == "气相色谱仪")
{
workInt = Convert.ToInt64(workTime) * 60 * 60;
if (Convert.ToInt64(value) > workInt)
{
ErrorList.Add("WorkTime", new List<string> { "气相色谱仪已超出最大工作时长" });
2024-04-11 09:49:43 +00:00
}
else
{
ErrorList.Remove("WorkTime");
2024-04-10 09:50:54 +00:00
}
}
else if (_sensorID == "基因测序仪")
{
workInt = Convert.ToInt64(workTime) * 60 * 60;
if (Convert.ToInt64(value) > workInt)
{
ErrorList.Add("WorkTime", new List<string> { "基因测序仪已超出最大工作时长" });
2024-04-11 09:49:43 +00:00
}
else
{
ErrorList.Remove("WorkTime");
2024-04-10 09:50:54 +00:00
}
}
else
{
ErrorList.Remove("WorkTime");
}
2024-04-11 09:49:43 +00:00
this.RaiseErrorsChanged("WorkTime");
2024-04-10 09:50:54 +00:00
}
}
2024-04-12 05:30:03 +00:00
/// <summary>
/// 循环时长
/// </summary>
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<string> { "当前配置已存在循环时长:" + SysSequentialdata.DurationTime +"s"+",无法修改" });
}
else
{
ErrorList.Remove("DurationTime");
}
this.RaiseErrorsChanged("DurationTime");
}
}
}
}
2024-04-10 09:50:54 +00:00
#endregion
2024-04-09 08:17:44 +00:00
ISysSequentialService _sequentialService;
IDeviceSvice _deviceSvice;
public ModifySequentialViewModel(ISysSequentialService sequentialService, IDeviceSvice deviceSvice)
2024-03-27 13:49:16 +00:00
{
2024-04-09 08:17:44 +00:00
_sequentialService = sequentialService;
_deviceSvice = deviceSvice;
2024-03-27 13:49:16 +00:00
}
public override void OnDialogOpened(IDialogParameters parameters)
{
2024-04-09 08:17:44 +00:00
var SequentialDetail = parameters.GetValue<SysSequentialDetails>("model");
startTime = parameters.GetValue<DateTime>("sequentialStartTime");
endTime = parameters.GetValue<DateTime>("sequentialEndTime");
kind = parameters.GetValue<string>("kind");
2024-04-11 09:49:43 +00:00
2024-04-09 08:17:44 +00:00
if (SequentialDetail == null)
{
this.Title = "新增时序配置";
}
else
{
this.Title = "编辑时序配置";
var su = _sequentialService.Find<SysSequentialDetails>(SequentialDetail.Number);
Number = su.Number;
SensorID = su.SensorID;
2024-04-11 09:49:43 +00:00
WorkTime = su.WorkTime;
SequenceGroup = su.SequenceGroup;
2024-04-09 08:17:44 +00:00
DurationTime = su.DurationTime;
}
DeviceNodes = _deviceSvice.GetDevices().ToList();
2024-03-27 13:49:16 +00:00
}
public override void DoSave()
{
2024-04-09 08:17:44 +00:00
try
{
2024-04-11 09:49:43 +00:00
if (SysSequentialdata != null)
{
if (SysSequentialdata.DurationTime == DurationTime)
{
ErrorList.Remove("DurationTime");
this.RaiseErrorsChanged("DurationTime");
}
}
2024-04-10 09:50:54 +00:00
if (this.HasErrors) return;
2024-04-09 08:17:44 +00:00
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<SysSequential>(m => m.Sequential == kind).OrderByDescending(n => n.CreateTime).First();
_sequentialService.Insert(new SysSequentialDetails
{
SysSquentialID = Squential.Number,
SensorID = SensorID,
DurationTime = DurationTime,
2024-04-11 09:49:43 +00:00
WorkTime = WorkTime,
SequenceGroup = SequenceGroup,
2024-04-09 08:17:44 +00:00
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,
CreateTime = DateTime.Now,
Sequential = kind,
IsDelete = 0
});
}
else
{
//修改传感器配置
var ssd = _sequentialService.Find<SysSequentialDetails>(Number);
ssd.SensorID = SensorID;
ssd.DurationTime = DurationTime;
2024-04-11 09:49:43 +00:00
ssd.WorkTime = WorkTime;
ssd.SequenceGroup = SequenceGroup;
2024-04-09 08:17:44 +00:00
_sequentialService.Update(ssd);
}
}
else
{
//新的时序
_sequentialService.Insert(new SysSequential
{
StartTime = startTime,
EndTime = endTime,
CreateTime = DateTime.Now,
Sequential = kind,
IsDelete = 0
});
var Squential = _sequentialService.Query<SysSequential>(m => m.Sequential == kind).OrderByDescending(n => n.CreateTime).First();
_sequentialService.Insert(new SysSequentialDetails
{
SysSquentialID = Squential.Number,
SensorID = SensorID,
DurationTime = DurationTime,
2024-04-11 09:49:43 +00:00
WorkTime = WorkTime,
SequenceGroup = SequenceGroup,
2024-04-09 08:17:44 +00:00
CreateTime = DateTime.Now,
Sequential = kind,
IsDelete = 0
});
}
}
2024-04-11 09:49:43 +00:00
2024-04-09 08:17:44 +00:00
base.DoSave();
2024-03-27 13:49:16 +00:00
2024-04-09 08:17:44 +00:00
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "提示");
}
2024-03-27 13:49:16 +00:00
}
}
}