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.
 
 

261 lines
8.9 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数据库:日志表 LogTable
  12. /// </summary>
  13. public class LogController : BaseController
  14. {
  15. MongoDbHelper<LogTable> mg = new MongoDbHelper<LogTable>(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<LogTable> Create(LogTable auth)
  23. {
  24. st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  25. try
  26. {
  27. if (string.IsNullOrEmpty(auth.ClientId))
  28. {
  29. return JsonMsg<LogTable>.Error(null, st, "设备ID不能为空"); ;
  30. }
  31. mg.Insert(auth);
  32. return JsonMsg<LogTable>.OK(auth, st);
  33. }
  34. catch (Exception ex)
  35. {
  36. return JsonMsg<LogTable>.Error(null, st, ex.Message);
  37. }
  38. }
  39. /// <summary>
  40. /// 批量新增数据
  41. /// </summary>
  42. /// <param name="auth">日志消息</param>
  43. [HttpPost]
  44. public JsonMsg<List<LogTable>> Creates(List<LogTable> auth)
  45. {
  46. st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  47. try
  48. {
  49. mg.Inserts(auth);
  50. return JsonMsg<List<LogTable>>.OK(auth, st);
  51. }
  52. catch (Exception ex)
  53. {
  54. return JsonMsg<List<LogTable>>.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<LogTable> Modify(LogTable auth)
  64. {
  65. st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  66. try
  67. {
  68. mg.UpdateID(auth);
  69. return JsonMsg<LogTable>.OK(auth, st);
  70. }
  71. catch (Exception ex)
  72. {
  73. return JsonMsg<LogTable>.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<LogTable> QueryOne(string id)
  83. {
  84. st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  85. try
  86. {
  87. return JsonMsg<LogTable>.OK(mg.QueryOne(id), st);
  88. }
  89. catch (Exception ex)
  90. {
  91. return JsonMsg<LogTable>.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<LogTable>> QueryClientId(string clientId)
  101. {
  102. st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  103. try
  104. {
  105. return JsonMsg<List<LogTable>>.OK(mg.QueryClientId(clientId), st);
  106. }
  107. catch (Exception ex)
  108. {
  109. return JsonMsg<List<LogTable>>.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<List<LogTable>> QueryDeviceId(string clientId, string deviceId)
  119. {
  120. st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  121. try
  122. {
  123. return JsonMsg<List<LogTable>>.OK(mg.QueryDeviceId(clientId, deviceId), st);
  124. }
  125. catch (Exception ex)
  126. {
  127. return JsonMsg<List<LogTable>>.Error(null, st, ex.Message);
  128. }
  129. }
  130. /// <summary>
  131. /// 根据设备名称查询当日未处理信息
  132. /// </summary>
  133. /// <param name="DeviceName"></param>
  134. /// <returns></returns>
  135. [HttpGet]
  136. public JsonMsg<List<LogTable>> QueryDeviceName(string DeviceName, string StartTime, string StopTime)
  137. {
  138. st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  139. try
  140. {
  141. DeviceTable device = mgsb.QueryDeviceName(DeviceName);
  142. if (device != null)
  143. {
  144. if (!string.IsNullOrEmpty(StartTime) && !string.IsNullOrEmpty(StopTime))
  145. {
  146. DateTime starttime, stoptime;
  147. starttime = Tools.ConvertLongToDateTime(long.Parse(StartTime));
  148. stoptime = Tools.ConvertLongToDateTime(long.Parse(StopTime));
  149. return JsonMsg<List<LogTable>>.OK(mg.QueryAllTime(device.ClientId, device.DeviceId, starttime, stoptime), st);
  150. }
  151. else return JsonMsg<List<LogTable>>.OK(mg.QueryClientId(device.ClientId), st);
  152. }
  153. return JsonMsg<List<LogTable>>.OK(null, st);
  154. }
  155. catch (Exception ex)
  156. {
  157. return JsonMsg<List<LogTable>>.Error(null, st, ex.Message);
  158. }
  159. }
  160. /// <summary>
  161. /// 根据客户端ID、设备ID、时间查询日志信息
  162. /// </summary>
  163. /// <param name="clientId">客户端ID</param>
  164. /// <param name="deviceId">设备ID</param>
  165. /// <param name="starttime">开始时间</param>
  166. /// <param name="endtime">结束时间</param>
  167. /// <returns></returns>
  168. [HttpGet]
  169. public JsonMsg<List<LogTable>> Query(string clientId, string deviceId, DateTime starttime, DateTime endtime)
  170. {
  171. st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  172. try
  173. {
  174. return JsonMsg<List<LogTable>>.OK(mg.QueryAllTime(clientId, deviceId, starttime, endtime), st);
  175. }
  176. catch (Exception ex)
  177. {
  178. return JsonMsg<List<LogTable>>.Error(null, st, ex.Message);
  179. }
  180. }
  181. /// <summary>
  182. /// 分页查询
  183. /// </summary>
  184. /// <param name="chid"></param>
  185. /// <param name="datesta"></param>
  186. /// <param name="datastop"></param>
  187. /// <param name="PageNumber"></param>
  188. /// <param name="PageSize"></param>
  189. /// <returns></returns>
  190. [HttpGet]
  191. public JsonMsg<PaginationListModel<LogTable>> BasePagQuery(string clientId, string deviceId, DateTime datesta, DateTime datastop, int PageNumber = 1, int PageSize = 100)
  192. {
  193. st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  194. try
  195. {
  196. return JsonMsg<PaginationListModel<LogTable>>.OK(mg.BasePagQuery(clientId, deviceId, datesta, datastop, PageNumber, PageSize), st);
  197. }
  198. catch (Exception ex)
  199. {
  200. return JsonMsg<PaginationListModel<LogTable>>.Error(null, st, ex.Message);
  201. }
  202. }
  203. /// <summary>
  204. /// 删除数据(假删除-只做标记)
  205. /// </summary>
  206. /// <param name="id"></param>
  207. /// <returns></returns>
  208. [HttpGet]
  209. public JsonMsg<string> Delete(string id)
  210. {
  211. st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  212. try
  213. {
  214. if (string.IsNullOrEmpty(id))
  215. {
  216. return JsonMsg<string>.Error(null, st, "设备ID不能为空"); ;
  217. }
  218. else
  219. {
  220. mg.Modify(id, "State", "n");
  221. return JsonMsg<string>.OK("success!", st);
  222. }
  223. }
  224. catch (Exception ex)
  225. {
  226. return JsonMsg<string>.Error(null, st, ex.Message);
  227. }
  228. }
  229. /// <summary>
  230. /// 删除数据
  231. /// </summary>
  232. /// <param name="id"></param>
  233. /// <returns></returns>
  234. [HttpGet]
  235. public JsonMsg<string> DeleteDate(string id)
  236. {
  237. st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  238. try
  239. {
  240. if (string.IsNullOrEmpty(id))
  241. {
  242. return JsonMsg<string>.Error(null, st, "设备ID不能为空"); ;
  243. }
  244. else
  245. {
  246. mg.Delete(id);
  247. return JsonMsg<string>.OK("success!", st);
  248. }
  249. }
  250. catch (Exception ex)
  251. {
  252. return JsonMsg<string>.Error(null, st, ex.Message);
  253. }
  254. }
  255. }
  256. }