unit OperatorLogUnit;
interface
uses
Business.System, Business.Model, Business.Data, Business.System.SysUtils,
Business.System.JSSysUtils;
type
//操作日志操作类型:(登录、退出、新增、修改、删除、打印)。
TOperatorLogAction = (olaLogin, olaLogout, olaInsert, olaEdit, olaDelete, olaPrint);
TOperatorLogUnit = class(TBizLibrary)
private
procedure StringAdd(var ASentence: string; ASegment: string; ALink: string);
public
static function WriterOperatorLog(AOperatorID: Integer; AAction: TOperatorLogAction;
ACaption, ATableName: String; ADataSet: TDataSet; var AReason: String): Boolean; overload;
static function WriterOperatorLog(AOperatorID: Integer; AAction: TOperatorLogAction;
ACaption, ATableName, AOldValue, ANewValue: String; var AReason: String): Boolean; overload;
end;
implementation
procedure TOperatorLogUnit.StringAdd(var ASentence: string; ASegment: string;
ALink: string);
begin
if ASegment = '' then
Exit;
if ASentence = '' then
ASentence := ASegment
else
ASentence := ASentence + ALink + ASegment;
end;
static function TOperatorLogUnit.WriterOperatorLog(AOperatorID: Integer;
AAction: TOperatorLogAction; ACaption, ATableName: String;
ADataSet: TDataSet; var AReason: String): Boolean;
var
sOldValue,
sNewValue: String;
i: integer;
begin
Result := False;
if not ((AAction = olaInsert) or (AAction = olaEdit) or (AAction = olaDelete)) then
begin
AReason := '传入的操作类型参数不合法,本函数仅支持新增、修改、删除三种类型!';
exit;
end;
if (ADataSet = nil) or not ADataSet.Active or ADataSet.IsEmpty then
begin
AReason := '传入的数据集参数不合法!';
exit;
end
else if (AAction = olaInsert) and (ADataset.State <> dsInsert) then
begin
AReason := '传入的数据集参数不合法,操作类型是新增时,数据集必须处于新增状态!';
exit;
end
else if (AAction = olaEdit) and (ADataset.State <> dsEdit) then
begin
AReason := '传入的数据集参数不合法,操作类型是编辑时,数据集必须处于编辑状态!';
exit;
end
else if (AAction = olaDelete) and (ADataset.State <> dsBrowse) then
begin
AReason := '传入的数据集参数不合法,操作类型是删除时,数据集必须处于浏览状态!';
exit;
end;
sOldValue := '';
sNewValue := '';
if ADataSet.FindField('Id') <> nil then
begin
case AAction of
olaInsert:
sNewValue := 'ID:' + ADataSet.FieldByName('ID').AsString;
olaEdit:
begin
sNewValue := 'ID:' + ADataSet.FieldByName('ID').AsString;
sOldValue := sNewValue;
end;
olaDelete:
sOldValue := 'ID:' + ADataSet.FieldByName('ID').AsString;
end;
end;
for i := 0 to ADataSet.FieldCount - 1 do
begin
if SameText('ID', ADataSet.Fields.FieldName) then
Continue;
case AAction of
olaInsert:
StringAdd(sNewValue, ADataSet.Fields.FieldName + ':' + ADataSet.Fields.AsString, ';');
olaEdit:
begin
if ADataSet.Fields.CurValue <> ADataSet.Fields.OldValue then
begin
StringAdd(sNewValue, ADataSet.Fields.FieldName + ':' + ADataSet.Fields.CurValue, ';');
StringAdd(sOldValue, ADataSet.Fields.FieldName + ':' + ADataSet.Fields.OldValue, ';');
end;
end;
olaDelete:
StringAdd(sOldValue, ADataSet.Fields.FieldName + ':' + ADataSet.Fields.AsString, ';');
end;
end;
Result := WriterOperatorLog(AOperatorID, AAction, ACaption, ATableName,
sOldValue, sNewValue, AReason);
end;
static function TOperatorLogUnit.WriterOperatorLog(AOperatorID: Integer;
AAction: TOperatorLogAction;
ACaption, ATableName, AOldValue, ANewValue: String; var AReason: String): Boolean;
var
qryWriter: TQuery;
sSql: string;
begin
Result := False;
if AOperatorID <= 0 then
begin
AReason := '传入的操作员参数不合法,本参数必须大于零!';
exit;
end;
if Trim(ACaption) = '' then
begin
AReason := '传入的标题参数不合法,本参数不允许空值!';
exit;
end;
if Trim(ATableName) = '' then
begin
AReason := '传入的数据表名参数不合法,本参数不允许空值!';
exit;
end;
sSql := Format('Insert Into tblOperatorLog(FOperatorID, FComputerName, FIP, ' +
'FAction, FCaption, FTableName, FOldValue, FNewValue) ' +
'Values(%d, %s, %s, %d, %s, %s, %s, %s)',
[AOperatorID, QuotedStr(GetComputerName), QuotedStr(GetLocalIPStr),
Ord(AAction), QuotedStr(ACaption), QuotedStr(ATableName),
QuotedStr(AOldValue), QuotedStr(ANewValue)]);
try
qryWriter := TQuery.Create(nil);
try
qryWriter.ConnectionString := 'DATABASEURL=Biz:\OPERATION\OPERATIONDB.DATABASE';
qryWriter.CommandText := sSql;
qryWriter.Execute;
Result := True;
finally
qryWriter.Free;
end;
except
On E:Exception do
begin
AReason := E.Message;
end;
end;
end;
end. |