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 { /// /// 弹窗服务 /// public IDialogService DialogService { get; } public IFreeSql FreeSql { get; } /// /// 配置服务 /// public ConfigDataService ConfigDataService { get; } public YuPuReportViewModel(IDialogService dialogService, IFreeSql freeSql, ConfigDataService configDataService) { DialogService = dialogService; FreeSql = freeSql; ConfigDataService = configDataService; this.Title = "修改"; var ConfigData = FreeSql.Select().OrderBy(a => a.Index).ToList(); ListPeakValleyDtoItems = new ObservableCollection(ConfigData); ListCurPeakValleyTimeConfig = ConfigData; YuPuEleFees = new YuPuEleFees(); } private YuPuEleFees _YuPuEleFees; /// /// 费用统计 /// public YuPuEleFees YuPuEleFees { get { return _YuPuEleFees; } set { _YuPuEleFees = value; RaisePropertyChanged(); } } private double _SolarPrice=0.88; /// /// 光伏费用 /// public double SolarPrice { get { return _SolarPrice; } set { _SolarPrice = value; RaisePropertyChanged(); } } private DelegateCommand _EleFeesCmd; /// /// 电费计算指令 /// public DelegateCommand EleFeesCmd { set { _EleFeesCmd = value; } get { if (_EleFeesCmd == null) { _EleFeesCmd = new DelegateCommand(() => EleFeesCmdMethod()); } return _EleFeesCmd; } } /// /// 电费计算执行方法 /// 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().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}"); } } /// /// 获取月度信息的最后一天 /// /// /// /// 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 _ListPeakValleyDtoItems = new ObservableCollection(); /// /// PeakValley数据集合 /// public ObservableCollection ListPeakValleyDtoItems { get { return _ListPeakValleyDtoItems; } set { _ListPeakValleyDtoItems = value; RaisePropertyChanged(); } } /// /// 搜索的小时数据 /// public List ListYuPuHourData { get; set; } = new List(); /// /// 根据小时信息获取价格 /// /// 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; } /// /// 根据小时信息获取峰谷价信息 /// /// 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; /// /// 开始月份 /// public string SelectedStartMonth { get { return _SelectedStartMonth; } set { _SelectedStartMonth = value; RaisePropertyChanged(); } } private string _SelectedYear; /// /// 年份 /// public string SelectedYear { get { return _SelectedYear; } set { _SelectedYear = value; RaisePropertyChanged(); } } private string _SelectedEndMonth; /// /// 结束月份 /// public string SelectedEndMonth { get { return _SelectedEndMonth; } set { _SelectedEndMonth = value; RaisePropertyChanged(); } } #region DataGrid操作 /// /// 储能柜的削峰填谷的配置数据集 /// public List ListCurPeakValleyTimeConfig { get; set; } = new List(); 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"); //程序名称 var TempValue = par.Parameters.GetValue("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(ReturnValue) .ExecuteAffrows(); var ConfigData = FreeSql.Select().OrderBy(a => a.Index).ToList(); ListPeakValleyDtoItems = new ObservableCollection(ConfigData); ListCurPeakValleyTimeConfig = 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(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("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().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); ListCurPeakValleyTimeConfig = 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(YuPuPeakValleyConfig 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 private DelegateCommand saveCmd; /// /// 保存命令 /// public DelegateCommand SaveCmd { set { saveCmd = value; } get { if (saveCmd == null) { saveCmd = new DelegateCommand(() => SaveCmdMethod()); } return saveCmd; } } /// /// 保存命令方法 /// /// private void SaveCmdMethod() { //DialogParameters pars = new DialogParameters // { // { "Model", ConfigModel } // }; RaiseRequestClose(new DialogResult(ButtonResult.OK, null)); } private DelegateCommand cancelCmd; /// /// 保存命令 /// public DelegateCommand CancelCmd { set { cancelCmd = value; } get { if (cancelCmd == null) { cancelCmd = new DelegateCommand(() => CancelCmdMethod()); } return cancelCmd; } } /// /// 取消命令方法 /// /// private void CancelCmdMethod() { RaiseRequestClose(new DialogResult(ButtonResult.Cancel)); } /// /// 窗口打开时的传递的参数 /// /// public override void OnDialogOpened(IDialogParameters parameters) { //ConfigModel = parameters.GetValue("Model"); } } }