Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

302 rader
11 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. public 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. List<AlarmTable> al = mg.QueryClientId(clientId);
  106. al?.ForEach(par =>
  107. {
  108. if (par.State == "y") { par.StatusMS = "未处理"; par.StatusColor = new { r = 255, g = 0, b = 0, a = 1 }; }
  109. else { par.StatusMS = "已处理"; par.StatusColor = new { r = 51, g = 232, b = 34, a = 1 }; }
  110. });
  111. al.Sort((a, b) => -a.State.CompareTo(b.State));
  112. return JsonMsg<List<AlarmTable>>.OK(al, st);
  113. }
  114. catch (Exception ex)
  115. {
  116. return JsonMsg<List<AlarmTable>>.Error(null, st, ex.Message);
  117. }
  118. }
  119. /// <summary>
  120. /// 根据客户端ID查询最近告警
  121. /// </summary>
  122. /// <param name="clientId"></param>
  123. /// <returns></returns>
  124. [HttpGet]
  125. public JsonMsg<AlarmTable> QueryClientIdMax(string clientId)
  126. {
  127. st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  128. try
  129. {
  130. return JsonMsg<AlarmTable>.OK(mg.QueryClientIdMax(clientId), st);
  131. }
  132. catch (Exception ex)
  133. {
  134. return JsonMsg<AlarmTable>.Error(null, st, ex.Message);
  135. }
  136. }
  137. /// <summary>
  138. /// 根据设备ID查询当日未处理信息
  139. /// </summary>
  140. /// <param name="clientId"></param>
  141. /// <returns></returns>
  142. [HttpGet]
  143. public JsonMsg<List<AlarmTable>> QueryDeviceId(string clientId, string deviceId)
  144. {
  145. st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  146. try
  147. {
  148. if (string.IsNullOrEmpty(clientId))
  149. return JsonMsg<List<AlarmTable>>.Error(null, st, "客户端ID不能为空"); ;
  150. return JsonMsg<List<AlarmTable>>.OK(mg.QueryDeviceId(clientId, deviceId), st);
  151. }
  152. catch (Exception ex)
  153. {
  154. return JsonMsg<List<AlarmTable>>.Error(null, st, ex.Message);
  155. }
  156. }
  157. /// <summary>
  158. /// 根据设备名称、时间查询未处理信息
  159. /// </summary>
  160. /// <param name="clientId"></param>
  161. /// <returns></returns>
  162. [HttpGet]
  163. public JsonMsg<List<AlarmTable>> QueryDeviceName(string DeviceName, string StartTime, string StopTime)
  164. {
  165. st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  166. try
  167. {
  168. DeviceTable device = mgsb.QueryDeviceName(DeviceName);
  169. if (device != null)
  170. {
  171. if (!string.IsNullOrEmpty(StartTime) && !string.IsNullOrEmpty(StopTime))
  172. {
  173. DateTime starttime, stoptime;
  174. starttime = Tools.ConvertLongToDateTime(long.Parse(StartTime));
  175. stoptime = Tools.ConvertLongToDateTime(long.Parse(StopTime));
  176. return JsonMsg<List<AlarmTable>>.OK(mg.QueryAllTime(device.ClientId, device.DeviceId, starttime, stoptime), st);
  177. }
  178. else return JsonMsg<List<AlarmTable>>.OK(mg.QueryClientId(device.ClientId), st);
  179. }
  180. return JsonMsg<List<AlarmTable>>.OK(null, st);
  181. }
  182. catch (Exception ex)
  183. {
  184. return JsonMsg<List<AlarmTable>>.Error(null, st, ex.Message);
  185. }
  186. }
  187. /// <summary>
  188. /// 根据客户端ID、设备ID、时间查询告警信息
  189. /// </summary>
  190. /// <param name="clientId">客户端ID</param>
  191. /// <param name="deviceId">设备ID</param>
  192. /// <param name="starttime">开始时间</param>
  193. /// <param name="endtime">结束时间</param>
  194. /// <returns></returns>
  195. [HttpGet]
  196. public JsonMsg<List<AlarmTable>> Query(string clientId, string deviceId, DateTime starttime, DateTime endtime)
  197. {
  198. st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  199. try
  200. {
  201. return JsonMsg<List<AlarmTable>>.OK(mg.QueryAllTime(clientId, deviceId, starttime, endtime), st);
  202. }
  203. catch (Exception ex)
  204. {
  205. return JsonMsg<List<AlarmTable>>.Error(null, st, ex.Message);
  206. }
  207. }
  208. /// <summary>
  209. /// 分页查询
  210. /// </summary>
  211. /// <param name="clientId">客户端ID</param>
  212. /// <param name="datesta">开始时间</param>
  213. /// <param name="datastop">结束时间</param>
  214. /// <param name="PageNumber">页</param>
  215. /// <param name="PageSize">大小</param>
  216. /// <returns></returns>
  217. [HttpGet]
  218. public JsonMsg<PaginationListModel<AlarmTable>> BasePagQuery(string clientId, string deviceId, DateTime datesta, DateTime datastop, int PageNumber = 1, int PageSize = 100)
  219. {
  220. st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  221. try
  222. {
  223. return JsonMsg<PaginationListModel<AlarmTable>>.OK(mg.BasePagQuery(clientId, deviceId, datesta, datastop, PageNumber, PageSize), st);
  224. }
  225. catch (Exception ex)
  226. {
  227. return JsonMsg<PaginationListModel<AlarmTable>>.Error(null, st, ex.Message);
  228. }
  229. }
  230. /// <summary>
  231. /// 删除数据(假删除-只做标记)
  232. /// </summary>
  233. /// <param name="id"></param>
  234. /// <returns></returns>
  235. [HttpGet]
  236. public JsonMsg<string> Delete(string id)
  237. {
  238. st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  239. try
  240. {
  241. if (string.IsNullOrEmpty(id))
  242. {
  243. return JsonMsg<string>.Error(null, st, "设备ID不能为空"); ;
  244. }
  245. else
  246. {
  247. mg.Modify(id, "State", "n");
  248. return JsonMsg<string>.OK("success!", st);
  249. }
  250. }
  251. catch (Exception ex)
  252. {
  253. return JsonMsg<string>.Error(null, st, ex.Message);
  254. }
  255. }
  256. /// <summary>
  257. /// 删除数据
  258. /// </summary>
  259. /// <param name="id"></param>
  260. /// <returns></returns>
  261. [HttpGet]
  262. public JsonMsg<string> DeleteDate(string id)
  263. {
  264. st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  265. try
  266. {
  267. if (string.IsNullOrEmpty(id))
  268. {
  269. return JsonMsg<string>.Error(null, st, "设备ID不能为空"); ;
  270. }
  271. else
  272. {
  273. mg.Delete(id);
  274. return JsonMsg<string>.OK("success!", st);
  275. }
  276. }
  277. catch (Exception ex)
  278. {
  279. return JsonMsg<string>.Error(null, st, ex.Message);
  280. }
  281. }
  282. [HttpGet]
  283. public bool QueryClientProcess(string clientId)
  284. {
  285. try
  286. {
  287. AlarmTable al = mg.QueryClientIdMax(clientId);
  288. return al != null ? true : false ;
  289. }
  290. catch (Exception ex)
  291. {
  292. return false;
  293. }
  294. }
  295. }
  296. }