unit ClientInfoService;

interface

uses
  SysUtils, Classes, DB, DBClient, TypeUnit, TypeConfig;

type
  TClientInfoDM = class(TDataModule)
    cdsClientInfo: TClientDataSet;
    cdsClientInfoIP: TWideStringField;
    cdsClientInfoLinkTime: TDateTimeField;
    cdsClientInfoFlag: TIntegerField;
    cdsClientInfoAppType: TWideStringField;
    cdsCloneClientInfo: TClientDataSet;
    cdsCloneClientInfoIP: TWideStringField;
    cdsCloneClientInfoFlag: TIntegerField;
    cdsCloneClientInfoLinkTime: TDateTimeField;
    cdsModify: TClientDataSet;
    cdsModifyLinkTime: TDateTimeField;
    cdsModifyIP: TWideStringField;
    cdsModifyFlag: TIntegerField;
    procedure DataModuleCreate(Sender: TObject);
  private
    function GetAppType(const AType: Integer): string;
    procedure ClearRecords;
    function DeleteRecord(const AIP: string; AUser: Integer): Boolean;
    procedure ModifyRecord(const AIP: string; AUser: Integer);
    function GetCurIP: string;
    function GetCurUser: Integer;
  public
    procedure DeleteCurRecord;
    procedure HandlerNotify(AType: TOperType; APointer: Pointer);

    property CurIP: string read GetCurIP;
    property CurUser: Integer read GetCurUser;
  end;

implementation

{$R *.dfm}

{ TClientInfoDM }

procedure TClientInfoDM.HandlerNotify(AType: TOperType;
  APointer: Pointer);
var
  infoRec: PInfoRecd;
begin
  if Assigned(APointer) then
  begin
    infoRec := APointer;
    if AType = otAdd then
    begin
      cdsClientInfo.Insert;
      cdsClientInfoIP.Value := infoRec.IP;
      cdsClientInfoLinkTime.Value := Now;
      cdsClientInfoFlag.Value := infoRec.User;
      cdsClientInfoAppType.Value := GetAppType(infoRec.User);
      cdsClientInfo.Post;
    end
    else if AType = otModify then
    begin
      ModifyRecord(infoRec.IP, infoRec.User);
    end
    else
    begin
      DeleteRecord(infoRec.IP, infoRec.User);
    end;
  end
  else
    ClearRecords;
end;

procedure TClientInfoDM.DataModuleCreate(Sender: TObject);
begin
  cdsClientInfo.Active := True;
  cdsClientInfo.IndexFieldNames := 'Flag';

  cdsCloneClientInfo.CloneCursor(cdsClientInfo, True);
  cdsCloneClientInfo.IndexFieldNames := 'Flag';

  cdsModify.CloneCursor(cdsClientInfo, True);
  cdsModify.IndexFieldNames := 'Flag';
end;

function TClientInfoDM.GetAppType(const AType: Integer): string;
begin
  Result := CfgManager.GetAppType(AType);
end;

function TClientInfoDM.DeleteRecord(const AIP: string;
  AUser: Integer): Boolean;
begin
  cdsCloneClientInfo.SetRange([AUser], [AUser]);
  try
    while not cdsCloneClientInfo.Eof do
    begin
      if SameText(cdsCloneClientInfoIP.AsString, AIP) then
      begin
        cdsCloneClientInfo.Delete;
        Break;
      end;
      cdsCloneClientInfo.Next;
    end;
  finally
    cdsCloneClientInfo.CancelRange;
  end;
end;

procedure TClientInfoDM.ClearRecords;
begin
  cdsClientInfo.EmptyDataSet;
end;

procedure TClientInfoDM.ModifyRecord(const AIP: string; AUser: Integer);
begin
  cdsModify.SetRange([AUser], [AUser]);
  try
    while not cdsModify.Eof do
    begin
      if SameText(cdsModifyIP.AsString, AIP) then
      begin
        cdsModify.Edit;
        cdsModifyLinkTime.Value := Now;
        cdsModify.Post;
        Break;
      end;
      cdsModify.Next;
    end;
  finally
    cdsModify.CancelRange;
  end;
end;

procedure TClientInfoDM.DeleteCurRecord;
begin
  cdsClientInfo.Delete;
end;

function TClientInfoDM.GetCurIP: string;
begin
  Result := cdsClientInfoIP.Value;
end;

function TClientInfoDM.GetCurUser: Integer;
begin
  Result := cdsClientInfoFlag.Value;
end;

end.
