using DevExpress.XtraCharts;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraRichEdit.Model;
using IStation.BLL;
using IStation.WinFrmUI.CalcErQu;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows.Forms;
namespace IStation.WinFrmUI.Monitor
{
public partial class ResultChartControl : DevExpress.XtraEditors.XtraUserControl
{
public ResultChartControl()
{
InitializeComponent();
}
private class CurrentModel
{
///
/// 名称
///
public string Name { get; set; }
///
/// 泵台数
///
public string PumpCount { get; set; }
///
/// 开机时间段
///
public string Time { get; set; }
///
/// 电费
///
public double ElectricityPrice { get; set; }
///
/// 用电量
///
public double Electricity { get; set; }
///
/// 取水量
///
public double Water { get; set; }
///
/// 持续时间
///
public double TimeSpan { get; set; }
///
/// 总取水量
///
public double TotalWater { get; set; }
///
/// 总电费
///
public double TotalElePrice { get; set; }
///
/// 总用电量
///
public double TotalEle { get; set; }
///
/// 千吨水能耗
///
public double QDSLN { get; set; }
}
DateTime DateTime; //api查询需要的日期
DateTime ListDayTime; //listBox当前选择的日期
List _currentModel = new List();
CalcModel.AnaPrj _ana = null;
Model.RunParasData _LastYeay = null;
Model.RunParasData _currentData = null;
///
/// 绑定数据
///
///
public void SetBindingData(DateTime datetime)
{
ListDayTime = datetime;
if (checkEditCalendar.SelectedIndex == 1)
{
DateTime = datetime.AddDays(-366);
}
else
{
DateTime = ConvertDateTime(datetime);
}
_currentModel.Clear();
HistoryDataAPiHelper historyDataAPiHelper = new HistoryDataAPiHelper();
PumpRunHelper pumpRunHelper = new PumpRunHelper();
_ana = IStation.BLL.AnaPrj.GetPrj(datetime); //方案数据
_LastYeay = pumpRunHelper.GetByHistoryOneData(DateTime, out string error); //去年同期
_currentData = pumpRunHelper.GetPumpRunParasOneDay(DateTime, out string err); //实际数据
var LastYearIsOpen = historyDataAPiHelper.MergeTimeSlots(MergeList(_LastYeay));
var CurrentDateOpen = historyDataAPiHelper.MergeTimeSlots(MergeList(_currentData));
if (LastYearIsOpen != null && _ana != null)
{
foreach (var model in LastYearIsOpen)
{
var LastYear = new CurrentModel
{
Name = "去年同期" + model.Item1.ToString("(yyyy-MM-dd)"),
PumpCount = string.Format("{0}台泵", model.Item3.ToString()),
Time = model.Item1.ToString("HH:mm") + "-" + model.Item2.ToString("HH:mm"),
ElectricityPrice = model.Item4,
Water = model.Item5,
Electricity = model.Item6,
TimeSpan = (model.Item2 - model.Item1).TotalMinutes,
TotalEle = _LastYeay.Electric,
TotalElePrice = _LastYeay.Money,
TotalWater = _LastYeay.FlowIn
};
_currentModel.Add(LastYear);
}
}
if (_ana != null)
{
foreach (var item in _ana.BlockTimes)
{
if (item.OpenPumpCount == 0)
{
continue;
}
var project = new CurrentModel
{
Name = "方案" + item.StartTime.ToString("(yyyy-MM-dd)"),
PumpCount = string.Format("{0}台泵", item.OpenPumpCount),
Time = item.StartTime.ToString("HH:mm") + "-" + item.EndTime.ToString("HH:mm"),
Electricity = Math.Round(item.SumPower / 1000, 2),
ElectricityPrice = Math.Round(item.SumMoney / 1000, 2),
Water = Math.Round(item.SumFlow, 2),
TimeSpan = (item.EndTime - item.StartTime).TotalMinutes,
TotalEle = Math.Round(_ana.SumPower4Disp / 1000, 2),
TotalWater = _ana.SumFlow4Disp,
TotalElePrice = Math.Round(_ana.SumMoney / 1000, 2),
QDSLN = _ana.QDSLN
};
_currentModel.Add(project);
}
}
if (DateTime.Now.Date > datetime && CurrentDateOpen != null && LastYearIsOpen != null && _ana != null)
{
foreach (var model in CurrentDateOpen)
{
var CurrentDate = new CurrentModel
{
Name = "实际" + model.Item1.ToString("(yyyy-MM-dd)"),
PumpCount = string.Format("{0}台泵", model.Item3.ToString()),
Time = model.Item1.ToString("HH:mm") + "-" + model.Item2.ToString("HH:mm"),
ElectricityPrice = model.Item4,
Water = model.Item5,
Electricity = model.Item6,
TimeSpan = (model.Item2 - model.Item1).TotalMinutes,
TotalEle = _currentData.Electric,
TotalElePrice = _currentData.Money,
TotalWater = _currentData.FlowIn
};
_currentModel.Add(CurrentDate);
}
}
GetTideData(Model.eTideSouce.Book, 5, datetime, new DateTime(datetime.Year, datetime.Month, datetime.Day, 23, 59, 59));
GetTideData(Model.eTideSouce.Web, 6, datetime, new DateTime(datetime.Year, datetime.Month, datetime.Day, 23, 59, 59));
this.bindingSource1.DataSource = _currentModel;
this.bindingSource1.ResetBindings(false);
}
//合并所有开始时间-结束时间
private List<(DateTime, DateTime, double, double, double)> MergeList(Model.RunParasData runParasData)
{
List<(DateTime, DateTime, double, double, double)> values = new List<(DateTime, DateTime, double, double, double)>();
foreach (var item in runParasData.Pump1)
{
values.Add((item.StartTime, item.EndTime, item.Money, item.FlowIn, item.Electric));
}
foreach (var item in runParasData.Pump2)
{
values.Add((item.StartTime, item.EndTime, item.Money, item.FlowIn, item.Electric));
}
foreach (var item in runParasData.Pump3)
{
values.Add((item.StartTime, item.EndTime, item.Money, item.FlowIn, item.Electric));
}
foreach (var item in runParasData.Pump4)
{
values.Add((item.StartTime, item.EndTime, item.Money, item.FlowIn, item.Electric));
}
foreach (var item in runParasData.Pump5)
{
values.Add((item.StartTime, item.EndTime, item.Money, item.FlowIn, item.Electric));
}
return values;
}
//农历阴历日期转换
private DateTime ConvertDateTime(DateTime lastYearDay)
{
// 创建一个 ChineseLunisolarCalendar 实例
ChineseLunisolarCalendar chineseCalendar = new ChineseLunisolarCalendar();
int thisYear = chineseCalendar.GetYear(lastYearDay);
int thisMonth = chineseCalendar.GetMonth(lastYearDay);
int thisDay = chineseCalendar.GetDayOfMonth(lastYearDay);
// 将阴历日期减去一年
DateTime lastYearLunarDate = new DateTime(thisYear, thisMonth, thisDay, 0, 0, 0, 0).AddYears(-1);
// 将去年的阴历日期转换为阳历日期
DateTime lastYearSolarDate = ConvertChineseToGregorian(lastYearLunarDate.Year, lastYearLunarDate.Month, lastYearLunarDate.Day);
return lastYearSolarDate;
}
// 将农历日期转换为阳历日期的方法
static DateTime ConvertChineseToGregorian(int year, int month, int day)
{
ChineseLunisolarCalendar chineseCalendar = new ChineseLunisolarCalendar();
// 将农历日期转换为阳历日期
DateTime gregorianDate = chineseCalendar.ToDateTime(year, month, day, 0, 0, 0, 0);
return gregorianDate;
}
//表格点击事件
private void gridControl2_MouseDown(object sender, MouseEventArgs e)
{
GridView gridView = gridControl2.MainView as GridView;
if (gridView != null)
{
// 获取鼠标所在的单元格
DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo hitInfo = gridView.CalcHitInfo(e.Location);
// 如果点击到单元格上
if (hitInfo.InRowCell)
{
// 获取点击的单元格的行和列
int rowHandle = hitInfo.RowHandle;
int columnHandle = hitInfo.Column.VisibleIndex;
object cellValue = gridView.GetRowCellValue(rowHandle, gridView.Columns[columnHandle]);
// 处理获取到的单元格数据
if (cellValue != null)
{
}
}
}
}
//获取水位数据
private void GetTideData(Model.eTideSouce eTideSouce, int sort, DateTime Startday, DateTime Endday)
{
XYDiagram main_diagram = chartControl实时数据.Diagram as XYDiagram;
var axisTime = main_diagram.AxisX;
axisTime.VisualRange.Auto = true;
axisTime.WholeRange.Auto = true;
axisTime.WholeRange.SideMarginsValue = 0;
axisTime.VisualRange.SideMarginsValue = 0;
axisTime.WholeRange.SetMinMaxValues(Startday, Startday.AddDays(1));
axisTime.VisualRange.SetMinMaxValues(Startday, Startday.AddDays(1));
var tidelist = BLL.TideHelper.GetByDayRange(eTideSouce, Startday, Endday, out string a);
var SeriseMonitor = this.chartControl实时数据.Series[sort];
SeriseMonitor.Points.Clear();
if (tidelist != null)
{
foreach (var item in tidelist)
{
SeriseMonitor.Points.Add(new SeriesPoint(item.Time, item.Level));
}
}
if (SeriseMonitor.Points.Count == 0)
{
SeriseMonitor.ShowInLegend = false;
}
else SeriseMonitor.ShowInLegend = true;
}
//比较
private void barBtnCompare_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
var dlg = new HistoryDlg();
dlg.SetBindingData(_ana);
dlg.ShowDialog();
}
//日历check选择变换事件
private void checkEditCalendar_SelectedIndexChanged(object sender, EventArgs e)
{
this.SetBindingData(ListDayTime);
}
/*
private void gridView2_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
{
GridView view = sender as GridView;
// 指定需要合并的列
if (e.Column.FieldName == "PumpCount")
{
// 获取当前单元格所在行的 Name 字段的值
object currentNameValue = view.GetRowCellValue(e.ListSourceRowIndex, "Name");
// int nRowIndex = e.ListSourceRowIndex + 1;
// 获取下一个单元格所在行的 Name 字段的值
int nextRowIndex = e.ListSourceRowIndex + 1;
object nextNameValue = nextRowIndex < view.RowCount ? view.GetRowCellValue(nextRowIndex, "Name") : null;
// 获取当前单元格的 PumpCount 字段的值
object currentPumpCountValue = view.GetRowCellValue(e.ListSourceRowIndex, "PumpCount");
// 获取下一个单元格的 PumpCount 字段的值
object nextPumpCountValue = nextRowIndex < view.RowCount ? view.GetRowCellValue(nextRowIndex, "PumpCount") : null;
// 检查当前单元格的 Name 字段的值与下一个单元格的值是否相同,且当前单元格的 PumpCount 字段的值与下一个单元格的值是否相同,如果满足条件则进行合并
if (currentNameValue != null && nextNameValue != null && currentPumpCountValue != null && nextPumpCountValue != null &&
currentNameValue.Equals(nextNameValue) && currentPumpCountValue.Equals(nextPumpCountValue))
{
// 将下一个单元格的 PumpCount 字段的值设为空字符串,以达到合并效果
// view.SetRowCellValue(nextRowIndex, "PumpCount", nextPumpCountValue);
// view.DeleteRow(nextRowIndex);
view.SetRowCellValue(nextRowIndex, "PumpCount", $"{currentPumpCountValue}");
}
}
}*/
private void gridView2_CellMerge(object sender, CellMergeEventArgs e)
{
foreach (DevExpress.XtraGrid.Columns.GridColumn column in gridView2.Columns)
{
if (column.FieldName == "PumpCount")
{
if (e.CellValue1 == e.CellValue2)
{
column.OptionsColumn.AllowMerge = DevExpress.Utils.DefaultBoolean.True;
}
}
else
column.OptionsColumn.AllowMerge = DevExpress.Utils.DefaultBoolean.True;
}
}
}
}