using DataVAPI.Model; using DataVAPI.ModelDataBus; using DataVAPI.ServerDB.MongoDB; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; namespace DataVAPI.Controllers { /// /// MG数据库:设备信息表 DeviceTable /// public class DeviceController : BaseController { MongoDbHelper mg = new MongoDbHelper(DataBus.connStr, DataBus.dbName); string st = System.Reflection.MethodBase.GetCurrentMethod().Name; /// /// 新增一条数据 /// [HttpPost] public JsonMsg Create(DeviceTable auth) { st = System.Reflection.MethodBase.GetCurrentMethod().Name; try { if (string.IsNullOrEmpty(auth.ClientId)) { return JsonMsg.Error(null, st, "设备ID不能为空"); ; } mg.Insert(auth); return JsonMsg.OK(auth, st); } catch (Exception ex) { return JsonMsg.Error(null, st, ex.Message); } } /// /// 批量新增N条数据 /// [HttpPost] public JsonMsg> Creates(List auth) { st = System.Reflection.MethodBase.GetCurrentMethod().Name; try { mg.Inserts(auth); return JsonMsg>.OK(auth, st); } catch (Exception ex) { return JsonMsg>.Error(null, st, ex.Message); } } /// /// 修改数据:IdStr不能为空 /// [HttpPost] public JsonMsg Modify(DeviceTable auth) { st = System.Reflection.MethodBase.GetCurrentMethod().Name; try { mg.UpdateID(auth); return JsonMsg.OK(auth, st); } catch (Exception ex) { return JsonMsg.Error(null, st, ex.Message); } } /// /// 根据ID查询数据 /// /// ID [HttpGet] public JsonMsg QueryOne(string id) { st = System.Reflection.MethodBase.GetCurrentMethod().Name; try { return JsonMsg.OK(mg.QueryOne(id), st); } catch (Exception ex) { return JsonMsg.Error(null, st, ex.Message); } } /// /// 根据设备名称查询数据 /// /// ID [HttpGet] public JsonMsg QueryDeviceName(string DeviceName) { st = System.Reflection.MethodBase.GetCurrentMethod().Name; try { DeviceTable device = mg.QueryDeviceName(DeviceName); return JsonMsg.OK(device, st); } catch (Exception ex) { return JsonMsg.Error(null, st, ex.Message); } } /// /// 根据客户端ID、设备ID、时间查询设备信息 /// /// 客户端ID /// 设备ID /// 开始时间 /// 结束时间 /// [HttpGet] public JsonMsg> Query(string clientId, string deviceId, DateTime starttime, DateTime endtime) { st = System.Reflection.MethodBase.GetCurrentMethod().Name; try { return JsonMsg>.OK(mg.QueryAllTime(clientId, deviceId, starttime, endtime), st); } catch (Exception ex) { return JsonMsg>.Error(null, st, ex.Message); } } /// /// 分页查询 /// /// 客户端ID /// 开始时间 /// 结束时间 /// 页码 /// 每页大小 /// [HttpGet] public JsonMsg> BasePagQuery(string clientId, string deviceId, DateTime starttime, DateTime endtime, int PageNumber = 1, int PageSize = 100) { st = System.Reflection.MethodBase.GetCurrentMethod().Name; try { return JsonMsg>.OK(mg.BasePagQuery(clientId, deviceId, starttime, endtime, PageNumber, PageSize), st); } catch (Exception ex) { return JsonMsg>.Error(null, st, ex.Message); } } /// /// 删除数据(假删除-只做标记) /// /// 唯一id [HttpGet] public JsonMsg Delete(string id) { st = System.Reflection.MethodBase.GetCurrentMethod().Name; try { if (string.IsNullOrEmpty(id)) { return JsonMsg.Error(null, st, "设备ID不能为空"); ; } else { mg.Modify(id, "State", "n"); return JsonMsg.OK("success!", st); } } catch (Exception ex) { return JsonMsg.Error(null, st, ex.Message); } } /// /// 删除数据 /// /// 唯一id [HttpGet] public JsonMsg DeleteDate(string id) { st = System.Reflection.MethodBase.GetCurrentMethod().Name; try { if (string.IsNullOrEmpty(id)) { return JsonMsg.Error(null, st, "设备ID不能为空"); ; } else { mg.Delete(id); return JsonMsg.OK("success!", st); } } catch (Exception ex) { return JsonMsg.Error(null, st, ex.Message); } } } }