using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static IoTIntegrationPlatform.Model.Enum.EnumMqtt;
using IoTIntegrationPlatform.Common;
using IoTIntegrationPlatform.Interface;
using IoTIntegrationPlatform.Model.common;
using IoTIntegrationPlatform.Model.Model;
using SqlSugar;
namespace IoTIntegrationPlatform.Services
{
///
/// 日志服务
///
public class LogService : ILogService
{
private ISqlSugarClient db { get; }
public LogService(ISqlSugarClient _db)
{
db = _db;
}
#region 类句柄操作
private IntPtr handle;
private bool disposed = false;
//关闭句柄
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1060:MovePInvokesToNativeMethodsClass"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), System.Runtime.InteropServices.DllImport("Kernel32")]
public extern static Boolean CloseHandle(IntPtr handle);
///
/// 释放对象资源
///
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
///
/// 释放对象资源
///
///
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (handle != IntPtr.Zero)
{
CloseHandle(handle);
handle = IntPtr.Zero;
}
}
disposed = true;
}
~LogService()
{
Dispose(false);
}
#endregion
///
/// 获取mqtt通讯日志列表
///
///
///
///
public List GetMqttLogList(BaseParm parm, ref int totalPage)
{
List list = new List();
try
{
list = db.Queryable()
.WhereIF(!string.IsNullOrEmpty(parm.Param), it => it.ClientId.Contains(parm.Param) || it.Content.Contains(parm.Param))
.WhereIF(parm.Int1 > 0, it => it.MessagePattern == parm.Int1)
.WhereIF(parm.Int2 > 0, it => it.TopicType == (EnumMqttTopicType)parm.Int2)
.OrderBy(it => it.CreateTime, OrderByType.Desc)//倒序
.ToPageList(parm.PageIndex, parm.PageSize, ref totalPage);
}
catch (Exception ex)
{
Logging.Error(ex, "GetMqttLogList");
}
return list;
}
///
/// 添加mqtt通讯日志
///
///
///
///
public bool AddMqttLog(MqttLog model)
{
bool result = false;
try
{
model.Uuid = Guid.NewGuid().ToString();
model.TopicTypeName = EnumExtension.GetDescription(model.TopicType);
model.CreateTime = TimeHelper.GetCurrentTimestampTicks();
int row = db.Insertable(model).ExecuteCommand();
if (row > 0)
{
result = true;
}
}
catch (Exception ex)
{
Logging.Error(ex, ex.Message);
}
return result;
}
}
}