using System; using Prism.Mvvm; using Prism.Events; using Prism.Commands; using Panuon.UI.Silver; using System.Windows.Forms; using SHJX.Service.Common.Event; using SHJX.Service.Common.Interface; using SHJX.Service.Control.Interface; namespace SHJX.Service.Shell.ViewModels.Setting { public class OtherSettingWindowViewModel : BindableBase { #region Fields private readonly ILogService _log; private readonly IEventAggregator _ea; private readonly ISettingService _service; #endregion #region Properties private string _templateFilePath; public string TemplateFilePath { get => _templateFilePath; set => SetProperty(ref _templateFilePath, value); } private string _templatePath; public string TemplatePath { get => _templatePath; set => SetProperty(ref _templatePath, value); } private string _sampleResultFilePath; public string SampleResultFilePath { get => _sampleResultFilePath; set => SetProperty(ref _sampleResultFilePath, value); } private string _resultFilePath; public string ResultPath { get => _resultFilePath; set => SetProperty(ref _resultFilePath, value); } private bool _automaticInLiquid; public bool AutomaticInLiquid { get { return _automaticInLiquid; } set { _automaticInLiquid = value; } } private double _manualTemperature; public double ManualTemperature { get => _manualTemperature; set => SetProperty(ref _manualTemperature, value); } private double _manualHumidity; public double ManualHumidity { get => _manualHumidity; set => SetProperty(ref _manualHumidity, value); } private double _finishTemprature; public double FinishTemprature { get => _finishTemprature; set => SetProperty(ref _finishTemprature, value); } private bool? _exportTypeByExcel; public bool? ExportTypeByExcel { get => _exportTypeByExcel; set => SetProperty(ref _exportTypeByExcel, value); } private bool? _exportTypeByWord; public bool? ExportTypeByWord { get => _exportTypeByWord; set => SetProperty(ref _exportTypeByWord, value); } private bool? _tongsFeedbackValue; public bool? TongsFeedbackValue { get => _tongsFeedbackValue; set => SetProperty(ref _tongsFeedbackValue, value); } #endregion public OtherSettingWindowViewModel(IEventAggregator ea, ILogService log, ISettingService service) { _ea = ea; _log = log; _service = service; AutomaticInLiquid = service.IsAutomaticInLiquid; FinishTemprature = service.TaskFinishTemprature; ExportTypeByExcel = service.IsExportTypeByExcel; ExportTypeByWord = service.IsExportTypeByWord; TongsFeedbackValue = service.TongsFeedback; TemplatePathDispalyValue(_service.GetConfigValue("template")); ResultPathDisplayValue(_service.GetConfigValue("result")); _ea.GetEvent().Subscribe(UpdateSetting); _ea.GetEvent().Subscribe(() => { _ea.GetEvent().Unsubscribe(UpdateSetting); }); } #region Command private DelegateCommand _optTemplatePathCommand; public DelegateCommand OptTemplatePathCommand => _optTemplatePathCommand ??= new DelegateCommand(ExecuteOptTemplatePathCommand); private DelegateCommand _optSampleResultPathCommand; public DelegateCommand OptSampleResultPathCommand => _optSampleResultPathCommand ??= new DelegateCommand(ExecuteOptSampleResultPathCommand); private DelegateCommand _manualSetTemperatureCommand; public DelegateCommand ManualSetTemperatureCommand => _manualSetTemperatureCommand ??= new DelegateCommand(() => { _ea.GetEvent().Publish((Temperature: ManualTemperature, Humidity: ManualHumidity)); }); #endregion #region Execute private void ExecuteOptTemplatePathCommand(object obj) { FolderBrowserDialog dialog = new() { SelectedPath = string.IsNullOrWhiteSpace(string.Empty) ? AppDomain.CurrentDomain.BaseDirectory : string.Empty }; if (dialog.ShowDialog().Equals(DialogResult.OK)) { string folderName = dialog.SelectedPath; string folderPath = folderName + "\\SampleTemplate.xlsx"; TemplatePathDispalyValue(folderPath); } } private void ExecuteOptSampleResultPathCommand(object obj) { FolderBrowserDialog dialog = new() { SelectedPath = string.IsNullOrWhiteSpace(string.Empty) ? AppDomain.CurrentDomain.BaseDirectory : string.Empty }; if (dialog.ShowDialog().Equals(DialogResult.OK)) { string folderName = dialog.SelectedPath; string folderPath = folderName + "\\"; ResultPathDisplayValue(folderPath); } } #endregion #region Method public void UpdateSetting() { _service.UpdatePath("template", TemplatePath); _service.UpdatePath("result", ResultPath); _service.UpdatePath("automatic", AutomaticInLiquid); _service.UpdatePath("finish", FinishTemprature); _service.UpdatePath("excel", ExportTypeByExcel); _service.UpdatePath("word", ExportTypeByWord); _service.UpdatePath("tongs", TongsFeedbackValue); Notice.Show($"设备相关设置更新成功", "Info", 3, Panuon.UI.Silver.MessageBoxIcon.Info); } private void TemplatePathDispalyValue(string folderPath) { string showStr = folderPath.Length > 70 ? $"......{folderPath[(folderPath.Length - 70)..]}" : folderPath; TemplateFilePath = showStr; TemplatePath = folderPath; } private void ResultPathDisplayValue(string folderPath) { string showStr = folderPath.Length > 70 ? $"......{folderPath[(folderPath.Length - 70)..]}" : folderPath; SampleResultFilePath = showStr; ResultPath = folderPath; } #endregion } }