This commit is contained in:
2024-12-18 15:50:21 +08:00
parent 684973e6b7
commit b2c54119ea
214 changed files with 65908 additions and 8461 deletions

View File

@@ -0,0 +1,334 @@
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 DialogUserViewModel : DialogViewModel
{
public DialogUserViewModel(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(); }
}
/// <summary>
/// 数据服务
/// </summary>
public MachineRtDataService MachineRtDataService { get; }
private UserDto _CurSelectedItem;
/// <summary>
/// 选中的数据
/// </summary>
public UserDto CurSelectedItem
{
get { return _CurSelectedItem; }
set { _CurSelectedItem = value; RaisePropertyChanged(); }
}
private ObservableCollection<UserDto> _UserDtoItems;
/// <summary>
/// 数据源控件 数据集合
/// </summary>
public ObservableCollection<UserDto> UserDtoItems
{
get { return _UserDtoItems; }
set { _UserDtoItems = 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 UserDto;
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()
{
UserDtoItems.Add(new UserDto()
{
IsEnable = true,
});
}
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.IsEnable = true;
//直接复制
UserDtoItems.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<User>(CurSelectedItem.Id).ExecuteAffrows();
UserDtoItems.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 _UserDtoItems)
{
if (string.IsNullOrEmpty(item.Name))
{
MessageBox.Show("请确认名称是否正确", "提示", MessageBoxButton.OK, MessageBoxImage.Hand);
return;
}
if (string.IsNullOrEmpty(item.Password))
{
MessageBox.Show("请确认密码是否正确", "提示", MessageBoxButton.OK, MessageBoxImage.Hand);
return;
}
if (string.IsNullOrEmpty(item.Level))
{
MessageBox.Show("请确认等级是否正确", "提示", MessageBoxButton.OK, MessageBoxImage.Hand);
return;
}
}
//检查数据是否正常
foreach (var item in UserDtoItems)
{
FreeSql.InsertOrUpdate<User>()
.SetSource(Mapper.Map<User>(item)).
ExecuteAffrows();
}
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<User>().ToList();
UserDtoItems = new ObservableCollection<UserDto>(Mapper.Map<List<UserDto>>(Data));
}
}
}