DayReportService.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using LeaRun.Application.Entity.WaterWellManage;
  2. using LeaRun.Data;
  3. using LeaRun.Util;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Data;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace LeaRun.Application.Service.WaterWellManage
  12. {
  13. /// <summary>
  14. /// 水源井日报表
  15. /// </summary>
  16. public class DayReportService
  17. {
  18. SqlHelper sqlHelper = new SqlHelper("WaterWellDb");
  19. #region 查询一日数据
  20. /// <summary>
  21. /// 查询一日数据
  22. /// </summary>
  23. /// <param name="waterWellId"></param>
  24. /// <param name="date"></param>
  25. /// <returns></returns>
  26. public List<DayReportEntity> GetDayReport(string waterWellId, string date)
  27. {
  28. if(string.IsNullOrEmpty(waterWellId.Trim())) {
  29. return new List<DayReportEntity>();
  30. }
  31. try
  32. {
  33. StringBuilder sb = new StringBuilder(@"
  34. select
  35. WaterWellId,
  36. Convert(Char(10), GetDateTime ,23) Day,
  37. CONVERT(varchar(20), datepart(HH,getDateTime), 0) + '时' Time,
  38. Convert(decimal(18,2), MAX(VoltageA)) MaxVoltageA ,
  39. Convert(decimal(18,2), MIN(VoltageA)) MinVoltageA,
  40. Convert(decimal(18,2), AVG(VoltageA)) AvgVoltageA,
  41. Convert(decimal(18,2), MAX(VoltageB)) MaxVoltageB,
  42. Convert(decimal(18,2), MIN(VoltageB)) MinVoltageB,
  43. Convert(decimal(18,2), AVG(VoltageB)) AvgVoltageB,
  44. Convert(decimal(18,2), MAX(VoltageC)) MaxVoltageC,
  45. Convert(decimal(18,2), MIN(VoltageC)) MinVoltageC,
  46. Convert(decimal(18,2), AVG(VoltageC)) AvgVoltageC,
  47. Convert(decimal(18,2), MAX(CurrentA)) MaxCurrentA,
  48. Convert(decimal(18,2), MIN(CurrentA)) MinCurrentA,
  49. Convert(decimal(18,2), AVG(CurrentA)) AvgCurrentA,
  50. Convert(decimal(18,2), MAX(CurrentB)) MaxCurrentB,
  51. Convert(decimal(18,2), MIN(CurrentB)) MinCurrentB,
  52. Convert(decimal(18,2), AVG(CurrentB)) AvgCurrentB,
  53. Convert(decimal(18,2), MAX(CurrentC)) MaxCurrentC,
  54. Convert(decimal(18,2), MIN(CurrentC)) MinCurrentC,
  55. Convert(decimal(18,2), AVG(CurrentC)) AvgCurrentC,
  56. Convert(decimal(18,2), MAX(ActiveElectricalEnergy)) MaxActiveElectricalEnergy,
  57. Convert(decimal(18,2), MIN(ActiveElectricalEnergy)) MinActiveElectricalEnergy,
  58. Convert(decimal(18,2), MAX(ActiveElectricalEnergy) - MIN(ActiveElectricalEnergy)) IncrementActiveElectricalEnergy,
  59. Convert(decimal(18,2), MAX(NetAccumulatedCurrent)) MaxNetAccumulatedCurrent,
  60. Convert(decimal(18,2), MIN(NetAccumulatedCurrent)) MinNetAccumulatedCurrent,
  61. Convert(decimal(18,2), MAX(NetAccumulatedCurrent) - MIN(NetAccumulatedCurrent)) IncrementNetAccumulatedCurrent,
  62. Convert(decimal(18,2), MAX(Pressure)) MaxPressure,
  63. Convert(decimal(18,2), MIN(Pressure)) MinPressure,
  64. Convert(decimal(18,2), AVG(Pressure)) AvgPressure,
  65. Convert(decimal(18,2), MAX(InstantaneousFlow)) MaxInstantaneousFlow,
  66. Convert(decimal(18,2), MIN(InstantaneousFlow)) MinInstantaneousFlow,
  67. Convert(decimal(18,2), AVG(InstantaneousFlow)) AvgInstantaneousFlow,
  68. Convert(decimal(18,2), MAX(Turbidity)) MaxTurbidity,
  69. Convert(decimal(18,2), MIN(Turbidity)) MinTurbidity,
  70. Convert(decimal(18,2), AVG(Turbidity)) AvgTurbidity,
  71. Convert(decimal(18,2), MAX(Temperature)) MaxTemperature,
  72. Convert(decimal(18,2), MIN(Temperature)) MinTemperature,
  73. Convert(decimal(18,2), AVG(Temperature)) AvgTemperature,
  74. Convert(decimal(18,2), MAX(HeightOfLiquidLevel)) MaxHeightOfLiquidLevel,
  75. Convert(decimal(18,2), MIN(HeightOfLiquidLevel)) MinHeightOfLiquidLevel,
  76. Convert(decimal(18,2), AVG(HeightOfLiquidLevel)) AvgHeightOfLiquidLevel,
  77. Convert(decimal(18,2), MAX(Frequency)) MaxFrequency,
  78. Convert(decimal(18,2), MIN(Frequency)) MinFrequency,
  79. Convert(decimal(18,2), AVG(Frequency)) AvgFrequency
  80. from ");
  81. sb.Append("WaterWell_" + waterWellId);
  82. sb.Append(" where Convert(Char(10), GetDateTime ,23) = '" + date + "' ");
  83. sb.Append(@"
  84. group by datepart(HH,getDateTime), WaterWellId, Convert(Char(10), GetDateTime ,23)
  85. order by datepart(HH,getDateTime) asc ");
  86. DataTable dt = sqlHelper.ExecuteDataTable(sb.ToString(), CommandType.Text, null);
  87. if (dt.Rows.Count == 0)
  88. {
  89. return AnalysisData(new List<DayReportEntity>());
  90. }
  91. else
  92. {
  93. List<DayReportEntity> list = DataHelper.DataTableToT<DayReportEntity>(dt);
  94. return AnalysisData(list);
  95. }
  96. }
  97. catch (Exception)
  98. {
  99. throw;
  100. }
  101. }
  102. #endregion
  103. #region 格式化数据
  104. /// <summary>
  105. /// 格式化数据
  106. /// </summary>
  107. /// <param name="list"></param>
  108. /// <returns></returns>
  109. private List<DayReportEntity> AnalysisData(List<DayReportEntity> list)
  110. {
  111. List<DayReportEntity> listArr = new List<DayReportEntity>();
  112. for (int i = 0; i < 24; i++)
  113. {
  114. bool isAdd = false;
  115. for (int j = 0; j < list.Count; j++)
  116. {
  117. if (list[j].Time.Equals(i + "时"))
  118. {
  119. listArr.Add(list[j]);
  120. isAdd = true;
  121. }
  122. }
  123. if (!isAdd)
  124. {
  125. DayReportEntity dayReportEntity = new DayReportEntity();
  126. dayReportEntity.Time = i + "时";
  127. dayReportEntity.MaxVoltageA = "--";
  128. dayReportEntity.MinVoltageA = "--";
  129. dayReportEntity.AvgVoltageA = "--";
  130. dayReportEntity.MaxVoltageB = "--";
  131. dayReportEntity.MinVoltageB = "--";
  132. dayReportEntity.AvgVoltageB = "--";
  133. dayReportEntity.MaxVoltageC = "--";
  134. dayReportEntity.MinVoltageC = "--";
  135. dayReportEntity.AvgVoltageC = "--";
  136. dayReportEntity.MaxCurrentA = "--";
  137. dayReportEntity.MinCurrentA = "--";
  138. dayReportEntity.AvgCurrentA = "--";
  139. dayReportEntity.MaxCurrentB = "--";
  140. dayReportEntity.MinCurrentB = "--";
  141. dayReportEntity.AvgCurrentB = "--";
  142. dayReportEntity.MaxCurrentC = "--";
  143. dayReportEntity.MinCurrentC = "--";
  144. dayReportEntity.AvgCurrentC = "--";
  145. dayReportEntity.MaxActiveElectricalEnergy = "--";
  146. dayReportEntity.MinActiveElectricalEnergy = "--";
  147. dayReportEntity.IncrementActiveElectricalEnergy = "--";
  148. dayReportEntity.MaxNetAccumulatedCurrent = "--";
  149. dayReportEntity.MinNetAccumulatedCurrent = "--";
  150. dayReportEntity.IncrementNetAccumulatedCurrent = "--";
  151. dayReportEntity.MaxPressure = "--";
  152. dayReportEntity.MinPressure = "--";
  153. dayReportEntity.AvgPressure = "--";
  154. dayReportEntity.MaxInstantaneousFlow = "--";
  155. dayReportEntity.MinInstantaneousFlow = "--";
  156. dayReportEntity.AvgInstantaneousFlow = "--";
  157. dayReportEntity.MaxTurbidity = "--";
  158. dayReportEntity.MinTurbidity = "--";
  159. dayReportEntity.AvgTurbidity = "--";
  160. dayReportEntity.MaxTemperature = "--";
  161. dayReportEntity.MinTemperature = "--";
  162. dayReportEntity.AvgTemperature = "--";
  163. dayReportEntity.MaxHeightOfLiquidLevel = "--";
  164. dayReportEntity.MinHeightOfLiquidLevel = "--";
  165. dayReportEntity.AvgHeightOfLiquidLevel = "--";
  166. dayReportEntity.MaxFrequency = "--";
  167. dayReportEntity.MinFrequency = "--";
  168. dayReportEntity.AvgFrequency = "--";
  169. listArr.Add(dayReportEntity);
  170. }
  171. }
  172. return listArr;
  173. }
  174. #endregion
  175. #region 获取子数据
  176. /// <summary>
  177. /// 获取子数据
  178. /// </summary>
  179. /// <param name="time"></param>
  180. /// <param name="day"></param>
  181. /// <param name="waterWellId"></param>
  182. /// <returns></returns>
  183. public List<ReportSubEntity> GetSubDayReport(string time, string day, string waterWellId)
  184. {
  185. if (string.IsNullOrEmpty(time.Trim()) || string.IsNullOrEmpty(day.Trim()) || string.IsNullOrEmpty(waterWellId.Trim()))
  186. {
  187. return new List<ReportSubEntity>();
  188. }
  189. StringBuilder sb = new StringBuilder("select * from " + "WaterWell_" + waterWellId);
  190. sb.Append(" where Convert(Char(10), GetDateTime ,23) = '" + day + "' and ");
  191. sb.Append(" datepart(HH,getDateTime) = " + time.Split('时')[0]);
  192. try
  193. {
  194. DataTable dt = sqlHelper.ExecuteDataTable(sb.ToString(), CommandType.Text, null);
  195. if (dt.Rows.Count == 0)
  196. {
  197. return new List<ReportSubEntity>();
  198. }
  199. else
  200. {
  201. List<ReportSubEntity> list = DataHelper.DataTableToT<ReportSubEntity>(dt);
  202. return list;
  203. }
  204. }
  205. catch (Exception)
  206. {
  207. throw;
  208. }
  209. }
  210. #endregion
  211. }
  212. }