Files
CapMachine/CapMachine.Wpf/ViewModels/DialogExpInfoViewModel.cs
2025-05-27 08:56:01 +08:00

363 lines
10 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using AutoMapper;
using CapMachine.Core;
using CapMachine.Model;
using CapMachine.Wpf.Dtos;
using CapMachine.Wpf.Services;
using Masuit.Tools;
using Prism.Commands;
using Prism.Services.Dialogs;
using System.Collections.ObjectModel;
using System.Windows;
namespace CapMachine.Wpf.ViewModels
{
/// <summary>
/// 试验信息拓展设置
/// </summary>
public class DialogExpInfoViewModel : DialogViewModel
{
public DialogExpInfoViewModel(ConfigService configService, IFreeSql freeSql, PPCService pPCService, IMapper mapper, MachineRtDataService machineRtDataService)
{
this.Title = "试验信息拓展设置";
ConfigService = configService;
FreeSql = freeSql;
PPCService = pPCService;
this.Mapper = mapper;
MachineRtDataService = machineRtDataService;
// 获取ProgramSeg的数据
var workCondList = FreeSql.Select<ProgramSeg>()
.ToList()
.Select(g => g.Name)
.ToList();
// 转换为CbxItems集合都是文本内容
WorkCondCbxItems = new ObservableCollection<CbxItems>(
workCondList.Select(workCond => new CbxItems
{
Key = workCond,
Text = workCond
}));
}
public ConfigService ConfigService { get; }
/// <summary>
/// FreeSql
/// </summary>
public IFreeSql FreeSql { get; }
public PPCService PPCService { get; }
private ObservableCollection<CbxItems> _WorkCondCbxItems;
/// <summary>
/// 供选择的工况集合信息
/// </summary>
public ObservableCollection<CbxItems> WorkCondCbxItems
{
get { return _WorkCondCbxItems; }
set { _WorkCondCbxItems = value; RaisePropertyChanged(); }
}
/// <summary>
/// AutoMap映射
/// </summary>
public IMapper Mapper { get; }
private bool _IsComplete;
/// <summary>
/// 当前行是否允许只读
/// </summary>
public bool IsComplete
{
get { return _IsComplete; }
set { _IsComplete = value; RaisePropertyChanged(); }
}
/// <summary>
/// 数据服务
/// </summary>
public MachineRtDataService MachineRtDataService { get; }
private ExpInfoDto _CurSelectedItem;
/// <summary>
/// 选中的数据
/// </summary>
public ExpInfoDto CurSelectedItem
{
get { return _CurSelectedItem; }
set { _CurSelectedItem = value; RaisePropertyChanged(); }
}
private ObservableCollection<ExpInfoDto> _ExpInfoDtoItems;
/// <summary>
/// 数据源控件 数据集合
/// </summary>
public ObservableCollection<ExpInfoDto> ExpInfoDtoItems
{
get { return _ExpInfoDtoItems; }
set { _ExpInfoDtoItems = value; RaisePropertyChanged(); }
}
private DelegateCommand<object> _GridSelectionChangedCmd;
/// <summary>
/// 选中行数据命令
/// </summary>
public DelegateCommand<object> GridSelectionChangedCmd
{
set
{
_GridSelectionChangedCmd = value;
}
get
{
if (_GridSelectionChangedCmd == null)
{
_GridSelectionChangedCmd = new DelegateCommand<object>((par) => GridSelectionChangedCmdMethod(par));
}
return _GridSelectionChangedCmd;
}
}
private void GridSelectionChangedCmdMethod(object par)
{
//先判断是否是正确的集合数据防止DataGrid的数据源刷新导致的触发事件
var Selecteddata = par as ExpInfoDto;
if (Selecteddata != null)
{
CurSelectedItem = Selecteddata;
IsComplete = CurSelectedItem.IsComplete;
}
}
private DelegateCommand _AddCmd;
/// <summary>
/// 增加方法命令
/// </summary>
public DelegateCommand AddCmd
{
set
{
_AddCmd = value;
}
get
{
if (_AddCmd == null)
{
_AddCmd = new DelegateCommand(() => AddCmdMethod());
}
return _AddCmd;
}
}
/// <summary>
/// 增加方法
/// </summary>
/// <exception cref="NotImplementedException"></exception>
private void AddCmdMethod()
{
ExpInfoDtoItems.Add(new ExpInfoDto()
{
RfNo = PPCService.SuperHeatCoolConfig.Cryogen,
});
}
private DelegateCommand _CopyCmd;
/// <summary>
/// 增加方法命令
/// </summary>
public DelegateCommand CopyCmd
{
set
{
_CopyCmd = value;
}
get
{
if (_CopyCmd == null)
{
_CopyCmd = new DelegateCommand(() => CopyCmdMethod());
}
return _CopyCmd;
}
}
/// <summary>
/// 复制方法
/// </summary>
/// <exception cref="NotImplementedException"></exception>
private void CopyCmdMethod()
{
if (CurSelectedItem != null)
{
var CopyData = CurSelectedItem.DeepClone();
CopyData.Id = 0;
CopyData.IsComplete = false;
//直接复制
ExpInfoDtoItems.Add(CopyData);
}
else
{
MessageBox.Show("请选中后再进行【复制】操作?", "提示", MessageBoxButton.OK, MessageBoxImage.Hand);
}
}
private DelegateCommand _DeleteCmd;
/// <summary>
/// 删除方法命令
/// </summary>
public DelegateCommand DeleteCmd
{
set
{
_DeleteCmd = value;
}
get
{
if (_DeleteCmd == null)
{
_DeleteCmd = new DelegateCommand(() => DeleteCmdMethod());
}
return _DeleteCmd;
}
}
/// <summary>
/// 删除方法
/// </summary>
/// <exception cref="NotImplementedException"></exception>
private void DeleteCmdMethod()
{
if (CurSelectedItem != null)
{
//直接删除掉
FreeSql.Delete<HistoryExp>(CurSelectedItem.Id).ExecuteAffrows();
ExpInfoDtoItems.Remove(CurSelectedItem);
CurSelectedItem = null;
}
else
{
MessageBox.Show("请选中后再进行【删除】操作?", "提示", MessageBoxButton.OK, MessageBoxImage.Hand);
}
}
private DelegateCommand saveCmd;
/// <summary>
/// 保存命令
/// </summary>
public DelegateCommand SaveCmd
{
set
{
saveCmd = value;
}
get
{
if (saveCmd == null)
{
saveCmd = new DelegateCommand(() => SaveCmdMethod());
}
return saveCmd;
}
}
/// <summary>
/// 保存命令方法
/// </summary>
/// <exception cref="NotImplementedException"></exception>
private void SaveCmdMethod()
{
if (CurSelectedItem == null)
{
MessageBox.Show("请选中试验工况后再进行【确定】操作", "提示", MessageBoxButton.OK, MessageBoxImage.Hand);
return;
}
//检查空的数据
foreach (var item in _ExpInfoDtoItems)
{
if (string.IsNullOrEmpty(item.Name))
{
MessageBox.Show("请确认名称是否正确", "提示", MessageBoxButton.OK, MessageBoxImage.Hand);
return;
}
if (string.IsNullOrEmpty(item.WorkCond))
{
MessageBox.Show("请确认工况是否正确", "提示", MessageBoxButton.OK, MessageBoxImage.Hand);
return;
}
}
//检查数据是否正常
foreach (var item in ExpInfoDtoItems)
{
FreeSql.InsertOrUpdate<HistoryExp>()
.SetSource(Mapper.Map<HistoryExp>(item)).
ExecuteAffrows();
}
//当前的工况信息
ConfigService.CurExpInfo = FreeSql.Select<HistoryExp>(CurSelectedItem.Id).IncludeMany(a => a.HistoryWorkCondFiles).First();
DialogParameters pars = new DialogParameters
{
{ "ReturnValue", CurSelectedItem }
};
RaiseRequestClose(new DialogResult(ButtonResult.OK, pars));
}
private DelegateCommand cancelCmd;
/// <summary>
/// 取消命令
/// </summary>
public DelegateCommand CancelCmd
{
set
{
cancelCmd = value;
}
get
{
if (cancelCmd == null)
{
cancelCmd = new DelegateCommand(() => CancelCmdMethod());
}
return cancelCmd;
}
}
/// <summary>
/// 取消命令方法
/// </summary>
/// <exception cref="NotImplementedException"></exception>
private void CancelCmdMethod()
{
RaiseRequestClose(new DialogResult(ButtonResult.Cancel));
}
/// <summary>
/// 窗口打开时的传递的参数
/// </summary>
/// <param name="parameters"></param>
public override void OnDialogOpened(IDialogParameters parameters)
{
//CurGroupTabIndex = parameters.GetValue<int>("TabIndex");
//RefreshChartSelectedData();
var Data = FreeSql.Select<HistoryExp>().ToList();
ExpInfoDtoItems = new ObservableCollection<ExpInfoDto>(Mapper.Map<List<ExpInfoDto>>(Data));
}
}
}