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

592 lines
21 KiB
C#

using OrpaonEMS.App.Com;
using OrpaonEMS.App.Models;
using OrpaonEMS.App.Services;
using OrpaonEMS.Core;
using OrpaonEMS.Model;
using OrpaonEMS.Model.Enums;
using Prism.Commands;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace OrpaonEMS.App.ViewModels
{
public class YuPuReportViewModel : DialogViewModel
{
/// <summary>
/// 弹窗服务
/// </summary>
public IDialogService DialogService { get; }
public IFreeSql FreeSql { get; }
/// <summary>
/// 配置服务
/// </summary>
public ConfigDataService ConfigDataService { get; }
public YuPuReportViewModel(IDialogService dialogService, IFreeSql freeSql, ConfigDataService configDataService)
{
DialogService = dialogService;
FreeSql = freeSql;
ConfigDataService = configDataService;
this.Title = "修改";
var ConfigData = FreeSql.Select<YuPuPeakValleyConfig>().OrderBy(a => a.Index).ToList();
ListPeakValleyDtoItems = new ObservableCollection<YuPuPeakValleyConfig>(ConfigData);
ListCurPeakValleyTimeConfig = ConfigData;
YuPuEleFees = new YuPuEleFees();
}
private YuPuEleFees _YuPuEleFees;
/// <summary>
/// 费用统计
/// </summary>
public YuPuEleFees YuPuEleFees
{
get { return _YuPuEleFees; }
set { _YuPuEleFees = value; RaisePropertyChanged(); }
}
private double _SolarPrice=0.88;
/// <summary>
/// 光伏费用
/// </summary>
public double SolarPrice
{
get { return _SolarPrice; }
set { _SolarPrice = value; RaisePropertyChanged(); }
}
private DelegateCommand _EleFeesCmd;
/// <summary>
/// 电费计算指令
/// </summary>
public DelegateCommand EleFeesCmd
{
set
{
_EleFeesCmd = value;
}
get
{
if (_EleFeesCmd == null)
{
_EleFeesCmd = new DelegateCommand(() => EleFeesCmdMethod());
}
return _EleFeesCmd;
}
}
/// <summary>
/// 电费计算执行方法
/// </summary>
private void EleFeesCmdMethod()
{
try
{
var year=int.Parse(SelectedYear.Replace("年", ""));
var start = int.Parse(SelectedStartMonth.Replace("月", ""));
var end = int.Parse(SelectedEndMonth.Replace("月", ""));
var startTime = new DateTime(year, start, 1, 0, 0, 0);
var endTime = new DateTime(year, end, GetCurMonthLastDay(DateTime.Now.Year.ToString(), end.ToString()).Day, 23, 59, 59);
var ConfigData = FreeSql.Select<YuPuHourData>().Where(a => a.CreateTime >= startTime && a.CreateTime <= endTime).ToList();
if (ConfigData.Count > 0)
{
ListYuPuHourData = ConfigData;
}
YuPuEleFees = new YuPuEleFees();
foreach (var item in ListYuPuHourData)
{
YuPuEleFees.TaxSolarDisChargPw = YuPuEleFees.TaxSolarDisChargPw + item.SolarTaxHourCharg;
YuPuEleFees.ManageSolarDisChargPw = YuPuEleFees.ManageSolarDisChargPw + item.SolarManageHourCharg;
YuPuEleFees.TaxSolarDisCharg = YuPuEleFees.TaxSolarDisChargPw * SolarPrice;
switch (GetElePVByHour(item.HourInfo))
{
case ElePVEnum.TopPeak:
YuPuEleFees.TopPeakCharg = YuPuEleFees.TopPeakCharg + item.EsManageHourCharg * GetPriceByHour(item.HourInfo);//充电耗费
YuPuEleFees.TopPeakDisCharg = YuPuEleFees.TopPeakDisCharg + item.EsManageHourDisCharg * GetPriceByHour(item.HourInfo);//放电耗费
YuPuEleFees.TopPeakChargPw = YuPuEleFees.TopPeakChargPw + item.EsManageHourCharg;//充电电量
YuPuEleFees.TopPeakDisChargPw = YuPuEleFees.TopPeakDisChargPw + item.EsManageHourDisCharg;//放电电量
break;
case ElePVEnum.Peak:
YuPuEleFees.PeakCharg = YuPuEleFees.PeakCharg + item.EsManageHourCharg * GetPriceByHour(item.HourInfo);//充电耗费
YuPuEleFees.PeakDisCharg = YuPuEleFees.PeakDisCharg + item.EsManageHourDisCharg * GetPriceByHour(item.HourInfo);//放电耗费
YuPuEleFees.PeakChargPw = YuPuEleFees.PeakChargPw + item.EsManageHourCharg;//充电电量
YuPuEleFees.PeakDisChargPw = YuPuEleFees.PeakDisChargPw + item.EsManageHourDisCharg;//放电电量
break;
case ElePVEnum.Valley:
YuPuEleFees.ValleyCharg = YuPuEleFees.ValleyCharg + item.EsManageHourCharg * GetPriceByHour(item.HourInfo);//充电耗费
YuPuEleFees.ValleyDisCharg = YuPuEleFees.ValleyDisCharg + item.EsManageHourDisCharg * GetPriceByHour(item.HourInfo);//放电耗费
YuPuEleFees.ValleyChargPw = YuPuEleFees.ValleyChargPw + item.EsManageHourCharg;//充电电量
YuPuEleFees.ValleyDisChargPw = YuPuEleFees.ValleyDisChargPw + item.EsManageHourDisCharg;//放电电量
break;
case ElePVEnum.Flat:
YuPuEleFees.FlatCharg = YuPuEleFees.FlatCharg + item.EsManageHourCharg * GetPriceByHour(item.HourInfo);//充电耗费
YuPuEleFees.FlatDisCharg = YuPuEleFees.FlatDisCharg + item.EsManageHourDisCharg * GetPriceByHour(item.HourInfo);//放电耗费
YuPuEleFees.FlatChargPw = YuPuEleFees.FlatChargPw + item.EsManageHourCharg;//充电电量
YuPuEleFees.FlatDisChargPw = YuPuEleFees.FlatDisChargPw + item.EsManageHourDisCharg;//放电电量
break;
default:
break;
}
}
YuPuEleFees.ManageTotalEleFees = YuPuEleFees.ManageTotalEleFees + YuPuEleFees.TopPeakDisCharg + YuPuEleFees.PeakDisCharg + YuPuEleFees.ValleyDisCharg + YuPuEleFees.FlatDisCharg -
YuPuEleFees.TopPeakCharg - YuPuEleFees.PeakCharg - YuPuEleFees.ValleyCharg - YuPuEleFees.FlatCharg;
YuPuEleFees.TotalEleFees = (YuPuEleFees.ManageTotalEleFees + YuPuEleFees.TaxSolarDisCharg) * 0.95;
}
catch (Exception ex)
{
MessageBox.Show($"计算出现错误:{ex.Message}");
}
}
/// <summary>
/// 获取月度信息的最后一天
/// </summary>
/// <param name="year"></param>
/// <param name="mon"></param>
/// <returns></returns>
public static DateTime GetCurMonthLastDay(string year, string mon)
{
DateTime AssemblDate = Convert.ToDateTime(year + "-" + mon + "-" + "01"); // 组装当前指定月份
return AssemblDate.AddDays(1 - AssemblDate.Day).AddMonths(1).AddDays(-1); // 返回指定当前月份的最后一天
}
private ObservableCollection<YuPuPeakValleyConfig> _ListPeakValleyDtoItems = new ObservableCollection<YuPuPeakValleyConfig>();
/// <summary>
/// PeakValley数据集合
/// </summary>
public ObservableCollection<YuPuPeakValleyConfig> ListPeakValleyDtoItems
{
get { return _ListPeakValleyDtoItems; }
set { _ListPeakValleyDtoItems = value; RaisePropertyChanged(); }
}
/// <summary>
/// 搜索的小时数据
/// </summary>
public List<YuPuHourData> ListYuPuHourData { get; set; } = new List<YuPuHourData>();
/// <summary>
/// 根据小时信息获取价格
/// </summary>
/// <returns></returns>
private double GetPriceByHour(int hour)
{
foreach (var item in ListCurPeakValleyTimeConfig)
{
var start = int.Parse(item.StartTime.Split(":")[0]);
var end = int.Parse(item.EndTime.Split(":")[0]);
if (hour >= start && hour <= end)
{
return item.Price;
}
}
return 0;
}
/// <summary>
/// 根据小时信息获取峰谷价信息
/// </summary>
/// <returns></returns>
private ElePVEnum GetElePVByHour(int hour)
{
foreach (var item in ListCurPeakValleyTimeConfig)
{
var start = int.Parse(item.StartTime.Split(":")[0]);
var end = int.Parse(item.EndTime.Split(":")[0]);
if (hour >= start && hour <= end)
{
return item.ElePV;
}
}
return ElePVEnum.Flat;
}
private string _SelectedStartMonth;
/// <summary>
/// 开始月份
/// </summary>
public string SelectedStartMonth
{
get { return _SelectedStartMonth; }
set { _SelectedStartMonth = value; RaisePropertyChanged(); }
}
private string _SelectedYear;
/// <summary>
/// 年份
/// </summary>
public string SelectedYear
{
get { return _SelectedYear; }
set { _SelectedYear = value; RaisePropertyChanged(); }
}
private string _SelectedEndMonth;
/// <summary>
/// 结束月份
/// </summary>
public string SelectedEndMonth
{
get { return _SelectedEndMonth; }
set { _SelectedEndMonth = value; RaisePropertyChanged(); }
}
#region DataGrid操作
/// <summary>
/// 储能柜的削峰填谷的配置数据集
/// </summary>
public List<YuPuPeakValleyConfig> ListCurPeakValleyTimeConfig { get; set; } = new List<YuPuPeakValleyConfig>();
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<YuPuPeakValleyConfig>("Model");
//程序名称
var TempValue = par.Parameters.GetValue<PeakValleyConfig>("Model");
var ReturnValue = new YuPuPeakValleyConfig()
{
ElePV = TempValue.ElePV,
Enable = TempValue.Enable,
EndTime = TempValue.EndTime,
Id = TempValue.Id,
Index = TempValue.Index,
Price = TempValue.Price,
StartTime = TempValue.StartTime,
};
if (!CheckIndexSame(ReturnValue.Index))
{
MessageBox.Show("序号重复!", "提示", MessageBoxButton.OK, MessageBoxImage.Hand);
return;
}
FreeSql.Insert<YuPuPeakValleyConfig>(ReturnValue)
.ExecuteAffrows();
var ConfigData = FreeSql.Select<YuPuPeakValleyConfig>().OrderBy(a => a.Index).ToList();
ListPeakValleyDtoItems = new ObservableCollection<YuPuPeakValleyConfig>(ConfigData);
ListCurPeakValleyTimeConfig = ConfigData;
}
else if (par.Result == ButtonResult.Cancel)
{
//取消
}
});
}
private DelegateCommand<YuPuPeakValleyConfig> _PeakValleyConfigEditCmd;
/// <summary>
/// 保存命令
/// </summary>
public DelegateCommand<YuPuPeakValleyConfig> PeakValleyConfigEditCmd
{
set
{
_PeakValleyConfigEditCmd = value;
}
get
{
if (_PeakValleyConfigEditCmd == null)
{
_PeakValleyConfigEditCmd = new DelegateCommand<YuPuPeakValleyConfig>((par) => PeakValleyConfigEditCmdMethod(par));
}
return _PeakValleyConfigEditCmd;
}
}
/// <summary>
/// 修改方法
/// </summary>
/// <exception cref="NotImplementedException"></exception>
private void PeakValleyConfigEditCmdMethod(YuPuPeakValleyConfig peakValleyConfig)
{
var Data = new PeakValleyConfig();
Data.StartTime = peakValleyConfig.StartTime;
Data.EndTime = peakValleyConfig.EndTime;
Data.Price = peakValleyConfig.Price;
Data.ElePV = peakValleyConfig.ElePV;
Data.Enable = peakValleyConfig.Enable;
Data.Id = peakValleyConfig.Id;
Data.Index = peakValleyConfig.Index;
//弹窗
DialogService.ShowDialog("TimeConfigView", new DialogParameters() { { "Model", Data } }, (par) =>
{
if (par.Result == ButtonResult.OK)
{
//程序名称
var TempValue = par.Parameters.GetValue<PeakValleyConfig>("Model");
var ReturnValue = new YuPuPeakValleyConfig()
{
ElePV = TempValue.ElePV,
Enable = TempValue.Enable,
EndTime = TempValue.EndTime,
Id = TempValue.Id,
Index = TempValue.Index,
Price = TempValue.Price,
StartTime = TempValue.StartTime,
};
//if (!CheckIndexSame(ReturnValue.Index))
//{
// MessageBox.Show("序号重复!", "提示", MessageBoxButton.OK, MessageBoxImage.Hand);
// //刷新,编辑的那个会更新到列表中
// var ConfigData1 = FreeSql.Select<YuPuPeakValleyConfig>().OrderBy(a => a.Index).ToList();
// ListPeakValleyDtoItems = new ObservableCollection<YuPuPeakValleyConfig>(ConfigData1);
// return;
//}
FreeSql.Update<YuPuPeakValleyConfig>()
.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<YuPuPeakValleyConfig>().OrderBy(a => a.Index).ToList();
ListPeakValleyDtoItems = new ObservableCollection<YuPuPeakValleyConfig>(ConfigData);
ListCurPeakValleyTimeConfig = ConfigData;
}
else if (par.Result == ButtonResult.Cancel)
{
//取消
}
});
}
/// <summary>
/// 判断是否相同
/// 返回True 代表不重复
/// 返回False 代表重复
/// </summary>
/// <returns></returns>
private bool CheckIndexSame(int index)
{
var IndexCount = FreeSql.Select<YuPuPeakValleyConfig>().Where(a => a.Index == index).Count();
if (IndexCount > 0)
{
return false;
}
return true;
}
private DelegateCommand<YuPuPeakValleyConfig> _PeakValleyConfigDeleteCmd;
/// <summary>
/// 删除命令
/// </summary>
public DelegateCommand<YuPuPeakValleyConfig> PeakValleyConfigDeleteCmd
{
set
{
_PeakValleyConfigDeleteCmd = value;
}
get
{
if (_PeakValleyConfigDeleteCmd == null)
{
_PeakValleyConfigDeleteCmd = new DelegateCommand<YuPuPeakValleyConfig>((par) => PeakValleyConfigDeleteCmdMethod(par));
}
return _PeakValleyConfigDeleteCmd;
}
}
/// <summary>
/// 删除方法
/// </summary>
/// <exception cref="NotImplementedException"></exception>
private void PeakValleyConfigDeleteCmdMethod(YuPuPeakValleyConfig peakValleyConfig)
{
var ddd = peakValleyConfig;
MessageBoxResult dialogResult = MessageBox.Show("你确定要删除当前的数据吗?", "提示", MessageBoxButton.OKCancel, MessageBoxImage.Hand);
if (dialogResult == MessageBoxResult.OK)
{
FreeSql.Delete<YuPuPeakValleyConfig>(peakValleyConfig.Id).ExecuteAffrows();
//更新界面数据
var ConfigData = FreeSql.Select<YuPuPeakValleyConfig>().OrderBy(a => a.Index).ToList();
ListPeakValleyDtoItems = new ObservableCollection<YuPuPeakValleyConfig>(ConfigData);
}
}
#endregion
private DelegateCommand saveCmd;
/// <summary>
/// 保存命令
/// </summary>
public DelegateCommand SaveCmd
{
set
{
saveCmd = value;
}
get
{
if (saveCmd == null)
{
saveCmd = new DelegateCommand(() => SaveCmdMethod());
}
return saveCmd;
}
}
/// <summary>
/// 保存命令方法
/// </summary>
/// <exception cref="NotImplementedException"></exception>
private void SaveCmdMethod()
{
//DialogParameters pars = new DialogParameters
// {
// { "Model", ConfigModel }
// };
RaiseRequestClose(new DialogResult(ButtonResult.OK, null));
}
private DelegateCommand cancelCmd;
/// <summary>
/// 保存命令
/// </summary>
public DelegateCommand CancelCmd
{
set
{
cancelCmd = value;
}
get
{
if (cancelCmd == null)
{
cancelCmd = new DelegateCommand(() => CancelCmdMethod());
}
return cancelCmd;
}
}
/// <summary>
/// 取消命令方法
/// </summary>
/// <exception cref="NotImplementedException"></exception>
private void CancelCmdMethod()
{
RaiseRequestClose(new DialogResult(ButtonResult.Cancel));
}
/// <summary>
/// 窗口打开时的传递的参数
/// </summary>
/// <param name="parameters"></param>
public override void OnDialogOpened(IDialogParameters parameters)
{
//ConfigModel = parameters.GetValue<BmsRwCell>("Model");
}
}
}