上位机学习记录(11) 用户操作记录日志功能编写
上位机学习记录(11) 用户操作记录日志功能编写
SystemLog.cs
public enum AlarmType
{
日志信息,
报警信息,
操作记录
}
public class SystemLog
{
public SystemLog()
{
}
public SystemLog(string alarmInfo,string alarmState,AlarmType alarmType,string user)
{
this.AlarmTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
this.AlarmInfo = alarmInfo;
this.AlarmState = alarmState;
this.AlarmType = alarmType;
this.User = user;
}
public string AlarmTime { get; set; }
public string AlarmInfo { get; set; }
public string AlarmState { get; set; }
public AlarmType AlarmType { get; set; }
public string User { get; set; }
}
SystemLogService.cs
public class SystemLogService
{
/// <summary>
/// 插入一条记录
/// </summary>
/// <param name="log">日志记录对象</param>
/// <returns>是否成功</returns>
public static bool AddSystemLog(SystemLog log)
{
return SqlSugarHelper.SqlSugarClient.Insertable(log).ExecuteCommand() == 1;
}
/// <summary>
/// 查询日志信息
/// </summary>
/// <param name="start">开始时间</param>
/// <param name="end">结束时间</param>
/// <param name="alarmtype">报警类型</param>
/// <param name="alarmstate">报警状态</param>
/// <returns>集合</returns>
public static List<SystemLog> GetSystemLogByCondition(string start, string end, string alarmtype, string alarmstate)
{
//根据时间查询
var query = SqlSugarHelper.SqlSugarClient.Queryable<SystemLog>().Where(c => SqlFunc.Between(c.AlarmTime, start, end));
//日志类型
if (alarmtype.Length > 0)
{
query.Where(c => c.AlarmType == (AlarmType)Enum.Parse(typeof(AlarmType), alarmtype));
}
//日志状态
if (alarmstate.Length > 0)
{
query.Where(c => c.AlarmState == alarmstate);
}
return query.ToList();
}
}
public partial class FrmSysLog : Form
{
public FrmSysLog()
{
InitializeComponent();
this.dgv_data.AutoGenerateColumns = false;
this.chk_LogInfo.Checked = true;
this.rdb_Ack.Checked = true;
this.dtp_Start.Value = this.dtp_End.Value.AddHours(-3.0);
}
private void btn_Query_Click(object sender, EventArgs e)
{
Task.Run(() =>
{
this.Invoke(new Action(() =>
{
QueryProcess(this.dtp_Start.Text, this.dtp_End.Text, GetAlarmType(), GetAlarmState());
}));
});
}
private void btn_Today_Click(object sender, EventArgs e)
{
Task.Run(() =>
{
this.Invoke(new Action(() =>
{
QueryProcess(DateTime.Now.ToString("yyyy-MM-dd 00:00:00"), DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), GetAlarmType(), GetAlarmState());
}));
});
}
private List<string> GetAlarmType()
{
List<string> result = new List<string>();
if (chk_LogInfo.Checked)
{
result.Add(this.chk_LogInfo.Text);
}
if (chk_AlarmInfo.Checked)
{
result.Add(this.chk_AlarmInfo.Text);
}
if (chk_OperationInfo.Checked)
{
result.Add(this.chk_OperationInfo.Text);
}
return result;
}
private string GetAlarmState()
{
if (rdb_Ack.Checked)
{
return "触发";
}
else if (rdb_UnAck.Checked)
{
return "消除";
}
else
{
return "全部";
}
}
/// <summary>
/// 查询方法
/// </summary>
/// <param name="start"></param>
/// <param name="end"></param>
/// <param name="alarmtype"></param>
/// <param name="alarmstate"></param>
private void QueryProcess(string start, string end, List<string> alarmtype, string alarmstate)
{
DateTime t1 = Convert.ToDateTime(start);
DateTime t2 = Convert.ToDateTime(end);
if (t1 > t2)
{
new FrmConfirmSingle("日志查询", "开始时间不能大于结束时间") { TopMost = true }.ShowDialog();
return;
}
if (alarmstate == "全部")
{
alarmstate = string.Empty;
}
var List = new List<SystemLog>();
if (alarmtype.Count > 0)
{
foreach (var item in alarmtype)
{
List.AddRange(SystemLogService.GetSystemLogByCondition(start, end, item, alarmstate));
}
}
//排序
List.Sort((p1, p2) =>
{
if (Convert.ToDateTime(p1.AlarmTime) > Convert.ToDateTime(p2.AlarmTime))
{
return 1;
}
else if (Convert.ToDateTime(p1.AlarmTime) < Convert.ToDateTime(p2.AlarmTime))
{
return -1;
}
else
{
return -0;
}
});
this.dgv_data.DataSource = null;
this.dgv_data.DataSource = List;
}
private void btn_Export_Click(object sender, EventArgs e)
{
//NPOI
SaveFileDialog saveFileDialog = new SaveFileDialog();
//设置文件类型
saveFileDialog.Filter = "XLS文件(*.xls)|*.xls|所有文件|*.*";
saveFileDialog.FileName = DateTime.Now.ToString("yyyyMMddHHmmss");
saveFileDialog.DefaultExt = ".xls";
saveFileDialog.AddExtension = true;
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
if (NPOIHelper.SaveToExcelNew(saveFileDialog.FileName, this.dgv_data))
{
new FrmConfirmSingle("日志导出", "日志记录导出成功!") { TopMost = true }.ShowDialog();
}
else
{
new FrmConfirmSingle("日志导出", "日志记录导出失败!") { TopMost = true }.ShowDialog();
}
}
}
private void btn_Print_Click(object sender, EventArgs e)
{
PrintDGV.Print_DataGridView(this.dgv_data);
}
private void dgv_data_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
DataGridViewStyleHelper.DgvRowPostPaint(this.dgv_data, e);
}
}