No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 

203 líneas
6.8 KiB

  1. using DataVAPI.Model;
  2. using DataVAPI.ModelDataBus;
  3. using DataVAPI.ServerDB.MongoDB;
  4. using Microsoft.AspNetCore.Mvc;
  5. using System;
  6. using System.Collections.Generic;
  7. namespace DataVAPI.Controllers
  8. {
  9. /// <summary>
  10. /// MG数据库:设备信息表 DeviceTable
  11. /// </summary>
  12. public class DeviceController : BaseController
  13. {
  14. MongoDbHelper<DeviceTable> mg = new MongoDbHelper<DeviceTable>(DataBus.connStr, DataBus.dbName);
  15. string st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  16. /// <summary>
  17. /// 新增一条数据
  18. /// </summary>
  19. [HttpPost]
  20. public JsonMsg<DeviceTable> Create(DeviceTable auth)
  21. {
  22. st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  23. try
  24. {
  25. if (string.IsNullOrEmpty(auth.ClientId))
  26. {
  27. return JsonMsg<DeviceTable>.Error(null, st, "设备ID不能为空"); ;
  28. }
  29. mg.Insert(auth);
  30. return JsonMsg<DeviceTable>.OK(auth, st);
  31. }
  32. catch (Exception ex)
  33. {
  34. return JsonMsg<DeviceTable>.Error(null, st, ex.Message);
  35. }
  36. }
  37. /// <summary>
  38. /// 批量新增N条数据
  39. /// </summary>
  40. [HttpPost]
  41. public JsonMsg<List<DeviceTable>> Creates(List<DeviceTable> auth)
  42. {
  43. st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  44. try
  45. {
  46. mg.Inserts(auth);
  47. return JsonMsg<List<DeviceTable>>.OK(auth, st);
  48. }
  49. catch (Exception ex)
  50. {
  51. return JsonMsg<List<DeviceTable>>.Error(null, st, ex.Message);
  52. }
  53. }
  54. /// <summary>
  55. /// 修改数据:IdStr不能为空
  56. /// </summary>
  57. [HttpPost]
  58. public JsonMsg<DeviceTable> Modify(DeviceTable auth)
  59. {
  60. st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  61. try
  62. {
  63. mg.UpdateID(auth);
  64. return JsonMsg<DeviceTable>.OK(auth, st);
  65. }
  66. catch (Exception ex)
  67. {
  68. return JsonMsg<DeviceTable>.Error(null, st, ex.Message);
  69. }
  70. }
  71. /// <summary>
  72. /// 根据ID查询数据
  73. /// </summary>
  74. /// <param name="id">ID</param>
  75. [HttpGet]
  76. public JsonMsg<DeviceTable> QueryOne(string id)
  77. {
  78. st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  79. try
  80. {
  81. return JsonMsg<DeviceTable>.OK(mg.QueryOne(id), st);
  82. }
  83. catch (Exception ex)
  84. {
  85. return JsonMsg<DeviceTable>.Error(null, st, ex.Message);
  86. }
  87. }
  88. /// <summary>
  89. /// 根据设备名称查询数据
  90. /// </summary>
  91. /// <param name="id">ID</param>
  92. [HttpGet]
  93. public JsonMsg<DeviceTable> QueryDeviceName(string DeviceName)
  94. {
  95. st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  96. try
  97. {
  98. DeviceTable device = mg.QueryDeviceName(DeviceName);
  99. return JsonMsg<DeviceTable>.OK(device, st);
  100. }
  101. catch (Exception ex)
  102. {
  103. return JsonMsg<DeviceTable>.Error(null, st, ex.Message);
  104. }
  105. }
  106. /// <summary>
  107. /// 根据客户端ID、设备ID、时间查询设备信息
  108. /// </summary>
  109. /// <param name="clientId">客户端ID</param>
  110. /// <param name="deviceId">设备ID</param>
  111. /// <param name="starttime">开始时间</param>
  112. /// <param name="endtime">结束时间</param>
  113. /// <returns></returns>
  114. [HttpGet]
  115. public JsonMsg<List<DeviceTable>> Query(string clientId, string deviceId, DateTime starttime, DateTime endtime)
  116. {
  117. st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  118. try
  119. {
  120. return JsonMsg<List<DeviceTable>>.OK(mg.QueryAllTime(clientId, deviceId, starttime, endtime), st);
  121. }
  122. catch (Exception ex)
  123. {
  124. return JsonMsg<List<DeviceTable>>.Error(null, st, ex.Message);
  125. }
  126. }
  127. /// <summary>
  128. /// 分页查询
  129. /// </summary>
  130. /// <param name="clientId">客户端ID</param>
  131. /// <param name="starttime">开始时间</param>
  132. /// <param name="endtime">结束时间</param>
  133. /// <param name="PageNumber">页码</param>
  134. /// <param name="PageSize">每页大小</param>
  135. /// <returns></returns>
  136. [HttpGet]
  137. public JsonMsg<PaginationListModel<DeviceTable>> BasePagQuery(string clientId, string deviceId, DateTime starttime, DateTime endtime, int PageNumber = 1, int PageSize = 100)
  138. {
  139. st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  140. try
  141. {
  142. return JsonMsg<PaginationListModel<DeviceTable>>.OK(mg.BasePagQuery(clientId, deviceId, starttime, endtime, PageNumber, PageSize), st);
  143. }
  144. catch (Exception ex)
  145. {
  146. return JsonMsg<PaginationListModel<DeviceTable>>.Error(null, st, ex.Message);
  147. }
  148. }
  149. /// <summary>
  150. /// 删除数据(假删除-只做标记)
  151. /// </summary>
  152. /// <param name="id">唯一id</param>
  153. [HttpGet]
  154. public JsonMsg<string> Delete(string id)
  155. {
  156. st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  157. try
  158. {
  159. if (string.IsNullOrEmpty(id))
  160. {
  161. return JsonMsg<string>.Error(null, st, "设备ID不能为空"); ;
  162. }
  163. else
  164. {
  165. mg.Modify(id, "State", "n");
  166. return JsonMsg<string>.OK("success!", st);
  167. }
  168. }
  169. catch (Exception ex)
  170. {
  171. return JsonMsg<string>.Error(null, st, ex.Message);
  172. }
  173. }
  174. /// <summary>
  175. /// 删除数据
  176. /// </summary>
  177. /// <param name="id">唯一id</param>
  178. [HttpGet]
  179. public JsonMsg<string> DeleteDate(string id)
  180. {
  181. st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  182. try
  183. {
  184. if (string.IsNullOrEmpty(id))
  185. {
  186. return JsonMsg<string>.Error(null, st, "设备ID不能为空"); ;
  187. }
  188. else
  189. {
  190. mg.Delete(id);
  191. return JsonMsg<string>.OK("success!", st);
  192. }
  193. }
  194. catch (Exception ex)
  195. {
  196. return JsonMsg<string>.Error(null, st, ex.Message);
  197. }
  198. }
  199. }
  200. }