Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 

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