using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using 服务API.Model;
using 服务API.ServerDB.MongoDB;
using 服务API.Tool.IOT;
namespace 服务API.Controllers
{
///
/// MG数据库:日志表 LogTable
///
public class LogController :BaseController
{
MongoDbHelper mg = new MongoDbHelper(DataBus.DataBus.connStr,DataBus.DataBus.dbName);
MongoDbHelper mgsb = new MongoDbHelper(DataBus.DataBus.connStr,DataBus.DataBus.dbName);
string st = System.Reflection.MethodBase.GetCurrentMethod().Name;
///
/// 新增一条数据
///
[HttpPost]
public JsonMsg Create(LogTable 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 (System.Exception ex)
{
return JsonMsg.Error(null,st,ex.Message);
}
}
///
/// 批量新增一条数据
///
/// 日志消息
[HttpPost]
public JsonMsg> Creates(List auth)
{
st = System.Reflection.MethodBase.GetCurrentMethod().Name;
try
{
mg.Inserts(auth);
return JsonMsg>.OK(auth,st);
}
catch (System.Exception ex)
{
return JsonMsg>.Error(null,st,ex.Message);
}
}
///
/// 修改数据
///
///
///
[HttpPost]
public JsonMsg Modify(LogTable auth)
{
st = System.Reflection.MethodBase.GetCurrentMethod().Name;
try
{
mg.Update(auth);
return JsonMsg.OK(auth,st);
}
catch (System.Exception ex)
{
return JsonMsg.Error(null,st,ex.Message);
}
}
///
/// 根据唯一ID查询数据
///
///
///
[HttpGet]
public JsonMsg QueryOne(string id)
{
st = System.Reflection.MethodBase.GetCurrentMethod().Name;
try
{
return JsonMsg.OK(mg.QueryOne(id),st);
}
catch (System.Exception ex)
{
return JsonMsg.Error(null,st,ex.Message);
}
}
///
/// 根据客户端ID查询当日未处理信息
///
///
///
[HttpGet]
public JsonMsg> QueryClientId(string clientId)
{
st = System.Reflection.MethodBase.GetCurrentMethod().Name;
try
{
return JsonMsg>.OK(mg.QueryClientId(clientId),st);
}
catch (System.Exception ex)
{
return JsonMsg>.Error(null,st,ex.Message);
}
}
///
/// 根据设备名称查询当日未处理信息
///
///
///
[HttpGet]
public JsonMsg> QueryDeviceName(string DeviceName,string StartTime,string StopTime)
{
st = System.Reflection.MethodBase.GetCurrentMethod().Name;
try
{
DeviceTable device = mgsb.QueryDeviceName(DeviceName);
if (device != null)
{
if (!string.IsNullOrEmpty(StartTime) && !string.IsNullOrEmpty(StopTime))
{
DateTime starttime, stoptime;
starttime = Tools.ConvertLongToDateTime(long.Parse(StartTime));
stoptime = Tools.ConvertLongToDateTime(long.Parse(StopTime));
return JsonMsg>.OK(mg.QueryAllTime(device.ClientId,starttime,stoptime),st);
}
else return JsonMsg>.OK(mg.QueryClientId(device.ClientId),st);
}
return JsonMsg>.OK(null,st);
}
catch (System.Exception ex)
{
return JsonMsg>.Error(null,st,ex.Message);
}
}
///
/// 根据客户端ID 时间查询设备日志信息
///
///
///
///
///
[HttpGet]
public JsonMsg> Query(string clientId,DateTime datesta,DateTime datastop)
{
st = System.Reflection.MethodBase.GetCurrentMethod().Name;
try
{
return JsonMsg>.OK(mg.QueryAllTime(clientId,datesta,datastop),st);
}
catch (System.Exception ex)
{
return JsonMsg>.Error(null,st,ex.Message);
}
}
///
/// 分页查询
///
///
///
///
///
///
///
[HttpGet]
public JsonMsg> BasePagQuery(string clientId,DateTime datesta,DateTime datastop,int PageNumber = 1,int PageSize = 100)
{
st = System.Reflection.MethodBase.GetCurrentMethod().Name;
try
{
return JsonMsg>.OK(mg.BasePagQuery(clientId,datesta,datastop,PageNumber,PageSize),st);
}
catch (System.Exception ex)
{
return JsonMsg>.Error(null,st,ex.Message);
}
}
///
/// 删除数据(假删除-只做标记)
///
///
///
[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 (System.Exception ex)
{
return JsonMsg.Error(null,st,ex.Message);
}
}
///
/// 删除数据
///
///
///
[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 (System.Exception ex)
{
return JsonMsg.Error(null,st,ex.Message);
}
}
}
}