DeviceService.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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.Entity.SecondaryWaterSupply;
  7. using LeaRun.Application.IService.SecondaryWaterSupply;
  8. using LeaRun.Data;
  9. using System.Data;
  10. using LeaRun.Util.WebControl;
  11. using LeaRun.Util;
  12. namespace LeaRun.Application.Service.SecondaryWaterSupply
  13. {
  14. public class DeviceService : IDeviceService
  15. {
  16. SqlHelper sqlHelper = new SqlHelper("SecondyWaterSupplyDb");//二次洪水
  17. /// <summary>
  18. /// 保存设备信息
  19. /// </summary>
  20. /// <param name="device"></param>
  21. /// <returns></returns>
  22. public bool SaveDevice(DeviceEntity device)
  23. {
  24. try
  25. {
  26. string insertSql = "INSERT INTO SecondaryWaterSupplyBase(PumpHouseName,ManufacturerCode,PumpingStationNumber,longitude,latitude)VALUES('"+device.PumpHouseName+"', '"+
  27. device.ManufacturerCode+"', '"+device.PumpingStationNumber+"', '"+device.Longitude+"', '"+ device.Latitude+"')";
  28. int result = sqlHelper.ExecuteNoParams(insertSql, CommandType.Text);
  29. return result > 0;
  30. }
  31. catch (Exception ex)
  32. {
  33. return false;
  34. }
  35. }
  36. /// <summary>
  37. /// 获取设备列表
  38. /// </summary>
  39. /// <param name="pagination">分页参数</param>
  40. /// <param name="keyWord">查询关键字</param>
  41. /// <returns></returns>
  42. public IEnumerable<DeviceEntity> GetDeviceList(Pagination pagination , string keyWord)
  43. {
  44. try
  45. {
  46. string selSql = "SELECT * FROM SecondaryWaterSupplyBase WHERE DeleteMark=0";
  47. int total = pagination.records;
  48. DataTable dtResult = sqlHelper.ExecuteDataTable(selSql, "SecondaryWaterSupplyId", true, pagination.rows, pagination.page, out total, null);
  49. pagination.records = total;
  50. return DataHelper.DataTableToT<DeviceEntity>(dtResult);
  51. }
  52. catch (Exception ex)
  53. {
  54. return null;
  55. }
  56. }
  57. /// <summary>
  58. /// 获取所有设备列表
  59. /// </summary>
  60. /// <returns></returns>
  61. public IEnumerable<DeviceEntity> GetAllDevice()
  62. {
  63. try
  64. {
  65. string selSql = "SELECT * FROM SecondaryWaterSupplyBase WHERE DeleteMark=0";
  66. DataTable dtResult = sqlHelper.ExecuteDataTable(selSql, CommandType.Text, null);
  67. return DataHelper.DataTableToT<DeviceEntity>(dtResult);
  68. }
  69. catch (Exception ex)
  70. {
  71. return null;
  72. }
  73. }
  74. /// <summary>
  75. /// 更新设备信息
  76. /// </summary>
  77. /// <param name="device"></param>
  78. /// <returns></returns>
  79. public bool UpdateDevice(DeviceEntity device)
  80. {
  81. try
  82. {
  83. string updataSql = "UPDATE SecondaryWaterSupplyBase SET PumpHouseName = '" + device.PumpHouseName + "', ManufacturerCode ='" + device.ManufacturerCode
  84. + "', PumpingStationNumber ='" + device.PumpingStationNumber + "', longitude ='" + device.Longitude + "', latitude ='"+ device.Latitude
  85. + "' WHERE SecondaryWaterSupplyId= " + device.SecondaryWaterSupplyId;
  86. int result = sqlHelper.ExecuteNoParams(updataSql, CommandType.Text);
  87. return result > 0;
  88. }
  89. catch (Exception ex)
  90. {
  91. return false;
  92. }
  93. }
  94. /// <summary>
  95. /// 获取单一表设备信息
  96. /// </summary>
  97. /// <param name="SecondaryWaterSupplyId"></param>
  98. /// <returns></returns>
  99. public DeviceEntity GetDeviceById(string SecondaryWaterSupplyId)
  100. {
  101. try
  102. {
  103. string selSql = "SELECT * FROM SecondaryWaterSupplyBase WHERE SecondaryWaterSupplyId= " + SecondaryWaterSupplyId;
  104. DataTable dtResult = sqlHelper.ExecuteDataTable(selSql, CommandType.Text, null);
  105. List<DeviceEntity> lists = DataHelper.DataTableToT<DeviceEntity>(dtResult);
  106. return lists[0];
  107. }
  108. catch (Exception ex)
  109. {
  110. return null;
  111. }
  112. }
  113. /// <summary>
  114. /// 删除设备信息
  115. /// </summary>
  116. /// <param name="SecondaryWaterSupplyId"></param>
  117. /// <returns></returns>
  118. public bool DeleteDevice(string SecondaryWaterSupplyId)
  119. {
  120. try
  121. {
  122. string delSql = "UPDATE SecondaryWaterSupplyBase SET DeleteMark=1 WHERE SecondaryWaterSupplyId= " + SecondaryWaterSupplyId;
  123. int result = sqlHelper.ExecuteNoParams(delSql, CommandType.Text);
  124. return result > 0;
  125. }
  126. catch (Exception ex)
  127. {
  128. return false;
  129. }
  130. }
  131. /// <summary>
  132. /// 获取所有设备最新信息
  133. /// </summary>
  134. /// <returns></returns>
  135. public IEnumerable<DeviceEntity> GetAllDeviceData()
  136. {
  137. try
  138. {
  139. string selSql = "SELECT * FROM dbo.SecondaryWaterSupplyBase WHERE DeleteMark=0 ";
  140. DataTable dtDevice = sqlHelper.ExecuteDataTable(selSql, CommandType.Text, null);
  141. StringBuilder hisSql = new StringBuilder();
  142. for (int row = 0; row < dtDevice.Rows.Count; row++)
  143. {
  144. string tabName = dtDevice.Rows[row]["TableName"].ToString();
  145. string deviceName = dtDevice.Rows[row]["PumpHouseName"].ToString();
  146. hisSql.Append("SELECT TOP 1 '" + deviceName + "' as PumpHouseName , * FROM " + tabName + " ORDER BY GetDateTime DESC");
  147. if (row < dtDevice.Rows.Count - 1)
  148. {
  149. hisSql.Append("UNION");
  150. }
  151. }
  152. DataTable dtHis = sqlHelper.ExecuteDataTable(hisSql.ToString(),CommandType.Text, null);
  153. return DataHelper.DataTableToT<DeviceEntity>(dtHis);
  154. }
  155. catch(Exception ex)
  156. {
  157. return null;
  158. }
  159. }
  160. /// <summary>
  161. /// 获取设备实时数据
  162. /// </summary>
  163. /// <param name="ids"></param>
  164. /// <param name="pagination"></param>
  165. /// <returns></returns>
  166. public IEnumerable<DeviceEntity> GetAllDeviceRealTimeData(string ids, Pagination pagination)
  167. {
  168. try
  169. {
  170. string selSql = "SELECT * FROM dbo.SecondaryWaterSupplyBase WHERE SecondaryWaterSupplyId IN (" + ids + ")";
  171. DataTable dtDevice = sqlHelper.ExecuteDataTable(selSql, CommandType.Text, null);
  172. StringBuilder hisSql = new StringBuilder();
  173. for (int row = 0; row < dtDevice.Rows.Count; row++)
  174. {
  175. string tabName = dtDevice.Rows[row]["TableName"].ToString();
  176. string deviceName = dtDevice.Rows[row]["PumpHouseName"].ToString();
  177. hisSql.Append("SELECT TOP 1 '" + deviceName + "' as PumpHouseName , * FROM " + tabName + " ORDER BY GetDateTime DESC");
  178. if (row < dtDevice.Rows.Count - 1)
  179. {
  180. hisSql.Append("UNION");
  181. }
  182. }
  183. int total = pagination.records;
  184. DataTable dtHis = sqlHelper.ExecuteDataTable(hisSql.ToString(), "SecondaryWaterSupplyId", true, pagination.rows, pagination.page, out total, null);
  185. pagination.records = total;
  186. return DataHelper.DataTableToT<DeviceEntity>(dtHis);
  187. }
  188. catch (Exception ex)
  189. {
  190. return null;
  191. }
  192. }
  193. }
  194. }