AnalysisService.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. using LeaRun.Application.IService.PipeNetworkManage;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using LeaRun.Data;
  9. using LeaRun.Util;
  10. using LeaRun.Util.WebControl;
  11. namespace LeaRun.Application.Service.PipeNetworkManage
  12. {
  13. public class AnalysisService : IAnalysisService
  14. {
  15. SqlHelper sqlHelper = new SqlHelper("PipeNetworkDb");
  16. /// <summary>
  17. /// 年度用水分析
  18. /// </summary>
  19. /// <param name="departId"></param>
  20. /// <param name="typeID"></param>
  21. /// <param name="devId"></param>
  22. /// <param name="time"></param>
  23. /// <returns></returns>
  24. public string GetYearAnalysis(string devId, string time, Pagination pagination)
  25. {
  26. string strSql = "Select MeterAssessmentId,MeterAssessmentName From MeterAssessmentBase Where MeterAssessmentId in (" + devId + ")";
  27. DateTime dateTime = DateTime.Parse(time + "-01-01");
  28. int months = 12;
  29. int total = pagination.records;
  30. //DataTable dt = sqlHelper.ExecuteDataTable(strSql, CommandType.Text, null);
  31. DataTable dt = sqlHelper.ExecuteDataTable(strSql, "", false, pagination.rows, pagination.page, out total, null);
  32. pagination.records = total;
  33. StringBuilder builder = new StringBuilder();
  34. builder.Append("[");
  35. for (int i = 0; i < dt.Rows.Count; i++)
  36. {
  37. string deviceId = dt.Rows[i]["MeterAssessmentId"].ToString();
  38. string name = dt.Rows[i]["MeterAssessmentName"].ToString();
  39. builder.Append("{");
  40. builder.Append("\"ID\":\"").Append(deviceId).Append("\",");
  41. builder.Append("\"DevName\":\"").Append(name).Append("\",");
  42. string strTableName = "MeterAssessment_" + deviceId;
  43. if (sqlHelper.DBTableIsExists(strTableName))
  44. {
  45. DateTime startDay = dateTime;
  46. for (int j = 0; j < months; j++)
  47. {
  48. DateTime nowDay = startDay.AddMonths(j);
  49. DateTime endDay = nowDay.AddMonths(1);
  50. 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") + "'";
  51. DataTable table = sqlHelper.ExecuteDataTable(strSql, CommandType.Text, null);
  52. if (table.Rows.Count > 0)
  53. {
  54. decimal min = decimal.Parse(table.Rows[0]["NetCumulativeFlow"].ToString());
  55. decimal max = decimal.Parse(table.Rows[table.Rows.Count - 1]["NetCumulativeFlow"].ToString());
  56. builder.Append("\"Month" + (j + 1) + "\":\"").Append((max - min).ToString()).Append("\"");
  57. }
  58. else
  59. {
  60. builder.Append("\"Month" + (j + 1) + "\":\"").Append("--\"");
  61. }
  62. if (j < months - 1)
  63. builder.Append(",");
  64. }
  65. }
  66. else
  67. {
  68. string strDay = time + "-01";
  69. for (int j = 0; j < months; j++)
  70. {
  71. builder.Append("\"Month" + (j + 1) + "\":\"").Append("--\"");
  72. if (j < months - 1)
  73. builder.Append(",");
  74. }
  75. }
  76. builder.Append("}");
  77. if (i < dt.Rows.Count - 1)
  78. builder.Append(",");
  79. }
  80. builder.Append("]");
  81. return builder.ToString();
  82. }
  83. /// <summary>
  84. /// 月度用水分析
  85. /// </summary>
  86. /// <param name="departId"></param>
  87. /// <param name="typeID"></param>
  88. /// <param name="devId"></param>
  89. /// <param name="time"></param>
  90. /// <returns></returns>
  91. public string GetMonthAnalysis(string devId, string time, Pagination pagination)
  92. {
  93. string strSql = "";
  94. strSql = "Select MeterAssessmentId,MeterAssessmentName From MeterAssessmentBase Where MeterAssessmentId in (" + devId + ")";
  95. DateTime dateTime = DateTime.Parse(time);
  96. int days = LeaRun.Util.Time.GetDaysOfMonth(dateTime.Year, dateTime.Month);
  97. int total = pagination.records;
  98. DataTable dt = sqlHelper.ExecuteDataTable(strSql, "", false, pagination.rows, pagination.page, out total, null);
  99. pagination.records = total;
  100. StringBuilder builder = new StringBuilder();
  101. builder.Append("[");
  102. for (int i = 0; i < dt.Rows.Count; i++)
  103. {
  104. string deviceId = dt.Rows[i]["MeterAssessmentId"].ToString();
  105. string name = dt.Rows[i]["MeterAssessmentName"].ToString();
  106. builder.Append("{");
  107. builder.Append("\"ID\":\"").Append(deviceId).Append("\",");
  108. builder.Append("\"DevName\":\"").Append(name).Append("\",");
  109. string strTableName = "MeterAssessment_" + deviceId;
  110. if (sqlHelper.DBTableIsExists(strTableName))
  111. {
  112. string strDay = time + "-01";
  113. DateTime startDay = DateTime.Parse(strDay);
  114. for (int j = 0; j < days; j++)
  115. {
  116. DateTime nowDay = startDay.AddDays(j);
  117. DateTime endDay = nowDay.AddDays(1);
  118. 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") + "'";
  119. DataTable table = sqlHelper.ExecuteDataTable(strSql, CommandType.Text, null);
  120. if (table.Rows.Count > 0)
  121. {
  122. decimal min = decimal.Parse(table.Rows[0]["NetCumulativeFlow"].ToString());
  123. decimal max = decimal.Parse(table.Rows[table.Rows.Count - 1]["NetCumulativeFlow"].ToString());
  124. builder.Append("\"Day" + (j + 1) + "\":\"").Append((max - min).ToString("0.00")).Append("\"");
  125. }
  126. else
  127. {
  128. builder.Append("\"Day" + (j + 1) + "\":\"").Append("--\"");
  129. }
  130. if (j < days - 1)
  131. builder.Append(",");
  132. }
  133. }
  134. else
  135. {
  136. string strDay = time + "-01";
  137. for (int j = 0; j < days; j++)
  138. {
  139. builder.Append("\"Day" + (j + 1) + "\":\"").Append("--\"");
  140. if (j < days - 1)
  141. builder.Append(",");
  142. }
  143. }
  144. builder.Append("}");
  145. if (i < dt.Rows.Count - 1)
  146. builder.Append(",");
  147. }
  148. builder.Append("]");
  149. return builder.ToString();
  150. }
  151. /// <summary>
  152. /// 每小时用水量
  153. /// </summary>
  154. /// <param name="deviceId">测点ID</param>
  155. /// <param name="time">时间</param>
  156. /// <returns></returns>
  157. public string GetHourFlow(string deviceId, string time)
  158. {
  159. var json = "[]";
  160. var strSql = "Select MeterAssessmentId,MeterAssessmentName From MeterAssessmentBase Where MeterAssessmentId=" + deviceId;
  161. DataTable dt = sqlHelper.ExecuteDataTable(strSql, CommandType.Text, null);
  162. if (dt.Rows.Count > 0)
  163. {
  164. string devName = dt.Rows[0]["MeterAssessmentName"].ToString();
  165. DateTime dateTime = DateTime.Parse(time);
  166. StringBuilder builder = new StringBuilder();
  167. builder.Append("[");
  168. string strTableName = "MeterAssessment_" + deviceId;
  169. if (sqlHelper.DBTableIsExists(strTableName))
  170. {
  171. for (int i = 0; i < 24; i++)
  172. {
  173. DateTime nowDay = dateTime.AddHours(i);
  174. DateTime endDay = nowDay.AddHours(1);
  175. 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";
  176. DataTable table = sqlHelper.ExecuteDataTable(strSql, CommandType.Text, null);
  177. builder.Append("{");
  178. if (table.Rows.Count > 0)
  179. {
  180. decimal min = decimal.Parse(table.Rows[0]["NetCumulativeFlow"].ToString());
  181. decimal max = decimal.Parse(table.Rows[table.Rows.Count - 1]["NetCumulativeFlow"].ToString());
  182. builder.Append("\"DevName\":\"").Append(devName).Append("\",");
  183. builder.Append("\"HourNetFlow\":\"").Append((max - min).ToString()).Append("\",");
  184. builder.Append("\"LedgerTime\":\"").Append(i + 1).Append("\"");
  185. }
  186. else
  187. {
  188. builder.Append("\"HourNetFlow\":\"0\",");
  189. builder.Append("\"LedgerTime\":\"").Append(i + 1).Append("\"");
  190. }
  191. builder.Append("}");
  192. if (i < 23)
  193. builder.Append(",");
  194. }
  195. }
  196. builder.Append("]");
  197. json = builder.ToString();
  198. }
  199. return json;
  200. }
  201. ///// <summary>
  202. ///// 前三天平均用水量
  203. ///// </summary>
  204. ///// <param name="devId"></param>
  205. ///// <param name="time"></param>
  206. ///// <returns></returns>
  207. public string GetHourAvgFlow(string deviceId, string time)
  208. {
  209. var json = "[]";
  210. var strSql = "Select MeterAssessmentId,MeterAssessmentName From MeterAssessmentBase Where MeterAssessmentId=" + deviceId;
  211. DataTable dt = sqlHelper.ExecuteDataTable(strSql, CommandType.Text, null);
  212. if (dt.Rows.Count > 0)
  213. {
  214. DateTime dateTime = DateTime.Parse(time);
  215. StringBuilder builder = new StringBuilder();
  216. builder.Append("[");
  217. string strTableName = "MeterAssessment_" + deviceId;
  218. if (sqlHelper.DBTableIsExists(strTableName))
  219. {
  220. DateTime yesterDay = dateTime.AddDays(-1);//昨天
  221. DateTime twoDaysBefore = dateTime.AddDays(-2);//两天前
  222. DateTime threeDaysBefore = dateTime.AddDays(-3);//三天前
  223. decimal yesterDayUsed = 0, twoDaysBeforeUsed = 0, threeDaysBeforeUsed = 0;
  224. for (int i = 0; i < 24; i++)
  225. {
  226. DateTime yesterDayHour = yesterDay.AddHours(i);
  227. DateTime yesterDayEndHour = yesterDayHour.AddHours(1);
  228. DateTime twoDaysBeforeHour = twoDaysBefore.AddHours(i);
  229. DateTime twoDaysBeforeEndHour = twoDaysBeforeHour.AddHours(1);
  230. DateTime threeDaysBeforeHour = threeDaysBefore.AddHours(i);
  231. DateTime threeDaysBeforeEndHour = threeDaysBeforeHour.AddHours(1);
  232. #region 昨天小时流量
  233. 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";
  234. DataTable tableYes = sqlHelper.ExecuteDataTable(strSql, CommandType.Text, null);
  235. if (tableYes.Rows.Count > 0)
  236. {
  237. decimal min = decimal.Parse(tableYes.Rows[0]["NetCumulativeFlow"].ToString());
  238. decimal max = decimal.Parse(tableYes.Rows[tableYes.Rows.Count - 1]["NetCumulativeFlow"].ToString());
  239. yesterDayUsed = max - min;
  240. }
  241. else
  242. {
  243. yesterDayUsed = 0;
  244. }
  245. #endregion
  246. #region 两天前小时流量
  247. 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";
  248. tableYes = sqlHelper.ExecuteDataTable(strSql, CommandType.Text, null);
  249. if (tableYes.Rows.Count > 0)
  250. {
  251. decimal min = decimal.Parse(tableYes.Rows[0]["NetCumulativeFlow"].ToString());
  252. decimal max = decimal.Parse(tableYes.Rows[tableYes.Rows.Count - 1]["NetCumulativeFlow"].ToString());
  253. twoDaysBeforeUsed = max - min;
  254. }
  255. else
  256. {
  257. twoDaysBeforeUsed = 0;
  258. }
  259. #endregion
  260. #region 三天前小时流量
  261. 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";
  262. tableYes = sqlHelper.ExecuteDataTable(strSql, CommandType.Text, null);
  263. if (tableYes.Rows.Count > 0)
  264. {
  265. decimal min = decimal.Parse(tableYes.Rows[0]["NetCumulativeFlow"].ToString());
  266. decimal max = decimal.Parse(tableYes.Rows[tableYes.Rows.Count - 1]["NetCumulativeFlow"].ToString());
  267. threeDaysBeforeUsed = max - min;
  268. }
  269. else
  270. {
  271. threeDaysBeforeUsed = 0;
  272. }
  273. #endregion
  274. decimal avgFlow = (yesterDayUsed + twoDaysBeforeUsed + threeDaysBeforeUsed) / 3;
  275. builder.Append(avgFlow);
  276. if (i < 23)
  277. builder.Append(",");
  278. }
  279. }
  280. builder.Append("]");
  281. json = builder.ToString();
  282. }
  283. return json;
  284. }
  285. /// <summary>
  286. /// 每日用水量
  287. /// </summary>
  288. /// <param name="deviceId"></param>
  289. /// <param name="time"></param>
  290. /// <returns></returns>
  291. public string GetDayFlow(string deviceId, string time)
  292. {
  293. var json = "[]";
  294. var strSql = "Select MeterAssessmentId,MeterAssessmentName From MeterAssessmentBase Where MeterAssessmentId=" + deviceId;
  295. DataTable dt = sqlHelper.ExecuteDataTable(strSql, CommandType.Text, null);
  296. if (dt.Rows.Count > 0)
  297. {
  298. string devName = dt.Rows[0]["MeterAssessmentName"].ToString();
  299. DateTime dateTime = DateTime.Parse(time + "-01");
  300. int days = Time.GetDaysOfMonth(dateTime);
  301. StringBuilder builder = new StringBuilder();
  302. builder.Append("[");
  303. string strTableName = "MeterAssessment_" + deviceId;
  304. if (sqlHelper.DBTableIsExists(strTableName))
  305. {
  306. for (int i = 0; i < days; i++)
  307. {
  308. DateTime nowDay = dateTime.AddDays(i);
  309. DateTime endDay = nowDay.AddDays(1);
  310. 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";
  311. DataTable table = sqlHelper.ExecuteDataTable(strSql, CommandType.Text, null);
  312. builder.Append("{");
  313. builder.Append("\"DevName\":\"").Append(devName).Append("\",");
  314. if (table.Rows.Count > 0)
  315. {
  316. decimal min = decimal.Parse(table.Rows[0]["NetCumulativeFlow"].ToString());
  317. decimal max = decimal.Parse(table.Rows[table.Rows.Count - 1]["NetCumulativeFlow"].ToString());
  318. builder.Append("\"HourNetFlow\":\"").Append((max - min).ToString()).Append("\",");
  319. builder.Append("\"LedgerTime\":\"").Append(i + 1).Append("\",");
  320. }
  321. else
  322. {
  323. builder.Append("\"HourNetFlow\":\"0\",");
  324. builder.Append("\"LedgerTime\":\"").Append(i + 1).Append("\",");
  325. }
  326. 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") + "'";
  327. DataTable dtP = sqlHelper.ExecuteDataTable(strSql, CommandType.Text, null);
  328. if (dtP.Rows.Count > 0)
  329. {
  330. builder.Append("\"minP\":\"").Append(dtP.Rows[0]["最小压力"].ToString()).Append("\",");
  331. builder.Append("\"maxP\":\"").Append(dtP.Rows[0]["最大压力"].ToString()).Append("\",");
  332. builder.Append("\"avgP\":\"").Append(dtP.Rows[0]["平均压力"].ToString()).Append("\"");
  333. }
  334. else
  335. {
  336. builder.Append("\"minP\":\"0\",");
  337. builder.Append("\"maxP\":\"0\",");
  338. builder.Append("\"avgP\":\"0\"");
  339. }
  340. builder.Append("}");
  341. if (i < days - 1)
  342. builder.Append(",");
  343. }
  344. }
  345. builder.Append("]");
  346. json = builder.ToString();
  347. }
  348. return json;
  349. }
  350. /// <summary>
  351. /// 每月用水量
  352. /// </summary>
  353. /// <param name="deviceId"></param>
  354. /// <param name="time"></param>
  355. /// <returns></returns>
  356. public string GetMonthFlow(string deviceId, string time)
  357. {
  358. var json = "[]";
  359. var strSql = "Select MeterAssessmentId,MeterAssessmentName From MeterAssessmentBase Where MeterAssessmentId=" + deviceId;
  360. DataTable dt = sqlHelper.ExecuteDataTable(strSql, CommandType.Text, null);
  361. if (dt.Rows.Count > 0)
  362. {
  363. string devName = dt.Rows[0]["MeterAssessmentName"].ToString();
  364. DateTime dateTime = DateTime.Parse(time + "-01-01");
  365. StringBuilder builder = new StringBuilder();
  366. builder.Append("[");
  367. string strTableName = "MeterAssessment_" + deviceId;
  368. if (sqlHelper.DBTableIsExists(strTableName))
  369. {
  370. for (int i = 0; i < 12; i++)
  371. {
  372. DateTime nowDay = dateTime.AddMonths(i);
  373. DateTime endDay = nowDay.AddMonths(1);
  374. 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";
  375. DataTable table = sqlHelper.ExecuteDataTable(strSql, CommandType.Text, null);
  376. builder.Append("{");
  377. if (table.Rows.Count > 0)
  378. {
  379. decimal min = decimal.Parse(table.Rows[0]["NetCumulativeFlow"].ToString());
  380. decimal max = decimal.Parse(table.Rows[table.Rows.Count - 1]["NetCumulativeFlow"].ToString());
  381. builder.Append("\"DevName\":\"").Append(devName).Append("\",");
  382. builder.Append("\"HourNetFlow\":\"").Append((max - min).ToString()).Append("\",");
  383. builder.Append("\"LedgerTime\":\"").Append(i + 1).Append("\"");
  384. }
  385. else
  386. {
  387. builder.Append("\"HourNetFlow\":\"0\",");
  388. builder.Append("\"LedgerTime\":\"").Append(i + 1).Append("\"");
  389. }
  390. builder.Append("}");
  391. if (i < 11)
  392. builder.Append(",");
  393. }
  394. }
  395. builder.Append("]");
  396. json = builder.ToString();
  397. }
  398. return json;
  399. }
  400. /// <summary>
  401. /// 瞬时流量
  402. /// </summary>
  403. /// <param name="deviceId"></param>
  404. /// <param name="start"></param>
  405. /// <param name="end"></param>
  406. /// <returns></returns>
  407. public string GetFlowRate(string deviceId, string start, string end)
  408. {
  409. var json = "[]";
  410. var sql = "Select MeterAssessmentName From MeterAssessmentBase Where MeterAssessmentId=" + deviceId;
  411. DataTable dtDev = sqlHelper.ExecuteDataTable(sql, CommandType.Text, null);
  412. string devName = "";
  413. if (dtDev.Rows.Count > 0)
  414. {
  415. devName = dtDev.Rows[0]["MeterAssessmentName"].ToString();
  416. }
  417. DateTime dateTime = DateTime.Parse(start);
  418. string strTableName = "MeterAssessment_" + deviceId;
  419. if (sqlHelper.DBTableIsExists(strTableName))
  420. {
  421. var strSql = "Select InstantaneousFlow,Pressure,GetDateTime From " + strTableName + " Where InstantaneousFlow is not null And GetDateTime between '" + start + "' and '" + end + "' Order by GetDateTime";
  422. DataTable dt = sqlHelper.ExecuteDataTable(strSql, CommandType.Text, null);
  423. StringBuilder builder = new StringBuilder();
  424. builder.Append("[");
  425. for (int i = 0; i < dt.Rows.Count; i++)
  426. {
  427. builder.Append("{");
  428. builder.Append("\"FlowRate\":\"").Append(dt.Rows[i]["InstantaneousFlow"].ToString()).Append("\",");
  429. builder.Append("\"Pressure\":\"").Append(string.IsNullOrEmpty(dt.Rows[i]["Pressure"].ToString()) == true ? "-" : dt.Rows[i]["Pressure"].ToString()).Append("\",");
  430. DateTime dtN = DateTime.Parse(dt.Rows[i]["GetDateTime"].ToString());
  431. builder.Append("\"FlowTime\":\"").Append(dtN.ToString("yyyy-MM-dd HH:mm:ss")).Append("\"");
  432. builder.Append("}");
  433. if (i < dt.Rows.Count - 1)
  434. builder.Append(",");
  435. }
  436. builder.Append("]");
  437. json = builder.ToString();
  438. json = "{\"devName\":\"" + devName + "\",\"devData\":" + json + ",";
  439. strSql = "Select avg(InstantaneousFlow) as 平均流量,MIN(InstantaneousFlow) as 最小流量 From " + strTableName + " Where InstantaneousFlow is not null And GetDateTime between '" + start + "' and '" + end + "'";
  440. DataTable dtAvg = sqlHelper.ExecuteDataTable(strSql, CommandType.Text, null);
  441. if (dtAvg.Rows.Count > 0)
  442. {
  443. //decimal avgFlow=decimal
  444. string strAvg = dtAvg.Rows[0]["平均流量"].ToString();
  445. #region
  446. if (!string.IsNullOrEmpty(strAvg))
  447. {
  448. decimal avgFlow = decimal.Parse(dtAvg.Rows[0]["平均流量"].ToString());
  449. decimal minFlow = decimal.Parse(dtAvg.Rows[0]["最小流量"].ToString());
  450. DateTime dtStart = DateTime.Parse(start);
  451. DateTime dtEnd = DateTime.Parse(end);
  452. List<decimal> totalMinFlow = new List<decimal>();
  453. int days = (dtEnd - dtStart).Days;
  454. for (int i = 0; i < days; i++)
  455. {
  456. DateTime dtTomorrow = dtStart.AddDays(1);
  457. 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") + "'";
  458. dtStart = dtStart.AddDays(1);
  459. DataTable dtMin = sqlHelper.ExecuteDataTable(strSql, CommandType.Text, null);
  460. if (dtMin.Rows.Count > 0)
  461. {
  462. if (!string.IsNullOrEmpty(dtMin.Rows[0]["最小流量"].ToString()))
  463. {
  464. totalMinFlow.Add(decimal.Parse(dtMin.Rows[0]["最小流量"].ToString()));
  465. }
  466. }
  467. }
  468. if (totalMinFlow.Count > 0)
  469. {
  470. decimal totalM = 0;
  471. foreach (var item in totalMinFlow)
  472. {
  473. totalM += item;
  474. }
  475. minFlow = Math.Round(totalM / totalMinFlow.Count, 3);
  476. }
  477. //疑似漏水量,疑似漏失率,夜间允许漏水量,期间供水量,期间售水量
  478. decimal suspectedLeakFlow, suspectedLeakRate, nightAllowLeakFlow, waterSupply, salesWater;
  479. suspectedLeakFlow = minFlow - avgFlow / 5;
  480. if (avgFlow != 0)
  481. {
  482. suspectedLeakRate = suspectedLeakFlow / avgFlow * 100;
  483. }
  484. else
  485. {
  486. suspectedLeakRate = 0;
  487. }
  488. nightAllowLeakFlow = minFlow - suspectedLeakFlow;
  489. waterSupply = avgFlow * 24;
  490. salesWater = waterSupply - suspectedLeakFlow * 24;
  491. json += "\"avgFlow\":" + dtAvg.Rows[0]["平均流量"].ToString() + ",\"minFlow\":" + dtAvg.Rows[0]["最小流量"].ToString() + ",";
  492. json += "\"suspectedLeakFlow\":" + Math.Round(suspectedLeakFlow, 3) + ",";
  493. json += "\"suspectedLeakRate\":" + Math.Round(suspectedLeakRate, 3) + ",";
  494. json += "\"nightAllowLeakFlow\":" + Math.Round(nightAllowLeakFlow, 3) + ",";
  495. json += "\"waterSupply\":" + Math.Round(waterSupply, 3) + ",";
  496. json += "\"salesWater\":" + Math.Round(salesWater, 3) + "}";
  497. }
  498. else
  499. {
  500. json += "\"avgFlow\":\"--\",\"minFlow\":\"--\",";
  501. json += "\"suspectedLeakFlow\":\"--\",";
  502. json += "\"suspectedLeakRate\":\"--\",";
  503. json += "\"nightAllowLeakFlow\":\"--\",";
  504. json += "\"waterSupply\":\"--\",";
  505. json += "\"salesWater\":\"--\"}";
  506. }
  507. #endregion
  508. }
  509. else
  510. {
  511. json += "\"avgFlow\":\"--\",\"minFlow\":\"--\",";
  512. json += "\"suspectedLeakFlow\":\"--\",";
  513. json += "\"suspectedLeakRate\":\"--\",";
  514. json += "\"nightAllowLeakFlow\":\"--\",";
  515. json += "\"waterSupply\":\"--\",";
  516. json += "\"salesWater\":\"--\"}";
  517. }
  518. }
  519. return json;
  520. }
  521. /// <summary>
  522. /// 最小流量分析
  523. /// </summary>
  524. /// <param name="deviceId"></param>
  525. /// <param name="start"></param>
  526. /// <param name="end"></param>
  527. /// <returns></returns>
  528. public string GetMinFlow(string deviceId, string start, string end)
  529. {
  530. var json = "{\"devName\":\"\",\"devData\":[]";
  531. var sql = "Select MeterAssessmentName From MeterAssessmentBase Where MeterAssessmentId=" + deviceId;
  532. DataTable dtDev = sqlHelper.ExecuteDataTable(sql, CommandType.Text, null);
  533. string devName = "";
  534. if (dtDev.Rows.Count > 0)
  535. {
  536. devName = dtDev.Rows[0]["MeterAssessmentName"].ToString();
  537. }
  538. DateTime dateTime = DateTime.Parse(start);
  539. string strTableName = "MeterAssessment_" + deviceId;
  540. DateTime dtStart = DateTime.Parse(start);
  541. DateTime dtEnd = DateTime.Parse(end);
  542. int days = (dtEnd - dtStart).Days;
  543. string strSql = "";
  544. StringBuilder builder = new StringBuilder();
  545. builder.Append("{\"devName\":\"").Append(devName);
  546. builder.Append("\",\"devData\":");
  547. builder.Append("[");
  548. for (int i = 0; i < days; i++)
  549. {
  550. DateTime dtTomorrow = dtStart.AddDays(1);
  551. 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") + "'";
  552. DataTable dtMin = sqlHelper.ExecuteDataTable(strSql, CommandType.Text, null);
  553. if (dtMin.Rows.Count > 0)
  554. {
  555. builder.Append("{");
  556. builder.Append("\"FlowRate\":\"").Append(dtMin.Rows[0]["最小流量"].ToString()).Append("\",");
  557. //计算前三天的平均瞬时流量
  558. if (i >= 3)
  559. {
  560. 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") + "'";
  561. DataTable dtAvg = sqlHelper.ExecuteDataTable(strSql, CommandType.Text, null);
  562. if (dtAvg.Rows.Count > 0)
  563. {
  564. builder.Append("\"AvgFlow\":\"").Append(dtAvg.Rows[0]["平均流量"].ToString()).Append("\",");
  565. }
  566. else
  567. {
  568. builder.Append("\"AvgFlow\":\"-\",");
  569. }
  570. }
  571. else
  572. {
  573. builder.Append("\"AvgFlow\":\"").Append(dtMin.Rows[0]["平均流量"].ToString()).Append("\",");
  574. }
  575. builder.Append("\"FlowTime\":\"").Append(dtStart.ToString("yyyy-MM-dd")).Append("\"");
  576. builder.Append("}");
  577. }
  578. else
  579. {
  580. builder.Append("{");
  581. builder.Append("\"FlowRate\":\"-\",");
  582. builder.Append("\"AvgFlow\":\"-\",");
  583. builder.Append("\"FlowTime\":\"").Append(dtStart.ToString("yyyy-MM-dd")).Append("\"");
  584. builder.Append("}");
  585. }
  586. if (i < days - 1)
  587. builder.Append(",");
  588. dtStart = dtStart.AddDays(1);
  589. }
  590. builder.Append("]}");
  591. json = builder.ToString();
  592. // + json + "}";
  593. return json;
  594. }
  595. }
  596. }