RealDataService.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using LeaRun.Application.Entity.WaterWellManage;
  2. using LeaRun.Data;
  3. using LeaRun.Util;
  4. using LeaRun.Util.WebControl;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Configuration;
  8. using System.Data;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace LeaRun.Application.Service.WaterWellManage
  13. {
  14. public class RealDataService
  15. {
  16. SqlHelper sqlHelper = new SqlHelper("WaterWellDb");
  17. #region 查询实时数据
  18. /// <summary>
  19. /// 查询实时数据
  20. /// </summary>
  21. /// <param name="pagination"></param>
  22. /// <param name="keyword"></param>
  23. /// <param name="value"></param>
  24. /// <param name="isAll"></param>
  25. /// <returns></returns>
  26. public string GetRealData(Pagination pagination, string keyword, string value, bool isAll)
  27. {
  28. var watch = CommonHelper.TimerStart();
  29. var where = " Where 1=1 ";
  30. StringBuilder sb = new StringBuilder();
  31. if ("".Equals(value) && !isAll)
  32. {
  33. var JsonData = new
  34. {
  35. rows = "",
  36. total = pagination.total,
  37. page = pagination.page,
  38. records = pagination.records,
  39. costtime = CommonHelper.TimerEnd(watch)
  40. };
  41. return JsonData.ToJson();
  42. }
  43. else
  44. {
  45. if (!isAll)
  46. {
  47. string[] arr = value.Split(',');
  48. string[] nodeArr = new string[arr.Length - 1];
  49. for (int i = 0; i < arr.Length - 1; i++)
  50. {
  51. nodeArr[i] = arr[i].Replace("dev", "");
  52. }
  53. sb.Append("where waterWellid in (");
  54. for (int i = 0; i < nodeArr.Length; i++)
  55. {
  56. sb.Append("'" + nodeArr[i] + "'");
  57. if (i < nodeArr.Length - 1)
  58. {
  59. sb.Append(", ");
  60. }
  61. }
  62. if (!"".Equals(keyword))
  63. {
  64. sb.Append(") and waterWellName like '%" + keyword + "%'");
  65. }
  66. else
  67. {
  68. sb.Append(")");
  69. }
  70. where = sb.ToString();
  71. }
  72. List<RealDataEntity> list = GetDataByTableName(pagination, keyword, where);
  73. var JsonData = new
  74. {
  75. rows = list,
  76. total = pagination.total,
  77. page = pagination.page,
  78. records = pagination.records,
  79. costtime = CommonHelper.TimerEnd(watch)
  80. };
  81. return JsonData.ToJson();
  82. }
  83. }
  84. #endregion
  85. #region 查询出表中所有的数据
  86. /// <summary>
  87. /// 查询出表中所有的数据
  88. /// </summary>
  89. /// <param name="keyword"></param>
  90. /// <param name="nodes"></param>
  91. /// <param name="isAll"></param>
  92. /// <returns></returns>
  93. private List<RealDataEntity> GetDataByTableName(Pagination pagination, string keyword, string where)
  94. {
  95. string sql = " select count(1) from WaterWellBase " + where;
  96. pagination.records = Convert.ToInt32(sqlHelper.ExecuteScalar(sql, CommandType.Text, null));
  97. int start = (pagination.page - 1) * pagination.rows + 1;
  98. int end = pagination.page * pagination.rows;
  99. sql = "SELECT * FROM " +
  100. "( " +
  101. "SELECT " +
  102. " ROW_NUMBER() OVER(ORDER BY GetDateTime) AS RowIndex," +
  103. " * " +
  104. "FROM " +
  105. " (select * " +
  106. " From WaterWellBase " + where + ") as mytable " +
  107. " )" +
  108. " AS PageTable " +
  109. " WHERE RowIndex BETWEEN " + start + " AND " + end;
  110. try
  111. {
  112. DataTable dt = sqlHelper.ExecuteDataTable(sql, CommandType.Text, null);
  113. if (dt.Rows.Count == 0)
  114. {
  115. return null;
  116. }
  117. List<RealDataEntity> list = DataHelper.DataTableToT<RealDataEntity>(dt);
  118. return list;
  119. }
  120. catch (Exception e)
  121. {
  122. throw e;
  123. }
  124. }
  125. #endregion
  126. }
  127. }