FlowService.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using LeaRun.Application.IService.PipeNetworkManage;
  7. using LeaRun.Data;
  8. using System.Data;
  9. using LeaRun.Util;
  10. namespace LeaRun.Application.Service.PipeNetworkManage
  11. {
  12. public class FlowService : IFlowService
  13. {
  14. SqlHelper sqlHelper = new SqlHelper("PipeNetworkDb");
  15. /// <summary>
  16. /// 压力报表
  17. /// </summary>
  18. /// <param name="typeFlag">0-日 1-月 2-年</param>
  19. /// <param name="ids">设备ID集合</param>
  20. /// <param name="time">查询日期</param>
  21. /// <returns></returns>
  22. public string GetFlowReport(string typeFlag, string ids, string time)
  23. {
  24. try
  25. {
  26. //DateTime nowTime = Convert.ToDateTime(time);
  27. string sql = "SELECT * FROM MeterAssessmentBase WHERE MeterAssessmentId IN (" + ids + ")";
  28. DataTable dt = sqlHelper.ExecuteDataTable(sql, CommandType.Text, null);
  29. StringBuilder jqData = new StringBuilder();
  30. jqData.Append("[");
  31. switch (typeFlag)
  32. {
  33. case "0":
  34. jqData.Append(GetDayReportData(dt, time));
  35. break;
  36. case "1":
  37. DateTime nowTime = Convert.ToDateTime(time + "/01");
  38. jqData.Append(GetMonthReportData(dt, nowTime));
  39. break;
  40. case "2":
  41. DateTime yearTime = Convert.ToDateTime(time + "/01/01");
  42. jqData.Append(GetYearReportData(dt, yearTime));
  43. break;
  44. }
  45. jqData.Append("]");
  46. return jqData.ToString();
  47. }
  48. catch (Exception ex)
  49. {
  50. return "[]";
  51. }
  52. }
  53. /// <summary>
  54. /// 压力日报表数据
  55. /// </summary>
  56. /// <param name="dt"></param>
  57. /// <param name="time"></param>
  58. /// <returns></returns>
  59. public StringBuilder GetDayReportData(DataTable dt, string time)
  60. {
  61. StringBuilder sbText = new StringBuilder();
  62. DataTable[] dtSources = new DataTable[dt.Rows.Count];
  63. double[] maxValue = new double[dt.Rows.Count];
  64. double[] minValue = new double[dt.Rows.Count];
  65. double[] sunValue = new double[dt.Rows.Count];
  66. DateTime[] maxValueTime = new DateTime[dt.Rows.Count];
  67. DateTime[] minValueTime = new DateTime[dt.Rows.Count];
  68. int numCount = 0; ;
  69. for (int i = 0; i < 24; i++)
  70. {
  71. sbText.Append("{");
  72. sbText.Append("\"showtime\":\"").Append(i + ":00").Append("\",");
  73. for (int row = 0; row < dt.Rows.Count; row++)
  74. {
  75. string deviceId = dt.Rows[row]["MeterAssessmentId"].ToString();
  76. string tableName = "MeterAssessment_" + deviceId;
  77. string selDr = "GetDateTime='" + time + " " + i + ":00:00" + "'";
  78. string selSql = "SELECT * FROM " + tableName + " WHERE DATEDIFF(DAY, '" + time + "' ,GetDateTime)=0 ORDER BY GetDateTime";
  79. DataTable dtResult;
  80. //结果存储 避免大量访问数据库
  81. if (dtSources[row] != null)
  82. {
  83. dtResult = dtSources[row];
  84. }
  85. else
  86. {
  87. dtResult = sqlHelper.ExecuteDataTable(selSql, CommandType.Text, null);
  88. dtSources[row] = dtResult;
  89. }
  90. DataRow[] drs = dtResult.Select(selDr);
  91. if (drs.Count() > 0)
  92. {
  93. numCount++;
  94. DateTime tempTime = Convert.ToDateTime(drs[0]["GetDateTime"].ToString());
  95. double instantaneousFlow = Convert.ToDouble(drs[0]["InstantaneousFlow"]);
  96. //初始化
  97. if (i == 0)
  98. {
  99. minValue[row] = instantaneousFlow;
  100. maxValue[row] = instantaneousFlow;
  101. sunValue[row] = instantaneousFlow;
  102. maxValueTime[row] = tempTime;
  103. minValueTime[row] = tempTime;
  104. }
  105. else
  106. {
  107. if (minValue[row] > instantaneousFlow)
  108. {
  109. minValue[row] = instantaneousFlow;
  110. minValueTime[row] = tempTime;
  111. }
  112. if (maxValue[row] < instantaneousFlow)
  113. {
  114. maxValue[row] = instantaneousFlow;
  115. maxValueTime[row] = tempTime;
  116. }
  117. sunValue[row] = sunValue[row] + instantaneousFlow;
  118. }
  119. sbText.Append("\"flow_" + deviceId + "\":\"").Append(drs[0]["InstantaneousFlow"]).Append("\"");
  120. }
  121. else
  122. {
  123. sbText.Append("\"flow_" + deviceId + "\":\"").Append("--").Append("\"");
  124. }
  125. if (row < dt.Rows.Count - 1) sbText.Append(",");
  126. }
  127. sbText.Append("},");
  128. }
  129. #region 平均值 最小值 最大值
  130. sbText.Append("{");
  131. sbText.Append("\"showtime\":\"").Append("平均值").Append("\",");
  132. //一天中的 平均值
  133. for (int i = 0; i < dt.Rows.Count; i++)
  134. {
  135. string deviceId = dt.Rows[i]["MeterAssessmentId"].ToString();
  136. sbText.Append("\"flow_" + deviceId + "\":\"").Append((sunValue[i] / numCount).ToString("0.00")).Append("\"");
  137. if (i < dt.Rows.Count - 1) sbText.Append(",");
  138. }
  139. sbText.Append("},");
  140. sbText.Append("{");
  141. sbText.Append("\"showtime\":\"").Append("最小值").Append("\",");
  142. //一天中的 最小值
  143. for (int i = 0; i < dt.Rows.Count; i++)
  144. {
  145. string deviceId = dt.Rows[i]["MeterAssessmentId"].ToString();
  146. sbText.Append("\"flow_" + deviceId + "\":\"").Append(minValue[i]).Append("\"");
  147. if (i < dt.Rows.Count - 1) sbText.Append(",");
  148. }
  149. sbText.Append("},");
  150. sbText.Append("{");
  151. sbText.Append("\"showtime\":\"").Append("最小值时间").Append("\",");
  152. //一天中的 最小值时间
  153. for (int i = 0; i < dt.Rows.Count; i++)
  154. {
  155. string deviceId = dt.Rows[i]["MeterAssessmentId"].ToString();
  156. sbText.Append("\"flow_" + deviceId + "\":\"").Append(minValueTime[i].ToString("HH:mm")).Append("\"");
  157. if (i < dt.Rows.Count - 1) sbText.Append(",");
  158. }
  159. sbText.Append("},");
  160. sbText.Append("{");
  161. sbText.Append("\"showtime\":\"").Append("最大值").Append("\",");
  162. //一天中的 最大值
  163. for (int i = 0; i < dt.Rows.Count; i++)
  164. {
  165. string deviceId = dt.Rows[i]["MeterAssessmentId"].ToString();
  166. sbText.Append("\"flow_" + deviceId + "\":\"").Append(maxValue[i]).Append("\"");
  167. if (i < dt.Rows.Count - 1) sbText.Append(",");
  168. }
  169. sbText.Append("},");
  170. sbText.Append("{");
  171. sbText.Append("\"showtime\":\"").Append("最大值时间").Append("\",");
  172. //一天中的 最小值时间
  173. for (int i = 0; i < dt.Rows.Count; i++)
  174. {
  175. string deviceId = dt.Rows[i]["MeterAssessmentId"].ToString();
  176. sbText.Append("\"flow_" + deviceId + "\":\"").Append(maxValueTime[i].ToString("HH:mm")).Append("\"");
  177. if (i < dt.Rows.Count - 1) sbText.Append(",");
  178. }
  179. sbText.Append("},");
  180. sbText.Append("{");
  181. sbText.Append("\"showtime\":\"").Append("变化系数").Append("\",");
  182. //一天中的 变化系数 最大值/平均值
  183. for (int i = 0; i < dt.Rows.Count; i++)
  184. {
  185. string deviceId = dt.Rows[i]["MeterAssessmentId"].ToString();
  186. sbText.Append("\"flow_" + deviceId + "\":\"").Append((maxValue[i] / (sunValue[i] / numCount)).ToString("0.00")).Append("\"");
  187. if (i < dt.Rows.Count - 1) sbText.Append(",");
  188. }
  189. sbText.Append("}");
  190. #endregion
  191. return sbText;
  192. }
  193. /// <summary>
  194. /// 压力报表 -日报表 动态表头创建
  195. /// </summary>
  196. /// <param name="ids"></param>
  197. /// <returns></returns>
  198. public string GetFlowDayReportHead(string ids)
  199. {
  200. try
  201. {
  202. string sql = "SELECT * FROM MeterAssessmentBase WHERE MeterAssessmentId IN (" + ids + ")";
  203. DataTable dt = sqlHelper.ExecuteDataTable(sql, CommandType.Text, null);
  204. StringBuilder sbColMod = new StringBuilder();
  205. sbColMod.Append("[");
  206. sbColMod.Append("{");
  207. sbColMod.Append("\"label\":\"").Append("时间").Append("\",");
  208. sbColMod.Append("\"name\":\"").Append("showtime").Append("\",");
  209. sbColMod.Append("\"align\":\"").Append("center").Append("\",");
  210. sbColMod.Append("\"width\":\"").Append("100").Append("\",");
  211. sbColMod.Append("\"sortable \":\"").Append("false").Append("\"");
  212. sbColMod.Append("}");
  213. for (int i = 0; i < dt.Rows.Count; i++)
  214. {
  215. string deviceId = dt.Rows[i]["MeterAssessmentId"].ToString();
  216. string name = dt.Rows[i]["MeterAssessmentName"].ToString();
  217. if (i == 0) sbColMod.Append(",");
  218. sbColMod.Append("{");
  219. sbColMod.Append("\"label\":\"").Append(name).Append("\",");
  220. sbColMod.Append("\"name\":\"").Append("flow_" + deviceId).Append("\",");
  221. sbColMod.Append("\"align\":\"").Append("center").Append("\",");
  222. sbColMod.Append("\"width\":\"").Append("150").Append("\",");
  223. sbColMod.Append("\"sortable \":\"").Append("false").Append("\"");
  224. sbColMod.Append("}");
  225. if (i < dt.Rows.Count - 1)
  226. {
  227. sbColMod.Append(",");
  228. }
  229. }
  230. sbColMod.Append("]");
  231. return sbColMod.ToString();
  232. }
  233. catch (Exception ex)
  234. {
  235. return "[]";
  236. }
  237. }
  238. /// <summary>
  239. /// 压力报表 -月(年)报表 动态表头创建
  240. /// </summary>
  241. /// <param name="ids"></param>
  242. /// <returns></returns>
  243. public string GetFlowMonthReportHead(string ids)
  244. {
  245. try
  246. {
  247. string sql = "SELECT * FROM MeterAssessmentBase WHERE MeterAssessmentId IN (" + ids + ")";
  248. DataTable dt = sqlHelper.ExecuteDataTable(sql, CommandType.Text, null);
  249. StringBuilder sbColMod = new StringBuilder();
  250. sbColMod.Append("[");
  251. sbColMod.Append("{");
  252. sbColMod.Append("\"label\":\"").Append("时间").Append("\",");
  253. sbColMod.Append("\"name\":\"").Append("showtime").Append("\",");
  254. sbColMod.Append("\"align\":\"").Append("center").Append("\",");
  255. sbColMod.Append("\"width\":\"").Append("100").Append("\",");
  256. sbColMod.Append("\"sortable \":\"").Append("false").Append("\"");
  257. sbColMod.Append("}");
  258. for (int i = 0; i < dt.Rows.Count; i++)
  259. {
  260. string deviceId = dt.Rows[i]["MeterAssessmentId"].ToString();
  261. string name = dt.Rows[i]["MeterAssessmentName"].ToString();
  262. if (i == 0) sbColMod.Append(",");
  263. sbColMod.Append("{");
  264. sbColMod.Append("\"label\":\"").Append("平均值").Append("\",");
  265. sbColMod.Append("\"name\":\"").Append("avg_" + deviceId).Append("\",");
  266. sbColMod.Append("\"align\":\"").Append("center").Append("\",");
  267. sbColMod.Append("\"width\":\"").Append("150").Append("\",");
  268. sbColMod.Append("\"sortable \":\"").Append("false").Append("\"");
  269. sbColMod.Append("},");
  270. sbColMod.Append("{");
  271. sbColMod.Append("\"label\":\"").Append("最大值").Append("\",");
  272. sbColMod.Append("\"name\":\"").Append("max_" + deviceId).Append("\",");
  273. sbColMod.Append("\"align\":\"").Append("center").Append("\",");
  274. sbColMod.Append("\"width\":\"").Append("150").Append("\",");
  275. sbColMod.Append("\"sortable \":\"").Append("false").Append("\"");
  276. sbColMod.Append("},");
  277. sbColMod.Append("{");
  278. sbColMod.Append("\"label\":\"").Append("最小值").Append("\",");
  279. sbColMod.Append("\"name\":\"").Append("min_" + deviceId).Append("\",");
  280. sbColMod.Append("\"align\":\"").Append("center").Append("\",");
  281. sbColMod.Append("\"width\":\"").Append("150").Append("\",");
  282. sbColMod.Append("\"sortable \":\"").Append("false").Append("\"");
  283. sbColMod.Append("}");
  284. if (i < dt.Rows.Count - 1)
  285. {
  286. sbColMod.Append(",");
  287. }
  288. }
  289. sbColMod.Append("]");
  290. return sbColMod.ToString();
  291. }
  292. catch (Exception ex)
  293. {
  294. return "[]";
  295. }
  296. }
  297. /// <summary>
  298. /// 压力月报表获取
  299. /// </summary>
  300. /// <param name="dt"></param>
  301. /// <param name="time"></param>`
  302. /// <returns></returns>
  303. public StringBuilder GetMonthReportData(DataTable dt, DateTime time)
  304. {
  305. StringBuilder sbText = new StringBuilder();
  306. DataTable[] dtSources = new DataTable[dt.Rows.Count];
  307. int numCount = 0;
  308. int days = Time.GetDaysOfMonth(time.Year, time.Month);
  309. for (int i = 0; i < days; i++)
  310. {
  311. sbText.Append("{");
  312. sbText.Append("\"showtime\":\"").Append(time.AddDays(i).ToShortDateString()).Append("\",");
  313. for (int row = 0; row < dt.Rows.Count; row++)
  314. {
  315. string deviceId = dt.Rows[row]["MeterAssessmentId"].ToString();
  316. string tableName = "MeterAssessment_" + deviceId;
  317. 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)";
  318. DataTable dtResult;
  319. //结果存储 避免大量访问数据库
  320. if (dtSources[row] != null)
  321. {
  322. dtResult = dtSources[row];
  323. }
  324. else
  325. {
  326. dtResult = sqlHelper.ExecuteDataTable(selSql, CommandType.Text, null);
  327. dtSources[row] = dtResult;
  328. }
  329. DataRow[] drs = dtResult.Select("monthDay=" + (i + 1));
  330. if (drs.Count() > 0)
  331. {
  332. numCount++;
  333. sbText.Append("\"avg_" + deviceId + "\":\"").Append(drs[0]["avgFlow"]).Append("\",");
  334. sbText.Append("\"max_" + deviceId + "\":\"").Append(drs[0]["maxFlow"]).Append("\",");
  335. sbText.Append("\"min_" + deviceId + "\":\"").Append(drs[0]["minFlow"]).Append("\"");
  336. }
  337. else
  338. {
  339. sbText.Append("\"avg_" + deviceId + "\":\"").Append("--").Append("\",");
  340. sbText.Append("\"max_" + deviceId + "\":\"").Append("--").Append("\",");
  341. sbText.Append("\"min_" + deviceId + "\":\"").Append("--").Append("\"");
  342. }
  343. if (row < dt.Rows.Count - 1) sbText.Append(",");
  344. }
  345. sbText.Append("},");
  346. //if (i < days - 1) sbText.Append(",");
  347. }
  348. #region 平均值 最小值 最大值
  349. double maxAvg = 0;
  350. double avgAvg = 0;
  351. double maxMax = 0;
  352. double avgMax = 0;
  353. double maxMin = 0;
  354. double avgMin = 0;
  355. sbText.Append("{");
  356. sbText.Append("\"showtime\":\"").Append("平均值").Append("\",");
  357. //一月中的 平均值
  358. for (int i = 0; i < dt.Rows.Count; i++)
  359. {
  360. DataTable tempDt = dtSources[i];
  361. var avg_avg = tempDt.Compute("avg(avgFlow)", "");
  362. var avg_max = tempDt.Compute("avg(maxFlow)", "");
  363. var avg_min = tempDt.Compute("avg(minFlow)", "");
  364. avgAvg = Convert.ToDouble(avg_avg);
  365. avgMax = Convert.ToDouble(avg_max);
  366. avgMin = Convert.ToDouble(avg_min);
  367. string deviceId = dt.Rows[i]["MeterAssessmentId"].ToString();
  368. sbText.Append("\"avg_" + deviceId + "\":\"").Append(avg_avg).Append("\",");
  369. sbText.Append("\"max_" + deviceId + "\":\"").Append(avg_max).Append("\",");
  370. sbText.Append("\"min_" + deviceId + "\":\"").Append(avg_min).Append("\"");
  371. if (i < dt.Rows.Count - 1) sbText.Append(",");
  372. }
  373. sbText.Append("},");
  374. sbText.Append("{");
  375. sbText.Append("\"showtime\":\"").Append("最小值").Append("\",");
  376. //一月中的 最小值
  377. for (int i = 0; i < dt.Rows.Count; i++)
  378. {
  379. string deviceId = dt.Rows[i]["MeterAssessmentId"].ToString();
  380. DataTable tempDt = dtSources[i];
  381. tempDt.DefaultView.Sort = "avgFlow";
  382. tempDt.AcceptChanges();
  383. sbText.Append("\"avg_" + deviceId + "\":\"").Append(tempDt.Rows.Count > 0 ? tempDt.Rows[0]["avgFlow"] : "--").Append("\",");
  384. tempDt.DefaultView.Sort = "maxFlow";
  385. tempDt.AcceptChanges();
  386. sbText.Append("\"max_" + deviceId + "\":\"").Append(tempDt.Rows.Count > 0 ? tempDt.Rows[0]["maxFlow"] : "--").Append("\",");
  387. tempDt.DefaultView.Sort = "minFlow";
  388. tempDt.AcceptChanges();
  389. sbText.Append("\"min_" + deviceId + "\":\"").Append(tempDt.Rows.Count > 0 ? tempDt.Rows[0]["minFlow"] : "--").Append("\"");
  390. if (i < dt.Rows.Count - 1) sbText.Append(",");
  391. }
  392. sbText.Append("},");
  393. sbText.Append("{");
  394. sbText.Append("\"showtime\":\"").Append("最小值时间").Append("\",");
  395. //一月中的 最小值时间
  396. for (int i = 0; i < dt.Rows.Count; i++)
  397. {
  398. string deviceId = dt.Rows[i]["MeterAssessmentId"].ToString();
  399. DataTable tempDt = dtSources[i];
  400. tempDt.DefaultView.Sort = "avgFlow";
  401. tempDt.AcceptChanges();
  402. sbText.Append("\"avg_" + deviceId + "\":\"").Append(tempDt.Rows.Count > 0 ? time.Year + "-" + time.Month + "-" + tempDt.Rows[0]["monthDay"] : "--").Append("\",");
  403. tempDt.DefaultView.Sort = "maxFlow";
  404. tempDt.AcceptChanges();
  405. sbText.Append("\"max_" + deviceId + "\":\"").Append(tempDt.Rows.Count > 0 ? time.Year + "-" + time.Month + "-" + tempDt.Rows[0]["monthDay"] : "--").Append("\",");
  406. tempDt.DefaultView.Sort = "minFlow";
  407. tempDt.AcceptChanges();
  408. sbText.Append("\"min_" + deviceId + "\":\"").Append(tempDt.Rows.Count > 0 ? time.Year + "-" + time.Month + "-" + tempDt.Rows[0]["monthDay"] : "--").Append("\"");
  409. if (i < dt.Rows.Count - 1) sbText.Append(",");
  410. }
  411. sbText.Append("},");
  412. sbText.Append("{");
  413. sbText.Append("\"showtime\":\"").Append("最大值").Append("\",");
  414. //一月中的 最大值
  415. for (int i = 0; i < dt.Rows.Count; i++)
  416. {
  417. string deviceId = dt.Rows[i]["MeterAssessmentId"].ToString();
  418. DataTable tempDt = dtSources[i];
  419. tempDt.DefaultView.Sort = "avgFlow desc";
  420. tempDt.AcceptChanges();
  421. sbText.Append("\"avg_" + deviceId + "\":\"").Append(tempDt.Rows.Count > 0 ? tempDt.Rows[0]["avgFlow"] : "--").Append("\",");
  422. maxAvg = tempDt.Rows.Count > 0 ? Convert.ToDouble(tempDt.Rows[0]["avgFlow"].ToString()) : 0;
  423. tempDt.DefaultView.Sort = "maxFlow desc";
  424. tempDt.AcceptChanges();
  425. sbText.Append("\"max_" + deviceId + "\":\"").Append(tempDt.Rows.Count > 0 ? tempDt.Rows[0]["maxFlow"] : "--").Append("\",");
  426. maxMax = tempDt.Rows.Count > 0 ? Convert.ToDouble(tempDt.Rows[0]["maxFlow"].ToString()) : 0;
  427. tempDt.DefaultView.Sort = "minFlow desc";
  428. tempDt.AcceptChanges();
  429. sbText.Append("\"min_" + deviceId + "\":\"").Append(tempDt.Rows.Count > 0 ? tempDt.Rows[0]["minFlow"] : "--").Append("\"");
  430. maxMin = tempDt.Rows.Count > 0 ? Convert.ToDouble(tempDt.Rows[0]["minFlow"].ToString()) : 0;
  431. if (i < dt.Rows.Count - 1) sbText.Append(",");
  432. }
  433. sbText.Append("},");
  434. sbText.Append("{");
  435. sbText.Append("\"showtime\":\"").Append("最大值时间").Append("\",");
  436. //一月中的 最小值时间
  437. for (int i = 0; i < dt.Rows.Count; i++)
  438. {
  439. string deviceId = dt.Rows[i]["MeterAssessmentId"].ToString();
  440. DataTable tempDt = dtSources[i];
  441. tempDt.DefaultView.Sort = "avgFlow desc";
  442. tempDt.AcceptChanges();
  443. sbText.Append("\"avg_" + deviceId + "\":\"").Append(tempDt.Rows.Count > 0 ? time.Year + "-" + time.Month + "-" + tempDt.Rows[0]["monthDay"] : "--").Append("\",");
  444. tempDt.DefaultView.Sort = "maxFlow desc";
  445. tempDt.AcceptChanges();
  446. sbText.Append("\"max_" + deviceId + "\":\"").Append(tempDt.Rows.Count > 0 ? time.Year + "-" + time.Month + "-" + tempDt.Rows[0]["monthDay"] : "--").Append("\",");
  447. tempDt.DefaultView.Sort = "minFlow desc";
  448. tempDt.AcceptChanges();
  449. sbText.Append("\"min_" + deviceId + "\":\"").Append(tempDt.Rows.Count > 0 ? time.Year + "-" + time.Month + "-" + tempDt.Rows[0]["monthDay"] : "--").Append("\"");
  450. if (i < dt.Rows.Count - 1) sbText.Append(",");
  451. }
  452. sbText.Append("},");
  453. sbText.Append("{");
  454. sbText.Append("\"showtime\":\"").Append("变化系数").Append("\",");
  455. //一月中的 变化系数 最大值/平均值
  456. for (int i = 0; i < dt.Rows.Count; i++)
  457. {
  458. string deviceId = dt.Rows[i]["MeterAssessmentId"].ToString();
  459. sbText.Append("\"avg_" + deviceId + "\":\"").Append((maxAvg / avgAvg).ToString("0.00")).Append("\",");
  460. sbText.Append("\"max_" + deviceId + "\":\"").Append((maxMax / avgMax).ToString("0.00")).Append("\",");
  461. sbText.Append("\"min_" + deviceId + "\":\"").Append((maxMin / avgMin).ToString("0.00")).Append("\"");
  462. if (i < dt.Rows.Count - 1) sbText.Append(",");
  463. }
  464. sbText.Append("}");
  465. #endregion
  466. return sbText;
  467. }
  468. /// <summary>
  469. /// 压力年报表获取
  470. /// </summary>
  471. /// <param name="dt"></param>
  472. /// <param name="time"></param>
  473. /// <returns></returns>
  474. public StringBuilder GetYearReportData(DataTable dt, DateTime time)
  475. {
  476. StringBuilder sbText = new StringBuilder();
  477. DataTable[] dtSources = new DataTable[dt.Rows.Count];
  478. int numCount = 0;
  479. for (int i = 0; i < 12; i++)
  480. {
  481. sbText.Append("{");
  482. sbText.Append("\"showtime\":\"").Append(time.AddMonths(i).ToString("yyyy-MM")).Append("\",");
  483. for (int row = 0; row < dt.Rows.Count; row++)
  484. {
  485. string deviceId = dt.Rows[row]["MeterAssessmentId"].ToString();
  486. string tableName = "MeterAssessment_" + deviceId;
  487. 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)";
  488. DataTable dtResult;
  489. //结果存储 避免大量访问数据库
  490. if (dtSources[row] != null)
  491. {
  492. dtResult = dtSources[row];
  493. }
  494. else
  495. {
  496. dtResult = sqlHelper.ExecuteDataTable(selSql, CommandType.Text, null);
  497. dtSources[row] = dtResult;
  498. }
  499. DataRow[] drs = dtResult.Select("monthDay=" + (i + 1));
  500. if (drs.Count() > 0)
  501. {
  502. numCount++;
  503. sbText.Append("\"avg_" + deviceId + "\":\"").Append(drs[0]["avgFlow"]).Append("\",");
  504. sbText.Append("\"max_" + deviceId + "\":\"").Append(drs[0]["maxFlow"]).Append("\",");
  505. sbText.Append("\"min_" + deviceId + "\":\"").Append(drs[0]["maxFlow"]).Append("\"");
  506. }
  507. else
  508. {
  509. sbText.Append("\"avg_" + deviceId + "\":\"").Append("--").Append("\",");
  510. sbText.Append("\"max_" + deviceId + "\":\"").Append("--").Append("\",");
  511. sbText.Append("\"min_" + deviceId + "\":\"").Append("--").Append("\"");
  512. }
  513. if (row < dt.Rows.Count - 1) sbText.Append(",");
  514. }
  515. sbText.Append("},");
  516. //if (i < days - 1) sbText.Append(",");
  517. }
  518. #region 平均值 最小值 最大值
  519. double maxAvg = 0;
  520. double avgAvg = 0;
  521. double maxMax = 0;
  522. double avgMax = 0;
  523. double maxMin = 0;
  524. double avgMin = 0;
  525. sbText.Append("{");
  526. sbText.Append("\"showtime\":\"").Append("平均值").Append("\",");
  527. //一月中的 平均值
  528. for (int i = 0; i < dt.Rows.Count; i++)
  529. {
  530. DataTable tempDt = dtSources[i];
  531. var avg_avg = tempDt.Compute("avg(avgFlow)", "");
  532. var avg_max = tempDt.Compute("avg(maxFlow)", "");
  533. var avg_min = tempDt.Compute("avg(maxFlow)", "");
  534. avgAvg = Convert.ToDouble(avg_avg);
  535. avgMax = Convert.ToDouble(avg_max);
  536. avgMin = Convert.ToDouble(avg_min);
  537. string deviceId = dt.Rows[i]["MeterAssessmentId"].ToString();
  538. sbText.Append("\"avg_" + deviceId + "\":\"").Append(avg_avg).Append("\",");
  539. sbText.Append("\"max_" + deviceId + "\":\"").Append(avg_max).Append("\",");
  540. sbText.Append("\"min_" + deviceId + "\":\"").Append(avg_min).Append("\"");
  541. if (i < dt.Rows.Count - 1) sbText.Append(",");
  542. }
  543. sbText.Append("},");
  544. sbText.Append("{");
  545. sbText.Append("\"showtime\":\"").Append("最小值").Append("\",");
  546. //一月中的 最小值
  547. for (int i = 0; i < dt.Rows.Count; i++)
  548. {
  549. string deviceId = dt.Rows[i]["MeterAssessmentId"].ToString();
  550. DataTable tempDt = dtSources[i];
  551. tempDt.DefaultView.Sort = "avgFlow";
  552. tempDt.AcceptChanges();
  553. sbText.Append("\"avg_" + deviceId + "\":\"").Append(tempDt.Rows.Count > 0 ? tempDt.Rows[0]["avgFlow"] : "--").Append("\",");
  554. tempDt.DefaultView.Sort = "maxFlow";
  555. tempDt.AcceptChanges();
  556. sbText.Append("\"max_" + deviceId + "\":\"").Append(tempDt.Rows.Count > 0 ? tempDt.Rows[0]["maxFlow"] : "--").Append("\",");
  557. tempDt.DefaultView.Sort = "minFlow";
  558. tempDt.AcceptChanges();
  559. sbText.Append("\"min_" + deviceId + "\":\"").Append(tempDt.Rows.Count > 0 ? tempDt.Rows[0]["minFlow"] : "--").Append("\"");
  560. if (i < dt.Rows.Count - 1) sbText.Append(",");
  561. }
  562. sbText.Append("},");
  563. sbText.Append("{");
  564. sbText.Append("\"showtime\":\"").Append("最小值时间").Append("\",");
  565. //一月中的 最小值时间
  566. for (int i = 0; i < dt.Rows.Count; i++)
  567. {
  568. string deviceId = dt.Rows[i]["MeterAssessmentId"].ToString();
  569. DataTable tempDt = dtSources[i];
  570. tempDt.DefaultView.Sort = "avgFlow";
  571. tempDt.AcceptChanges();
  572. sbText.Append("\"avg_" + deviceId + "\":\"").Append(tempDt.Rows.Count > 0 ? time.Year + "-" + tempDt.Rows[0]["monthDay"] : "--").Append("\",");
  573. tempDt.DefaultView.Sort = "maxFlow";
  574. tempDt.AcceptChanges();
  575. sbText.Append("\"max_" + deviceId + "\":\"").Append(tempDt.Rows.Count > 0 ? time.Year + "-" + tempDt.Rows[0]["monthDay"] : "--").Append("\",");
  576. tempDt.DefaultView.Sort = "minFlow";
  577. tempDt.AcceptChanges();
  578. sbText.Append("\"min_" + deviceId + "\":\"").Append(tempDt.Rows.Count > 0 ? time.Year + "-" + tempDt.Rows[0]["monthDay"] : "--").Append("\"");
  579. if (i < dt.Rows.Count - 1) sbText.Append(",");
  580. }
  581. sbText.Append("},");
  582. sbText.Append("{");
  583. sbText.Append("\"showtime\":\"").Append("最大值").Append("\",");
  584. //一月中的 最大值
  585. for (int i = 0; i < dt.Rows.Count; i++)
  586. {
  587. string deviceId = dt.Rows[i]["MeterAssessmentId"].ToString();
  588. DataTable tempDt = dtSources[i];
  589. tempDt.DefaultView.Sort = "avgFlow desc";
  590. tempDt.AcceptChanges();
  591. sbText.Append("\"avg_" + deviceId + "\":\"").Append(tempDt.Rows.Count > 0 ? tempDt.Rows[0]["avgFlow"] : "--").Append("\",");
  592. maxAvg = tempDt.Rows.Count > 0 ? Convert.ToDouble(tempDt.Rows[0]["avgFlow"].ToString()) : 0;
  593. tempDt.DefaultView.Sort = "maxFlow desc";
  594. tempDt.AcceptChanges();
  595. sbText.Append("\"max_" + deviceId + "\":\"").Append(tempDt.Rows.Count > 0 ? tempDt.Rows[0]["maxFlow"] : "--").Append("\",");
  596. maxMax = tempDt.Rows.Count > 0 ? Convert.ToDouble(tempDt.Rows[0]["maxFlow"].ToString()) : 0;
  597. tempDt.DefaultView.Sort = "minFlow desc";
  598. tempDt.AcceptChanges();
  599. sbText.Append("\"min_" + deviceId + "\":\"").Append(tempDt.Rows.Count > 0 ? tempDt.Rows[0]["maxFlow"] : "--").Append("\"");
  600. maxMin = tempDt.Rows.Count > 0 ? Convert.ToDouble(tempDt.Rows[0]["maxFlow"].ToString()) : 0;
  601. if (i < dt.Rows.Count - 1) sbText.Append(",");
  602. }
  603. sbText.Append("},");
  604. sbText.Append("{");
  605. sbText.Append("\"showtime\":\"").Append("最大值时间").Append("\",");
  606. //一月中的 最小值时间
  607. for (int i = 0; i < dt.Rows.Count; i++)
  608. {
  609. string deviceId = dt.Rows[i]["MeterAssessmentId"].ToString();
  610. DataTable tempDt = dtSources[i];
  611. tempDt.DefaultView.Sort = "avgFlow desc";
  612. tempDt.AcceptChanges();
  613. sbText.Append("\"avg_" + deviceId + "\":\"").Append(tempDt.Rows.Count > 0 ? time.Year + "-" + tempDt.Rows[0]["monthDay"] : "--").Append("\",");
  614. tempDt.DefaultView.Sort = "maxFlow desc";
  615. tempDt.AcceptChanges();
  616. sbText.Append("\"max_" + deviceId + "\":\"").Append(tempDt.Rows.Count > 0 ? time.Year + "-" + tempDt.Rows[0]["monthDay"] : "--").Append("\",");
  617. tempDt.DefaultView.Sort = "minFlow desc";
  618. tempDt.AcceptChanges();
  619. sbText.Append("\"min_" + deviceId + "\":\"").Append(tempDt.Rows.Count > 0 ? time.Year + "-" + tempDt.Rows[0]["monthDay"] : "--").Append("\"");
  620. if (i < dt.Rows.Count - 1) sbText.Append(",");
  621. }
  622. sbText.Append("},");
  623. sbText.Append("{");
  624. sbText.Append("\"showtime\":\"").Append("变化系数").Append("\",");
  625. //一月中的 变化系数 最大值/平均值
  626. for (int i = 0; i < dt.Rows.Count; i++)
  627. {
  628. string deviceId = dt.Rows[i]["MeterAssessmentId"].ToString();
  629. sbText.Append("\"avg_" + deviceId + "\":\"").Append((maxAvg / avgAvg).ToString("0.00")).Append("\",");
  630. sbText.Append("\"max_" + deviceId + "\":\"").Append((maxMax / avgMax).ToString("0.00")).Append("\",");
  631. sbText.Append("\"min_" + deviceId + "\":\"").Append((maxMin / avgMin).ToString("0.00")).Append("\"");
  632. if (i < dt.Rows.Count - 1) sbText.Append(",");
  633. }
  634. sbText.Append("}");
  635. #endregion
  636. return sbText;
  637. }
  638. }
  639. }