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 { /// /// 用户编辑信息 /// 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; UserLevelOptions = new ObservableCollection { "操作员", "管理员" }; } public ConfigService ConfigService { get; } /// /// FreeSql /// public IFreeSql FreeSql { get; } /// /// AutoMap映射 /// public IMapper Mapper { get; } private bool _IsComplete; /// /// 当前行是否允许只读 /// public bool IsComplete { get { return _IsComplete; } set { _IsComplete = value; RaisePropertyChanged(); } } /// /// 数据服务 /// public MachineRtDataService MachineRtDataService { get; } /// /// 用户等级可选项 /// public ObservableCollection UserLevelOptions { get; } private UserDto _CurSelectedItem; /// /// 选中的数据 /// public UserDto CurSelectedItem { get { return _CurSelectedItem; } set { _CurSelectedItem = value; RaisePropertyChanged(); } } private ObservableCollection _UserDtoItems; /// /// 数据源控件 数据集合 /// public ObservableCollection UserDtoItems { get { return _UserDtoItems; } set { _UserDtoItems = value; RaisePropertyChanged(); } } private DelegateCommand _GridSelectionChangedCmd; /// /// 选中行数据命令 /// public DelegateCommand GridSelectionChangedCmd { set { _GridSelectionChangedCmd = value; } get { if (_GridSelectionChangedCmd == null) { _GridSelectionChangedCmd = new DelegateCommand((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; /// /// 增加方法命令 /// public DelegateCommand AddCmd { set { _AddCmd = value; } get { if (_AddCmd == null) { _AddCmd = new DelegateCommand(() => AddCmdMethod()); } return _AddCmd; } } /// /// 增加方法 /// /// private void AddCmdMethod() { UserDtoItems.Add(new UserDto() { IsEnable = true, Level = "操作员" }); } private DelegateCommand _CopyCmd; /// /// 增加方法命令 /// public DelegateCommand CopyCmd { set { _CopyCmd = value; } get { if (_CopyCmd == null) { _CopyCmd = new DelegateCommand(() => CopyCmdMethod()); } return _CopyCmd; } } /// /// 复制方法 /// /// 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; /// /// 删除方法命令 /// public DelegateCommand DeleteCmd { set { _DeleteCmd = value; } get { if (_DeleteCmd == null) { _DeleteCmd = new DelegateCommand(() => DeleteCmdMethod()); } return _DeleteCmd; } } /// /// 删除方法 /// /// private void DeleteCmdMethod() { if (CurSelectedItem != null) { //直接删除掉 FreeSql.Delete(CurSelectedItem.Id).ExecuteAffrows(); UserDtoItems.Remove(CurSelectedItem); CurSelectedItem = null; } else { MessageBox.Show("请选中后再进行【删除】操作?", "提示", MessageBoxButton.OK, MessageBoxImage.Hand); } } private DelegateCommand saveCmd; /// /// 保存命令 /// public DelegateCommand SaveCmd { set { saveCmd = value; } get { if (saveCmd == null) { saveCmd = new DelegateCommand(() => SaveCmdMethod()); } return saveCmd; } } /// /// 保存命令方法 /// /// 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; } if (!UserLevelOptions.Contains(item.Level)) { MessageBox.Show("等级仅支持【操作员】或【管理员】", "提示", MessageBoxButton.OK, MessageBoxImage.Hand); return; } } //检查数据是否正常 foreach (var item in UserDtoItems) { FreeSql.InsertOrUpdate() .SetSource(Mapper.Map(item)). ExecuteAffrows(); } DialogParameters pars = new DialogParameters { { "ReturnValue", CurSelectedItem } }; RaiseRequestClose(new DialogResult(ButtonResult.OK, pars)); } private DelegateCommand cancelCmd; /// /// 取消命令 /// public DelegateCommand CancelCmd { set { cancelCmd = value; } get { if (cancelCmd == null) { cancelCmd = new DelegateCommand(() => CancelCmdMethod()); } return cancelCmd; } } /// /// 取消命令方法 /// /// private void CancelCmdMethod() { RaiseRequestClose(new DialogResult(ButtonResult.Cancel)); } /// /// 窗口打开时的传递的参数 /// /// public override void OnDialogOpened(IDialogParameters parameters) { //CurGroupTabIndex = parameters.GetValue("TabIndex"); //RefreshChartSelectedData(); var Data = FreeSql.Select().ToList(); UserDtoItems = new ObservableCollection(Mapper.Map>(Data)); } } }