Files
YuPu-OrpaonEMS/OrpaonEMS.App/ViewModels/SysConfigViewModel.cs
2025-02-28 22:23:13 +08:00

508 lines
16 KiB
C#

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<PeakValleyConfig>().OrderBy(a => a.Index).ToList();
ListPeakValleyDtoItems = new ObservableCollection<PeakValleyConfig>(ConfigData);
//ConfigDataService.energyStorageRunConfig.MaxBatChargRatio
}
/// <summary>
/// 弹窗服务
/// </summary>
public IDialogService DialogService { get; }
public IFreeSql FreeSql { get; }
/// <summary>
/// 配置服务
/// </summary>
public ConfigDataService ConfigDataService { get; }
#region MqttConfigSave
private DelegateCommand _BtnMqttConfigSave;
/// <summary>
/// BtnMqtt配置 命令
/// </summary>
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;
/// <summary>
/// 通信 配置 命令
/// </summary>
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;
/// <summary>
/// 运行 配置 命令
/// </summary>
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;
/// <summary>
/// 运行 配置 命令
/// </summary>
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;
/// <summary>
/// 运行 配置 命令
/// </summary>
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<PeakValleyConfig> _ListPeakValleyDtoItems = new ObservableCollection<PeakValleyConfig>();
/// <summary>
/// PeakValley数据集合
/// </summary>
public ObservableCollection<PeakValleyConfig> ListPeakValleyDtoItems
{
get { return _ListPeakValleyDtoItems; }
set { _ListPeakValleyDtoItems = value; RaisePropertyChanged(); }
}
private DelegateCommand _TimeConfigBtnCmd;
/// <summary>
/// 时间设置命令
/// </summary>
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<BmsRwCell>("Model");
}
else if (par.Result == ButtonResult.Cancel)
{
//取消
}
});
}
private DelegateCommand _BtnDataLogFileOpen;
/// <summary>
/// 数据记录日志打开文件 命令
/// </summary>
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;
/// <summary>
/// 当前选中的
/// </summary>
public PeakValleyConfig PeakValleyConfigSelected
{
get { return _PeakValleyConfigSelected; }
set { _PeakValleyConfigSelected = value; RaisePropertyChanged(); }
}
//private DelegateCommand _PeakValleyConfigUpCmd;
///// <summary>
///// 保存命令
///// </summary>
//public DelegateCommand PeakValleyConfigUpCmd
//{
// set
// {
// _PeakValleyConfigUpCmd = value;
// }
// get
// {
// if (_PeakValleyConfigUpCmd == null)
// {
// _PeakValleyConfigUpCmd = new DelegateCommand(() => PeakValleyConfigUpCmdMethod());
// }
// return _PeakValleyConfigUpCmd;
// }
//}
private DelegateCommand _PeakValleyConfigAddCmd;
/// <summary>
/// 保存命令
/// </summary>
public DelegateCommand PeakValleyConfigAddCmd
{
set
{
_PeakValleyConfigAddCmd = value;
}
get
{
if (_PeakValleyConfigAddCmd == null)
{
_PeakValleyConfigAddCmd = new DelegateCommand(() => PeakValleyConfigAddCmdMethod());
}
return _PeakValleyConfigAddCmd;
}
}
/// <summary>
/// 新增
/// </summary>
/// <exception cref="NotImplementedException"></exception>
private void PeakValleyConfigAddCmdMethod()
{
//弹窗
DialogService.ShowDialog("TimeConfigView", new DialogParameters() { { "Model", null } }, (par) =>
{
if (par.Result == ButtonResult.OK)
{
//程序名称
var ReturnValue = par.Parameters.GetValue<PeakValleyConfig>("Model");
if (!CheckIndexSame(ReturnValue.Index))
{
MessageBox.Show("序号重复!", "提示", MessageBoxButton.OK, MessageBoxImage.Hand);
return;
}
FreeSql.Insert<PeakValleyConfig>(ReturnValue)
.ExecuteAffrows();
var ConfigData = FreeSql.Select<PeakValleyConfig>().OrderBy(a => a.Index).ToList();
ListPeakValleyDtoItems = new ObservableCollection<PeakValleyConfig>(ConfigData);
ConfigDataService.ListEnergyStoragePeakValleyTimeConfig = ConfigData;
}
else if (par.Result == ButtonResult.Cancel)
{
//取消
}
});
}
private DelegateCommand<PeakValleyConfig> _PeakValleyConfigEditCmd;
/// <summary>
/// 保存命令
/// </summary>
public DelegateCommand<PeakValleyConfig> PeakValleyConfigEditCmd
{
set
{
_PeakValleyConfigEditCmd = value;
}
get
{
if (_PeakValleyConfigEditCmd == null)
{
_PeakValleyConfigEditCmd = new DelegateCommand<PeakValleyConfig>((par) => PeakValleyConfigEditCmdMethod(par));
}
return _PeakValleyConfigEditCmd;
}
}
/// <summary>
/// 修改方法
/// </summary>
/// <exception cref="NotImplementedException"></exception>
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<PeakValleyConfig>("Model");
//if (!CheckIndexSame(ReturnValue.Index))
//{
// MessageBox.Show("序号重复!", "提示", MessageBoxButton.OK, MessageBoxImage.Hand);
// //刷新,编辑的那个会更新到列表中
// var ConfigData1 = FreeSql.Select<PeakValleyConfig>().OrderBy(a => a.Index).ToList();
// ListPeakValleyDtoItems = new ObservableCollection<PeakValleyConfig>(ConfigData1);
// return;
//}
FreeSql.Update<PeakValleyConfig>()
.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<PeakValleyConfig>().OrderBy(a => a.Index).ToList();
ListPeakValleyDtoItems = new ObservableCollection<PeakValleyConfig>(ConfigData);
ConfigDataService.ListEnergyStoragePeakValleyTimeConfig = ConfigData;
}
else if (par.Result == ButtonResult.Cancel)
{
//取消
}
});
}
/// <summary>
/// 判断是否相同
/// 返回True 代表不重复
/// 返回False 代表重复
/// </summary>
/// <returns></returns>
private bool CheckIndexSame(int index)
{
var IndexCount = FreeSql.Select<PeakValleyConfig>().Where(a => a.Index == index).Count();
if (IndexCount > 0)
{
return false;
}
return true;
}
private DelegateCommand<PeakValleyConfig> _PeakValleyConfigDeleteCmd;
/// <summary>
/// 删除命令
/// </summary>
public DelegateCommand<PeakValleyConfig> PeakValleyConfigDeleteCmd
{
set
{
_PeakValleyConfigDeleteCmd = value;
}
get
{
if (_PeakValleyConfigDeleteCmd == null)
{
_PeakValleyConfigDeleteCmd = new DelegateCommand<PeakValleyConfig>((par) => PeakValleyConfigDeleteCmdMethod(par));
}
return _PeakValleyConfigDeleteCmd;
}
}
/// <summary>
/// 删除方法
/// </summary>
/// <exception cref="NotImplementedException"></exception>
private void PeakValleyConfigDeleteCmdMethod(PeakValleyConfig peakValleyConfig)
{
var ddd = peakValleyConfig;
MessageBoxResult dialogResult = MessageBox.Show("你确定要删除当前的数据吗?", "提示", MessageBoxButton.OKCancel, MessageBoxImage.Hand);
if (dialogResult == MessageBoxResult.OK)
{
FreeSql.Delete<PeakValleyConfig>(peakValleyConfig.Id).ExecuteAffrows();
//更新界面数据
var ConfigData = FreeSql.Select<PeakValleyConfig>().OrderBy(a => a.Index).ToList();
ListPeakValleyDtoItems = new ObservableCollection<PeakValleyConfig>(ConfigData);
}
}
#endregion
}
}