📄 enguardeventlogunit.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "EnguardEventLogUnit.h"
#include "DBAccessUnit.h"
#include "DMDataAccess.h"
#include "DeviceAccessUnit.h"
#include "EventEngineUnit.h"
#include <ComObj.hpp> //EXCEL导出
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "BaseLogUnit"
#pragma resource "*.dfm"
TEnguardEventLogFrm *EnguardEventLogFrm;
//---------------------------------------------------------------------------
__fastcall TEnguardEventLogFrm::TEnguardEventLogFrm(TComponent* Owner)
: TBaseLogFrm(Owner)
{
}
//---------------------------------------------------------------------------
int __fastcall TEnguardEventLogFrm::QueryLogData()
{
int lDeviceID;
AnsiString lSQL = "select l.*,e.EmpName,d.DeviceName,TypeName from EventLog l,Employee e,Device d,LogType t where l.DeviceID=d.DeviceID and MainEmpID=EmpID and l.EventType=t.TypeID ";
bool lOK;
lOK = GetSelectedDeviceID(lDeviceID);
if (lOK & lDeviceID != 0)
{
lSQL = lSQL + " and l.DeviceID =" + AnsiString(lDeviceID);
}
TDateTime lStartTime,EndTime;
lOK = GetSelectedTime(lStartTime,EndTime);
if (lOK)
{
lSQL = lSQL + " and EventTime between :BeginTime and :EndTime";
}
dmDatabase->adoLogData->Close();
dmDatabase->adoLogData->SQL->Text = lSQL;
if (lOK)
{
dmDatabase->adoLogData->Parameters->ParamByName("BeginTime")->Value = lStartTime;
dmDatabase->adoLogData->Parameters->ParamByName("EndTime")->Value = EndTime;
}
try
{
dmDatabase->adoLogData->Open();
}
catch(...)
{
return DBAccess_E;
}
sbInfo->Panels->Items[0]->Text = " 共查询到记录总数:" + AnsiString(dmDatabase->adoLogData->RecordCount) + "条";
return OK;
}
//---------------------------------------------------------------------------
int __fastcall TEnguardEventLogFrm::ClearLogData()
{
bool lOK;
int lDeviceID;
int lRV;
if (GetSelectedDeviceID(lDeviceID))
{
AnsiString lMsg;
if (lDeviceID == 0)
{
if (Application->MessageBoxA("您确认要清除电脑所有设备的门禁事件日志吗?","提示信息",MB_YESNO + MB_ICONWARNING) == ID_NO)
return lRV;
lRV = aDBAccess.ClearEventLog();
}
else
{
lMsg = "您确认要删除电脑中的所有 " + edtCurDeviceName->Text + " 设备的门禁事件日志吗?";
if (Application->MessageBoxA(lMsg.c_str(),"提示信息",MB_OK + MB_ICONWARNING) == ID_NO)
return lRV;
lRV = aDBAccess.ClearEventLog(lDeviceID);
}
if (lRV == OK)
{
Application->MessageBoxA("清除电脑门禁事件日志成功!","提示信息",MB_OK + MB_ICONINFORMATION);
}
else
{
Application->MessageBoxA("清除电脑门禁事件日志失败!","提示信息",MB_OK + MB_ICONERROR);
}
}
return lRV;
}
//---------------------------------------------------------------------------
void __fastcall TEnguardEventLogFrm::aLogOutPutExecute(TObject *Sender)
{
TADOQuery* pQuery = dmDatabase->adoLogData;
if (!pQuery->Active)
{
aLogQuery->Execute();
}
if(pQuery->RecordCount < 1)
{
Application->MessageBoxA("列表中无员工信息可以导出!","提示信息",MB_OK + MB_ICONINFORMATION);
return;
}
Variant ExcelApp,NewXls,Cellms;
try
{
ExcelApp = CreateOleObject("Excel.Application"); //启动Excel
ExcelApp.OlePropertySet("Visible",(Variant)false); //使Excel不可见
NewXls = (ExcelApp.OlePropertyGet("Workbooks")).OleFunction("Add"); //添加一个工作薄
Cellms = NewXls.OlePropertyGet("ActiveSheet"); //创建工作区
}
catch(...)
{
Application->MessageBoxA("无法启动Excel服务失败,请判断本机是否正常安装Excel!","提示信息",MB_OK + MB_ICONWARNING);
return;
}
Cellms.OlePropertyGet("Cells",1,1).OlePropertySet("Value",(WideString)"日志记录号");
Cellms.OlePropertyGet("Cells",1,2).OlePropertySet("Value",(WideString)"设备编号");
Cellms.OlePropertyGet("Cells",1,3).OlePropertySet("Value",(WideString)"设备名称");
Cellms.OlePropertyGet("Cells",1,4).OlePropertySet("Value",(WideString)"开门员工");
Cellms.OlePropertyGet("Cells",1,5).OlePropertySet("Value",(WideString)"员工姓名");
Cellms.OlePropertyGet("Cells",1,6).OlePropertySet("Value",(WideString)"发生时间");
Cellms.OlePropertyGet("Cells",1,7).OlePropertySet("Value",(WideString)"消息类型");
int lIndex = 2;
for(pQuery->First();!pQuery->Eof;pQuery->Next())
{
int lLogID = pQuery->FieldByName("DeviceLogID")->AsInteger;
int lDeviceID = pQuery->FieldByName("DeviceID")->AsInteger;
AnsiString lDeviceName = pQuery->FieldByName("DeviceName")->AsString;
int lEmpID = pQuery->FieldByName("MainEmpID")->AsInteger;
AnsiString lEmpName = pQuery->FieldByName("EmpName")->AsString;
TDateTime lDateTime = pQuery->FieldByName("EventTime")->AsDateTime;
AnsiString lMsgName = pQuery->FieldByName("TypeName")->AsString;
Cellms.OlePropertyGet("Cells",lIndex,1).OlePropertySet("Value",(WideString)lLogID);
Cellms.OlePropertyGet("Cells",lIndex,2).OlePropertySet("Value",(WideString)lDeviceID);
Cellms.OlePropertyGet("Cells",lIndex,3).OlePropertySet("Value",(WideString)lDeviceName);
Cellms.OlePropertyGet("Cells",lIndex,4).OlePropertySet("Value",(WideString)lEmpID);
Cellms.OlePropertyGet("Cells",lIndex,5).OlePropertySet("Value",(WideString)lEmpName);
Cellms.OlePropertyGet("Cells",lIndex,6).OlePropertySet("Value",(WideString)lDateTime.DateTimeString());
Cellms.OlePropertyGet("Cells",lIndex,7).OlePropertySet("Value",(WideString)lMsgName);
lIndex++;
}
sdExport->Title = "日志导出Excel文件...";
if(sdExport->Execute())
{
AnsiString lSaveFileName = sdExport->FileName;
if (FileExists(lSaveFileName))
{
if(Application->MessageBoxA("文件已经存在,你要覆盖吗?","提示信息",MB_YESNO + MB_ICONQUESTION) == ID_NO)
return;
DeleteFile(lSaveFileName);
}
NewXls.OleProcedure("SaveAs",lSaveFileName.c_str()); //保存Excel文件
Application->MessageBoxA("日志导出Excel完毕!","提示信息",MB_OK + MB_ICONINFORMATION);
ExcelApp.OleFunction("Quit"); //退出Excel
ExcelApp = Unassigned;
NewXls = Unassigned;
Cellms = Unassigned;
}
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -