using System; using Prism.Mvvm; using System.Linq; using Prism.Commands; using SHJX.Service.Model.Control; using System.Windows.Controls; using SHJX.Service.CustomControl; using System.Collections.Generic; using SHJX.Service.Common.Extend; using SHJX.Service.Common.ReadXML; using System.Collections.ObjectModel; using SHJX.Service.Common.UserDelegate; using SHJX.Service.Common.ExtendElement; using SHJX.Service.Control.ServiceController; namespace SHJX.Service.ModelView { public class TemplateWindowViewModel : BindableBase { #region Field private string _searchStr = string.Empty; private readonly TemplateController _control; #endregion #region Property public List DetailSourceInfos { get; set; } public ObservableCollection DetailInfos { get; set; } private object _selectedEmployee; public object SelectedEmployee { get => _selectedEmployee; set => SetProperty(ref _selectedEmployee, value); } #endregion public TemplateWindowViewModel(ReadConfigUtil config) { _control = new TemplateController(config); DetailSourceInfos = new List(); DetailInfos = new ObservableCollection(); ImportTemlateFromLocaltion(); } #region Command private DelegateCommand _interfaceDisplayCommand; public DelegateCommand InterfaceDisplayCommand => _interfaceDisplayCommand ?? new DelegateCommand(ExecuteInterfaceDisplayCommand); private DelegateCommand _templateSearchChangedCommand; public DelegateCommand TemplateSearchChangedCommand => _templateSearchChangedCommand ?? new DelegateCommand(ExecuteTemplateSearchChangedCommand); private DelegateCommand _savaTemplateCommand; public DelegateCommand SavaTemplateCommand => _savaTemplateCommand ?? new DelegateCommand(ExecuteSavaTemplateCommand); private DelegateCommand _importTemplateToInterfaceCommand; public DelegateCommand ImportTemplateToInterfaceCommand => _importTemplateToInterfaceCommand ?? new DelegateCommand(ExecuteImportTemplateToInterfaceCommand); private DelegateCommand _importTemplateCommand; public DelegateCommand ImportTemplateCommand => _importTemplateCommand ?? new DelegateCommand(ExecuteImportTemplateCommand); private DelegateCommand _insertOneDataCommand; public DelegateCommand InsertOneDataCommand => _insertOneDataCommand ?? new DelegateCommand(ExecuteInsertOneDataCommand); private DelegateCommand _deleteOneDataCommand; public DelegateCommand DeleteOneDataCommand => _deleteOneDataCommand ?? new DelegateCommand(ExecuteDeleteOneDataCommand); private DelegateCommand _updateOneDataCommand; public DelegateCommand UpdateOneDataCommand => _updateOneDataCommand ?? new DelegateCommand(ExecuteUpdateOneDataCommand); #endregion #region Execute private void ExecuteUpdateOneDataCommand() { if (SelectedEmployee is not SampleDetail detail) { UMessageBox.Error("未选中对象,无法进行修改操作!"); return; } InputDialog inputDialog = new(false, detail); if (inputDialog.ShowDialog().Equals(true)) { var infos = DetailSourceInfos.Clone(); infos.Where(item => item.NodeName.Equals(detail.NodeName)).ToArray()[0].SampleType = inputDialog.TextType; infos.Where(item => item.NodeName.Equals(detail.NodeName)).ToArray()[0].Concentration = inputDialog.TextConcentration.Substring(0, 1); SettingTemplateDataGrid(infos); }; } /// /// 模板导入 /// private void ExecuteImportTemplateCommand() { ImportTemlateFromLocaltion(); } /// /// 删除一条数据 /// /// private void ExecuteDeleteOneDataCommand() { if (SelectedEmployee is not SampleDetail detail) { UMessageBox.Error("未选中对象,无法进行删除操作!"); return; } var infos = DetailSourceInfos.Clone(); infos.Remove(infos.FirstOrDefault(item => item.NodeName.Equals(detail.NodeName))); SettingTemplateDataGrid(infos); } /// /// 插入新数据 /// private void ExecuteInsertOneDataCommand() { InputDialog inputDialog = new(); if (inputDialog.ShowDialog().Equals(false)) return; string pointName = inputDialog.TextPoint; string sampleType = inputDialog.TextType; string sampleConcentration = inputDialog.TextConcentration.Substring(0, 1); var infos = DetailSourceInfos.Clone(); if (infos.Where(it => it.NodeName.Trim().Equals(pointName.Trim())).Any()) { UMessageBox.SingleBtnInfo("已有节点数据,请勿重复添加!"); return; } SampleDetail detail = new(); detail.NodeName = pointName; detail.SampleType = sampleType; detail.Concentration = sampleConcentration; if (sampleType.In("空白", "标定")) { detail.Detail = sampleType; } infos.Add(detail); SettingTemplateDataGrid(infos); } /// /// 导入模板到界面 /// private void ExecuteImportTemplateToInterfaceCommand() { Messager.Send("ReceiveTemplateData", DetailInfos.ToList()); } /// /// 界面选中数据展示 /// private void ExecuteInterfaceDisplayCommand() { Action> action = (details) => SettingTemplateDataGrid(details); Messager.Send("ReceiveTemplateDataFromMain", action); } private void SettingTemplateDataGrid(List templates) { if (DetailSourceInfos.Count > 0) { DetailSourceInfos.Clear(); } if (DetailInfos.Count > 0) { DetailInfos.Clear(); } List templates2 = new List(); ; foreach (var item in templates) { if (item.Concentration != "") { templates2.Add(item); } } DetailSourceInfos = templates2.OrderBy(item => Convert.ToInt32(item?.NodeName.Substring(1))).ToList(); DetailInfos.AddRange(DetailSourceInfos); SearchSampleDetails(); } /// /// 模板保存 /// private void ExecuteSavaTemplateCommand() { _control.SaveTemplate(DetailInfos.ToList()); } /// /// 搜索 /// /// private void ExecuteTemplateSearchChangedCommand(object parameter) { _searchStr = Convert.ToString(((TextBox)parameter)?.Text); SearchSampleDetails(); } private void SearchSampleDetails() { if (string.IsNullOrWhiteSpace(_searchStr)) { DetailInfos.Clear(); DetailInfos.AddRange(DetailSourceInfos); return; } var findList = DetailSourceInfos.Where(item => item.NodeName.ToLower().Trim().Contains(_searchStr.ToLower().Trim())).ToList(); DetailInfos.Clear(); DetailInfos.AddRange(findList); } /// /// 从本地导入模板 /// private void ImportTemlateFromLocaltion() { var templates = _control.GetSampleDetailsByTemplate(); if (templates is null) { return; } SettingTemplateDataGrid(templates); } #endregion } }