using AutoMapper; using BizService; using Model.Dto; using Prism.Commands; using Prism.Events; using Prism.Mvvm; using Prism.Services.Dialogs; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; namespace PLCTool.ViewModels.BasicConfigViewModel { public class AddOrEditDeviceViewModel : BindableBase, IDialogAware { private readonly IEventAggregator _aggregator; private readonly IBasicDeviceService _iBasicDeviceService; private readonly IBasicDeviceKindService _iBasicDeviceKindService; private readonly IBasicProjectService _iBasicProjectService; private readonly IMapper _mapper; private long id = 0; public AddOrEditDeviceViewModel(IEventAggregator aggregator, IBasicDeviceService iBasicDeviceService, IBasicDeviceKindService iBasicDeviceKindService,IBasicProjectService iBasicProjectService, IMapper mapper) { _aggregator = aggregator; _iBasicDeviceService = iBasicDeviceService; _iBasicDeviceKindService = iBasicDeviceKindService; _iBasicProjectService= iBasicProjectService; _mapper = mapper; CloseCommand = new DelegateCommand(Close); SureCommand = new DelegateCommand(Sure); CancelCommand = new DelegateCommand(Close); DeviceKindNameList= _iBasicDeviceKindService.FindAllDeviceKind(); ProjectNameList = _iBasicProjectService.FindAllProject(); } #region idialog接口实现 public string Title { set; get; } = "新增设备"; public event Action RequestClose; public bool CanCloseDialog() { return true; } public void OnDialogClosed() { } public void OnDialogOpened(IDialogParameters parameters) { var getMsg = parameters.GetValues("Key"); ///值不为空,表示修改 if (getMsg != null) { foreach (var item in getMsg) { if (item != null) { Title = "修改设备"; DeviceName = item.DeviceName; DeviceNo = item.DeviceNo; ProjectName = _iBasicProjectService.Find((int)item.ProjectId)?.project_name; DeviceKindName = _iBasicDeviceKindService.Find((int)item.DeviceKindId)?.devicekind_name; Remark = item.Remark; } } } } #endregion #region 私有方法 private void Close() { RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel)); } /// /// 确认 /// /// private void Sure(string obj) { if (string.IsNullOrEmpty(DeviceName)) { MessageBox.Show("请填写设备名称!", "提示", MessageBoxButton.OK, MessageBoxImage.Information); return; } if (string.IsNullOrEmpty(DeviceNo)) { MessageBox.Show("请填写设备编号!", "提示", MessageBoxButton.OK, MessageBoxImage.Information); return; } if (string.IsNullOrEmpty(ProjectName)) { MessageBox.Show("请选择项目名称!", "提示", MessageBoxButton.OK, MessageBoxImage.Information); return; } if (string.IsNullOrEmpty(DeviceKindName)) { MessageBox.Show("请填写设备类型!", "提示", MessageBoxButton.OK, MessageBoxImage.Information); return; } BasDeviceDto basDevice = new BasDeviceDto(); basDevice.DeviceName = DeviceName; basDevice.DeviceNo = DeviceNo; basDevice.ProjectId= (long)(_iBasicProjectService.FindByProjectName(ProjectName)?.project_id); basDevice.DeviceKindId= (long)(_iBasicDeviceKindService.FindByDeviceKindName(DeviceKindName)?.devicekind_id); basDevice.Remark = Remark; DialogParameters parm = new DialogParameters(); parm.Add("ReturnValue", basDevice); RequestClose?.Invoke(new DialogResult(ButtonResult.OK, parm)); } /// /// 重置 /// /// private void ResetMethod(string obj) { } #endregion #region 命令绑定 public DelegateCommand CloseCommand { set; get; } public DelegateCommand SureCommand { set; get; } public DelegateCommand CancelCommand { set; get; } public DelegateCommand TxtLostFocusCommand { set; get; } public DelegateCommand TxtPLCValueLostFocusCommand { set; get; } #endregion #region 变量绑定 private string deviceName; public string DeviceName { get { return deviceName; } set { deviceName = value; RaisePropertyChanged(); } } private string deviceNo; public string DeviceNo { get { return deviceNo; } set { deviceNo = value; RaisePropertyChanged(); } } private string projectName; public string ProjectName { get { return projectName; } set { projectName = value; RaisePropertyChanged(); } } private string deviceKindName; public string DeviceKindName { get { return deviceKindName; } set { deviceKindName = value; RaisePropertyChanged(); } } private List projectNameList; public List ProjectNameList { get { return projectNameList; } set { projectNameList = value; RaisePropertyChanged(); } } private List deviceKindNameList; public List DeviceKindNameList { get { return deviceKindNameList; } set { deviceKindNameList = value; RaisePropertyChanged(); } } private string remark; public string Remark { get { return remark; } set { remark = value; RaisePropertyChanged(); } } #endregion } }