123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using LeaRun.Application.IService.PipeNetworkManage;
- using LeaRun.Data;
- using System.Data;
- using LeaRun.Util;
- namespace LeaRun.Application.Service.PipeNetworkManage
- {
- public class FlowService : IFlowService
- {
- SqlHelper sqlHelper = new SqlHelper("PipeNetworkDb");
- /// <summary>
- /// 压力报表
- /// </summary>
- /// <param name="typeFlag">0-日 1-月 2-年</param>
- /// <param name="ids">设备ID集合</param>
- /// <param name="time">查询日期</param>
- /// <returns></returns>
- public string GetFlowReport(string typeFlag, string ids, string time)
- {
- try
- {
- //DateTime nowTime = Convert.ToDateTime(time);
- string sql = "SELECT * FROM MeterAssessmentBase WHERE MeterAssessmentId IN (" + ids + ")";
- DataTable dt = sqlHelper.ExecuteDataTable(sql, CommandType.Text, null);
- StringBuilder jqData = new StringBuilder();
- jqData.Append("[");
- switch (typeFlag)
- {
- case "0":
- jqData.Append(GetDayReportData(dt, time));
- break;
- case "1":
- DateTime nowTime = Convert.ToDateTime(time + "/01");
- jqData.Append(GetMonthReportData(dt, nowTime));
- break;
- case "2":
- DateTime yearTime = Convert.ToDateTime(time + "/01/01");
- jqData.Append(GetYearReportData(dt, yearTime));
- break;
- }
- jqData.Append("]");
- return jqData.ToString();
- }
- catch (Exception ex)
- {
- return "[]";
- }
- }
- /// <summary>
- /// 压力日报表数据
- /// </summary>
- /// <param name="dt"></param>
- /// <param name="time"></param>
- /// <returns></returns>
- public StringBuilder GetDayReportData(DataTable dt, string time)
- {
- StringBuilder sbText = new StringBuilder();
- DataTable[] dtSources = new DataTable[dt.Rows.Count];
- double[] maxValue = new double[dt.Rows.Count];
- double[] minValue = new double[dt.Rows.Count];
- double[] sunValue = new double[dt.Rows.Count];
- DateTime[] maxValueTime = new DateTime[dt.Rows.Count];
- DateTime[] minValueTime = new DateTime[dt.Rows.Count];
- int numCount = 0; ;
- for (int i = 0; i < 24; i++)
- {
- sbText.Append("{");
- sbText.Append("\"showtime\":\"").Append(i + ":00").Append("\",");
- for (int row = 0; row < dt.Rows.Count; row++)
- {
- string deviceId = dt.Rows[row]["MeterAssessmentId"].ToString();
- string tableName = "MeterAssessment_" + deviceId;
- string selDr = "GetDateTime='" + time + " " + i + ":00:00" + "'";
- string selSql = "SELECT * FROM " + tableName + " WHERE DATEDIFF(DAY, '" + time + "' ,GetDateTime)=0 ORDER BY GetDateTime";
- DataTable dtResult;
- //结果存储 避免大量访问数据库
- if (dtSources[row] != null)
- {
- dtResult = dtSources[row];
- }
- else
- {
- dtResult = sqlHelper.ExecuteDataTable(selSql, CommandType.Text, null);
- dtSources[row] = dtResult;
- }
- DataRow[] drs = dtResult.Select(selDr);
- if (drs.Count() > 0)
- {
- numCount++;
- DateTime tempTime = Convert.ToDateTime(drs[0]["GetDateTime"].ToString());
- double instantaneousFlow = Convert.ToDouble(drs[0]["InstantaneousFlow"]);
- //初始化
- if (i == 0)
- {
- minValue[row] = instantaneousFlow;
- maxValue[row] = instantaneousFlow;
- sunValue[row] = instantaneousFlow;
- maxValueTime[row] = tempTime;
- minValueTime[row] = tempTime;
- }
- else
- {
- if (minValue[row] > instantaneousFlow)
- {
- minValue[row] = instantaneousFlow;
- minValueTime[row] = tempTime;
- }
- if (maxValue[row] < instantaneousFlow)
- {
- maxValue[row] = instantaneousFlow;
- maxValueTime[row] = tempTime;
- }
- sunValue[row] = sunValue[row] + instantaneousFlow;
- }
- sbText.Append("\"flow_" + deviceId + "\":\"").Append(drs[0]["InstantaneousFlow"]).Append("\"");
- }
- else
- {
- sbText.Append("\"flow_" + deviceId + "\":\"").Append("--").Append("\"");
- }
- if (row < dt.Rows.Count - 1) sbText.Append(",");
- }
- sbText.Append("},");
- }
- #region 平均值 最小值 最大值
- sbText.Append("{");
- sbText.Append("\"showtime\":\"").Append("平均值").Append("\",");
- //一天中的 平均值
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- string deviceId = dt.Rows[i]["MeterAssessmentId"].ToString();
- sbText.Append("\"flow_" + deviceId + "\":\"").Append((sunValue[i] / numCount).ToString("0.00")).Append("\"");
- if (i < dt.Rows.Count - 1) sbText.Append(",");
- }
- sbText.Append("},");
- sbText.Append("{");
- sbText.Append("\"showtime\":\"").Append("最小值").Append("\",");
- //一天中的 最小值
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- string deviceId = dt.Rows[i]["MeterAssessmentId"].ToString();
- sbText.Append("\"flow_" + deviceId + "\":\"").Append(minValue[i]).Append("\"");
- if (i < dt.Rows.Count - 1) sbText.Append(",");
- }
- sbText.Append("},");
- sbText.Append("{");
- sbText.Append("\"showtime\":\"").Append("最小值时间").Append("\",");
- //一天中的 最小值时间
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- string deviceId = dt.Rows[i]["MeterAssessmentId"].ToString();
- sbText.Append("\"flow_" + deviceId + "\":\"").Append(minValueTime[i].ToString("HH:mm")).Append("\"");
- if (i < dt.Rows.Count - 1) sbText.Append(",");
- }
- sbText.Append("},");
- sbText.Append("{");
- sbText.Append("\"showtime\":\"").Append("最大值").Append("\",");
- //一天中的 最大值
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- string deviceId = dt.Rows[i]["MeterAssessmentId"].ToString();
- sbText.Append("\"flow_" + deviceId + "\":\"").Append(maxValue[i]).Append("\"");
- if (i < dt.Rows.Count - 1) sbText.Append(",");
- }
- sbText.Append("},");
- sbText.Append("{");
- sbText.Append("\"showtime\":\"").Append("最大值时间").Append("\",");
- //一天中的 最小值时间
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- string deviceId = dt.Rows[i]["MeterAssessmentId"].ToString();
- sbText.Append("\"flow_" + deviceId + "\":\"").Append(maxValueTime[i].ToString("HH:mm")).Append("\"");
- if (i < dt.Rows.Count - 1) sbText.Append(",");
- }
- sbText.Append("},");
- sbText.Append("{");
- sbText.Append("\"showtime\":\"").Append("变化系数").Append("\",");
- //一天中的 变化系数 最大值/平均值
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- string deviceId = dt.Rows[i]["MeterAssessmentId"].ToString();
- sbText.Append("\"flow_" + deviceId + "\":\"").Append((maxValue[i] / (sunValue[i] / numCount)).ToString("0.00")).Append("\"");
- if (i < dt.Rows.Count - 1) sbText.Append(",");
- }
- sbText.Append("}");
- #endregion
- return sbText;
- }
- /// <summary>
- /// 压力报表 -日报表 动态表头创建
- /// </summary>
- /// <param name="ids"></param>
- /// <returns></returns>
- public string GetFlowDayReportHead(string ids)
- {
- try
- {
- string sql = "SELECT * FROM MeterAssessmentBase WHERE MeterAssessmentId IN (" + ids + ")";
- DataTable dt = sqlHelper.ExecuteDataTable(sql, CommandType.Text, null);
- StringBuilder sbColMod = new StringBuilder();
- sbColMod.Append("[");
- sbColMod.Append("{");
- sbColMod.Append("\"label\":\"").Append("时间").Append("\",");
- sbColMod.Append("\"name\":\"").Append("showtime").Append("\",");
- sbColMod.Append("\"align\":\"").Append("center").Append("\",");
- sbColMod.Append("\"width\":\"").Append("100").Append("\",");
- sbColMod.Append("\"sortable \":\"").Append("false").Append("\"");
- sbColMod.Append("}");
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- string deviceId = dt.Rows[i]["MeterAssessmentId"].ToString();
- string name = dt.Rows[i]["MeterAssessmentName"].ToString();
- if (i == 0) sbColMod.Append(",");
- sbColMod.Append("{");
- sbColMod.Append("\"label\":\"").Append(name).Append("\",");
- sbColMod.Append("\"name\":\"").Append("flow_" + deviceId).Append("\",");
- sbColMod.Append("\"align\":\"").Append("center").Append("\",");
- sbColMod.Append("\"width\":\"").Append("150").Append("\",");
- sbColMod.Append("\"sortable \":\"").Append("false").Append("\"");
- sbColMod.Append("}");
- if (i < dt.Rows.Count - 1)
- {
- sbColMod.Append(",");
- }
- }
- sbColMod.Append("]");
- return sbColMod.ToString();
- }
- catch (Exception ex)
- {
- return "[]";
- }
- }
- /// <summary>
- /// 压力报表 -月(年)报表 动态表头创建
- /// </summary>
- /// <param name="ids"></param>
- /// <returns></returns>
- public string GetFlowMonthReportHead(string ids)
- {
- try
- {
- string sql = "SELECT * FROM MeterAssessmentBase WHERE MeterAssessmentId IN (" + ids + ")";
- DataTable dt = sqlHelper.ExecuteDataTable(sql, CommandType.Text, null);
- StringBuilder sbColMod = new StringBuilder();
- sbColMod.Append("[");
- sbColMod.Append("{");
- sbColMod.Append("\"label\":\"").Append("时间").Append("\",");
- sbColMod.Append("\"name\":\"").Append("showtime").Append("\",");
- sbColMod.Append("\"align\":\"").Append("center").Append("\",");
- sbColMod.Append("\"width\":\"").Append("100").Append("\",");
- sbColMod.Append("\"sortable \":\"").Append("false").Append("\"");
- sbColMod.Append("}");
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- string deviceId = dt.Rows[i]["MeterAssessmentId"].ToString();
- string name = dt.Rows[i]["MeterAssessmentName"].ToString();
- if (i == 0) sbColMod.Append(",");
- sbColMod.Append("{");
- sbColMod.Append("\"label\":\"").Append("平均值").Append("\",");
- sbColMod.Append("\"name\":\"").Append("avg_" + deviceId).Append("\",");
- sbColMod.Append("\"align\":\"").Append("center").Append("\",");
- sbColMod.Append("\"width\":\"").Append("150").Append("\",");
- sbColMod.Append("\"sortable \":\"").Append("false").Append("\"");
- sbColMod.Append("},");
- sbColMod.Append("{");
- sbColMod.Append("\"label\":\"").Append("最大值").Append("\",");
- sbColMod.Append("\"name\":\"").Append("max_" + deviceId).Append("\",");
- sbColMod.Append("\"align\":\"").Append("center").Append("\",");
- sbColMod.Append("\"width\":\"").Append("150").Append("\",");
- sbColMod.Append("\"sortable \":\"").Append("false").Append("\"");
- sbColMod.Append("},");
- sbColMod.Append("{");
- sbColMod.Append("\"label\":\"").Append("最小值").Append("\",");
- sbColMod.Append("\"name\":\"").Append("min_" + deviceId).Append("\",");
- sbColMod.Append("\"align\":\"").Append("center").Append("\",");
- sbColMod.Append("\"width\":\"").Append("150").Append("\",");
- sbColMod.Append("\"sortable \":\"").Append("false").Append("\"");
- sbColMod.Append("}");
- if (i < dt.Rows.Count - 1)
- {
- sbColMod.Append(",");
- }
- }
- sbColMod.Append("]");
- return sbColMod.ToString();
- }
- catch (Exception ex)
- {
- return "[]";
- }
- }
- /// <summary>
- /// 压力月报表获取
- /// </summary>
- /// <param name="dt"></param>
- /// <param name="time"></param>`
- /// <returns></returns>
- public StringBuilder GetMonthReportData(DataTable dt, DateTime time)
- {
- StringBuilder sbText = new StringBuilder();
- DataTable[] dtSources = new DataTable[dt.Rows.Count];
- int numCount = 0;
- int days = Time.GetDaysOfMonth(time.Year, time.Month);
- for (int i = 0; i < days; i++)
- {
- sbText.Append("{");
- sbText.Append("\"showtime\":\"").Append(time.AddDays(i).ToShortDateString()).Append("\",");
- for (int row = 0; row < dt.Rows.Count; row++)
- {
- string deviceId = dt.Rows[row]["MeterAssessmentId"].ToString();
- string tableName = "MeterAssessment_" + deviceId;
- string selSql = "SELECT DAY(GetDateTime) AS monthDay ,max(InstantaneousFlow) AS maxFlow ,MIN(InstantaneousFlow) AS minFlow,AVG(InstantaneousFlow) AS avgFlow FROM " + tableName + " WHERE DATEDIFF(MONTH,'" + time + "',GetDateTime)=0 GROUP BY DAY(GetDateTime) ORDER BY DAY(GetDateTime)";
- DataTable dtResult;
- //结果存储 避免大量访问数据库
- if (dtSources[row] != null)
- {
- dtResult = dtSources[row];
- }
- else
- {
- dtResult = sqlHelper.ExecuteDataTable(selSql, CommandType.Text, null);
- dtSources[row] = dtResult;
- }
- DataRow[] drs = dtResult.Select("monthDay=" + (i + 1));
- if (drs.Count() > 0)
- {
- numCount++;
- sbText.Append("\"avg_" + deviceId + "\":\"").Append(drs[0]["avgFlow"]).Append("\",");
- sbText.Append("\"max_" + deviceId + "\":\"").Append(drs[0]["maxFlow"]).Append("\",");
- sbText.Append("\"min_" + deviceId + "\":\"").Append(drs[0]["minFlow"]).Append("\"");
- }
- else
- {
- sbText.Append("\"avg_" + deviceId + "\":\"").Append("--").Append("\",");
- sbText.Append("\"max_" + deviceId + "\":\"").Append("--").Append("\",");
- sbText.Append("\"min_" + deviceId + "\":\"").Append("--").Append("\"");
- }
- if (row < dt.Rows.Count - 1) sbText.Append(",");
- }
- sbText.Append("},");
- //if (i < days - 1) sbText.Append(",");
- }
- #region 平均值 最小值 最大值
- double maxAvg = 0;
- double avgAvg = 0;
- double maxMax = 0;
- double avgMax = 0;
- double maxMin = 0;
- double avgMin = 0;
- sbText.Append("{");
- sbText.Append("\"showtime\":\"").Append("平均值").Append("\",");
- //一月中的 平均值
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- DataTable tempDt = dtSources[i];
- var avg_avg = tempDt.Compute("avg(avgFlow)", "");
- var avg_max = tempDt.Compute("avg(maxFlow)", "");
- var avg_min = tempDt.Compute("avg(minFlow)", "");
- avgAvg = Convert.ToDouble(avg_avg);
- avgMax = Convert.ToDouble(avg_max);
- avgMin = Convert.ToDouble(avg_min);
- string deviceId = dt.Rows[i]["MeterAssessmentId"].ToString();
- sbText.Append("\"avg_" + deviceId + "\":\"").Append(avg_avg).Append("\",");
- sbText.Append("\"max_" + deviceId + "\":\"").Append(avg_max).Append("\",");
- sbText.Append("\"min_" + deviceId + "\":\"").Append(avg_min).Append("\"");
- if (i < dt.Rows.Count - 1) sbText.Append(",");
- }
- sbText.Append("},");
- sbText.Append("{");
- sbText.Append("\"showtime\":\"").Append("最小值").Append("\",");
- //一月中的 最小值
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- string deviceId = dt.Rows[i]["MeterAssessmentId"].ToString();
- DataTable tempDt = dtSources[i];
- tempDt.DefaultView.Sort = "avgFlow";
- tempDt.AcceptChanges();
- sbText.Append("\"avg_" + deviceId + "\":\"").Append(tempDt.Rows.Count > 0 ? tempDt.Rows[0]["avgFlow"] : "--").Append("\",");
- tempDt.DefaultView.Sort = "maxFlow";
- tempDt.AcceptChanges();
- sbText.Append("\"max_" + deviceId + "\":\"").Append(tempDt.Rows.Count > 0 ? tempDt.Rows[0]["maxFlow"] : "--").Append("\",");
- tempDt.DefaultView.Sort = "minFlow";
- tempDt.AcceptChanges();
- sbText.Append("\"min_" + deviceId + "\":\"").Append(tempDt.Rows.Count > 0 ? tempDt.Rows[0]["minFlow"] : "--").Append("\"");
- if (i < dt.Rows.Count - 1) sbText.Append(",");
- }
- sbText.Append("},");
- sbText.Append("{");
- sbText.Append("\"showtime\":\"").Append("最小值时间").Append("\",");
- //一月中的 最小值时间
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- string deviceId = dt.Rows[i]["MeterAssessmentId"].ToString();
- DataTable tempDt = dtSources[i];
- tempDt.DefaultView.Sort = "avgFlow";
- tempDt.AcceptChanges();
- sbText.Append("\"avg_" + deviceId + "\":\"").Append(tempDt.Rows.Count > 0 ? time.Year + "-" + time.Month + "-" + tempDt.Rows[0]["monthDay"] : "--").Append("\",");
- tempDt.DefaultView.Sort = "maxFlow";
- tempDt.AcceptChanges();
- sbText.Append("\"max_" + deviceId + "\":\"").Append(tempDt.Rows.Count > 0 ? time.Year + "-" + time.Month + "-" + tempDt.Rows[0]["monthDay"] : "--").Append("\",");
- tempDt.DefaultView.Sort = "minFlow";
- tempDt.AcceptChanges();
- sbText.Append("\"min_" + deviceId + "\":\"").Append(tempDt.Rows.Count > 0 ? time.Year + "-" + time.Month + "-" + tempDt.Rows[0]["monthDay"] : "--").Append("\"");
- if (i < dt.Rows.Count - 1) sbText.Append(",");
- }
- sbText.Append("},");
- sbText.Append("{");
- sbText.Append("\"showtime\":\"").Append("最大值").Append("\",");
- //一月中的 最大值
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- string deviceId = dt.Rows[i]["MeterAssessmentId"].ToString();
- DataTable tempDt = dtSources[i];
- tempDt.DefaultView.Sort = "avgFlow desc";
- tempDt.AcceptChanges();
- sbText.Append("\"avg_" + deviceId + "\":\"").Append(tempDt.Rows.Count > 0 ? tempDt.Rows[0]["avgFlow"] : "--").Append("\",");
- maxAvg = tempDt.Rows.Count > 0 ? Convert.ToDouble(tempDt.Rows[0]["avgFlow"].ToString()) : 0;
- tempDt.DefaultView.Sort = "maxFlow desc";
- tempDt.AcceptChanges();
- sbText.Append("\"max_" + deviceId + "\":\"").Append(tempDt.Rows.Count > 0 ? tempDt.Rows[0]["maxFlow"] : "--").Append("\",");
- maxMax = tempDt.Rows.Count > 0 ? Convert.ToDouble(tempDt.Rows[0]["maxFlow"].ToString()) : 0;
- tempDt.DefaultView.Sort = "minFlow desc";
- tempDt.AcceptChanges();
- sbText.Append("\"min_" + deviceId + "\":\"").Append(tempDt.Rows.Count > 0 ? tempDt.Rows[0]["minFlow"] : "--").Append("\"");
- maxMin = tempDt.Rows.Count > 0 ? Convert.ToDouble(tempDt.Rows[0]["minFlow"].ToString()) : 0;
- if (i < dt.Rows.Count - 1) sbText.Append(",");
- }
- sbText.Append("},");
- sbText.Append("{");
- sbText.Append("\"showtime\":\"").Append("最大值时间").Append("\",");
- //一月中的 最小值时间
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- string deviceId = dt.Rows[i]["MeterAssessmentId"].ToString();
- DataTable tempDt = dtSources[i];
- tempDt.DefaultView.Sort = "avgFlow desc";
- tempDt.AcceptChanges();
- sbText.Append("\"avg_" + deviceId + "\":\"").Append(tempDt.Rows.Count > 0 ? time.Year + "-" + time.Month + "-" + tempDt.Rows[0]["monthDay"] : "--").Append("\",");
- tempDt.DefaultView.Sort = "maxFlow desc";
- tempDt.AcceptChanges();
- sbText.Append("\"max_" + deviceId + "\":\"").Append(tempDt.Rows.Count > 0 ? time.Year + "-" + time.Month + "-" + tempDt.Rows[0]["monthDay"] : "--").Append("\",");
- tempDt.DefaultView.Sort = "minFlow desc";
- tempDt.AcceptChanges();
- sbText.Append("\"min_" + deviceId + "\":\"").Append(tempDt.Rows.Count > 0 ? time.Year + "-" + time.Month + "-" + tempDt.Rows[0]["monthDay"] : "--").Append("\"");
- if (i < dt.Rows.Count - 1) sbText.Append(",");
- }
- sbText.Append("},");
- sbText.Append("{");
- sbText.Append("\"showtime\":\"").Append("变化系数").Append("\",");
- //一月中的 变化系数 最大值/平均值
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- string deviceId = dt.Rows[i]["MeterAssessmentId"].ToString();
- sbText.Append("\"avg_" + deviceId + "\":\"").Append((maxAvg / avgAvg).ToString("0.00")).Append("\",");
- sbText.Append("\"max_" + deviceId + "\":\"").Append((maxMax / avgMax).ToString("0.00")).Append("\",");
- sbText.Append("\"min_" + deviceId + "\":\"").Append((maxMin / avgMin).ToString("0.00")).Append("\"");
- if (i < dt.Rows.Count - 1) sbText.Append(",");
- }
- sbText.Append("}");
- #endregion
- return sbText;
- }
- /// <summary>
- /// 压力年报表获取
- /// </summary>
- /// <param name="dt"></param>
- /// <param name="time"></param>
- /// <returns></returns>
- public StringBuilder GetYearReportData(DataTable dt, DateTime time)
- {
- StringBuilder sbText = new StringBuilder();
- DataTable[] dtSources = new DataTable[dt.Rows.Count];
- int numCount = 0;
- for (int i = 0; i < 12; i++)
- {
- sbText.Append("{");
- sbText.Append("\"showtime\":\"").Append(time.AddMonths(i).ToString("yyyy-MM")).Append("\",");
- for (int row = 0; row < dt.Rows.Count; row++)
- {
- string deviceId = dt.Rows[row]["MeterAssessmentId"].ToString();
- string tableName = "MeterAssessment_" + deviceId;
- string selSql = "SELECT MONTH(GetDateTime) AS monthDay ,max(InstantaneousFlow) AS maxFlow ,MIN(InstantaneousFlow) AS minFlow,AVG(InstantaneousFlow) AS avgFlow FROM " + tableName + " WHERE DATEDIFF(YEAR,'" + time + "',GetDateTime)=0 GROUP BY MONTH(GetDateTime) ORDER BY MONTH(GetDateTime)";
- DataTable dtResult;
- //结果存储 避免大量访问数据库
- if (dtSources[row] != null)
- {
- dtResult = dtSources[row];
- }
- else
- {
- dtResult = sqlHelper.ExecuteDataTable(selSql, CommandType.Text, null);
- dtSources[row] = dtResult;
- }
- DataRow[] drs = dtResult.Select("monthDay=" + (i + 1));
- if (drs.Count() > 0)
- {
- numCount++;
- sbText.Append("\"avg_" + deviceId + "\":\"").Append(drs[0]["avgFlow"]).Append("\",");
- sbText.Append("\"max_" + deviceId + "\":\"").Append(drs[0]["maxFlow"]).Append("\",");
- sbText.Append("\"min_" + deviceId + "\":\"").Append(drs[0]["maxFlow"]).Append("\"");
- }
- else
- {
- sbText.Append("\"avg_" + deviceId + "\":\"").Append("--").Append("\",");
- sbText.Append("\"max_" + deviceId + "\":\"").Append("--").Append("\",");
- sbText.Append("\"min_" + deviceId + "\":\"").Append("--").Append("\"");
- }
- if (row < dt.Rows.Count - 1) sbText.Append(",");
- }
- sbText.Append("},");
- //if (i < days - 1) sbText.Append(",");
- }
- #region 平均值 最小值 最大值
- double maxAvg = 0;
- double avgAvg = 0;
- double maxMax = 0;
- double avgMax = 0;
- double maxMin = 0;
- double avgMin = 0;
- sbText.Append("{");
- sbText.Append("\"showtime\":\"").Append("平均值").Append("\",");
- //一月中的 平均值
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- DataTable tempDt = dtSources[i];
- var avg_avg = tempDt.Compute("avg(avgFlow)", "");
- var avg_max = tempDt.Compute("avg(maxFlow)", "");
- var avg_min = tempDt.Compute("avg(maxFlow)", "");
- avgAvg = Convert.ToDouble(avg_avg);
- avgMax = Convert.ToDouble(avg_max);
- avgMin = Convert.ToDouble(avg_min);
- string deviceId = dt.Rows[i]["MeterAssessmentId"].ToString();
- sbText.Append("\"avg_" + deviceId + "\":\"").Append(avg_avg).Append("\",");
- sbText.Append("\"max_" + deviceId + "\":\"").Append(avg_max).Append("\",");
- sbText.Append("\"min_" + deviceId + "\":\"").Append(avg_min).Append("\"");
- if (i < dt.Rows.Count - 1) sbText.Append(",");
- }
- sbText.Append("},");
- sbText.Append("{");
- sbText.Append("\"showtime\":\"").Append("最小值").Append("\",");
- //一月中的 最小值
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- string deviceId = dt.Rows[i]["MeterAssessmentId"].ToString();
- DataTable tempDt = dtSources[i];
- tempDt.DefaultView.Sort = "avgFlow";
- tempDt.AcceptChanges();
- sbText.Append("\"avg_" + deviceId + "\":\"").Append(tempDt.Rows.Count > 0 ? tempDt.Rows[0]["avgFlow"] : "--").Append("\",");
- tempDt.DefaultView.Sort = "maxFlow";
- tempDt.AcceptChanges();
- sbText.Append("\"max_" + deviceId + "\":\"").Append(tempDt.Rows.Count > 0 ? tempDt.Rows[0]["maxFlow"] : "--").Append("\",");
- tempDt.DefaultView.Sort = "minFlow";
- tempDt.AcceptChanges();
- sbText.Append("\"min_" + deviceId + "\":\"").Append(tempDt.Rows.Count > 0 ? tempDt.Rows[0]["minFlow"] : "--").Append("\"");
- if (i < dt.Rows.Count - 1) sbText.Append(",");
- }
- sbText.Append("},");
- sbText.Append("{");
- sbText.Append("\"showtime\":\"").Append("最小值时间").Append("\",");
- //一月中的 最小值时间
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- string deviceId = dt.Rows[i]["MeterAssessmentId"].ToString();
- DataTable tempDt = dtSources[i];
- tempDt.DefaultView.Sort = "avgFlow";
- tempDt.AcceptChanges();
- sbText.Append("\"avg_" + deviceId + "\":\"").Append(tempDt.Rows.Count > 0 ? time.Year + "-" + tempDt.Rows[0]["monthDay"] : "--").Append("\",");
- tempDt.DefaultView.Sort = "maxFlow";
- tempDt.AcceptChanges();
- sbText.Append("\"max_" + deviceId + "\":\"").Append(tempDt.Rows.Count > 0 ? time.Year + "-" + tempDt.Rows[0]["monthDay"] : "--").Append("\",");
- tempDt.DefaultView.Sort = "minFlow";
- tempDt.AcceptChanges();
- sbText.Append("\"min_" + deviceId + "\":\"").Append(tempDt.Rows.Count > 0 ? time.Year + "-" + tempDt.Rows[0]["monthDay"] : "--").Append("\"");
- if (i < dt.Rows.Count - 1) sbText.Append(",");
- }
- sbText.Append("},");
- sbText.Append("{");
- sbText.Append("\"showtime\":\"").Append("最大值").Append("\",");
- //一月中的 最大值
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- string deviceId = dt.Rows[i]["MeterAssessmentId"].ToString();
- DataTable tempDt = dtSources[i];
- tempDt.DefaultView.Sort = "avgFlow desc";
- tempDt.AcceptChanges();
- sbText.Append("\"avg_" + deviceId + "\":\"").Append(tempDt.Rows.Count > 0 ? tempDt.Rows[0]["avgFlow"] : "--").Append("\",");
- maxAvg = tempDt.Rows.Count > 0 ? Convert.ToDouble(tempDt.Rows[0]["avgFlow"].ToString()) : 0;
- tempDt.DefaultView.Sort = "maxFlow desc";
- tempDt.AcceptChanges();
- sbText.Append("\"max_" + deviceId + "\":\"").Append(tempDt.Rows.Count > 0 ? tempDt.Rows[0]["maxFlow"] : "--").Append("\",");
- maxMax = tempDt.Rows.Count > 0 ? Convert.ToDouble(tempDt.Rows[0]["maxFlow"].ToString()) : 0;
- tempDt.DefaultView.Sort = "minFlow desc";
- tempDt.AcceptChanges();
- sbText.Append("\"min_" + deviceId + "\":\"").Append(tempDt.Rows.Count > 0 ? tempDt.Rows[0]["maxFlow"] : "--").Append("\"");
- maxMin = tempDt.Rows.Count > 0 ? Convert.ToDouble(tempDt.Rows[0]["maxFlow"].ToString()) : 0;
- if (i < dt.Rows.Count - 1) sbText.Append(",");
- }
- sbText.Append("},");
- sbText.Append("{");
- sbText.Append("\"showtime\":\"").Append("最大值时间").Append("\",");
- //一月中的 最小值时间
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- string deviceId = dt.Rows[i]["MeterAssessmentId"].ToString();
- DataTable tempDt = dtSources[i];
- tempDt.DefaultView.Sort = "avgFlow desc";
- tempDt.AcceptChanges();
- sbText.Append("\"avg_" + deviceId + "\":\"").Append(tempDt.Rows.Count > 0 ? time.Year + "-" + tempDt.Rows[0]["monthDay"] : "--").Append("\",");
- tempDt.DefaultView.Sort = "maxFlow desc";
- tempDt.AcceptChanges();
- sbText.Append("\"max_" + deviceId + "\":\"").Append(tempDt.Rows.Count > 0 ? time.Year + "-" + tempDt.Rows[0]["monthDay"] : "--").Append("\",");
- tempDt.DefaultView.Sort = "minFlow desc";
- tempDt.AcceptChanges();
- sbText.Append("\"min_" + deviceId + "\":\"").Append(tempDt.Rows.Count > 0 ? time.Year + "-" + tempDt.Rows[0]["monthDay"] : "--").Append("\"");
- if (i < dt.Rows.Count - 1) sbText.Append(",");
- }
- sbText.Append("},");
- sbText.Append("{");
- sbText.Append("\"showtime\":\"").Append("变化系数").Append("\",");
- //一月中的 变化系数 最大值/平均值
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- string deviceId = dt.Rows[i]["MeterAssessmentId"].ToString();
- sbText.Append("\"avg_" + deviceId + "\":\"").Append((maxAvg / avgAvg).ToString("0.00")).Append("\",");
- sbText.Append("\"max_" + deviceId + "\":\"").Append((maxMax / avgMax).ToString("0.00")).Append("\",");
- sbText.Append("\"min_" + deviceId + "\":\"").Append((maxMin / avgMin).ToString("0.00")).Append("\"");
- if (i < dt.Rows.Count - 1) sbText.Append(",");
- }
- sbText.Append("}");
- #endregion
- return sbText;
- }
- }
- }
|