SampleLeftPosViewModel.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. using System;
  2. using Prism.Mvvm;
  3. using System.Linq;
  4. using Prism.Events;
  5. using Prism.Commands;
  6. using System.Windows.Media;
  7. using SHJX.Service.Common.Event;
  8. using System.Collections.Generic;
  9. using SHJX.Service.Model.EventArgs;
  10. using SHJX.Service.Common.Interface;
  11. using SHJX.Service.Common.UserColor;
  12. using SHJX.Service.Model.XmlModules;
  13. using System.Collections.ObjectModel;
  14. using SHJX.Service.Control.Interface;
  15. using SHJX.Service.Model.CRUDModules;
  16. using SHJX.Service.Model.ControlModules;
  17. using SHJX.Service.Model.Enums;
  18. using SHJX.Service.Control.Extends;
  19. namespace SHJX.Service.Main.ViewModels
  20. {
  21. public class SampleLeftPosViewModel : BindableBase
  22. {
  23. #region Fields
  24. private readonly ILogService _log;
  25. private readonly ISampleService _sampleService;
  26. #endregion
  27. #region Properties
  28. public ObservableCollection<SampleDetailControl> DetailControls { get; set; }
  29. #endregion
  30. public SampleLeftPosViewModel(IEventAggregator ea, ISampleService sampleService, ILogService log)
  31. {
  32. _log = log;
  33. _sampleService = sampleService;
  34. DetailControls = new ObservableCollection<SampleDetailControl>();
  35. SamplePosition position = _sampleService.GetSamplePosition("Left");
  36. for (int i = 0; i < position.Count; i++)
  37. {
  38. SampleDetailControl sampleDetail = new()
  39. {
  40. Index = i,
  41. Name = $"{position.Value}{(i + 1)}",
  42. TagCurrentTagNo = $"{i + 1}"
  43. };
  44. DetailControls.Add(sampleDetail);
  45. }
  46. #region Subscribe Event
  47. ea.GetEvent<SampleDetailEvent>().Subscribe(SampleDetailReceived);
  48. ea.GetEvent<UpdateSampleDetailEvent>().Subscribe(UpdateSampleDetailReceived);
  49. ea.GetEvent<UpdateSampleDetailNullEvent>().Subscribe(UpdateSampleDetailReceivedNull);
  50. ea.GetEvent<TaskResultNoticeEvent>().Subscribe(UpdateTaskResult);
  51. ea.GetEvent<TaskFinishNoticeEvent>().Subscribe(TaskFinishNotice);
  52. ea.GetEvent<AdjustResultEvent>().Subscribe(AdjustResult);
  53. #endregion
  54. }
  55. #region Commands
  56. private DelegateCommand _loadingCommand;
  57. public DelegateCommand LoadingCommand => _loadingCommand ??= new DelegateCommand(ExecuteLoadingCommand);
  58. #endregion
  59. #region Executes
  60. private void ExecuteLoadingCommand()
  61. {
  62. List<SampleDetail> details = _sampleService.GetSampleDetailByPosition("Left");
  63. foreach (SampleDetail detail in details)
  64. {
  65. DetailControls.FirstOrDefault(item => item.Name.Equals(detail.NodeName, StringComparison.Ordinal)).SampleDetailInfo = detail.DetailInfo;
  66. DetailControls.FirstOrDefault(item => item.Name.Equals(detail.NodeName, StringComparison.Ordinal)).SampleTagColor =
  67. detail.TaskStatus.Equals(DetailState.New) ? UColor.CheckedColor : detail.TaskStatus.Equals(DetailState.Doing)
  68. ? UColor.SentColor : UColor.MainColor;
  69. }
  70. }
  71. #endregion
  72. #region Event Methods
  73. private void SampleDetailReceived(List<SampleDetail> detailArgs)
  74. {
  75. try
  76. {
  77. foreach (SampleDetail detailArg in detailArgs)
  78. {
  79. SampleDetailControl detail = DetailControls.FirstOrDefault(item => item.Name.Equals(detailArg.NodeName));
  80. if (detail is null)
  81. {
  82. continue;
  83. }
  84. SampleDetail sample = _sampleService.GetSampleDetailByName(detailArg.NodeName);
  85. if (sample.TaskStatus.Equals(DetailState.Doing))
  86. {
  87. continue;
  88. }
  89. sample.AcidBaseProp = detailArg.AcidBaseProp;
  90. sample.DetailInfo = detailArg.DetailInfo;
  91. sample.SampleType = detailArg.SampleType;
  92. sample.TaskStatus = detailArg.TaskStatus;
  93. sample.SampleVolume = detailArg.SampleVolume;
  94. sample.SampleMultiple = detailArg.SampleMultiple;
  95. sample.QuicklyTitration = detailArg.QuicklyTitration;
  96. sample.IsCalibration = detailArg.IsCalibration;
  97. (string detailinfo, SolidColorBrush tagColor) = detailArg.TaskStatus switch
  98. {
  99. DetailState.Init => (string.Empty, UColor.MainColor),
  100. DetailState.New => (detailArg.DetailInfo, UColor.CheckedColor),
  101. _ => throw new ArgumentException("未找到对应的枚举状态定义")
  102. };
  103. DetailControls[detail.Index].SampleTagColor = tagColor;
  104. DetailControls[detail.Index].SampleDetailInfo = detailinfo;
  105. DetailControls[detail.Index].SampleResultColor = "#000000";
  106. _sampleService.UpdateSampleDetail(sample);
  107. }
  108. }
  109. catch (Exception ex)
  110. {
  111. _log.ErrorFormat(ex.ToString());
  112. }
  113. }
  114. private void UpdateSampleDetailReceived(List<object> objs)
  115. {
  116. List<SampleDetailControl> details = DetailControls.Where(item => objs.Contains(item.Name)).ToList();
  117. foreach (SampleDetailControl detail in details)
  118. {
  119. DetailControls[detail.Index].SampleTagColor = UColor.SentColor;
  120. DetailControls[detail.Index].SampleDetailInfo = detail.SampleDetailInfo;
  121. }
  122. }
  123. private void UpdateSampleDetailReceivedNull(List<EquipmentArea> objs)
  124. {
  125. EquipmentArea obj = objs[0];
  126. List<SampleDetailControl> details = DetailControls.Where(item => obj.PointName.Contains(item.Name)).ToList();
  127. foreach (SampleDetailControl detail in details)
  128. {
  129. if (obj.Enable == true)
  130. {
  131. DetailControls[detail.Index].SampleTagColor = UColor.MainColor;
  132. }
  133. else
  134. {
  135. DetailControls[detail.Index].SampleTagColor = UColor.SentColor;
  136. }
  137. DetailControls[detail.Index].SampleDetailInfo = "";
  138. }
  139. }
  140. private void UpdateTaskResult(TaskResultArgs arg)
  141. {
  142. if (arg is null)
  143. {
  144. return;
  145. }
  146. SampleDetailControl detail = DetailControls.FirstOrDefault(item => item.Name.Equals(arg.Name));
  147. if (detail is null)
  148. {
  149. return;
  150. }
  151. DetailControls[detail.Index].SampleVolume = arg.Amount;
  152. DetailControls[detail.Index].SampleResult = arg.Result;
  153. DetailControls[detail.Index].SampleResultColor = arg.ResultColor;
  154. }
  155. private void TaskFinishNotice(string arg)
  156. {
  157. if (arg is null)
  158. {
  159. return;
  160. }
  161. SampleDetailControl detail = DetailControls.FirstOrDefault(item => item.Name.Equals(arg));
  162. if (detail is null)
  163. {
  164. return;
  165. }
  166. DetailControls[detail.Index].SampleTagColor = UColor.MainColor;
  167. }
  168. private void AdjustResult(AdjustResultArgs arg)
  169. {
  170. List<EquipmentTask> tasks = _sampleService.GetSampleTaskByWaveKey(arg.Key, "Left");
  171. foreach (EquipmentTask task in tasks)
  172. {
  173. SampleDetailControl detail = DetailControls.Where(item => item.Name.Equals(task.Source)).FirstOrDefault();
  174. if (detail is null)
  175. {
  176. continue;
  177. }
  178. task.Result = CalculationResult.CalculateSample(task, arg.Value);
  179. _sampleService.UpdateResult(task);
  180. DetailControls[detail.Index].SampleResult = task.Result.ToString("F2");
  181. }
  182. }
  183. #endregion
  184. }
  185. }