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

616 lines
25 KiB
C#

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
{
/// <summary>
/// 新增或者增加时序
/// </summary>
public class ModifySequentialViewModel : DialogViewModelBase
{
#region Model
/// <summary>
/// 传感器设备集合
/// </summary>
public List<SysDevice> DeviceNodes { get; set; } = new List<SysDevice>();
public SysSequentialDetails SysSequentialdata { get; set; }
public string? workTime { get; set; }
public long? workInt { get; set; }
/// <summary>
/// 记录编辑时第一次带入的设备名称
/// </summary>
public string Device { get; set; }
/// <summary>
/// 开始时间
/// </summary>
public DateTime? startTime { get; set; }
/// <summary>
/// 结束时间
/// </summary>
public DateTime? endTime { get; set; }
/// <summary>
/// 序号
/// </summary>
public int Number { get; set; }
/// <summary>
/// 时序类别
/// </summary>
public string kind { get; set; }
private string _sequenceGroup;
/// <summary>
/// 配置
/// </summary>
public string SequenceGroup
{
get { return _sequenceGroup; }
set
{
_sequenceGroup = value;
//SysSequentialdata = _sequentialService.GetSequentialSequenceGroup(SequenceGroup, kind);
//if (SysSequentialdata != null)
//{
// ErrorList.Add("DurationTime", new List<string> { "当前配置存在循环时长:" + SysSequentialdata.DurationTime + "s" });
//}
//else
//{
// ErrorList.Remove("DurationTime");
//}
//this.RaiseErrorsChanged("DurationTime");
}
}
private string? _sensorID;
/// <summary>
/// 传感器设备
/// </summary>
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<SysSequentialDetails>(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<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> { "甲烷同位素分析仪已超出最大工作时长" });
// }
// 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> { "基因测序仪已超出最大工作时长" });
// }
// 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;
/// <summary>
/// 工作时长
/// </summary>
public string WorkTime
{
get { return _workTime; }
set
{
_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> { "质谱仪已超出最大工作时长" });
// }
// else
// {
// ErrorList.Remove("WorkTime");
// }
//}
//else if (_sensorID == "二氧化碳同位素分析仪")
//{
// workInt = Convert.ToInt64(workTime) * 60 * 60;
// if (Convert.ToInt64(value) > workInt)
// {
// ErrorList.Add("WorkTime", new List<string> { "二氧化碳同位素分析仪已超出最大工作时长" });
// }
// else
// {
// ErrorList.Remove("WorkTime");
// }
//}
//else if (_sensorID == "甲烷同位素分析仪")
//{
// workInt = Convert.ToInt64(workTime) * 60 * 60;
// if (Convert.ToInt64(value) > workInt)
// {
// ErrorList.Add("WorkTime", new List<string> { "甲烷同位素分析仪已超出最大工作时长" });
// }
// else
// {
// ErrorList.Remove("WorkTime");
// }
//}
//else if (_sensorID == "气相色谱仪")
//{
// workInt = Convert.ToInt64(workTime) * 60 * 60;
// if (Convert.ToInt64(value) > workInt)
// {
// ErrorList.Add("WorkTime", new List<string> { "气相色谱仪已超出最大工作时长" });
// }
// else
// {
// ErrorList.Remove("WorkTime");
// }
//}
//else if (_sensorID == "基因测序仪")
//{
// workInt = Convert.ToInt64(workTime) * 60 * 60;
// if (Convert.ToInt64(value) > workInt)
// {
// ErrorList.Add("WorkTime", new List<string> { "基因测序仪已超出最大工作时长" });
// }
// else
// {
// ErrorList.Remove("WorkTime");
// }
//}
//else
//{
// ErrorList.Remove("WorkTime");
//}
//this.RaiseErrorsChanged("WorkTime");
}
}
private string _durationTime;
/// <summary>
/// 循环时长
/// </summary>
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");
// }
//}
}
}
#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<SysSequentialDetails>("model");
startTime = parameters.GetValue<DateTime>("sequentialStartTime");
endTime = parameters.GetValue<DateTime>("sequentialEndTime");
kind = parameters.GetValue<string>("kind");
if (SequentialDetail == null)
{
this.Title = "新增时序配置";
}
else
{
this.Title = "编辑时序配置";
var su = _sequentialService.Find<SysSequentialDetails>(SequentialDetail.Number);
Number = su.Number;
SequenceGroup = su.SequenceGroup;
SensorID = su.SensorID;
WorkTime = su.WorkTime;
DurationTime = su.DurationTime;
Device = SensorID;
}
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;
}
var data = _sequentialService.GetSequentials(kind).ToList();
if (data.Count != 0)
{
var data1 = _sequentialService.GetSequentials(kind).First();
if (data1.StartTime == startTime && data1.EndTime == endTime)
{
//编辑情况下
if (Number != 0)
{
if (Device != SensorID)
{
//传感器是否已存在校验
var selectFirst = _sequentialService.Query<SysSequentialDetails>(m => m.SensorID == SensorID && m.SequenceGroup == SequenceGroup && m.Sequential == kind && m.IsDelete == 0).ToList();
if (selectFirst.Count != 0)
{
System.Windows.MessageBox.Show(kind + "- 配置" + SequenceGroup + "中已存在" + SensorID, "提示");
return;
}
}
}
else
{
//传感器是否已存在校验
var select = _sequentialService.Query<SysSequentialDetails>(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;
}
}
}
///循环时长校验
if (data1.StartTime == startTime && data1.EndTime == endTime)
{
var SysSequentialdata1 = _sequentialService.GetSequentialSequenceGroup(SequenceGroup, kind, data1.Number);
if (SysSequentialdata1 != null)
{
if (DurationTime != SysSequentialdata1.DurationTime)
{
System.Windows.MessageBox.Show(kind + "- 配置" + SequenceGroup + " 已存在循环时长:" + SysSequentialdata1.DurationTime + "s" + ",无法修改", "提示");
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;
}
//保存时 首次去查询 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,
WorkTime = WorkTime,
SequenceGroup = SequenceGroup,
CreateTime = DateTime.Now,
Sequential = kind,
IsDelete = 0
});
//向时序总表中插入相应的数据
_sequentialService.Insert(new SysSequentialTotal
{
Sequential = kind,
StartTime = startTime,
EndTime = endTime,
SensorID = SensorID,
DurationTime = DurationTime,
WorkTime = WorkTime,
SequenceGroup = SequenceGroup,
CreateTime = DateTime.Now,
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
});
//向时序总表中插入相应的数据
_sequentialService.Insert(new SysSequentialTotal
{
Sequential = kind,
StartTime = startTime,
EndTime = endTime,
SensorID = SensorID,
DurationTime = DurationTime,
WorkTime = WorkTime,
SequenceGroup = SequenceGroup,
CreateTime = DateTime.Now,
IsDelete = 0
});
}
else
{
//修改传感器配置
var ssd = _sequentialService.Find<SysSequentialDetails>(Number);
ssd.SensorID = SensorID;
ssd.DurationTime = DurationTime;
ssd.WorkTime = WorkTime;
ssd.SequenceGroup = SequenceGroup;
_sequentialService.Update(ssd);
//修改时序总表中的相应的数据
var ssm = _sequentialService.Find<SysSequentialTotal>(Number);
ssm.SensorID = SensorID;
ssm.DurationTime = DurationTime;
ssm.WorkTime = WorkTime;
ssm.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<SysSequential>(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
});
//向时序总表中插入相应的数据
_sequentialService.Insert(new SysSequentialTotal
{
Sequential = kind,
StartTime = startTime,
EndTime = endTime,
SensorID = SensorID,
DurationTime = DurationTime,
WorkTime = WorkTime,
SequenceGroup = SequenceGroup,
CreateTime = DateTime.Now,
IsDelete = 0
});
}
}
base.DoSave();
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.Message, "提示");
}
}
}
}