Não pode escolher mais do que 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.

289 linhas
9.8 KiB

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