HistoryService.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. using LeaRun.Application.IService.PipeNetworkManage;
  2. using LeaRun.Data.Repository;
  3. using LeaRun.Util.WebControl;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Data;
  7. using LeaRun.Util;
  8. using LeaRun.Data;
  9. using LeaRun.Util.Extension;
  10. using System;
  11. using System.Text;
  12. using LeaRun.Util.Enums;
  13. using LeaRun.Application.Entity.PipeNetworkManage;
  14. using System.Data.SqlClient;
  15. namespace LeaRun.Application.Service.PipeNetworkManage
  16. {
  17. /// <summary>
  18. /// 版 本 6.1
  19. /// Copyright (c) 2013-2016 上海力软信息技术有限公司
  20. /// 创 建:administrator
  21. /// 日 期:2017-08-05 09:35
  22. /// 描 述:历史记录模板
  23. /// </summary>
  24. public class HistoryService : IHistoryService
  25. {
  26. //数据操作类
  27. SqlHelper sqlHelper = new SqlHelper("PipeNetworkDb");
  28. /// <summary>
  29. /// 查询设备历史记录
  30. /// </summary>
  31. /// <param name="deviceId">设备ID</param>
  32. /// <param name="startT">开始时间</param>
  33. /// <param name="endT">结束时间</param>
  34. /// <param name="pagination">分页参数</param>
  35. /// <returns></returns>
  36. public string GetGridData(string deviceId, string startT, string endT, Pagination pagination)
  37. {
  38. try
  39. {
  40. string selSql = "Select MeterAssessmentId,MeterAssessmentName From MeterAssessmentBase Where MeterAssessmentId in (" + deviceId + ")";
  41. DataTable dt = sqlHelper.ExecuteDataTable(selSql, CommandType.Text, null);
  42. StringBuilder returnJson = new StringBuilder();
  43. var strSql = new StringBuilder();
  44. for (int i = 0; i < dt.Rows.Count; i++)
  45. {
  46. string id = dt.Rows[i]["MeterAssessmentId"].ToString();
  47. string name = dt.Rows[i]["MeterAssessmentName"].ToString();
  48. string strTableName = "MeterAssessment_" + id;
  49. strSql.Append("Select '" + name + "' as name, * From " + strTableName + " Where GetDateTime between '" + startT + "' and '" + endT + "' ");
  50. if (i < dt.Rows.Count - 1)
  51. strSql.Append(" union ");
  52. }
  53. int total = pagination.records;
  54. DataTable dtResult = sqlHelper.ExecuteDataTable(strSql.ToString(), "MeterAssessmentId", true, pagination.rows, pagination.page, out total, null);
  55. pagination.records = total;
  56. for (int row = 0; row < dtResult.Rows.Count; row++)
  57. {
  58. returnJson.Append("{");
  59. returnJson.Append("\"测点名称\":\"").Append(dtResult.Rows[row]["name"].ToString()).Append("\",");
  60. returnJson.Append("\"采集时间\":\"").Append(dtResult.Rows[row]["GetDateTime"].ToString()).Append("\",");
  61. returnJson.Append("\"瞬时流量\":\"").Append(dtResult.Rows[row]["InstantaneousFlow"].ToString()).Append("\",");
  62. returnJson.Append("\"电池电压\":\"").Append(dtResult.Rows[row]["BatteryVoltageValue"].ToString()).Append("\",");
  63. returnJson.Append("\"正累计流量\":\"").Append(dtResult.Rows[row]["PositiveCumulativeFlow"].ToString()).Append("\",");
  64. returnJson.Append("\"负累计流量\":\"").Append(dtResult.Rows[row]["NegativeCumulativeFlow"].ToString()).Append("\",");
  65. returnJson.Append("\"净累计流量\":\"").Append(dtResult.Rows[row]["NetCumulativeFlow"].ToString()).Append("\",");
  66. returnJson.Append("\"压力\":\"").Append(dtResult.Rows[row]["Pressure"].ToString()).Append("\"");
  67. returnJson.Append("}");
  68. returnJson.Append(",");
  69. }
  70. string returnData = returnJson.ToString();
  71. return "[" + returnData.TrimEnd(',') + "]";
  72. }
  73. catch (Exception ex)
  74. {
  75. throw ex;
  76. }
  77. }
  78. /// <summary>
  79. /// 查询设备历史记录
  80. /// </summary>
  81. /// <param name="deviceId">设备ID</param>
  82. /// <param name="startT">开始时间</param>
  83. /// <param name="endT">结束时间</param>
  84. /// <returns></returns>
  85. public DataTable GetHistoryData(string deviceId, string startT, string endT)
  86. {
  87. try
  88. {
  89. string selSql = "Select MeterAssessmentId,MeterAssessmentName From MeterAssessmentBase Where MeterAssessmentId in (" + deviceId + ")";
  90. DataTable dt = sqlHelper.ExecuteDataTable(selSql, CommandType.Text, null);
  91. StringBuilder returnJson = new StringBuilder();
  92. var strSql = new StringBuilder();
  93. if (dt.Rows.Count > 0)
  94. {
  95. for (int i = 0; i < dt.Rows.Count; i++)
  96. {
  97. string id = dt.Rows[i]["MeterAssessmentId"].ToString();
  98. string name = dt.Rows[i]["MeterAssessmentName"].ToString();
  99. string strTableName = "MeterAssessment_" + id;
  100. strSql.Append("Select '" + name + "' as name, * From " + strTableName + " Where GetDateTime between '" + startT + "' and '" + endT + "'");
  101. if (i < dt.Rows.Count - 1)
  102. strSql.Append(" union ");
  103. }
  104. strSql.Append(" order by MeterAssessmentId,GetDateTime ASC ");
  105. DataTable dtResult = sqlHelper.ExecuteDataTable(strSql.ToString(), CommandType.Text, null);
  106. return dtResult;
  107. }
  108. return null;
  109. }
  110. catch (Exception ex)
  111. {
  112. throw ex;
  113. }
  114. }
  115. /// <summary>
  116. /// 凌晨流量Json
  117. /// </summary>
  118. /// <param name="entity"></param>
  119. /// <param name="dtStart"></param>
  120. /// <param name="flowType">0 入口 1 出口</param>
  121. /// <param name="lineType"></param>
  122. /// <param name="datas"></param>
  123. /// <param name="size"></param>
  124. /// <returns></returns>
  125. public string GetDaybreakJsonData(MeterEntity entity, DateTime dtStart, string lineType, List<string> datas, int size, int flowType)
  126. {
  127. var json = "";
  128. try
  129. {
  130. StringBuilder builder = new StringBuilder();
  131. builder.Append("{\"devName\":\"").Append(entity.MeterAssessmentName).Append("\",\"lineType\":\"").Append(lineType).Append("\",\"flowType\":\"").Append(flowType).Append("\",\"devData\":");
  132. builder.Append("[");
  133. DateTime dtTime = dtStart;
  134. for (int i = 0; i <= size; i++)
  135. {
  136. builder.Append("{");
  137. DateTime dtS = dtStart.AddMinutes(i * 30).AddMinutes(-1);
  138. DateTime dtE = dtStart.AddMinutes(i * 30).AddMinutes(1);
  139. DateTime dtT = dtStart.AddMinutes(i * 30);
  140. var sql = "Select Top 1 InstantaneousFlow From " + entity.TableName + " Where InstantaneousFlow is not null And GetDateTime >='" + dtS.ToString("yyyy-MM-dd HH:mm") + "' And GetDateTime <='" + dtE.ToString("yyyy-MM-dd HH:mm") + "'";
  141. DataTable dt = sqlHelper.ExecuteDataTable(sql, CommandType.Text, null);
  142. if (dt.Rows.Count > 0)
  143. {
  144. builder.Append("\"Flow\":").Append(dt.Rows[0]["InstantaneousFlow"].ToString()).Append(",\"RTime\":\"").Append(dtT.ToString("yyyy-MM-dd HH:mm")).Append("\"");
  145. datas.Add(dt.Rows[0]["InstantaneousFlow"].ToString());
  146. }
  147. else
  148. {
  149. builder.Append("\"Flow\":\"-\"").Append(",\"RTime\":\"").Append(dtT.ToString("yyyy-MM-dd HH:mm")).Append("\"");
  150. datas.Add("-");
  151. }
  152. builder.Append("}");
  153. if (i < size)
  154. {
  155. builder.Append(",");
  156. }
  157. }
  158. builder.Append("]}");
  159. json = builder.ToString();
  160. }
  161. catch (Exception ex)
  162. {
  163. json = "";
  164. }
  165. return json;
  166. }
  167. /// <summary>
  168. /// 获取某一段时间的流量
  169. /// </summary>
  170. /// <param name="entity"></param>
  171. /// <param name="dtStart"></param>
  172. /// <param name="dtEnd"></param>
  173. /// <returns></returns>
  174. public string GetInsData(MeterEntity entity, string dtStart, string dtEnd)
  175. {
  176. var json = "0";
  177. try
  178. {
  179. var sql = "Select (MAX(NetCumulativeFlow)-MIN(NetCumulativeFlow)) NetCumulativeFlow From " + entity.TableName + " Where InstantaneousFlow is not null And InstantaneousFlow<>0 And GetDateTime >='" + dtStart + "' And GetDateTime <='" + dtEnd + "'";
  180. DataTable dt = sqlHelper.ExecuteDataTable(sql, CommandType.Text, null);
  181. if (dt.Rows.Count > 0)
  182. {
  183. json= dt.Rows[0]["NetCumulativeFlow"].ToString();
  184. }
  185. }
  186. catch (Exception ex)
  187. {
  188. json = "";
  189. }
  190. return json==""?"0":json;
  191. }
  192. /// <summary>
  193. /// 获取某一段时间的流量
  194. /// </summary>
  195. /// <param name="entity"></param>
  196. /// <param name="dtStart"></param>
  197. /// <param name="dtEnd"></param>
  198. /// <returns></returns>
  199. public string GetInsData(MeterEntity entity, string dtStart, string dtEnd,List<string> datas)
  200. {
  201. var json = "";
  202. try
  203. {
  204. var sql = "Select (MAX(NetCumulativeFlow)-MIN(NetCumulativeFlow)) NetCumulativeFlow From " + entity.TableName + " Where InstantaneousFlow is not null And InstantaneousFlow<>0 And GetDateTime >='" + dtStart + "' And GetDateTime <='" + dtEnd + "'";
  205. DataTable dt = sqlHelper.ExecuteDataTable(sql, CommandType.Text, null);
  206. if (dt.Rows.Count > 0)
  207. {
  208. datas.Add(dt.Rows[0]["NetCumulativeFlow"].ToString());
  209. }
  210. }
  211. catch (Exception ex)
  212. {
  213. json = "";
  214. }
  215. return json;
  216. }
  217. /// <summary>
  218. /// 获取K线数据
  219. /// </summary>
  220. /// <param name="devId"></param>
  221. /// <param name="start"></param>
  222. /// <param name="end"></param>
  223. /// <param name="interval"></param>
  224. /// <returns></returns>
  225. public string GetFlowKData(string devId, string start, string end, string interval)
  226. {
  227. var json = "[]";
  228. try
  229. {
  230. SqlParameter[] param = new SqlParameter[] {
  231. new SqlParameter("@sTime",Convert.ToDateTime(start)),
  232. new SqlParameter("@eTime",Convert.ToDateTime(end)),
  233. new SqlParameter("@devId",devId),
  234. new SqlParameter("@interval",Convert.ToInt32(interval)),
  235. };
  236. DataTable dt = sqlHelper.ExecuteDataTable("dbo.Proc_GetFlowKData", CommandType.StoredProcedure, param);
  237. json = Json.ToJson(dt);
  238. }
  239. catch (Exception ex)
  240. {
  241. json = "[]";
  242. }
  243. return json;
  244. }
  245. }
  246. }