You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

281 lines
9.8 KiB

  1. using DataVAPI.Model;
  2. using DataVAPI.ModelDataBus;
  3. using DataVAPI.ServerDB.MongoDB;
  4. using DataVAPI.Tool.IOT;
  5. using Microsoft.AspNetCore.Mvc;
  6. using System;
  7. using System.Collections.Generic;
  8. namespace DataVAPI.Controllers
  9. {
  10. /// <summary>
  11. /// MG数据库:告警表 AlarmTable
  12. /// </summary>
  13. public class AlarmController : BaseController
  14. {
  15. MongoDbHelper<AlarmTable> mg = new MongoDbHelper<AlarmTable>(DataBus.connStr, DataBus.dbName);
  16. MongoDbHelper<DeviceTable> mgsb = new MongoDbHelper<DeviceTable>(DataBus.connStr, DataBus.dbName);
  17. string st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  18. /// <summary>
  19. /// 新增一条数据
  20. /// </summary>
  21. [HttpPost]
  22. public JsonMsg<AlarmTable> Create(AlarmTable auth)
  23. {
  24. st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  25. try
  26. {
  27. if (string.IsNullOrEmpty(auth.ClientId))
  28. {
  29. return JsonMsg<AlarmTable>.Error(null, st, "设备ID不能为空"); ;
  30. }
  31. mg.Insert(auth);
  32. return JsonMsg<AlarmTable>.OK(auth, st);
  33. }
  34. catch (Exception ex)
  35. {
  36. return JsonMsg<AlarmTable>.Error(null, st, ex.Message);
  37. }
  38. }
  39. /// <summary>
  40. /// 批量新增一条数据
  41. /// </summary>
  42. /// <param name="auth">告警消息</param>
  43. [HttpPost]
  44. public JsonMsg<List<AlarmTable>> Creates(List<AlarmTable> auth)
  45. {
  46. st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  47. try
  48. {
  49. mg.Inserts(auth);
  50. return JsonMsg<List<AlarmTable>>.OK(auth, st);
  51. }
  52. catch (Exception ex)
  53. {
  54. return JsonMsg<List<AlarmTable>>.Error(null, st, ex.Message);
  55. }
  56. }
  57. /// <summary>
  58. /// 修改数据
  59. /// </summary>
  60. /// <param name="auth"></param>
  61. /// <returns></returns>
  62. [HttpPost]
  63. public JsonMsg<AlarmTable> Modify(AlarmTable auth)
  64. {
  65. st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  66. try
  67. {
  68. mg.UpdateID(auth);
  69. return JsonMsg<AlarmTable>.OK(auth, st);
  70. }
  71. catch (Exception ex)
  72. {
  73. return JsonMsg<AlarmTable>.Error(null, st, ex.Message);
  74. }
  75. }
  76. /// <summary>
  77. /// 根据唯一ID查询数据
  78. /// </summary>
  79. /// <param name="id"></param>
  80. /// <returns></returns>
  81. [HttpGet]
  82. public JsonMsg<AlarmTable> QueryOne(string id)
  83. {
  84. st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  85. try
  86. {
  87. return JsonMsg<AlarmTable>.OK(mg.QueryOne(id), st);
  88. }
  89. catch (Exception ex)
  90. {
  91. return JsonMsg<AlarmTable>.Error(null, st, ex.Message);
  92. }
  93. }
  94. /// <summary>
  95. /// 根据客户端ID查询当日未处理信息
  96. /// </summary>
  97. /// <param name="clientId"></param>
  98. /// <returns></returns>
  99. [HttpGet]
  100. public JsonMsg<List<AlarmTable>> QueryClientId(string clientId)
  101. {
  102. st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  103. try
  104. {
  105. return JsonMsg<List<AlarmTable>>.OK(mg.QueryClientId(clientId), st);
  106. }
  107. catch (Exception ex)
  108. {
  109. return JsonMsg<List<AlarmTable>>.Error(null, st, ex.Message);
  110. }
  111. }
  112. /// <summary>
  113. /// 根据客户端ID查询最近告警
  114. /// </summary>
  115. /// <param name="clientId"></param>
  116. /// <returns></returns>
  117. [HttpGet]
  118. public JsonMsg<AlarmTable> QueryClientIdMax(string clientId)
  119. {
  120. st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  121. try
  122. {
  123. return JsonMsg<AlarmTable>.OK(mg.QueryClientIdMax(clientId), st);
  124. }
  125. catch (Exception ex)
  126. {
  127. return JsonMsg<AlarmTable>.Error(null, st, ex.Message);
  128. }
  129. }
  130. /// <summary>
  131. /// 根据设备ID查询当日未处理信息
  132. /// </summary>
  133. /// <param name="clientId"></param>
  134. /// <returns></returns>
  135. [HttpGet]
  136. public JsonMsg<List<AlarmTable>> QueryDeviceId(string clientId, string deviceId)
  137. {
  138. st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  139. try
  140. {
  141. if (string.IsNullOrEmpty(clientId))
  142. return JsonMsg<List<AlarmTable>>.Error(null, st, "客户端ID不能为空"); ;
  143. return JsonMsg<List<AlarmTable>>.OK(mg.QueryDeviceId(clientId, deviceId), st);
  144. }
  145. catch (Exception ex)
  146. {
  147. return JsonMsg<List<AlarmTable>>.Error(null, st, ex.Message);
  148. }
  149. }
  150. /// <summary>
  151. /// 根据设备名称、时间查询未处理信息
  152. /// </summary>
  153. /// <param name="clientId"></param>
  154. /// <returns></returns>
  155. [HttpGet]
  156. public JsonMsg<List<AlarmTable>> QueryDeviceName(string DeviceName, string StartTime, string StopTime)
  157. {
  158. st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  159. try
  160. {
  161. DeviceTable device = mgsb.QueryDeviceName(DeviceName);
  162. if (device != null)
  163. {
  164. if (!string.IsNullOrEmpty(StartTime) && !string.IsNullOrEmpty(StopTime))
  165. {
  166. DateTime starttime, stoptime;
  167. starttime = Tools.ConvertLongToDateTime(long.Parse(StartTime));
  168. stoptime = Tools.ConvertLongToDateTime(long.Parse(StopTime));
  169. return JsonMsg<List<AlarmTable>>.OK(mg.QueryAllTime(device.ClientId, device.DeviceId, starttime, stoptime), st);
  170. }
  171. else return JsonMsg<List<AlarmTable>>.OK(mg.QueryClientId(device.ClientId), st);
  172. }
  173. return JsonMsg<List<AlarmTable>>.OK(null, st);
  174. }
  175. catch (Exception ex)
  176. {
  177. return JsonMsg<List<AlarmTable>>.Error(null, st, ex.Message);
  178. }
  179. }
  180. /// <summary>
  181. /// 根据客户端ID、设备ID、时间查询告警信息
  182. /// </summary>
  183. /// <param name="clientId">客户端ID</param>
  184. /// <param name="deviceId">设备ID</param>
  185. /// <param name="starttime">开始时间</param>
  186. /// <param name="endtime">结束时间</param>
  187. /// <returns></returns>
  188. [HttpGet]
  189. public JsonMsg<List<AlarmTable>> Query(string clientId, string deviceId, DateTime starttime, DateTime endtime)
  190. {
  191. st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  192. try
  193. {
  194. return JsonMsg<List<AlarmTable>>.OK(mg.QueryAllTime(clientId, deviceId, starttime, endtime), st);
  195. }
  196. catch (Exception ex)
  197. {
  198. return JsonMsg<List<AlarmTable>>.Error(null, st, ex.Message);
  199. }
  200. }
  201. /// <summary>
  202. /// 分页查询
  203. /// </summary>
  204. /// <param name="clientId">客户端ID</param>
  205. /// <param name="datesta">开始时间</param>
  206. /// <param name="datastop">结束时间</param>
  207. /// <param name="PageNumber">页</param>
  208. /// <param name="PageSize">大小</param>
  209. /// <returns></returns>
  210. [HttpGet]
  211. public JsonMsg<PaginationListModel<AlarmTable>> BasePagQuery(string clientId, string deviceId, DateTime datesta, DateTime datastop, int PageNumber = 1, int PageSize = 100)
  212. {
  213. st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  214. try
  215. {
  216. return JsonMsg<PaginationListModel<AlarmTable>>.OK(mg.BasePagQuery(clientId, deviceId, datesta, datastop, PageNumber, PageSize), st);
  217. }
  218. catch (Exception ex)
  219. {
  220. return JsonMsg<PaginationListModel<AlarmTable>>.Error(null, st, ex.Message);
  221. }
  222. }
  223. /// <summary>
  224. /// 删除数据(假删除-只做标记)
  225. /// </summary>
  226. /// <param name="id"></param>
  227. /// <returns></returns>
  228. [HttpGet]
  229. public JsonMsg<string> Delete(string id)
  230. {
  231. st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  232. try
  233. {
  234. if (string.IsNullOrEmpty(id))
  235. {
  236. return JsonMsg<string>.Error(null, st, "设备ID不能为空"); ;
  237. }
  238. else
  239. {
  240. mg.Modify(id, "State", "n");
  241. return JsonMsg<string>.OK("success!", st);
  242. }
  243. }
  244. catch (Exception ex)
  245. {
  246. return JsonMsg<string>.Error(null, st, ex.Message);
  247. }
  248. }
  249. /// <summary>
  250. /// 删除数据
  251. /// </summary>
  252. /// <param name="id"></param>
  253. /// <returns></returns>
  254. [HttpGet]
  255. public JsonMsg<string> DeleteDate(string id)
  256. {
  257. st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  258. try
  259. {
  260. if (string.IsNullOrEmpty(id))
  261. {
  262. return JsonMsg<string>.Error(null, st, "设备ID不能为空"); ;
  263. }
  264. else
  265. {
  266. mg.Delete(id);
  267. return JsonMsg<string>.OK("success!", st);
  268. }
  269. }
  270. catch (Exception ex)
  271. {
  272. return JsonMsg<string>.Error(null, st, ex.Message);
  273. }
  274. }
  275. }
  276. }