Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 

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