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

265 lines
7.5 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.Models;
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 DialogYAxisConfigViewModel : DialogViewModel
{
public DialogYAxisConfigViewModel(IFreeSql freeSql, IMapper mapper, MachineRtDataService machineRtDataService)
{
this.Title = "曲线Y轴配置";
FreeSql = freeSql;
this.Mapper = mapper;
MachineRtDataService = machineRtDataService;
}
/// <summary>
/// FreeSql
/// </summary>
public IFreeSql FreeSql { get; }
/// <summary>
/// AutoMap映射
/// </summary>
public IMapper Mapper { get; }
/// <summary>
/// 数据服务
/// </summary>
public MachineRtDataService MachineRtDataService { get; }
/// <summary>
/// 选中的数据
/// </summary>
public ChartYAxisDto CurSelectedItem { get; set; }
private ObservableCollection<ChartYAxisDto> _ChartYAxisDtoItems;
/// <summary>
/// 数据源控件 数据集合
/// </summary>
public ObservableCollection<ChartYAxisDto> ChartYAxisDtoItems
{
get { return _ChartYAxisDtoItems; }
set { _ChartYAxisDtoItems = 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 ChartYAxisDto;
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()
{
ChartYAxisDtoItems.Add(new ChartYAxisDto()
{
Index = ChartYAxisDtoItems.Max(a => a.Index) + 1
});
}
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<ConfigChartYAxis>(CurSelectedItem.Id).ExecuteAffrows();
ChartYAxisDtoItems.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()
{
//检查空的数据
foreach (var item in _ChartYAxisDtoItems)
{
if (string.IsNullOrEmpty(item.Name))
{
MessageBox.Show("请确认名称是否正确", "提示", MessageBoxButton.OK, MessageBoxImage.Hand);
return;
}
if (string.IsNullOrEmpty(item.Unit))
{
MessageBox.Show("请确认单位是否正确", "提示", MessageBoxButton.OK, MessageBoxImage.Hand);
return;
}
}
//检查数据是否正常
foreach (var item in ChartYAxisDtoItems)
{
FreeSql.InsertOrUpdate<ConfigChartYAxis>()
.SetSource(Mapper.Map<ConfigChartYAxis>(item)).
ExecuteAffrows();
}
DialogParameters pars = new DialogParameters
{
{ "Name", "" }
};
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<ConfigChartYAxis>().ToList();
ChartYAxisDtoItems = new ObservableCollection<ChartYAxisDto>(Mapper.Map<List<ChartYAxisDto>>(Data));
}
}
}