MeterService.cs 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  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.IService.PipeNetworkManage;
  7. using System.Data;
  8. using LeaRun.Data;
  9. using LeaRun.Application.Entity.PipeNetworkManage;
  10. using System.Data;
  11. using LeaRun.Util;
  12. using Newtonsoft.Json;
  13. using LeaRun.Util.WebControl;
  14. using LeaRun.Util.Extension;
  15. using LeaRun.Util.Log;
  16. using LeaRun.Application.Service.DMAManage;
  17. namespace LeaRun.Application.Service.PipeNetworkManage
  18. {
  19. public class MeterService : IMeterService
  20. {
  21. SqlHelper sqlHelper = new SqlHelper("PipeNetworkDb");
  22. DifferenceAnalysisService differenceAnalysisService = new DifferenceAnalysisService();
  23. Log log = LogFactory.GetLogger(typeof(MeterService));
  24. #region 表具类型-service
  25. /// <summary>
  26. /// 获取所有的 考核表 类型
  27. /// </summary>
  28. /// <returns></returns>
  29. public string GetMeterType()
  30. {
  31. try
  32. {
  33. string strSql = "SELECT MeterAssessmentType, TypeName, MeterType, Caliber, Q1, Q2, Q3, Q4, [Range], Manufacturer, PressureLoss, ManufacturerPhone, Memo FROM MeterType ";
  34. DataTable dtResult = sqlHelper.ExecuteDataTable(strSql, CommandType.Text, null);
  35. StringBuilder returnJson = new StringBuilder();
  36. for (int row = 0; row < dtResult.Rows.Count; row++)
  37. {
  38. returnJson.Append("{");
  39. returnJson.Append("\"ID\":\"").Append(dtResult.Rows[row]["MeterAssessmentType"].ToString()).Append("\",");
  40. returnJson.Append("\"类型名称\":\"").Append(dtResult.Rows[row]["TypeName"].ToString()).Append("\",");
  41. returnJson.Append("\"类型Value\":\"").Append(dtResult.Rows[row]["MeterType"].ToString()).Append("\",");
  42. returnJson.Append("\"类型\":\"").Append(GetMeterType(dtResult.Rows[row]["MeterType"].ToString())).Append("\",");
  43. returnJson.Append("\"口径\":\"").Append("DN" + dtResult.Rows[row]["Caliber"].ToString()).Append("\",");
  44. returnJson.Append("\"起始流量\":\"").Append(dtResult.Rows[row]["Q1"].ToString()).Append("\",");
  45. returnJson.Append("\"分界流量\":\"").Append(dtResult.Rows[row]["Q2"].ToString()).Append("\",");
  46. returnJson.Append("\"常用流量\":\"").Append(dtResult.Rows[row]["Q3"].ToString()).Append("\",");
  47. returnJson.Append("\"过载流量\":\"").Append(dtResult.Rows[row]["Q4"].ToString()).Append("\",");
  48. returnJson.Append("\"量程\":\"").Append(dtResult.Rows[row]["Range"].ToString()).Append("\",");
  49. returnJson.Append("\"压力损失\":\"").Append(dtResult.Rows[row]["PressureLoss"].ToString()).Append("\",");
  50. returnJson.Append("\"生产厂家\":\"").Append(dtResult.Rows[row]["Manufacturer"].ToString()).Append("\",");
  51. returnJson.Append("\"厂家电话\":\"").Append(dtResult.Rows[row]["ManufacturerPhone"].ToString()).Append("\",");
  52. returnJson.Append("\"备注\":\"").Append(dtResult.Rows[row]["Memo"].ToString()).Append("\"");
  53. returnJson.Append("}");
  54. returnJson.Append(",");
  55. }
  56. string returnData = returnJson.ToString();
  57. return "[" + returnData.TrimEnd(',') + "]";
  58. }
  59. catch (Exception ex)
  60. {
  61. return "";
  62. }
  63. }
  64. /// <summary>
  65. /// 获取指定的 表类型列表
  66. /// </summary>
  67. /// <param name="pagination">分页参数</param>
  68. /// <param name="queryJson">查询条件</param>
  69. /// <returns></returns>
  70. public IEnumerable<MeterTypeEntity> GetMeterType(Pagination pagination, string queryJson)
  71. {
  72. try
  73. {
  74. string strSql = "SELECT * FROM MeterType ";
  75. var queryParam = queryJson.ToJObject();
  76. if (!queryParam["keyword"].IsEmpty())
  77. {
  78. string keyord = queryParam["keyword"].ToString();
  79. strSql += " WHERE TypeName LIKE '%" + keyord + "%'";
  80. }
  81. int total = pagination.records;
  82. DataTable dtResult = sqlHelper.ExecuteDataTable(strSql, "MeterAssessmentType", true, pagination.rows, pagination.page, out total, null);
  83. pagination.records = total;
  84. #region 废弃
  85. //DataTable dtResult = sqlHelper.ExecuteDataTable(strSql, CommandType.Text, null);
  86. //StringBuilder returnJson = new StringBuilder();
  87. //for (int row = 0; row < dtResult.Rows.Count; row++)
  88. //{
  89. // returnJson.Append("{");
  90. // returnJson.Append("\"ID\":\"").Append(dtResult.Rows[row]["MeterAssessmentType"].ToString()).Append("\",");
  91. // returnJson.Append("\"类型名称\":\"").Append(dtResult.Rows[row]["TypeName"].ToString()).Append("\",");
  92. // returnJson.Append("\"类型Value\":\"").Append(dtResult.Rows[row]["MeterType"].ToString()).Append("\",");
  93. // returnJson.Append("\"类型\":\"").Append(GetMeterType(dtResult.Rows[row]["MeterType"].ToString())).Append("\",");
  94. // returnJson.Append("\"口径\":\"").Append("DN" + dtResult.Rows[row]["Caliber"].ToString()).Append("\",");
  95. // returnJson.Append("\"起始流量\":\"").Append(dtResult.Rows[row]["Q1"].ToString()).Append("\",");
  96. // returnJson.Append("\"分界流量\":\"").Append(dtResult.Rows[row]["Q2"].ToString()).Append("\",");
  97. // returnJson.Append("\"常用流量\":\"").Append(dtResult.Rows[row]["Q3"].ToString()).Append("\",");
  98. // returnJson.Append("\"过载流量\":\"").Append(dtResult.Rows[row]["Q4"].ToString()).Append("\",");
  99. // returnJson.Append("\"量程\":\"").Append(dtResult.Rows[row]["Range"].ToString()).Append("\",");
  100. // returnJson.Append("\"压力损失\":\"").Append(dtResult.Rows[row]["PressureLoss"].ToString()).Append("\",");
  101. // returnJson.Append("\"生产厂家\":\"").Append(dtResult.Rows[row]["Manufacturer"].ToString()).Append("\",");
  102. // returnJson.Append("\"厂家电话\":\"").Append(dtResult.Rows[row]["ManufacturerPhone"].ToString()).Append("\",");
  103. // returnJson.Append("\"备注\":\"").Append(dtResult.Rows[row]["Memo"].ToString()).Append("\"");
  104. // returnJson.Append("}");
  105. // returnJson.Append(",");
  106. //}
  107. //string returnData = returnJson.ToString();
  108. //return "[" + returnData.TrimEnd(',') + "]";
  109. #endregion
  110. return DataHelper.DataTableToT<MeterTypeEntity>(dtResult);
  111. }
  112. catch (Exception ex)
  113. {
  114. log.Error(ex.Message);
  115. return null;
  116. }
  117. }
  118. /// <summary>
  119. /// 获取类型列表
  120. /// </summary>
  121. /// <returns></returns>
  122. public IEnumerable<MeterTypeEntity> GetMeterTypeList()
  123. {
  124. try
  125. {
  126. string strSql = "SELECT * FROM MeterType ";
  127. DataTable dtResult = sqlHelper.ExecuteDataTable(strSql, CommandType.Text, null);
  128. return DataHelper.DataTableToT<MeterTypeEntity>(dtResult);
  129. }
  130. catch (Exception ex)
  131. {
  132. return null;
  133. }
  134. }
  135. /// <summary>
  136. /// 获取表具类型
  137. /// </summary>
  138. /// <param name="meterTypeId"></param>
  139. /// <returns></returns>
  140. public MeterTypeEntity GetMeterTypeEntity(int meterTypeId)
  141. {
  142. try
  143. {
  144. string selSql = "SELECT * FROM MeterType WHERE MeterAssessmentType = " + meterTypeId;
  145. DataTable dtResult = sqlHelper.ExecuteDataTable(selSql, CommandType.Text, null);
  146. List<MeterTypeEntity> lists = DataHelper.DataTableToT<MeterTypeEntity>(dtResult);
  147. return lists[0];
  148. }
  149. catch (Exception ex)
  150. {
  151. return null;
  152. }
  153. }
  154. /// <summary>
  155. /// 根据数据库保存的值,获取类型名称
  156. /// </summary>
  157. /// <param name="meterType">类型值</param>
  158. /// <returns>类型名</returns>
  159. private string GetMeterType(string meterType)
  160. {
  161. switch (meterType)
  162. {
  163. case "0":
  164. return "水平旋翼式水表";
  165. case "1":
  166. return "水平螺翼式水表";
  167. case "2":
  168. return "垂直螺翼式水表";
  169. case "3":
  170. return "超声波水表";
  171. case "4":
  172. return "电磁流量计";
  173. default:
  174. return "";
  175. }
  176. }
  177. /// <summary>
  178. /// 保存表具类型
  179. /// </summary>
  180. /// <param name="meterType">类型实体</param>
  181. /// <returns></returns>
  182. public bool SaveMeterType(MeterTypeEntity meterType)
  183. {
  184. try
  185. {
  186. //string insertSql = "INSERT INTO MeterType(TypeName, MeterType, Caliber, Q1, Q2, Q3, Q4, [Range], PressureLoss, Manufacturer, ManufacturerPhone, Memo)Values('" +
  187. // meterType.TypeName + "'," + meterType.MeterType + "," + meterType.Caliber + "," + meterType.Q1 + "," + meterType.Q2 + "," + meterType.Q3 + "," + meterType.Q4 + "," +
  188. // meterType.Range + "," + meterType.PressureLoss + ",'" + meterType.Manufacturer + "','" + meterType.ManufacturerPhone + "','" + meterType.Memo + "')";
  189. string insertSql = LeaRun.Util.DataHelper.ModelToInsertSql<MeterTypeEntity>(meterType, "MeterType");
  190. return sqlHelper.ExecuteNoParams(insertSql, CommandType.Text) == 1 ? true : false;
  191. }
  192. catch (Exception ex)
  193. {
  194. return false;
  195. }
  196. }
  197. /// <summary>
  198. /// 修改表具类型
  199. /// </summary>
  200. /// <param name="meterType">类型实体</param>
  201. /// <returns></returns>
  202. public bool UpdateMeterType(MeterTypeEntity meterType)
  203. {
  204. try
  205. {
  206. //string insertSql = "UPDATE MeterType SET TypeName = '" + meterType.TypeName + "', MeterType= " + meterType.MeterType + ", Caliber= " + meterType.Caliber +
  207. // ", Caliber= " + meterType.Caliber + ", Q1= " + meterType.Q1 + ", Q2= " + meterType.Q2 + ", Q3= " + meterType.Q3 + ", Q4= " + meterType.Q4 +
  208. // ", [Range]= " + meterType.Range + ", PressureLoss= " + meterType.PressureLoss + ", Manufacturer= '" + meterType.Manufacturer + "'," +
  209. // "ManufacturerPhone= '" + meterType.ManufacturerPhone + "'," + "Memo= '" + meterType.Memo + "' WHERE MeterAssessmentType=" + meterType.MeterAssessmentType;
  210. string updateSql = DataHelper.ModelToUpdateSql<MeterTypeEntity>(meterType, "MeterType");
  211. return sqlHelper.ExecuteNoParams(updateSql, CommandType.Text) == 1 ? true : false;
  212. }
  213. catch (Exception ex)
  214. {
  215. return false;
  216. }
  217. }
  218. /// <summary>
  219. /// 删除表数据类型
  220. /// </summary>
  221. /// <param name="meterAssessmentType"></param>
  222. /// <returns></returns>
  223. public string DelteMemterType(int meterAssessmentType)
  224. {
  225. try
  226. {
  227. //删除前判断 考核表有数据 则提示不能删除
  228. string selSql = "SELECT * FROM MeterAssessmentBase WHERE MeterTypeId = " + meterAssessmentType;
  229. DataTable dtResult = sqlHelper.ExecuteDataTable(selSql, CommandType.Text, null);
  230. if (dtResult.Rows.Count > 0)
  231. {
  232. return "此类型下有表数据,不能删除!";
  233. }
  234. string delSql = "DELETE FROM MeterType WHERE MeterAssessmentType=" + meterAssessmentType;
  235. return sqlHelper.ExecuteNoParams(delSql, CommandType.Text) > 0 ? "删除成功" : "删除失败";
  236. }
  237. catch (Exception ex)
  238. {
  239. return "删除失败";
  240. }
  241. }
  242. #endregion
  243. #region 表设备信息-service
  244. /// <summary>
  245. /// 获取表设备信息列表
  246. /// </summary>
  247. /// <returns></returns>
  248. public string GetMeterList(Pagination pagination, string queryJson)
  249. {
  250. string selectSql = "SELECT * FROM dbo.MeterAssessmentBase WHERE DeleteMark=0";//未删除
  251. int total = pagination.records;
  252. DataTable dtResult = sqlHelper.ExecuteDataTable(selectSql, "MeterAssessmentId", true, pagination.rows, pagination.page, out total, null);
  253. pagination.records = total;
  254. //DataTable dtResult = sqlHelper.ExecuteDataTable(selectSql, CommandType.Text, null);
  255. dtResult.Columns.Add("MeterTypeName", System.Type.GetType("System.String"));
  256. // 数据格式化
  257. for (int row = 0; row < dtResult.Rows.Count; row++)
  258. {
  259. int meterTypeId = int.Parse(dtResult.Rows[row]["MeterTypeId"].ToString());
  260. dtResult.Rows[row]["MeterTypeName"] = GetMeterTypeNameById(meterTypeId);
  261. }
  262. return JsonConvert.SerializeObject(dtResult);
  263. }
  264. /// <summary>
  265. /// 获取表设备信息列表
  266. /// </summary>
  267. /// <returns></returns>
  268. public IEnumerable<MeterEntity> GetMeters()
  269. {
  270. try
  271. {
  272. string selectSql = "SELECT * FROM dbo.MeterAssessmentBase WHERE DeleteMark=0";//未删除
  273. DataTable dtResult = sqlHelper.ExecuteDataTable(selectSql, CommandType.Text, null);
  274. return DataHelper.DataTableToT<MeterEntity>(dtResult);
  275. }
  276. catch (Exception ex)
  277. {
  278. return null;
  279. }
  280. }
  281. /// <summary>
  282. /// 获取表具类型名称
  283. /// </summary>
  284. /// <returns></returns>
  285. public string GetMeterTypeNameById(int meterTypeId)
  286. {
  287. try
  288. {
  289. string selectSql = "SELECT TypeName FROM MeterType WHERE MeterAssessmentType= " + meterTypeId;
  290. DataTable dtResult = sqlHelper.ExecuteDataTable(selectSql, CommandType.Text);
  291. return dtResult.Rows[0][0].ToString();
  292. }
  293. catch (Exception ex)
  294. {
  295. return "";
  296. }
  297. }
  298. /// <summary>
  299. /// 保存表设备信息
  300. /// </summary>
  301. /// <param name="meter">表设备实体</param>
  302. /// <returns></returns>
  303. public bool SaveMeter(MeterEntity meter)
  304. {
  305. try
  306. {
  307. //string insertSql = DataHelper.ModelToInsertSql<MeterEntity>(meter, "MeterAssessmentBase");
  308. string insertSql = "INSERT INTO MeterAssessmentBase(MeterAssessmentName,MeterTypeId,ManufacturerCode,MeterAssessmentCode,CompanyId,LngAndLat,IsKaoHe)VALUES('"
  309. + meter.MeterAssessmentName + "'," + meter.MeterTypeId + ",'" + meter.ManufacturerCode + "','" + meter.MeterAssessmentCode + "','" + meter.CompanyId + "','" + meter.LngAndLat + "', " + meter.IsKaoHe + ")";
  310. return sqlHelper.ExecuteNoParams(insertSql, CommandType.Text) > 0 ? true : false;
  311. }
  312. catch (Exception ex)
  313. {
  314. return false;
  315. }
  316. }
  317. /// <summary>
  318. /// 更新表设备信息
  319. /// </summary>
  320. /// <param name="meter">表设备实体</param>
  321. /// <returns></returns>
  322. public bool UpdateMeter(MeterEntity meter)
  323. {
  324. try
  325. {
  326. //string updateSql = DataHelper.ModelToUpdateSql<MeterEntity>(meter, "MeterAssessmentBase");
  327. string updateSql = "UPDATE MeterAssessmentBase SET MeterAssessmentName ='" + meter.MeterAssessmentName
  328. + "' , MeterTypeId=" + meter.MeterTypeId
  329. + " , ManufacturerCode='" + meter.ManufacturerCode
  330. + "' , LngAndLat='" + meter.LngAndLat
  331. + "' , MeterAssessmentCode='" + meter.MeterAssessmentCode + "' ,IsKaoHe = " + meter.IsKaoHe + " WHERE MeterAssessmentId = " + meter.MeterAssessmentId;
  332. return sqlHelper.ExecuteNoParams(updateSql, CommandType.Text) > 0 ? true : false;
  333. }
  334. catch (Exception ex)
  335. {
  336. return false;
  337. }
  338. }
  339. /// <summary>
  340. /// 删除表设备信息
  341. /// </summary>
  342. /// <param name="MeterAssessmentId">设备信息ID</param>
  343. /// <returns></returns>
  344. public bool DeleteMeter(int meterAssessmentId)
  345. {
  346. //标记位更新
  347. try
  348. {
  349. string deleteSql = "UPDATE MeterAssessmentBase SET DeleteMark=1 WHERE MeterAssessmentId=" + meterAssessmentId;
  350. return sqlHelper.ExecuteNoParams(deleteSql, CommandType.Text) > 0 ? true : false;
  351. }
  352. catch (Exception ex)
  353. {
  354. return false;
  355. }
  356. }
  357. /// <summary>
  358. /// 获取表设备信息
  359. /// </summary>
  360. /// <param name="meterAssessmentId"></param>
  361. /// <returns></returns>
  362. public MeterEntity GetMeterEntity(int meterAssessmentId)
  363. {
  364. try
  365. {
  366. string selSql = "SELECT * FROM MeterAssessmentBase WHERE MeterAssessmentId = " + meterAssessmentId;
  367. DataTable dtResult = sqlHelper.ExecuteDataTable(selSql, CommandType.Text, null);
  368. List<MeterEntity> lists = DataHelper.DataTableToT<MeterEntity>(dtResult);
  369. return lists[0];
  370. }
  371. catch (Exception ex)
  372. {
  373. return null;
  374. }
  375. }
  376. #endregion
  377. #region 水务宝 用到的方法
  378. /// <summary>
  379. /// 获取所属公司的表
  380. /// </summary>
  381. /// <param name="companyId">公司ID</param>
  382. /// <param name="isKaoHe">1.考核表 0.贸易表 2.无所谓</param>
  383. /// <returns></returns>
  384. public IEnumerable<MeterEntity> GetMeterEntityByCompanyId(string companyId, int isKaoHe = 2)
  385. {
  386. try
  387. {
  388. string selSql = "SELECT * FROM MeterAssessmentBase WHERE CompanyId = '" + companyId + "'";
  389. if (isKaoHe != 2)
  390. {
  391. selSql += " AND IsKaoHe = " + isKaoHe;
  392. }
  393. DataTable dtResult = sqlHelper.ExecuteDataTable(selSql, CommandType.Text, null);
  394. List<MeterEntity> lists = DataHelper.DataTableToT<MeterEntity>(dtResult);
  395. return lists;
  396. }
  397. catch (Exception ex)
  398. {
  399. return null;
  400. }
  401. }
  402. /// <summary>
  403. /// 特定时间段内,平均压力小于某个值的 设备
  404. /// </summary>
  405. /// <param name="pressValue"></param>
  406. /// <param name="startT"></param>
  407. /// <param name="endT"></param>
  408. /// <returns></returns>
  409. public IEnumerable<MeterEntity> GetMeterByPressLessValue(decimal pressValue, string startT, string endT, IEnumerable<MeterEntity> entities)
  410. {
  411. try
  412. {
  413. List<MeterEntity> returnEntity = new List<MeterEntity>();
  414. foreach (var item in entities)
  415. {
  416. string tableName = item.TableName;
  417. //取历史记录
  418. string selSql = "SELECT avg(Pressure) as avgPressure FROM " + tableName + " WHERE GetDateTime BETWEEN '" + startT + "' AND '" + endT + "' AND Pressure IS NOT NULL AND Pressure <> 0 ";
  419. DataTable dtResult = sqlHelper.ExecuteDataTable(selSql, CommandType.Text, null);
  420. if (dtResult.Rows.Count > 0)
  421. {
  422. if (!string.IsNullOrEmpty(dtResult.Rows[0]["avgPressure"].ToString()))
  423. {
  424. decimal pressure = decimal.Parse(dtResult.Rows[0]["avgPressure"].ToString());
  425. if (pressure <= pressValue)
  426. {
  427. returnEntity.Add(item);
  428. }
  429. }
  430. }
  431. }
  432. return returnEntity;
  433. }
  434. catch (Exception ex)
  435. {
  436. return null;
  437. }
  438. }
  439. /// <summary>
  440. /// 特定时间段内,最小流量小于某个值的 设备
  441. /// </summary>
  442. /// <param name="pressValue"></param>
  443. /// <param name="startT"></param>
  444. /// <param name="endT"></param>
  445. /// <returns></returns>
  446. public IEnumerable<MeterEntity> GetMeterByMinFlowLessValue(decimal flowValue, string startT, string endT, IEnumerable<MeterEntity> entities)
  447. {
  448. try
  449. {
  450. List<MeterEntity> returnEntity = new List<MeterEntity>();
  451. foreach (var item in entities)
  452. {
  453. string tableName = item.TableName;
  454. //取历史记录
  455. string selSql = "SELECT min(InstantaneousFlow) as minInstantaneousFlow FROM " + tableName + " WHERE GetDateTime BETWEEN '" + startT + "' AND '" + endT + "' AND InstantaneousFlow IS NOT NULL AND InstantaneousFlow <> 0 ";
  456. DataTable dtResult = sqlHelper.ExecuteDataTable(selSql, CommandType.Text, null);
  457. if (dtResult.Rows.Count > 0)
  458. {
  459. if (!string.IsNullOrEmpty(dtResult.Rows[0]["minInstantaneousFlow"].ToString()))
  460. {
  461. decimal minFlow = decimal.Parse(dtResult.Rows[0]["minInstantaneousFlow"].ToString());
  462. if (minFlow <= flowValue)
  463. {
  464. returnEntity.Add(item);
  465. }
  466. }
  467. }
  468. }
  469. return returnEntity;
  470. }
  471. catch (Exception ex)
  472. {
  473. return null;
  474. }
  475. }
  476. /// <summary>
  477. /// 特定时间段内,电池电压小于某个值的 设备
  478. /// </summary>
  479. /// <param name="voltageValue"></param>
  480. /// <param name="startT"></param>
  481. /// <param name="endT"></param>
  482. /// <returns></returns>
  483. public IEnumerable<MeterEntity> GetMeterByBatteryVoltageValue(decimal voltageValue, IEnumerable<MeterEntity> entities)
  484. {
  485. try
  486. {
  487. List<MeterEntity> returnEntity = new List<MeterEntity>();
  488. foreach (var item in entities)
  489. {
  490. if (item.BatteryVoltageValue!=null)
  491. {
  492. decimal batteryVoltageValue = (decimal)item.BatteryVoltageValue;
  493. if (batteryVoltageValue <= voltageValue)
  494. {
  495. returnEntity.Add(item);
  496. }
  497. }
  498. }
  499. return returnEntity;
  500. }
  501. catch (Exception ex)
  502. {
  503. return null;
  504. }
  505. }
  506. /// <summary>
  507. /// 根据口径筛选设备
  508. /// </summary>
  509. /// <param name="entities"></param>
  510. /// <param name="caliberValue">口径值</param>
  511. /// <param name="chooseType">1.大于 2.等于 3小于</param>
  512. /// <returns></returns>
  513. public IEnumerable<MeterEntity> GetMeterEntityByCaliber(IEnumerable<MeterEntity> entities, int caliberValue, int chooseType)
  514. {
  515. try
  516. {
  517. List<MeterEntity> returnEntity = new List<MeterEntity>();
  518. foreach (var item in entities)
  519. {
  520. int meterTypeId = item.MeterTypeId;
  521. //取表设备类型信息
  522. string selSql = " SELECT * FROM MeterType WHERE MeterAssessmentType = " + meterTypeId;
  523. DataTable dtResult = sqlHelper.ExecuteDataTable(selSql, CommandType.Text, null);
  524. if (dtResult.Rows.Count > 0)
  525. {
  526. if (!string.IsNullOrEmpty(dtResult.Rows[0]["Caliber"].ToString()))
  527. {
  528. int Caliber = int.Parse(dtResult.Rows[0]["Caliber"].ToString());
  529. switch (chooseType)
  530. {
  531. case 1:
  532. if (Caliber > caliberValue) { returnEntity.Add(item); }
  533. break;
  534. case 2:
  535. if (Caliber == caliberValue) { returnEntity.Add(item); }
  536. break;
  537. case 3:
  538. if (Caliber < caliberValue) { returnEntity.Add(item); }
  539. break;
  540. }
  541. }
  542. }
  543. }
  544. return returnEntity;
  545. }
  546. catch (Exception ex)
  547. {
  548. return null;
  549. }
  550. }
  551. /// <summary>
  552. /// 获取有故障的设备
  553. /// 瞬时流量 压力 净累计都为0 视为故障
  554. /// 最近更新时间大于一天的 视为故障
  555. /// </summary>
  556. /// <param name="entities"></param>
  557. /// <returns></returns>
  558. public IEnumerable<MeterEntity> GetFaultMeter(IEnumerable<MeterEntity> entities)
  559. {
  560. try
  561. {
  562. List<MeterEntity> returnEntity = new List<MeterEntity>();
  563. foreach (var item in entities)
  564. {
  565. DateTime? getDataTime = item.GetDateTime;
  566. decimal? instantaneousFlow = item.InstantaneousFlow;//瞬时流量
  567. decimal? pressure = item.Pressure;//压力
  568. decimal? negativeCumulativeFlow = item.NegativeCumulativeFlow;//净累计
  569. decimal shusD = 0, jingLeiJiD = 0, yaLiD = 0;
  570. if (getDataTime != null)
  571. {
  572. if ((DateTime.Now - (DateTime)getDataTime).Days > 1)
  573. {
  574. returnEntity.Add(item);
  575. continue;
  576. }
  577. }
  578. if (instantaneousFlow != null)
  579. {
  580. shusD = (decimal)instantaneousFlow;
  581. }
  582. if (pressure != null)
  583. {
  584. yaLiD = (decimal)pressure;
  585. }
  586. if (negativeCumulativeFlow != null)
  587. {
  588. jingLeiJiD = (decimal)negativeCumulativeFlow;
  589. }
  590. if (shusD == 0 && jingLeiJiD == 0 && yaLiD == 0)
  591. {
  592. returnEntity.Add(item);
  593. }
  594. }
  595. return returnEntity;
  596. }
  597. catch (Exception ex)
  598. {
  599. return null;
  600. }
  601. }
  602. /// <summary>
  603. /// 计算漏失
  604. /// </summary>
  605. /// <param name="entity">表设备</param>
  606. /// <param name="start">开始时间</param>
  607. /// <param name="end">结束时间</param>
  608. /// <param name="continuous">连续用水量</param>
  609. /// <param name="minFlowRate">校核按钮所使用的值 最小</param>
  610. /// <param name="type">校核 状态0 初始化 1.点击校核按钮</param>
  611. /// <param name="leakRate">漏损倍率</param>
  612. /// <returns></returns>
  613. public CalcLeakEntity GetCalcLeakEntity(MeterEntity entity, string startT, string endT, string continuous, string minFlowRate, string type)
  614. {
  615. decimal leakRate;
  616. if (entity.LeakRate == null)
  617. {
  618. leakRate = 0;
  619. }
  620. else
  621. {
  622. leakRate = (decimal)entity.LeakRate;
  623. }
  624. CalcLeakEntity model = new CalcLeakEntity();
  625. //获取时间段内的 平均 最大 最小流量
  626. string strSql = "SELECT AVG(InstantaneousFlow) avgInstantaneousFlow,MIN(InstantaneousFlow) minInstantaneousFlow,MAX(InstantaneousFlow) maxInstantaneousFlow FROM " + entity.TableName + " WHERE InstantaneousFlow<>0 AND InstantaneousFlow IS NOT NULL AND GetDateTime BETWEEN '" + startT + "' AND '" + endT + "'";
  627. DataTable dtAvg = sqlHelper.ExecuteDataTable(strSql, CommandType.Text, null);
  628. model.ContinuousFlow = continuous;
  629. string strAvg = dtAvg.Rows[0]["avgInstantaneousFlow"].ToString();
  630. if (!string.IsNullOrEmpty(strAvg))
  631. {
  632. decimal avgFlow = Math.Round(decimal.Parse(strAvg), 2);
  633. decimal minFlow = decimal.Parse(dtAvg.Rows[0]["minInstantaneousFlow"].ToString());
  634. decimal maxFlow = decimal.Parse(dtAvg.Rows[0]["maxInstantaneousFlow"].ToString());
  635. model.CalMinFlow = minFlow.ToString();//最小流量
  636. if (entity.IsKaoHe == 1) //如果是考核表计算
  637. {
  638. MeterTypeEntity meterTypeEntity = GetMeterTypeEntity(entity.MeterTypeId);
  639. if (meterTypeEntity.Caliber >= 100 && meterTypeEntity.Caliber <= 1000)//100-1000
  640. {
  641. if (leakRate == 0)
  642. {
  643. model.ContinuousFlow = Math.Round(avgFlow / 3.5m, 4).ToString();
  644. }
  645. else
  646. {
  647. model.ContinuousFlow = Math.Round(avgFlow / leakRate, 4).ToString();
  648. }
  649. }
  650. }
  651. continuous = model.ContinuousFlow;
  652. DateTime dtStart = DateTime.Parse(startT);
  653. DateTime dtEnd = DateTime.Parse(endT);
  654. List<decimal> totalMinFlow = new List<decimal>();//计算平均最小流量
  655. int days = (dtEnd - dtStart).Days;
  656. model.IntervalDays = days.ToString();
  657. if (type.Equals("1"))
  658. {
  659. minFlow = decimal.Parse(minFlowRate);
  660. }
  661. else
  662. {
  663. for (int i = 0; i < days; i++)
  664. {
  665. DateTime dtTomorrow = dtStart.AddDays(1);
  666. strSql = "Select avg(InstantaneousFlow) as avgInstantaneousFlow, MIN(InstantaneousFlow) as minInstantaneousFlow From " + entity.TableName + " Where InstantaneousFlow<>0 and InstantaneousFlow is not null And GetDateTime between '" + dtStart.ToString("yyyy-MM-dd 00:00") + "' and '" + dtTomorrow.ToString("yyyy-MM-dd 00:00") + "'";
  667. dtStart = dtStart.AddDays(1);
  668. DataTable dtMin = sqlHelper.ExecuteDataTable(strSql, CommandType.Text, null);
  669. if (dtMin.Rows.Count > 0)
  670. {
  671. if (!string.IsNullOrEmpty(dtMin.Rows[0]["minInstantaneousFlow"].ToString()))
  672. {
  673. totalMinFlow.Add(decimal.Parse(dtMin.Rows[0]["minInstantaneousFlow"].ToString()));
  674. }
  675. }
  676. }
  677. }
  678. if (totalMinFlow.Count > 0)
  679. {
  680. decimal totalM = 0;
  681. foreach (var item in totalMinFlow)
  682. {
  683. totalM += item;
  684. }
  685. minFlow = Math.Round(totalM / totalMinFlow.Count, 3);
  686. minFlow = minFlow * 0.82M;
  687. }
  688. //疑似漏水量,疑似漏失率,夜间允许漏水量,期间供水量,期间售水量,连续用水量
  689. decimal suspectedLeakFlow, suspectedLeakRate, nightAllowLeakFlow, waterSupply, salesWater, continuousWater;
  690. continuousWater = 0;
  691. continuous = "0";//2018/04/02 更改
  692. if (string.IsNullOrEmpty(continuous))
  693. {
  694. continuousWater = 0;
  695. }
  696. else
  697. {
  698. continuousWater = decimal.Parse(continuous);
  699. }
  700. model.ContinuousFlow = continuousWater.ToString();
  701. //计算疑似漏失量 实际最小流量-连续或转输用水-平均瞬时/5
  702. decimal suspectedLeakFlowNum =decimal.Parse(Config.GetValue("suspectedLeakFlow"));
  703. suspectedLeakFlow = minFlow - continuousWater - avgFlow / suspectedLeakFlowNum;
  704. suspectedLeakFlow = Math.Round(suspectedLeakFlow, 2);
  705. bool color = false;
  706. //如果漏失量小于零
  707. if (suspectedLeakFlow < 0)
  708. {
  709. suspectedLeakFlow = 0;
  710. color = true;
  711. }
  712. if (avgFlow != 0)
  713. {
  714. suspectedLeakRate = suspectedLeakFlow / avgFlow * 100;
  715. }
  716. else
  717. {
  718. suspectedLeakRate = 0;
  719. }
  720. if (suspectedLeakRate < 0)
  721. {
  722. suspectedLeakRate = 0;
  723. color = true;
  724. }
  725. nightAllowLeakFlow = minFlow - suspectedLeakFlow;
  726. nightAllowLeakFlow = avgFlow / 5;
  727. if (nightAllowLeakFlow < 0)
  728. {
  729. nightAllowLeakFlow = 0;
  730. color = true;
  731. }
  732. waterSupply = avgFlow * 24;
  733. salesWater = waterSupply - suspectedLeakFlow * 24;
  734. waterSupply = waterSupply * days;
  735. salesWater = salesWater * days;
  736. decimal sysWaterSupply = 0, sysSalesWater = 0;
  737. differenceAnalysisService.GetSysWaterSupply(entity.MeterAssessmentId.ToString(), startT, endT, ref sysWaterSupply, ref sysSalesWater); //根据DMA分区
  738. model.Color = color;
  739. decimal lastSysWaterSupply = 0, lastSalesWater = 0;
  740. string lastStart = Convert.ToDateTime(startT).AddMonths(-1).ToString("yyyy-MM-10 09:00");
  741. string lastEnd = Convert.ToDateTime(startT).ToString("yyyy-MM-10 09:00");
  742. differenceAnalysisService.GetSysWaterSupply(entity.MeterAssessmentId.ToString(), lastStart, lastEnd, ref lastSysWaterSupply, ref lastSalesWater);
  743. model.SuspectedLeakFlow = Math.Round(suspectedLeakFlow, 2).ToString();
  744. model.SuspectedLeakRate = Math.Round(suspectedLeakRate, 2).ToString();
  745. model.NightAllowLeakFlow = Math.Round(nightAllowLeakFlow, 2).ToString();
  746. model.WaterSupply = Math.Round(waterSupply, 0).ToString();
  747. model.SalesWater = Math.Round(salesWater, 0).ToString();
  748. model.AvgFlow = dtAvg.Rows[0]["avgInstantaneousFlow"].ToString();
  749. model.MinFlow = Math.Round(minFlow, 2).ToString();
  750. model.MaxFlow = Math.Round(maxFlow, 2).ToString();
  751. model.SysWaterSupply = Math.Round(sysWaterSupply, 0).ToString();
  752. model.SysSalesWater = Math.Round(sysSalesWater, 0).ToString();
  753. model.LastSysWaterSupply = Math.Round(lastSysWaterSupply, 0).ToString();
  754. model.LastSysSalesWater = Math.Round(lastSalesWater, 0).ToString();
  755. model.LastStart = lastStart;
  756. model.LastEnd = lastEnd;
  757. model.LastIntervalDays = (DateTime.Parse(lastEnd) - DateTime.Parse(lastStart)).Days.ToString();
  758. }
  759. return model;
  760. }
  761. /// <summary>
  762. /// 计算设备漏失率低于某个值
  763. /// </summary>
  764. /// <param name="entities"></param>
  765. /// <param name="value"></param>
  766. /// <returns></returns>
  767. public IEnumerable<MeterEntity> GetDeviceLossMoreThanValue(IEnumerable<MeterEntity> entities, decimal value)
  768. {
  769. try
  770. {
  771. List<MeterEntity> returnEntity = new List<MeterEntity>();
  772. var start = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
  773. var end = DateTime.Now.ToString("yyyy-MM-dd");
  774. foreach (var item in entities)
  775. {
  776. CalcLeakEntity clacLeakEntity = GetCalcLeakEntity(item, start, end, "0", "", "0");
  777. if (!string.IsNullOrEmpty(clacLeakEntity.SuspectedLeakRate) && !clacLeakEntity.SuspectedLeakRate.Equals("--"))
  778. {
  779. if (decimal.Parse(clacLeakEntity.SuspectedLeakRate) >= 3)
  780. {
  781. returnEntity.Add(item);
  782. }
  783. }
  784. }
  785. return returnEntity;
  786. }
  787. catch (Exception ex)
  788. {
  789. return null;
  790. }
  791. }
  792. /// <summary>
  793. /// 更新贸易表的连续用水量
  794. /// </summary>
  795. /// <param name="continueUserWater"></param>
  796. /// <param name="meterAssessmentId"></param>
  797. /// <returns></returns>
  798. public bool UpdateMaoYiMeterContinueUserWater(string continueUserWater,string meterAssessmentId)
  799. {
  800. try
  801. {
  802. string updateSql = "UPDATE MeterAssessmentBase SET ContinueUserWater =" + continueUserWater + " WHERE MeterAssessmentId = " + meterAssessmentId;
  803. return sqlHelper.ExecuteNoParams(updateSql, CommandType.Text) > 0 ? true : false;
  804. }
  805. catch (Exception ex)
  806. {
  807. return false;
  808. }
  809. }
  810. /// <summary>
  811. /// 设置考核表报警参数
  812. /// </summary>
  813. /// <param name="meterId">设备ID</param>
  814. /// <param name="continueUseWater">连续用水量</param>
  815. /// <param name="supplySaleDValue">产销差</param>
  816. /// <returns></returns>
  817. public bool SetMeterAlermParam(string meterId, string continueUseWater, string supplySaleDValue)
  818. {
  819. try
  820. {
  821. string updateSql = "UPDATE MeterAssessmentBase SET ContinueUserWater =" + continueUseWater + ",SupplySaleDValue = "+ supplySaleDValue + " WHERE MeterAssessmentId = " + meterId;
  822. return sqlHelper.ExecuteNoParams(updateSql, CommandType.Text) > 0 ? true : false;
  823. }
  824. catch (Exception ex)
  825. {
  826. return false;
  827. }
  828. }
  829. /// <summary>
  830. /// 更新报警开关
  831. /// </summary>
  832. /// <param name="meterId">设备ID</param>
  833. /// <param name="alermType">报警类型 0-1-1|2|3|4 第一位 报警开关 第二位 大类型 第三位 小类型</param>
  834. /// <returns></returns>
  835. public bool SetMeterAlermState(string meterId, string alermType)
  836. {
  837. try
  838. {
  839. string updateSql = "UPDATE MeterAssessmentBase SET AlermType = '" + alermType + "' WHERE MeterAssessmentId = " + meterId;
  840. return sqlHelper.ExecuteNoParams(updateSql, CommandType.Text) > 0 ? true : false;
  841. }
  842. catch (Exception ex)
  843. {
  844. return false;
  845. }
  846. }
  847. }
  848. #endregion
  849. }