Files
CapMachine/CapMachine.Wpf/ViewModels/DialogLimitConfigViewModel.cs
2024-12-18 15:50:21 +08:00

352 lines
10 KiB
C#
Raw 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;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace CapMachine.Wpf.ViewModels
{
public class DialogLimitConfigViewModel : DialogViewModel
{
/// <summary>
/// 实例化函数
/// </summary>
/// <param name="configService"></param>
/// <param name="freeSql"></param>
/// <param name="mapper"></param>
/// <param name="machineRtDataService"></param>
public DialogLimitConfigViewModel(ConfigService configService, IFreeSql freeSql, IMapper mapper, MachineRtDataService machineRtDataService)
{
this.Title = "限幅设置";
ConfigService = configService;
FreeSql = freeSql;
this.Mapper = mapper;
MachineRtDataService = machineRtDataService;
}
public ConfigService ConfigService { get; }
/// <summary>
/// FreeSql
/// </summary>
public IFreeSql FreeSql { get; }
/// <summary>
/// AutoMap映射
/// </summary>
public IMapper Mapper { get; }
private bool _IsComplete;
/// <summary>
/// 当前行是否允许只读
/// </summary>
public bool IsComplete
{
get { return _IsComplete; }
set { _IsComplete = value; RaisePropertyChanged(); }
}
private string _CurMeterName;
/// <summary>
/// 当前的仪表
/// </summary>
public string CurMeterName
{
get { return _CurMeterName; }
set { _CurMeterName = value; RaisePropertyChanged(); }
}
/// <summary>
/// 数据服务
/// </summary>
public MachineRtDataService MachineRtDataService { get; }
private ConfigLimitDto _CurSelectedItem;
/// <summary>
/// 选中的数据
/// </summary>
public ConfigLimitDto CurSelectedItem
{
get { return _CurSelectedItem; }
set { _CurSelectedItem = value; RaisePropertyChanged(); }
}
private ObservableCollection<ConfigLimitDto> _ConfigLimitDtoItems = new ObservableCollection<ConfigLimitDto>();
/// <summary>
/// 数据源控件 数据集合
/// </summary>
public ObservableCollection<ConfigLimitDto> ConfigLimitDtoItems
{
get { return _ConfigLimitDtoItems; }
set { _ConfigLimitDtoItems = 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 ConfigLimitDto;
if (Selecteddata != null)
{
CurSelectedItem = Selecteddata;
}
}
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()
{
ConfigLimitDtoItems.Add(new ConfigLimitDto()
{
MeterName = CurMeterName,
});
}
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;
//直接复制
ConfigLimitDtoItems.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<ConfigLimit>(CurSelectedItem.Id).ExecuteAffrows();
ConfigLimitDtoItems.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 ConfigLimitDtoItems)
//{
// if (string.IsNullOrEmpty(item.IndexNo))
// {
// MessageBox.Show("请确认名称是否正确", "提示", MessageBoxButton.OK, MessageBoxImage.Hand);
// return;
// }
// if (string.IsNullOrEmpty(item.WorkCond))
// {
// MessageBox.Show("请确认工况是否正确", "提示", MessageBoxButton.OK, MessageBoxImage.Hand);
// return;
// }
//}
//检查数据是否正常
foreach (var item in ConfigLimitDtoItems)
{
FreeSql.InsertOrUpdate<ConfigLimit>()
.SetSource(Mapper.Map<ConfigLimit>(item)).
ExecuteAffrows();
}
////当前的工况信息
//ConfigService.CurConfigLimit = FreeSql.Select<ConfigLimit>(CurSelectedItem.Id).IncludeMany(a => a.HistoryWorkCondFiles).First();
DialogParameters pars = new DialogParameters
{
{ "ReturnValue", "" }
};
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)
{
CurMeterName = parameters.GetValue<string>("Name");
//RefreshChartSelectedData();
var Data = FreeSql.Select<ConfigLimit>().Where(a => a.MeterName == CurMeterName).ToList();
ConfigLimitDtoItems = new ObservableCollection<ConfigLimitDto>(Mapper.Map<List<ConfigLimitDto>>(Data));
}
}
}