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