TemplateWindowViewModel.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using System;
  2. using Prism.Mvvm;
  3. using System.Linq;
  4. using Prism.Commands;
  5. using SHJX.Service.Model.Control;
  6. using System.Windows.Controls;
  7. using SHJX.Service.CustomControl;
  8. using System.Collections.Generic;
  9. using SHJX.Service.Common.Extend;
  10. using SHJX.Service.Common.ReadXML;
  11. using System.Collections.ObjectModel;
  12. using SHJX.Service.Common.UserDelegate;
  13. using SHJX.Service.Common.ExtendElement;
  14. using SHJX.Service.Control.ServiceController;
  15. namespace SHJX.Service.ModelView
  16. {
  17. public class TemplateWindowViewModel : BindableBase
  18. {
  19. #region Field
  20. private string _searchStr = string.Empty;
  21. private readonly TemplateController _control;
  22. #endregion
  23. #region Property
  24. public List<SampleDetail> DetailSourceInfos { get; set; }
  25. public ObservableCollection<SampleDetail> DetailInfos { get; set; }
  26. private object _selectedEmployee;
  27. public object SelectedEmployee
  28. {
  29. get => _selectedEmployee;
  30. set => SetProperty(ref _selectedEmployee, value);
  31. }
  32. #endregion
  33. public TemplateWindowViewModel(ReadConfigUtil config)
  34. {
  35. _control = new TemplateController(config);
  36. DetailSourceInfos = new List<SampleDetail>();
  37. DetailInfos = new ObservableCollection<SampleDetail>();
  38. ImportTemlateFromLocaltion();
  39. }
  40. #region Command
  41. private DelegateCommand _interfaceDisplayCommand;
  42. public DelegateCommand InterfaceDisplayCommand => _interfaceDisplayCommand ?? new DelegateCommand(ExecuteInterfaceDisplayCommand);
  43. private DelegateCommand<object> _templateSearchChangedCommand;
  44. public DelegateCommand<object> TemplateSearchChangedCommand => _templateSearchChangedCommand ?? new DelegateCommand<object>(ExecuteTemplateSearchChangedCommand);
  45. private DelegateCommand _savaTemplateCommand;
  46. public DelegateCommand SavaTemplateCommand => _savaTemplateCommand ?? new DelegateCommand(ExecuteSavaTemplateCommand);
  47. private DelegateCommand _importTemplateToInterfaceCommand;
  48. public DelegateCommand ImportTemplateToInterfaceCommand => _importTemplateToInterfaceCommand ?? new DelegateCommand(ExecuteImportTemplateToInterfaceCommand);
  49. private DelegateCommand _importTemplateCommand;
  50. public DelegateCommand ImportTemplateCommand => _importTemplateCommand ?? new DelegateCommand(ExecuteImportTemplateCommand);
  51. private DelegateCommand _insertOneDataCommand;
  52. public DelegateCommand InsertOneDataCommand => _insertOneDataCommand ?? new DelegateCommand(ExecuteInsertOneDataCommand);
  53. private DelegateCommand _deleteOneDataCommand;
  54. public DelegateCommand DeleteOneDataCommand => _deleteOneDataCommand ?? new DelegateCommand(ExecuteDeleteOneDataCommand);
  55. private DelegateCommand _updateOneDataCommand;
  56. public DelegateCommand UpdateOneDataCommand => _updateOneDataCommand ?? new DelegateCommand(ExecuteUpdateOneDataCommand);
  57. #endregion
  58. #region Execute
  59. private void ExecuteUpdateOneDataCommand()
  60. {
  61. if (SelectedEmployee is not SampleDetail detail)
  62. {
  63. UMessageBox.Error("未选中对象,无法进行修改操作!");
  64. return;
  65. }
  66. InputDialog inputDialog = new(false, detail);
  67. if (inputDialog.ShowDialog().Equals(true))
  68. {
  69. var infos = DetailSourceInfos.Clone();
  70. infos.Where(item => item.NodeName.Equals(detail.NodeName)).ToArray()[0].SampleType = inputDialog.TextType;
  71. infos.Where(item => item.NodeName.Equals(detail.NodeName)).ToArray()[0].Concentration = inputDialog.TextConcentration.Substring(0, 1);
  72. SettingTemplateDataGrid(infos);
  73. };
  74. }
  75. /// <summary>
  76. /// 模板导入
  77. /// </summary>
  78. private void ExecuteImportTemplateCommand()
  79. {
  80. ImportTemlateFromLocaltion();
  81. }
  82. /// <summary>
  83. /// 删除一条数据
  84. /// </summary>
  85. /// <param name="obj"></param>
  86. private void ExecuteDeleteOneDataCommand()
  87. {
  88. if (SelectedEmployee is not SampleDetail detail)
  89. {
  90. UMessageBox.Error("未选中对象,无法进行删除操作!");
  91. return;
  92. }
  93. var infos = DetailSourceInfos.Clone();
  94. infos.Remove(infos.FirstOrDefault(item => item.NodeName.Equals(detail.NodeName)));
  95. SettingTemplateDataGrid(infos);
  96. }
  97. /// <summary>
  98. /// 插入新数据
  99. /// </summary>
  100. private void ExecuteInsertOneDataCommand()
  101. {
  102. InputDialog inputDialog = new();
  103. if (inputDialog.ShowDialog().Equals(false)) return;
  104. string pointName = inputDialog.TextPoint;
  105. string sampleType = inputDialog.TextType;
  106. string sampleConcentration = inputDialog.TextConcentration.Substring(0, 1);
  107. var infos = DetailSourceInfos.Clone();
  108. if (infos.Where(it => it.NodeName.Trim().Equals(pointName.Trim())).Any())
  109. {
  110. UMessageBox.SingleBtnInfo("已有节点数据,请勿重复添加!");
  111. return;
  112. }
  113. SampleDetail detail = new();
  114. detail.NodeName = pointName;
  115. detail.SampleType = sampleType;
  116. detail.Concentration = sampleConcentration;
  117. if (sampleType.In("空白", "标定"))
  118. {
  119. detail.Detail = sampleType;
  120. }
  121. infos.Add(detail);
  122. SettingTemplateDataGrid(infos);
  123. }
  124. /// <summary>
  125. /// 导入模板到界面
  126. /// </summary>
  127. private void ExecuteImportTemplateToInterfaceCommand()
  128. {
  129. Messager.Send("ReceiveTemplateData", DetailInfos.ToList());
  130. }
  131. /// <summary>
  132. /// 界面选中数据展示
  133. /// </summary>
  134. private void ExecuteInterfaceDisplayCommand()
  135. {
  136. Action<List<SampleDetail>> action = (details) => SettingTemplateDataGrid(details);
  137. Messager.Send("ReceiveTemplateDataFromMain", action);
  138. }
  139. private void SettingTemplateDataGrid(List<SampleDetail> templates)
  140. {
  141. if (DetailSourceInfos.Count > 0)
  142. {
  143. DetailSourceInfos.Clear();
  144. }
  145. if (DetailInfos.Count > 0)
  146. {
  147. DetailInfos.Clear();
  148. }
  149. List<SampleDetail> templates2 = new List<SampleDetail>(); ;
  150. foreach (var item in templates)
  151. {
  152. if (item.Concentration != "")
  153. { templates2.Add(item); }
  154. }
  155. DetailSourceInfos = templates2.OrderBy(item => Convert.ToInt32(item?.NodeName.Substring(1))).ToList();
  156. DetailInfos.AddRange(DetailSourceInfos);
  157. SearchSampleDetails();
  158. }
  159. /// <summary>
  160. /// 模板保存
  161. /// </summary>
  162. private void ExecuteSavaTemplateCommand()
  163. {
  164. _control.SaveTemplate(DetailInfos.ToList());
  165. }
  166. /// <summary>
  167. /// 搜索
  168. /// </summary>
  169. /// <param name="parameter"></param>
  170. private void ExecuteTemplateSearchChangedCommand(object parameter)
  171. {
  172. _searchStr = Convert.ToString(((TextBox)parameter)?.Text);
  173. SearchSampleDetails();
  174. }
  175. private void SearchSampleDetails()
  176. {
  177. if (string.IsNullOrWhiteSpace(_searchStr))
  178. {
  179. DetailInfos.Clear();
  180. DetailInfos.AddRange(DetailSourceInfos);
  181. return;
  182. }
  183. var findList = DetailSourceInfos.Where(item => item.NodeName.ToLower().Trim().Contains(_searchStr.ToLower().Trim())).ToList();
  184. DetailInfos.Clear();
  185. DetailInfos.AddRange(findList);
  186. }
  187. /// <summary>
  188. /// 从本地导入模板
  189. /// </summary>
  190. private void ImportTemlateFromLocaltion()
  191. {
  192. var templates = _control.GetSampleDetailsByTemplate();
  193. if (templates is null)
  194. {
  195. return;
  196. }
  197. SettingTemplateDataGrid(templates);
  198. }
  199. #endregion
  200. }
  201. }