AutoMeterService.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Data;
  7. using LeaRun.Data;
  8. namespace LeaRun.Application.Service.PipeNetworkManage
  9. {
  10. public class AutoMeterService
  11. {
  12. SqlHelper sqlHelper = new SqlHelper("PipeNetworkDb");
  13. /// <summary>
  14. /// 智能配表数据
  15. /// </summary>
  16. /// <param name="meterId">设备ID</param>
  17. /// <param name="start">开始时间</param>
  18. /// <param name="end">结束时间</param>
  19. /// <returns></returns>
  20. public string AutoMeterData(string meterId, string start, string end)
  21. {
  22. try
  23. {
  24. StringBuilder sbJson = new StringBuilder();
  25. sbJson.Append("[{");
  26. string selSql = "SELECT MeterAssessmentId,TableName, MeterTypeId, MeterAssessmentName FROM dbo.MeterAssessmentBase WHERE MeterAssessmentId = " + meterId;
  27. DataTable dtResult = sqlHelper.ExecuteDataTable(selSql, CommandType.Text, null);
  28. string devName = dtResult.Rows[0]["MeterAssessmentName"].ToString();
  29. string devType = dtResult.Rows[0]["MeterTypeId"].ToString();
  30. string tabName = dtResult.Rows[0]["TableName"].ToString();
  31. string typeSql = "SELECT * FROM MeterType WHERE MeterAssessmentType="+devType;
  32. DataTable dtType = sqlHelper.ExecuteDataTable(typeSql, CommandType.Text, null);
  33. string Q1 = dtType.Rows[0]["Q1"].ToString();//起始流量
  34. string Q2 = dtType.Rows[0]["Q2"].ToString();//分界流量
  35. string Q3 = dtType.Rows[0]["Q3"].ToString();//常用流量
  36. string Q4 = dtType.Rows[0]["Q4"].ToString();//过载流量
  37. sbJson.Append("\"Q1\":\"" + Q1+ "\",\"Q2\":\"" + Q2 + "\",\"Q3\":\"" + Q3 + "\",\"Q4\":\"" + Q4 + "\",\"flowData\":[");
  38. string flowSql = "SELECT InstantaneousFlow,GetDateTime FROM " + tabName+" WHERE InstantaneousFlow<>0 AND GetDateTime BETWEEN '"+start+"' AND '"+end+"'";
  39. DataTable dtFlow = sqlHelper.ExecuteDataTable(flowSql,CommandType.Text,null);
  40. for (int row = 0; row < dtFlow.Rows.Count; row++)
  41. {
  42. sbJson.Append("{");
  43. sbJson.Append("\"flowTime\":\"").Append(Convert.ToDateTime(dtFlow.Rows[row]["GetDateTime"]).ToString("yyyy-MM-dd HH:mm:ss")).Append("\",");
  44. sbJson.Append("\"flow\":\"").Append(dtFlow.Rows[row]["InstantaneousFlow"]).Append("\"");
  45. sbJson.Append("}");
  46. if (row < dtFlow.Rows.Count-1)
  47. sbJson.Append(",");
  48. }
  49. sbJson.Append("]}]");
  50. return sbJson.ToString();
  51. }
  52. catch (Exception ex)
  53. {
  54. return "";
  55. }
  56. }
  57. }
  58. }