using OrpaonEMS.App.Dto; using OrpaonEMS.App.Models; using OrpaonEMS.App.Services; using OrpaonEMS.Core; using OrpaonEMS.Model; using Prism.Commands; using Prism.Services.Dialogs; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Windows; namespace OrpaonEMS.App.ViewModels { public class SysConfigViewModel : NavigationViewModel { public SysConfigViewModel(IDialogService dialogService, IFreeSql freeSql, ConfigDataService configDataService) { DialogService = dialogService; FreeSql = freeSql; ConfigDataService = configDataService; var ConfigData = FreeSql.Select().OrderBy(a => a.Index).ToList(); ListPeakValleyDtoItems = new ObservableCollection(ConfigData); //ConfigDataService.energyStorageRunConfig.MaxBatChargRatio } /// /// 弹窗服务 /// public IDialogService DialogService { get; } public IFreeSql FreeSql { get; } /// /// 配置服务 /// public ConfigDataService ConfigDataService { get; } #region MqttConfigSave private DelegateCommand _BtnMqttConfigSave; /// /// BtnMqtt配置 命令 /// public DelegateCommand BtnMqttConfigSave { set { _BtnMqttConfigSave = value; } get { if (_BtnMqttConfigSave == null) { _BtnMqttConfigSave = new DelegateCommand(() => BtnMqttConfigSaveMethod()); } return _BtnMqttConfigSave; } } private void BtnMqttConfigSaveMethod() { ConfigHelper.SetValue("MqttServerUrl", ConfigDataService.MqttServerUrl.ToString()); ConfigHelper.SetValue("MqttServerPort", ConfigDataService.MqttServerPort.ToString()); ConfigHelper.SetValue("MqttUser", ConfigDataService.MqttUser.ToString()); ConfigHelper.SetValue("MqttPwd", ConfigDataService.MqttPwd.ToString()); ConfigHelper.SetValue("MqttClientId", ConfigDataService.MqttClientId.ToString()); } private DelegateCommand _BtnComConfigSave; /// /// 通信 配置 命令 /// public DelegateCommand BtnComConfigSave { set { _BtnComConfigSave = value; } get { if (_BtnComConfigSave == null) { _BtnComConfigSave = new DelegateCommand(() => BtnComConfigSaveMethod()); } return _BtnComConfigSave; } } private void BtnComConfigSaveMethod() { ConfigHelper.SetValue("IsMaster", ConfigDataService.IsMaster == true ? "1" : "0"); ConfigHelper.SetValue("Station", ConfigDataService.Station.ToString()); ConfigHelper.SetValue("BMSIP", ConfigDataService.BMSIP.ToString()); ConfigHelper.SetValue("PCSIP", ConfigDataService.PCSIP.ToString()); ConfigHelper.SetValue("ServerUrl", ConfigDataService.ServerUrl.ToString()); } private DelegateCommand _BtnRunConfigSave1; /// /// 运行 配置 命令 /// public DelegateCommand BtnRunConfigSave1 { set { _BtnRunConfigSave1 = value; } get { if (_BtnRunConfigSave1 == null) { _BtnRunConfigSave1 = new DelegateCommand(() => BtnRunConfigSave1Method()); } return _BtnRunConfigSave1; } } private void BtnRunConfigSave1Method() { ConfigHelper.SetValue("MaxBatChargRatio", ConfigDataService.energyStorageRunConfig.MaxBatChargRatio.ToString("f2")); ConfigHelper.SetValue("MaxBatDisChargRatio", ConfigDataService.energyStorageRunConfig.MaxBatDisChargRatio.ToString("f2")); } private DelegateCommand _BtnRunConfigSave2; /// /// 运行 配置 命令 /// public DelegateCommand BtnRunConfigSave2 { set { _BtnRunConfigSave2 = value; } get { if (_BtnRunConfigSave2 == null) { _BtnRunConfigSave2 = new DelegateCommand(() => BtnRunConfigSave2Method()); } return _BtnRunConfigSave2; } } private void BtnRunConfigSave2Method() { ConfigHelper.SetValue("BMSSocUpSignLimitValue", ConfigDataService.energyStorageRunConfig.BMSSocUpSignLimitValue.ToString("f2")); ConfigHelper.SetValue("BMSSocDownSignLimitValue", ConfigDataService.energyStorageRunConfig.BMSSocDownSignLimitValue.ToString("f2")); } private DelegateCommand _BtnRunConfigSave3; /// /// 运行 配置 命令 /// public DelegateCommand BtnRunConfigSave3 { set { _BtnRunConfigSave3 = value; } get { if (_BtnRunConfigSave3 == null) { _BtnRunConfigSave3 = new DelegateCommand(() => BtnRunConfigSave3Method()); } return _BtnRunConfigSave3; } } private void BtnRunConfigSave3Method() { ConfigHelper.SetValue("PcsChangeValue", ConfigDataService.PcsChangeValue.ToString("f2")); ConfigHelper.SetValue("ControlTargetValue", ConfigDataService.EMSActionLimitValue.TargetValue.ToString("f1")); } #endregion private ObservableCollection _ListPeakValleyDtoItems = new ObservableCollection(); /// /// PeakValley数据集合 /// public ObservableCollection ListPeakValleyDtoItems { get { return _ListPeakValleyDtoItems; } set { _ListPeakValleyDtoItems = value; RaisePropertyChanged(); } } private DelegateCommand _TimeConfigBtnCmd; /// /// 时间设置命令 /// public DelegateCommand TimeConfigBtnCmd { set { _TimeConfigBtnCmd = value; } get { if (_TimeConfigBtnCmd == null) { _TimeConfigBtnCmd = new DelegateCommand(() => TimeConfigBtnCmdMethod()); } return _TimeConfigBtnCmd; } } private void TimeConfigBtnCmdMethod() { //弹窗 DialogService.ShowDialog("TimeConfigView", new DialogParameters() { { "Data", null } }, (par) => { if (par.Result == ButtonResult.OK) { //程序名称 var ReturnValue = par.Parameters.GetValue("Model"); } else if (par.Result == ButtonResult.Cancel) { //取消 } }); } private DelegateCommand _BtnDataLogFileOpen; /// /// 数据记录日志打开文件 命令 /// public DelegateCommand BtnDataLogFileOpen { set { _BtnDataLogFileOpen = value; } get { if (_BtnDataLogFileOpen == null) { _BtnDataLogFileOpen = new DelegateCommand(() => BtnDataLogFileOpenMethod()); } return _BtnDataLogFileOpen; } } private void BtnDataLogFileOpenMethod() { //弹窗 DialogService.ShowDialog("DataLogView", (par) => { if (par.Result == ButtonResult.OK) { } else if (par.Result == ButtonResult.Cancel) { //取消 } }); } #region DataGrid操作 private PeakValleyConfig _PeakValleyConfigSelected; /// /// 当前选中的 /// public PeakValleyConfig PeakValleyConfigSelected { get { return _PeakValleyConfigSelected; } set { _PeakValleyConfigSelected = value; RaisePropertyChanged(); } } //private DelegateCommand _PeakValleyConfigUpCmd; ///// ///// 保存命令 ///// //public DelegateCommand PeakValleyConfigUpCmd //{ // set // { // _PeakValleyConfigUpCmd = value; // } // get // { // if (_PeakValleyConfigUpCmd == null) // { // _PeakValleyConfigUpCmd = new DelegateCommand(() => PeakValleyConfigUpCmdMethod()); // } // return _PeakValleyConfigUpCmd; // } //} private DelegateCommand _PeakValleyConfigAddCmd; /// /// 保存命令 /// public DelegateCommand PeakValleyConfigAddCmd { set { _PeakValleyConfigAddCmd = value; } get { if (_PeakValleyConfigAddCmd == null) { _PeakValleyConfigAddCmd = new DelegateCommand(() => PeakValleyConfigAddCmdMethod()); } return _PeakValleyConfigAddCmd; } } /// /// 新增 /// /// private void PeakValleyConfigAddCmdMethod() { //弹窗 DialogService.ShowDialog("TimeConfigView", new DialogParameters() { { "Model", null } }, (par) => { if (par.Result == ButtonResult.OK) { //程序名称 var ReturnValue = par.Parameters.GetValue("Model"); if (!CheckIndexSame(ReturnValue.Index)) { MessageBox.Show("序号重复!", "提示", MessageBoxButton.OK, MessageBoxImage.Hand); return; } FreeSql.Insert(ReturnValue) .ExecuteAffrows(); var ConfigData = FreeSql.Select().OrderBy(a => a.Index).ToList(); ListPeakValleyDtoItems = new ObservableCollection(ConfigData); ConfigDataService.ListEnergyStoragePeakValleyTimeConfig = ConfigData; } else if (par.Result == ButtonResult.Cancel) { //取消 } }); } private DelegateCommand _PeakValleyConfigEditCmd; /// /// 保存命令 /// public DelegateCommand PeakValleyConfigEditCmd { set { _PeakValleyConfigEditCmd = value; } get { if (_PeakValleyConfigEditCmd == null) { _PeakValleyConfigEditCmd = new DelegateCommand((par) => PeakValleyConfigEditCmdMethod(par)); } return _PeakValleyConfigEditCmd; } } /// /// 修改方法 /// /// private void PeakValleyConfigEditCmdMethod(PeakValleyConfig peakValleyConfig) { //var ddd=peakValleyConfig; //弹窗 DialogService.ShowDialog("TimeConfigView", new DialogParameters() { { "Model", peakValleyConfig } }, (par) => { if (par.Result == ButtonResult.OK) { //程序名称 var ReturnValue = par.Parameters.GetValue("Model"); //if (!CheckIndexSame(ReturnValue.Index)) //{ // MessageBox.Show("序号重复!", "提示", MessageBoxButton.OK, MessageBoxImage.Hand); // //刷新,编辑的那个会更新到列表中 // var ConfigData1 = FreeSql.Select().OrderBy(a => a.Index).ToList(); // ListPeakValleyDtoItems = new ObservableCollection(ConfigData1); // return; //} FreeSql.Update() .Set(a => a.Index, ReturnValue.Index) .Set(a => a.StartTime, ReturnValue.StartTime) .Set(a => a.EndTime, ReturnValue.EndTime) .Set(a => a.ElePV, ReturnValue.ElePV) .Set(a => a.Enable, ReturnValue.Enable) .Set(a => a.Price, ReturnValue.Price) .Where(a => a.Id == peakValleyConfig.Id) .ExecuteAffrows(); var ConfigData = FreeSql.Select().OrderBy(a => a.Index).ToList(); ListPeakValleyDtoItems = new ObservableCollection(ConfigData); ConfigDataService.ListEnergyStoragePeakValleyTimeConfig = ConfigData; } else if (par.Result == ButtonResult.Cancel) { //取消 } }); } /// /// 判断是否相同 /// 返回True 代表不重复 /// 返回False 代表重复 /// /// private bool CheckIndexSame(int index) { var IndexCount = FreeSql.Select().Where(a => a.Index == index).Count(); if (IndexCount > 0) { return false; } return true; } private DelegateCommand _PeakValleyConfigDeleteCmd; /// /// 删除命令 /// public DelegateCommand PeakValleyConfigDeleteCmd { set { _PeakValleyConfigDeleteCmd = value; } get { if (_PeakValleyConfigDeleteCmd == null) { _PeakValleyConfigDeleteCmd = new DelegateCommand((par) => PeakValleyConfigDeleteCmdMethod(par)); } return _PeakValleyConfigDeleteCmd; } } /// /// 删除方法 /// /// private void PeakValleyConfigDeleteCmdMethod(PeakValleyConfig peakValleyConfig) { var ddd = peakValleyConfig; MessageBoxResult dialogResult = MessageBox.Show("你确定要删除当前的数据吗?", "提示", MessageBoxButton.OKCancel, MessageBoxImage.Hand); if (dialogResult == MessageBoxResult.OK) { FreeSql.Delete(peakValleyConfig.Id).ExecuteAffrows(); //更新界面数据 var ConfigData = FreeSql.Select().OrderBy(a => a.Index).ToList(); ListPeakValleyDtoItems = new ObservableCollection(ConfigData); } } #endregion } }