using System; using Prism.Mvvm; using System.Linq; using Prism.Events; using Prism.Commands; using System.Windows.Media; using SHJX.Service.Common.Event; using System.Collections.Generic; using SHJX.Service.Model.EventArgs; using SHJX.Service.Common.UserColor; using SHJX.Service.Model.XmlModules; using SHJX.Service.Model.CRUDModules; using SHJX.Service.Control.Interface; using System.Collections.ObjectModel; using SHJX.Service.Model.ControlModules; using SHJX.Service.Model.Enums; using SHJX.Service.Control.Extends; namespace SHJX.Service.Main.ViewModels { public class SampleRightPosViewModel : BindableBase { #region Fields private readonly ISampleService _sampleService; #endregion #region Peoperties public ObservableCollection DetailControls { get; set; } #endregion public SampleRightPosViewModel(ISampleService sampleService, IEventAggregator ea) { _sampleService = sampleService; DetailControls = new ObservableCollection(); SamplePosition leftPosition = _sampleService.GetSamplePosition("Left"); SamplePosition middlePosition = _sampleService.GetSamplePosition("Middle"); int startCount = leftPosition.Count + middlePosition.Count + 1; SamplePosition position = _sampleService.GetSamplePosition("Right"); for (int i = 0; i < position.Count; i++) { SampleDetailControl sampleDetail = new() { Index = i, Name = $"{position.Value}{i + startCount}", TagCurrentTagNo = $"{i + startCount}" }; DetailControls.Add(sampleDetail); } #region Subscribe Event ea.GetEvent().Subscribe(SampleDetailReceived); ea.GetEvent().Subscribe(UpdateSampleDetailReceived); ea.GetEvent().Subscribe(UpdateSampleDetailReceivedNull); ea.GetEvent().Subscribe(UpdateTaskResult); ea.GetEvent().Subscribe(TaskFinishNotice); ea.GetEvent().Subscribe(AdjustResult); #endregion } #region Commands private DelegateCommand _loadingCommand; public DelegateCommand LoadingCommand => _loadingCommand ??= new DelegateCommand(ExecuteLoadingCommand); #endregion #region Executes private void ExecuteLoadingCommand() { List details = _sampleService.GetSampleDetailByPosition("Right"); foreach (SampleDetail detail in details) { DetailControls.FirstOrDefault(item => item.Name.Equals(detail.NodeName, StringComparison.Ordinal)).SampleDetailInfo = detail.DetailInfo; DetailControls.FirstOrDefault(item => item.Name.Equals(detail.NodeName, StringComparison.Ordinal)).SampleTagColor = detail.TaskStatus.Equals(DetailState.New) ? UColor.CheckedColor : detail.TaskStatus.Equals(DetailState.Doing) ? UColor.SentColor : UColor.MainColor; } } #endregion #region Event Methods private void SampleDetailReceived(List detailArgs) { foreach (SampleDetail detailArg in detailArgs) { SampleDetailControl detail = DetailControls.FirstOrDefault(item => item.Name.Equals(detailArg.NodeName)); if (detail is null) { continue; } SampleDetail sample = _sampleService.GetSampleDetailByName(detailArg.NodeName); if (sample.TaskStatus.Equals(DetailState.Doing)) { continue; } sample.AcidBaseProp = detailArg.AcidBaseProp; sample.DetailInfo = detailArg.DetailInfo; sample.SampleType = detailArg.SampleType; sample.TaskStatus = detailArg.TaskStatus; sample.SampleVolume = detailArg.SampleVolume; sample.SampleMultiple = detailArg.SampleMultiple; sample.QuicklyTitration = detailArg.QuicklyTitration; sample.IsCalibration = detailArg.IsCalibration; (string detailinfo, SolidColorBrush tagColor) = detailArg.TaskStatus switch { DetailState.Init => (string.Empty, UColor.MainColor), DetailState.New => (detailArg.DetailInfo, UColor.CheckedColor), _ => throw new ArgumentException("未找到对应的枚举状态定义") }; DetailControls[detail.Index].SampleTagColor = tagColor; DetailControls[detail.Index].SampleDetailInfo = detailinfo; DetailControls[detail.Index].SampleResultColor = "#000000"; _sampleService.UpdateSampleDetail(sample); } } private void UpdateSampleDetailReceived(List objs) { List details = DetailControls.Where(item => objs.Contains(item.Name)).ToList(); foreach (SampleDetailControl detail in details) { DetailControls[detail.Index].SampleTagColor = UColor.SentColor; DetailControls[detail.Index].SampleDetailInfo = detail.SampleDetailInfo; } } private void UpdateSampleDetailReceivedNull(List objs) { EquipmentArea obj = objs[0]; List details = DetailControls.Where(item => obj.PointName.Contains(item.Name)).ToList(); foreach (SampleDetailControl detail in details) { if (obj.Enable == true) { DetailControls[detail.Index].SampleTagColor = UColor.MainColor; } else { DetailControls[detail.Index].SampleTagColor = UColor.SentColor; } DetailControls[detail.Index].SampleDetailInfo = ""; } } private void UpdateTaskResult(TaskResultArgs arg) { if (arg is null) { return; } SampleDetailControl detail = DetailControls.Where(item => item.Name.Equals(arg.Name)).FirstOrDefault(); if (detail is null) { return; } DetailControls[detail.Index].SampleVolume = arg.Amount; DetailControls[detail.Index].SampleResult = arg.Result; DetailControls[detail.Index].SampleResultColor = arg.ResultColor; } private void TaskFinishNotice(string arg) { if (arg is null) { return; } SampleDetailControl detail = DetailControls.FirstOrDefault(item => item.Name.Equals(arg)); if (detail is null) { return; } DetailControls[detail.Index].SampleTagColor = UColor.MainColor; } private void AdjustResult(AdjustResultArgs arg) { List tasks = _sampleService.GetSampleTaskByWaveKey(arg.Key, "Right"); foreach (EquipmentTask task in tasks) { SampleDetailControl detail = DetailControls.Where(item => item.Name.Equals(task.Source)).FirstOrDefault(); if (detail is null) { continue; } task.Result = CalculationResult.CalculateSample(task, arg.Value); _sampleService.UpdateResult(task); DetailControls[detail.Index].SampleResult = task.Result.ToString("F2"); } } #endregion } }