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.Markup;
using System.Windows.Media.Animation;
namespace InSituLaboratory.ViewModels.Pages.Dialogs
{
///
/// 新增或者增加时序
///
public class ModifySequentialViewModel : DialogViewModelBase
{
#region Model
///
/// 传感器设备集合
///
public List DeviceNodes { get; set; } = new List();
public List SysNumberList { get; set; } = new List();
public SysSequentialDetails SysSequentialdata { get; set; }
public static List Intlist { get; set; } = new List() { 1, 2, 3, 4, 5, 6 };
public string? workTime { get; set; }
public string? minworkTime { get; set; }
public long? workInt { get; set; }
public long? minworkInt { get; set; }
///
/// 记录编辑时第一次带入的设备名称
///
public string Device { 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;
}
}
private string? _sensorID;
///
/// 传感器设备
///
public string? SensorID
{
get { return _sensorID; }
set
{
_sensorID = value;
}
}
private float? _workTime;
///
/// 工作时长
///
public float? WorkTime
{
get { return _workTime; }
set
{
_workTime = value;
}
}
private float? _durationTime;
///
/// 循环时长
///
public float? DurationTime
{
get { return _durationTime; }
set
{
_durationTime = value;
}
}
private string totalKind;
#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;
Device = SensorID;
}
DeviceNodes = _deviceSvice.GetDevices().ToList();
//更换子时序号数据源
SysNumberList = _deviceSvice.GetNum().ToList();
}
public override void DoSave()
{
try
{
#region 非空校验
if (string.IsNullOrEmpty(SequenceGroup))
{
throw new Exception("子时序号不能为空");
}
else if (string.IsNullOrEmpty(SensorID))
{
throw new Exception("设备编号不能为空");
}
else if (WorkTime == null || WorkTime == 0)
{
throw new Exception("工作时长不能为空");
}
else if (DurationTime == null || DurationTime == 0)
{
throw new Exception("循环间隔时长不能为空");
}
#endregion
#region 传感器是否已存在校验 | 循环时长校验
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(m => m.Number == Number).ToList();
if (selectFirst.Count != 0)
{
System.Windows.Forms.MessageBox.Show(kind + "- 子时序" + selectFirst[0].SequenceGroup + "中,已配置" + SensorID + "!!!" + " 请重新配置。", "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
}
}
else
{
//传感器是否已存在校验
var select = _sequentialService.Query(m => m.SensorID == SensorID && m.Sequential == kind && m.IsDelete == 0).ToList();
if (select.Count != 0)
{
foreach (var item in select)
{
System.Windows.Forms.MessageBox.Show(kind + "- 子时序" + item.SequenceGroup + "中,已配置" + SensorID + "!!!" + " 请重新配置。", "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
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.Forms.MessageBox.Show(kind + "- 子时序" + SequenceGroup + " 已存在循环时长:" + SysSequentialdata1.DurationTime + "分钟" + ",无法修改", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
return;
}
}
}
}
#endregion
#region 工作时长是否已超过传感器预设时间
workTime = _deviceSvice.GetDeviceByDeviceName(SensorID);
minworkTime = _deviceSvice.GetDeviceByDeviceNameMinTime(SensorID);
if (workTime == "无")
{
workTime = "0";
}
if (minworkTime == "无")
{
minworkTime = "0";
}
workInt = Convert.ToInt64(workTime); //设备预设最大工作时长
minworkInt = Convert.ToInt64(minworkTime);//设备预设最小工作时长
switch (SensorID)
{
case "气相色谱仪":
if (Convert.ToInt64(WorkTime) > workInt)
{
throw new Exception("您填写的" + SensorID + "设置的工作时长:" + WorkTime + "分钟" + ",已超过设备最大工作时长:" + workTime + "分钟" + "\n 请修改您填写的设备工作时长后重试!!!");
}
else if (Convert.ToInt64(WorkTime) < minworkInt)
{
throw new Exception("您填写的" + SensorID + "设置的工作时长:" + WorkTime + "分钟" + ",未能达到设备最小工作时长:" + minworkInt + "分钟" + "\n 请修改您填写的设备工作时长后重试!!!");
}
break;
case "质谱仪":
if (Convert.ToInt64(WorkTime) > workInt)
{
throw new Exception("您填写的" + SensorID + "设置的工作时长:" + WorkTime + "分钟" + ",已超过设备最大工作时长:" + workTime + "分钟" + "\n 请修改您填写的设备工作时长后重试!!!");
}
else if (Convert.ToInt64(WorkTime) < minworkInt)
{
throw new Exception("您填写的" + SensorID + "设置的工作时长:" + WorkTime + "分钟" + ",未能达到设备最小工作时长:" + minworkInt + "分钟" + "\n 请修改您填写的设备工作时长后重试!!!");
}
break;
case "二氧化碳同位素分析仪":
if (Convert.ToInt64(WorkTime) > workInt)
{
throw new Exception("您填写的" + SensorID + "设置的工作时长:" + WorkTime + "分钟" + ",已超过设备最大工作时长:" + workTime + "分钟" + "\n 请修改您填写的设备工作时长后重试!!!");
}
else if (Convert.ToInt64(WorkTime) < minworkInt)
{
throw new Exception("您填写的" + SensorID + "设置的工作时长:" + WorkTime + "分钟" + ",未能达到设备最小工作时长:" + minworkInt + "分钟" + "\n 请修改您填写的设备工作时长后重试!!!");
}
break;
case "甲烷传感器":
if (Convert.ToInt64(WorkTime) > workInt)
{
throw new Exception("您填写的" + SensorID + "设置的工作时长:" + WorkTime + "分钟" + ",已超过设备最大工作时长:" + workTime + "分钟" + "\n 请修改您填写的设备工作时长后重试!!!");
}
else if (Convert.ToInt64(WorkTime) < minworkInt)
{
throw new Exception("您填写的" + SensorID + "设置的工作时长:" + WorkTime + "分钟" + ",未能达到设备最小工作时长:" + minworkInt + "分钟" + "\n 请修改您填写的设备工作时长后重试!!!");
}
break;
case "显微拉曼分析仪":
if (Convert.ToInt64(WorkTime) < minworkInt)
{
throw new Exception("您填写的" + SensorID + "设置的工作时长:" + WorkTime + "分钟" + ",已超过设备最大工作时长:" + workTime + "分钟" + "\n 请修改您填写的设备工作时长后重试!!!");
}
break;
case "色质联用":
if (Convert.ToInt64(WorkTime) < minworkInt)
{
throw new Exception("您填写的" + SensorID + "设置的工作时长:" + WorkTime + "分钟" + ",未能达到设备最小工作时长:" + minworkInt + "分钟" + "\n 请修改您填写的设备工作时长后重试!!!");
}
break;
default:
break;
}
///计算设备工作时长是否超过总时序时长
System.TimeSpan t1 = endTime - startTime;
float getMinute = (float)t1.TotalMinutes;
//根据开始时间、结束时间、是否删除查询母时序表
//若存在记录,则根据母表ID去查询子表中所有子时序的工作时长与时序总时长进行对比
var serID = 0;
float? time = 0;
var DataList = _sequentialService.Query(m => m.StartTime == startTime && m.EndTime == endTime && m.IsDelete == 0 && m.Sequential == kind).ToList();
if (DataList.Count() != 0)
{
foreach (var Data in DataList)
{
serID = Data.Number;
}
var data_child = _sequentialService.Query(m => m.SysSquentialID == serID && m.IsDelete == 0).ToList();
foreach (var Data in data_child)
{
time += Data.WorkTime;
}
//编辑情况下
if (Number != 0)
{
var child = _sequentialService.Query(m => m.Number == Number).ToList();
if (WorkTime > child[0].WorkTime)
{
time = time - child[0].WorkTime;
var total = time + WorkTime;
if (total > getMinute)
{
throw new Exception("当前设置的传感器总工作时长" + "(" + total + "分钟" + ")" + " 已超过时序总时长!" + "(" + getMinute + "分钟" + ")");
}
}
}
else
{
var total = time + WorkTime;
if (total > getMinute)
{
throw new Exception("当前设置的传感器总工作时长" + "(" + total + "分钟" + ")" + " 已超过时序总时长!" + "(" + getMinute + "分钟" + ")");
}
}
}
else
{
if (WorkTime > getMinute)
{
throw new Exception("当前设置的传感器总工作时长" + "(" + WorkTime + "分钟" + ")" + " 已超过时序总时长!" + "(" + getMinute + "分钟" + ")");
}
}
#endregion
#region 保存
//保存时 首次去查询 data == null 说明数据库中无数据 为第一次插入
if (data.Count() == 0)
{
_sequentialService.Insert(new SysSequential
{
StartTime = startTime,
EndTime = endTime,
CreateTime = DateTime.Now,
Sequential = kind,
Status = "已生成未下发",
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
});
//向时序总表中插入相应的数据
_sequentialService.Insert(new SysSequentialTotal
{
Sequential = kind,
StartTime = startTime,
EndTime = endTime,
SensorID = SensorID,
DurationTime = DurationTime,
WorkTime = WorkTime,
SequenceGroup = SequenceGroup,
CreateTime = DateTime.Now,
Status = "已生成未下发",
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,
Status = "已生成未下发",
IsDelete = 0
});
}
else
{
//修改传感器配置
var ssd = _sequentialService.Find(Number);
ssd.SensorID = SensorID;
ssd.DurationTime = DurationTime;
ssd.WorkTime = WorkTime;
ssd.SequenceGroup = SequenceGroup;
_sequentialService.Update(ssd);
//修改时序总表中的相应的数据
var ssm = _sequentialService.Find(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,
Status = "已生成未下发",
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
});
//向时序总表中插入相应的数据
_sequentialService.Insert(new SysSequentialTotal
{
Sequential = kind,
StartTime = startTime,
EndTime = endTime,
SensorID = SensorID,
DurationTime = DurationTime,
WorkTime = WorkTime,
SequenceGroup = SequenceGroup,
CreateTime = DateTime.Now,
Status = "已生成未下发",
IsDelete = 0
});
}
}
#endregion
base.DoSave();
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
}
}
}