123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638 |
- using LeaRun.Application.IService.PipeNetworkManage;
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using LeaRun.Data;
- using LeaRun.Util;
- using LeaRun.Util.WebControl;
- namespace LeaRun.Application.Service.PipeNetworkManage
- {
- public class AnalysisService : IAnalysisService
- {
- SqlHelper sqlHelper = new SqlHelper("PipeNetworkDb");
- /// <summary>
- /// 年度用水分析
- /// </summary>
- /// <param name="departId"></param>
- /// <param name="typeID"></param>
- /// <param name="devId"></param>
- /// <param name="time"></param>
- /// <returns></returns>
- public string GetYearAnalysis(string devId, string time, Pagination pagination)
- {
- string strSql = "Select MeterAssessmentId,MeterAssessmentName From MeterAssessmentBase Where MeterAssessmentId in (" + devId + ")";
- DateTime dateTime = DateTime.Parse(time + "-01-01");
- int months = 12;
- int total = pagination.records;
- //DataTable dt = sqlHelper.ExecuteDataTable(strSql, CommandType.Text, null);
- DataTable dt = sqlHelper.ExecuteDataTable(strSql, "", false, pagination.rows, pagination.page, out total, null);
- pagination.records = total;
- StringBuilder builder = new StringBuilder();
- builder.Append("[");
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- string deviceId = dt.Rows[i]["MeterAssessmentId"].ToString();
- string name = dt.Rows[i]["MeterAssessmentName"].ToString();
- builder.Append("{");
- builder.Append("\"ID\":\"").Append(deviceId).Append("\",");
- builder.Append("\"DevName\":\"").Append(name).Append("\",");
- string strTableName = "MeterAssessment_" + deviceId;
- if (sqlHelper.DBTableIsExists(strTableName))
- {
- DateTime startDay = dateTime;
- for (int j = 0; j < months; j++)
- {
- DateTime nowDay = startDay.AddMonths(j);
- DateTime endDay = nowDay.AddMonths(1);
- strSql = "Select GetDateTime,NetCumulativeFlow From " + strTableName + " Where NetCumulativeFlow<>0 And NetCumulativeFlow is not null And GetDateTime between '" + nowDay.ToString("yyyy-MM-dd") + "' And '" + endDay.ToString("yyyy-MM-dd") + "'";
- DataTable table = sqlHelper.ExecuteDataTable(strSql, CommandType.Text, null);
- if (table.Rows.Count > 0)
- {
- decimal min = decimal.Parse(table.Rows[0]["NetCumulativeFlow"].ToString());
- decimal max = decimal.Parse(table.Rows[table.Rows.Count - 1]["NetCumulativeFlow"].ToString());
- builder.Append("\"Month" + (j + 1) + "\":\"").Append((max - min).ToString()).Append("\"");
- }
- else
- {
- builder.Append("\"Month" + (j + 1) + "\":\"").Append("--\"");
- }
- if (j < months - 1)
- builder.Append(",");
- }
- }
- else
- {
- string strDay = time + "-01";
- for (int j = 0; j < months; j++)
- {
- builder.Append("\"Month" + (j + 1) + "\":\"").Append("--\"");
- if (j < months - 1)
- builder.Append(",");
- }
- }
- builder.Append("}");
- if (i < dt.Rows.Count - 1)
- builder.Append(",");
- }
- builder.Append("]");
- return builder.ToString();
- }
- /// <summary>
- /// 月度用水分析
- /// </summary>
- /// <param name="departId"></param>
- /// <param name="typeID"></param>
- /// <param name="devId"></param>
- /// <param name="time"></param>
- /// <returns></returns>
- public string GetMonthAnalysis(string devId, string time, Pagination pagination)
- {
- string strSql = "";
- strSql = "Select MeterAssessmentId,MeterAssessmentName From MeterAssessmentBase Where MeterAssessmentId in (" + devId + ")";
- DateTime dateTime = DateTime.Parse(time);
- int days = LeaRun.Util.Time.GetDaysOfMonth(dateTime.Year, dateTime.Month);
- int total = pagination.records;
- DataTable dt = sqlHelper.ExecuteDataTable(strSql, "", false, pagination.rows, pagination.page, out total, null);
- pagination.records = total;
- StringBuilder builder = new StringBuilder();
- builder.Append("[");
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- string deviceId = dt.Rows[i]["MeterAssessmentId"].ToString();
- string name = dt.Rows[i]["MeterAssessmentName"].ToString();
- builder.Append("{");
- builder.Append("\"ID\":\"").Append(deviceId).Append("\",");
- builder.Append("\"DevName\":\"").Append(name).Append("\",");
- string strTableName = "MeterAssessment_" + deviceId;
- if (sqlHelper.DBTableIsExists(strTableName))
- {
- string strDay = time + "-01";
- DateTime startDay = DateTime.Parse(strDay);
- for (int j = 0; j < days; j++)
- {
- DateTime nowDay = startDay.AddDays(j);
- DateTime endDay = nowDay.AddDays(1);
- strSql = "Select GetDateTime, NetCumulativeFlow From " + strTableName + " Where NetCumulativeFlow<>0 And NetCumulativeFlow is not null And GetDateTime between '" + nowDay.ToString("yyyy-MM-dd") + "' And '" + endDay.ToString("yyyy-MM-dd") + "'";
- DataTable table = sqlHelper.ExecuteDataTable(strSql, CommandType.Text, null);
- if (table.Rows.Count > 0)
- {
- decimal min = decimal.Parse(table.Rows[0]["NetCumulativeFlow"].ToString());
- decimal max = decimal.Parse(table.Rows[table.Rows.Count - 1]["NetCumulativeFlow"].ToString());
- builder.Append("\"Day" + (j + 1) + "\":\"").Append((max - min).ToString("0.00")).Append("\"");
- }
- else
- {
- builder.Append("\"Day" + (j + 1) + "\":\"").Append("--\"");
- }
- if (j < days - 1)
- builder.Append(",");
- }
- }
- else
- {
- string strDay = time + "-01";
- for (int j = 0; j < days; j++)
- {
- builder.Append("\"Day" + (j + 1) + "\":\"").Append("--\"");
- if (j < days - 1)
- builder.Append(",");
- }
- }
- builder.Append("}");
- if (i < dt.Rows.Count - 1)
- builder.Append(",");
- }
- builder.Append("]");
- return builder.ToString();
- }
- /// <summary>
- /// 每小时用水量
- /// </summary>
- /// <param name="deviceId">测点ID</param>
- /// <param name="time">时间</param>
- /// <returns></returns>
- public string GetHourFlow(string deviceId, string time)
- {
- var json = "[]";
- var strSql = "Select MeterAssessmentId,MeterAssessmentName From MeterAssessmentBase Where MeterAssessmentId=" + deviceId;
- DataTable dt = sqlHelper.ExecuteDataTable(strSql, CommandType.Text, null);
- if (dt.Rows.Count > 0)
- {
- string devName = dt.Rows[0]["MeterAssessmentName"].ToString();
- DateTime dateTime = DateTime.Parse(time);
- StringBuilder builder = new StringBuilder();
- builder.Append("[");
- string strTableName = "MeterAssessment_" + deviceId;
- if (sqlHelper.DBTableIsExists(strTableName))
- {
- for (int i = 0; i < 24; i++)
- {
- DateTime nowDay = dateTime.AddHours(i);
- DateTime endDay = nowDay.AddHours(1);
- strSql = "Select NetCumulativeFlow From " + strTableName + " Where NetCumulativeFlow<>0 And NetCumulativeFlow is not null And GetDateTime between '" + nowDay.ToString("yyyy-MM-dd HH:mm:ss") + "' And '" + endDay.ToString("yyyy-MM-dd HH:mm:ss") + "' Order by GetDateTime";
- DataTable table = sqlHelper.ExecuteDataTable(strSql, CommandType.Text, null);
- builder.Append("{");
- if (table.Rows.Count > 0)
- {
- decimal min = decimal.Parse(table.Rows[0]["NetCumulativeFlow"].ToString());
- decimal max = decimal.Parse(table.Rows[table.Rows.Count - 1]["NetCumulativeFlow"].ToString());
- builder.Append("\"DevName\":\"").Append(devName).Append("\",");
- builder.Append("\"HourNetFlow\":\"").Append((max - min).ToString()).Append("\",");
- builder.Append("\"LedgerTime\":\"").Append(i + 1).Append("\"");
- }
- else
- {
- builder.Append("\"HourNetFlow\":\"0\",");
- builder.Append("\"LedgerTime\":\"").Append(i + 1).Append("\"");
- }
- builder.Append("}");
- if (i < 23)
- builder.Append(",");
- }
- }
- builder.Append("]");
- json = builder.ToString();
- }
- return json;
- }
- ///// <summary>
- ///// 前三天平均用水量
- ///// </summary>
- ///// <param name="devId"></param>
- ///// <param name="time"></param>
- ///// <returns></returns>
- public string GetHourAvgFlow(string deviceId, string time)
- {
- var json = "[]";
- var strSql = "Select MeterAssessmentId,MeterAssessmentName From MeterAssessmentBase Where MeterAssessmentId=" + deviceId;
- DataTable dt = sqlHelper.ExecuteDataTable(strSql, CommandType.Text, null);
- if (dt.Rows.Count > 0)
- {
- DateTime dateTime = DateTime.Parse(time);
- StringBuilder builder = new StringBuilder();
- builder.Append("[");
- string strTableName = "MeterAssessment_" + deviceId;
- if (sqlHelper.DBTableIsExists(strTableName))
- {
- DateTime yesterDay = dateTime.AddDays(-1);//昨天
- DateTime twoDaysBefore = dateTime.AddDays(-2);//两天前
- DateTime threeDaysBefore = dateTime.AddDays(-3);//三天前
- decimal yesterDayUsed = 0, twoDaysBeforeUsed = 0, threeDaysBeforeUsed = 0;
- for (int i = 0; i < 24; i++)
- {
- DateTime yesterDayHour = yesterDay.AddHours(i);
- DateTime yesterDayEndHour = yesterDayHour.AddHours(1);
- DateTime twoDaysBeforeHour = twoDaysBefore.AddHours(i);
- DateTime twoDaysBeforeEndHour = twoDaysBeforeHour.AddHours(1);
- DateTime threeDaysBeforeHour = threeDaysBefore.AddHours(i);
- DateTime threeDaysBeforeEndHour = threeDaysBeforeHour.AddHours(1);
- #region 昨天小时流量
- strSql = "Select NetCumulativeFlow From " + strTableName + " Where NetCumulativeFlow<>0 And NetCumulativeFlow is not null And GetDateTime between '" + yesterDayHour.ToString("yyyy-MM-dd HH:mm:ss") + "' And '" + yesterDayEndHour.ToString("yyyy-MM-dd HH:mm:ss") + "' Order by GetDateTime";
- DataTable tableYes = sqlHelper.ExecuteDataTable(strSql, CommandType.Text, null);
- if (tableYes.Rows.Count > 0)
- {
- decimal min = decimal.Parse(tableYes.Rows[0]["NetCumulativeFlow"].ToString());
- decimal max = decimal.Parse(tableYes.Rows[tableYes.Rows.Count - 1]["NetCumulativeFlow"].ToString());
- yesterDayUsed = max - min;
- }
- else
- {
- yesterDayUsed = 0;
- }
- #endregion
- #region 两天前小时流量
- strSql = "Select NetCumulativeFlow From " + strTableName + " Where NetCumulativeFlow<>0 And NetCumulativeFlow is not null And GetDateTime between '" + twoDaysBeforeHour.ToString("yyyy-MM-dd HH:mm:ss") + "' And '" + twoDaysBeforeEndHour.ToString("yyyy-MM-dd HH:mm:ss") + "' Order by GetDateTime";
- tableYes = sqlHelper.ExecuteDataTable(strSql, CommandType.Text, null);
- if (tableYes.Rows.Count > 0)
- {
- decimal min = decimal.Parse(tableYes.Rows[0]["NetCumulativeFlow"].ToString());
- decimal max = decimal.Parse(tableYes.Rows[tableYes.Rows.Count - 1]["NetCumulativeFlow"].ToString());
- twoDaysBeforeUsed = max - min;
- }
- else
- {
- twoDaysBeforeUsed = 0;
- }
- #endregion
- #region 三天前小时流量
- strSql = "Select NetCumulativeFlow From " + strTableName + " Where NetCumulativeFlow<>0 And NetCumulativeFlow is not null And GetDateTime between '" + threeDaysBeforeHour.ToString("yyyy-MM-dd HH:mm:ss") + "' And '" + threeDaysBeforeEndHour.ToString("yyyy-MM-dd HH:mm:ss") + "' Order by GetDateTime";
- tableYes = sqlHelper.ExecuteDataTable(strSql, CommandType.Text, null);
- if (tableYes.Rows.Count > 0)
- {
- decimal min = decimal.Parse(tableYes.Rows[0]["NetCumulativeFlow"].ToString());
- decimal max = decimal.Parse(tableYes.Rows[tableYes.Rows.Count - 1]["NetCumulativeFlow"].ToString());
- threeDaysBeforeUsed = max - min;
- }
- else
- {
- threeDaysBeforeUsed = 0;
- }
- #endregion
- decimal avgFlow = (yesterDayUsed + twoDaysBeforeUsed + threeDaysBeforeUsed) / 3;
- builder.Append(avgFlow);
- if (i < 23)
- builder.Append(",");
- }
- }
- builder.Append("]");
- json = builder.ToString();
- }
- return json;
- }
- /// <summary>
- /// 每日用水量
- /// </summary>
- /// <param name="deviceId"></param>
- /// <param name="time"></param>
- /// <returns></returns>
- public string GetDayFlow(string deviceId, string time)
- {
- var json = "[]";
- var strSql = "Select MeterAssessmentId,MeterAssessmentName From MeterAssessmentBase Where MeterAssessmentId=" + deviceId;
- DataTable dt = sqlHelper.ExecuteDataTable(strSql, CommandType.Text, null);
- if (dt.Rows.Count > 0)
- {
- string devName = dt.Rows[0]["MeterAssessmentName"].ToString();
- DateTime dateTime = DateTime.Parse(time + "-01");
- int days = Time.GetDaysOfMonth(dateTime);
- StringBuilder builder = new StringBuilder();
- builder.Append("[");
- string strTableName = "MeterAssessment_" + deviceId;
- if (sqlHelper.DBTableIsExists(strTableName))
- {
- for (int i = 0; i < days; i++)
- {
- DateTime nowDay = dateTime.AddDays(i);
- DateTime endDay = nowDay.AddDays(1);
- strSql = "Select NetCumulativeFlow From " + strTableName + " Where NetCumulativeFlow<>0 And NetCumulativeFlow is not null And GetDateTime between '" + nowDay.ToString("yyyy-MM-dd HH:mm:ss") + "' And '" + endDay.ToString("yyyy-MM-dd HH:mm:ss") + "' Order by GetDateTime";
- DataTable table = sqlHelper.ExecuteDataTable(strSql, CommandType.Text, null);
- builder.Append("{");
- builder.Append("\"DevName\":\"").Append(devName).Append("\",");
- if (table.Rows.Count > 0)
- {
- decimal min = decimal.Parse(table.Rows[0]["NetCumulativeFlow"].ToString());
- decimal max = decimal.Parse(table.Rows[table.Rows.Count - 1]["NetCumulativeFlow"].ToString());
- builder.Append("\"HourNetFlow\":\"").Append((max - min).ToString()).Append("\",");
- builder.Append("\"LedgerTime\":\"").Append(i + 1).Append("\",");
- }
- else
- {
- builder.Append("\"HourNetFlow\":\"0\",");
- builder.Append("\"LedgerTime\":\"").Append(i + 1).Append("\",");
- }
- strSql = "Select min(Pressure) as 最小压力,max(Pressure) as 最大压力,avg(Pressure) as 平均压力 From " + strTableName + " Where Pressure is not null And GetDateTime between '" + nowDay.ToString("yyyy-MM-dd HH:mm:ss") + "' And '" + endDay.ToString("yyyy-MM-dd HH:mm:ss") + "'";
- DataTable dtP = sqlHelper.ExecuteDataTable(strSql, CommandType.Text, null);
- if (dtP.Rows.Count > 0)
- {
- builder.Append("\"minP\":\"").Append(dtP.Rows[0]["最小压力"].ToString()).Append("\",");
- builder.Append("\"maxP\":\"").Append(dtP.Rows[0]["最大压力"].ToString()).Append("\",");
- builder.Append("\"avgP\":\"").Append(dtP.Rows[0]["平均压力"].ToString()).Append("\"");
- }
- else
- {
- builder.Append("\"minP\":\"0\",");
- builder.Append("\"maxP\":\"0\",");
- builder.Append("\"avgP\":\"0\"");
- }
- builder.Append("}");
- if (i < days - 1)
- builder.Append(",");
- }
- }
- builder.Append("]");
- json = builder.ToString();
- }
- return json;
- }
- /// <summary>
- /// 每月用水量
- /// </summary>
- /// <param name="deviceId"></param>
- /// <param name="time"></param>
- /// <returns></returns>
- public string GetMonthFlow(string deviceId, string time)
- {
- var json = "[]";
- var strSql = "Select MeterAssessmentId,MeterAssessmentName From MeterAssessmentBase Where MeterAssessmentId=" + deviceId;
- DataTable dt = sqlHelper.ExecuteDataTable(strSql, CommandType.Text, null);
- if (dt.Rows.Count > 0)
- {
- string devName = dt.Rows[0]["MeterAssessmentName"].ToString();
- DateTime dateTime = DateTime.Parse(time + "-01-01");
- StringBuilder builder = new StringBuilder();
- builder.Append("[");
- string strTableName = "MeterAssessment_" + deviceId;
- if (sqlHelper.DBTableIsExists(strTableName))
- {
- for (int i = 0; i < 12; i++)
- {
- DateTime nowDay = dateTime.AddMonths(i);
- DateTime endDay = nowDay.AddMonths(1);
- strSql = "Select NetCumulativeFlow From " + strTableName + " Where NetCumulativeFlow<>0 And NetCumulativeFlow is not null And GetDateTime between '" + nowDay.ToString("yyyy-MM-dd HH:mm:ss") + "' And '" + endDay.ToString("yyyy-MM-dd HH:mm:ss") + "' Order by GetDateTime";
- DataTable table = sqlHelper.ExecuteDataTable(strSql, CommandType.Text, null);
- builder.Append("{");
- if (table.Rows.Count > 0)
- {
- decimal min = decimal.Parse(table.Rows[0]["NetCumulativeFlow"].ToString());
- decimal max = decimal.Parse(table.Rows[table.Rows.Count - 1]["NetCumulativeFlow"].ToString());
- builder.Append("\"DevName\":\"").Append(devName).Append("\",");
- builder.Append("\"HourNetFlow\":\"").Append((max - min).ToString()).Append("\",");
- builder.Append("\"LedgerTime\":\"").Append(i + 1).Append("\"");
- }
- else
- {
- builder.Append("\"HourNetFlow\":\"0\",");
- builder.Append("\"LedgerTime\":\"").Append(i + 1).Append("\"");
- }
- builder.Append("}");
- if (i < 11)
- builder.Append(",");
- }
- }
- builder.Append("]");
- json = builder.ToString();
- }
- return json;
- }
- /// <summary>
- /// 瞬时流量
- /// </summary>
- /// <param name="deviceId"></param>
- /// <param name="start"></param>
- /// <param name="end"></param>
- /// <returns></returns>
- public string GetFlowRate(string deviceId, string start, string end)
- {
- var json = "[]";
- var sql = "Select MeterAssessmentName From MeterAssessmentBase Where MeterAssessmentId=" + deviceId;
- DataTable dtDev = sqlHelper.ExecuteDataTable(sql, CommandType.Text, null);
- string devName = "";
- if (dtDev.Rows.Count > 0)
- {
- devName = dtDev.Rows[0]["MeterAssessmentName"].ToString();
- }
- DateTime dateTime = DateTime.Parse(start);
- string strTableName = "MeterAssessment_" + deviceId;
- if (sqlHelper.DBTableIsExists(strTableName))
- {
- var strSql = "Select InstantaneousFlow,Pressure,GetDateTime From " + strTableName + " Where InstantaneousFlow is not null And GetDateTime between '" + start + "' and '" + end + "' Order by GetDateTime";
- DataTable dt = sqlHelper.ExecuteDataTable(strSql, CommandType.Text, null);
- StringBuilder builder = new StringBuilder();
- builder.Append("[");
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- builder.Append("{");
- builder.Append("\"FlowRate\":\"").Append(dt.Rows[i]["InstantaneousFlow"].ToString()).Append("\",");
- builder.Append("\"Pressure\":\"").Append(string.IsNullOrEmpty(dt.Rows[i]["Pressure"].ToString()) == true ? "-" : dt.Rows[i]["Pressure"].ToString()).Append("\",");
- DateTime dtN = DateTime.Parse(dt.Rows[i]["GetDateTime"].ToString());
- builder.Append("\"FlowTime\":\"").Append(dtN.ToString("yyyy-MM-dd HH:mm:ss")).Append("\"");
- builder.Append("}");
- if (i < dt.Rows.Count - 1)
- builder.Append(",");
- }
- builder.Append("]");
- json = builder.ToString();
- json = "{\"devName\":\"" + devName + "\",\"devData\":" + json + ",";
- strSql = "Select avg(InstantaneousFlow) as 平均流量,MIN(InstantaneousFlow) as 最小流量 From " + strTableName + " Where InstantaneousFlow is not null And GetDateTime between '" + start + "' and '" + end + "'";
- DataTable dtAvg = sqlHelper.ExecuteDataTable(strSql, CommandType.Text, null);
- if (dtAvg.Rows.Count > 0)
- {
- //decimal avgFlow=decimal
- string strAvg = dtAvg.Rows[0]["平均流量"].ToString();
- #region
- if (!string.IsNullOrEmpty(strAvg))
- {
- decimal avgFlow = decimal.Parse(dtAvg.Rows[0]["平均流量"].ToString());
- decimal minFlow = decimal.Parse(dtAvg.Rows[0]["最小流量"].ToString());
- DateTime dtStart = DateTime.Parse(start);
- DateTime dtEnd = DateTime.Parse(end);
- List<decimal> totalMinFlow = new List<decimal>();
- int days = (dtEnd - dtStart).Days;
- for (int i = 0; i < days; i++)
- {
- DateTime dtTomorrow = dtStart.AddDays(1);
- strSql = "Select avg(InstantaneousFlow) as 平均流量,MIN(InstantaneousFlow) as 最小流量 From " + strTableName + " Where InstantaneousFlow is not null And GetDateTime between '" + dtStart.ToString("yyyy-MM-dd 00:00") + "' and '" + dtTomorrow.ToString("yyyy-MM-dd 00:00") + "'";
- dtStart = dtStart.AddDays(1);
- DataTable dtMin = sqlHelper.ExecuteDataTable(strSql, CommandType.Text, null);
- if (dtMin.Rows.Count > 0)
- {
- if (!string.IsNullOrEmpty(dtMin.Rows[0]["最小流量"].ToString()))
- {
- totalMinFlow.Add(decimal.Parse(dtMin.Rows[0]["最小流量"].ToString()));
- }
- }
- }
- if (totalMinFlow.Count > 0)
- {
- decimal totalM = 0;
- foreach (var item in totalMinFlow)
- {
- totalM += item;
- }
- minFlow = Math.Round(totalM / totalMinFlow.Count, 3);
- }
- //疑似漏水量,疑似漏失率,夜间允许漏水量,期间供水量,期间售水量
- decimal suspectedLeakFlow, suspectedLeakRate, nightAllowLeakFlow, waterSupply, salesWater;
- suspectedLeakFlow = minFlow - avgFlow / 5;
- if (avgFlow != 0)
- {
- suspectedLeakRate = suspectedLeakFlow / avgFlow * 100;
- }
- else
- {
- suspectedLeakRate = 0;
- }
- nightAllowLeakFlow = minFlow - suspectedLeakFlow;
- waterSupply = avgFlow * 24;
- salesWater = waterSupply - suspectedLeakFlow * 24;
- json += "\"avgFlow\":" + dtAvg.Rows[0]["平均流量"].ToString() + ",\"minFlow\":" + dtAvg.Rows[0]["最小流量"].ToString() + ",";
- json += "\"suspectedLeakFlow\":" + Math.Round(suspectedLeakFlow, 3) + ",";
- json += "\"suspectedLeakRate\":" + Math.Round(suspectedLeakRate, 3) + ",";
- json += "\"nightAllowLeakFlow\":" + Math.Round(nightAllowLeakFlow, 3) + ",";
- json += "\"waterSupply\":" + Math.Round(waterSupply, 3) + ",";
- json += "\"salesWater\":" + Math.Round(salesWater, 3) + "}";
- }
- else
- {
- json += "\"avgFlow\":\"--\",\"minFlow\":\"--\",";
- json += "\"suspectedLeakFlow\":\"--\",";
- json += "\"suspectedLeakRate\":\"--\",";
- json += "\"nightAllowLeakFlow\":\"--\",";
- json += "\"waterSupply\":\"--\",";
- json += "\"salesWater\":\"--\"}";
- }
- #endregion
- }
- else
- {
- json += "\"avgFlow\":\"--\",\"minFlow\":\"--\",";
- json += "\"suspectedLeakFlow\":\"--\",";
- json += "\"suspectedLeakRate\":\"--\",";
- json += "\"nightAllowLeakFlow\":\"--\",";
- json += "\"waterSupply\":\"--\",";
- json += "\"salesWater\":\"--\"}";
- }
- }
- return json;
- }
- /// <summary>
- /// 最小流量分析
- /// </summary>
- /// <param name="deviceId"></param>
- /// <param name="start"></param>
- /// <param name="end"></param>
- /// <returns></returns>
- public string GetMinFlow(string deviceId, string start, string end)
- {
- var json = "{\"devName\":\"\",\"devData\":[]";
- var sql = "Select MeterAssessmentName From MeterAssessmentBase Where MeterAssessmentId=" + deviceId;
- DataTable dtDev = sqlHelper.ExecuteDataTable(sql, CommandType.Text, null);
- string devName = "";
- if (dtDev.Rows.Count > 0)
- {
- devName = dtDev.Rows[0]["MeterAssessmentName"].ToString();
- }
- DateTime dateTime = DateTime.Parse(start);
- string strTableName = "MeterAssessment_" + deviceId;
- DateTime dtStart = DateTime.Parse(start);
- DateTime dtEnd = DateTime.Parse(end);
- int days = (dtEnd - dtStart).Days;
- string strSql = "";
- StringBuilder builder = new StringBuilder();
- builder.Append("{\"devName\":\"").Append(devName);
- builder.Append("\",\"devData\":");
- builder.Append("[");
- for (int i = 0; i < days; i++)
- {
- DateTime dtTomorrow = dtStart.AddDays(1);
- strSql = "Select avg(InstantaneousFlow) as 平均流量,MIN(InstantaneousFlow) as 最小流量 From " + strTableName + " Where InstantaneousFlow is not null And GetDateTime between '" + dtStart.ToString("yyyy-MM-dd 00:00") + "' and '" + dtTomorrow.ToString("yyyy-MM-dd 00:00") + "'";
- DataTable dtMin = sqlHelper.ExecuteDataTable(strSql, CommandType.Text, null);
- if (dtMin.Rows.Count > 0)
- {
- builder.Append("{");
- builder.Append("\"FlowRate\":\"").Append(dtMin.Rows[0]["最小流量"].ToString()).Append("\",");
- //计算前三天的平均瞬时流量
- if (i >= 3)
- {
- strSql = "Select Avg(InstantaneousFlow) as 平均流量 From " + strTableName + " Where InstantaneousFlow is not null And GetDateTime between '" + dtStart.AddDays(-3).ToString("yyyy-MM-dd 00:00") + "' and '" + dtStart.ToString("yyyy-MM-dd 00:00") + "'";
- DataTable dtAvg = sqlHelper.ExecuteDataTable(strSql, CommandType.Text, null);
- if (dtAvg.Rows.Count > 0)
- {
- builder.Append("\"AvgFlow\":\"").Append(dtAvg.Rows[0]["平均流量"].ToString()).Append("\",");
- }
- else
- {
- builder.Append("\"AvgFlow\":\"-\",");
- }
- }
- else
- {
- builder.Append("\"AvgFlow\":\"").Append(dtMin.Rows[0]["平均流量"].ToString()).Append("\",");
- }
- builder.Append("\"FlowTime\":\"").Append(dtStart.ToString("yyyy-MM-dd")).Append("\"");
- builder.Append("}");
- }
- else
- {
- builder.Append("{");
- builder.Append("\"FlowRate\":\"-\",");
- builder.Append("\"AvgFlow\":\"-\",");
- builder.Append("\"FlowTime\":\"").Append(dtStart.ToString("yyyy-MM-dd")).Append("\"");
- builder.Append("}");
- }
- if (i < days - 1)
- builder.Append(",");
- dtStart = dtStart.AddDays(1);
- }
- builder.Append("]}");
- json = builder.ToString();
- // + json + "}";
- return json;
- }
- }
- }
|