using DataVAPI.Model;
using DataVAPI.ModelDataBus;
using DataVAPI.ServerDB.MongoDB;
using DataVAPI.Tool.IOT;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
namespace DataVAPI.Controllers
{
///
/// MG数据库:日志表 LogTable
///
public class LogController : BaseController
{
MongoDbHelper mg = new MongoDbHelper(DataBus.connStr, DataBus.dbName);
MongoDbHelper mgsb = new MongoDbHelper(DataBus.connStr, 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 (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 (Exception ex)
{
return JsonMsg>.Error(null, st, ex.Message);
}
}
///
/// 修改数据
///
///
///
[HttpPost]
public JsonMsg Modify(LogTable 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查询数据
///
///
///
[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> QueryClientId(string clientId)
{
st = System.Reflection.MethodBase.GetCurrentMethod().Name;
try
{
return JsonMsg>.OK(mg.QueryClientId(clientId), st);
}
catch (Exception ex)
{
return JsonMsg>.Error(null, st, ex.Message);
}
}
///
/// 根据设备ID查询当日未处理信息
///
///
///
[HttpGet]
public JsonMsg> QueryDeviceId(string clientId, string deviceId)
{
st = System.Reflection.MethodBase.GetCurrentMethod().Name;
try
{
return JsonMsg>.OK(mg.QueryDeviceId(clientId, deviceId), st);
}
catch (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, device.DeviceId, starttime, stoptime), st);
}
else return JsonMsg>.OK(mg.QueryClientId(device.ClientId), st);
}
return JsonMsg>.OK(null, 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);
}
}
///
/// 分页查询
///
///
///
///
///
///
///
[HttpGet]
public JsonMsg> BasePagQuery(string clientId, string deviceId, DateTime datesta, DateTime datastop, int PageNumber = 1, int PageSize = 100)
{
st = System.Reflection.MethodBase.GetCurrentMethod().Name;
try
{
return JsonMsg>.OK(mg.BasePagQuery(clientId, deviceId, datesta, datastop, PageNumber, PageSize), st);
}
catch (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 (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 (Exception ex)
{
return JsonMsg.Error(null, st, ex.Message);
}
}
}
}