unit TenderBackupManager;

interface

uses
  Classes, SysUtils, ZhAPI, Windows;

type
  TFileInfo = class
  private
    FFileName: string;
    FLastWriteDateTime: TDateTime;
    FSimpleName: string;

    function FileTimeToDateTime(AFileTime: TFileTime): TDateTime;
    procedure LoadFileDateTime;

    procedure SetFileName(const Value: string);
  public
    property FileName: string read FFileName write SetFileName;
    property SimpleName: string read FSimpleName;
    property LastWriteDateTime: TDateTime read FLastWriteDateTime;
  end;

  TBackupFileList = class
  private
    FFiles: TList;
    FFileRange: Integer;
    FPrefix: string;
  public
    constructor Create(AFileRange: Integer; APrefix: string);
    destructor Destroy; override;

    procedure LoadBackupFiles(APath: string);
    function OldestFile: TFileInfo;
    function LastestFile: TFileInfo;
  end;

  TBackupManager = class
  private
    FBackupPath: string;
    FOpenFiles: TBackupFileList;
    FSaveFiles: TBackupFileList;
  public
    constructor Create;
    destructor Destroy; override;

    procedure LoadBackupFile(ABackupPath: string);

    function LastestOpenBackupIsToday: Boolean;

    function OpenBackupFile: string;
    function SaveBackupFile: string;
  end;

implementation

{ TBackupManager }

constructor TBackupManager.Create;
begin
  FOpenFiles := TBackupFileList.Create(5, 'o');
  FSaveFiles := TBackupFileList.Create(5, 's');
end;

destructor TBackupManager.Destroy;
begin
  FOpenFiles.Free;
  FSaveFiles.Free;
  inherited;
end;

function TBackupManager.LastestOpenBackupIsToday: Boolean;
var
  FileInfo: TFileInfo;
begin
  FileInfo := FOpenFiles.LastestFile;
  Result := Trunc(FileInfo.LastWriteDateTime) = Date;
end;

procedure TBackupManager.LoadBackupFile(ABackupPath: string);
begin
  //if FBackupPath = ABackupPath then Exit;
  FBackupPath := ABackupPath;
  FOpenFiles.LoadBackupFiles(FBackupPath);
  FSaveFiles.LoadBackupFiles(ABackupPath);
end;

function TBackupManager.OpenBackupFile: string;
var
  FileInfo: TFileInfo;
begin
  FileInfo := FOpenFiles.OldestFile;
  Result := FileInfo.FileName;
end;

function TBackupManager.SaveBackupFile: string;
var
  FileInfo: TFileInfo;
begin
  FileInfo := FSaveFiles.OldestFile;
  Result := FileInfo.FileName;
end;

{ TFileInfo }

function TFileInfo.FileTimeToDateTime(AFileTime: TFileTime): TDateTime;
var
  dft: DWord;
  lft: TFileTime;
begin
  FileTimeToLocalFileTime(AFileTime, lft);
  FileTimeToDosDateTime(lft, LongRec(dft).Hi, LongRec(dft).Lo);
  Result := FileDateToDateTime(dft);
end;

procedure TFileInfo.LoadFileDateTime;
var
  ffd: TWin32FindData;
  h: THandle;
begin
  FLastWriteDateTime := 0;
  h := FindFirstFile(PChar(FFileName), ffd);
  if h <> INVALID_HANDLE_VALUE then
    FLastWriteDateTime := FileTimeToDateTime(ffd.ftLastWriteTime);
  windows.FindClose(h);
end;

procedure TFileInfo.SetFileName(const Value: string);
begin
  FFileName := Value;
  FSimpleName := ExtractSimpleFileName(FFileName);
  LoadFileDateTime;
end;

{ TBackupFileList }

constructor TBackupFileList.Create(AFileRange: Integer; APrefix: string);
var
  i: Integer;
  FileInfo: TFileInfo;
begin
  FFileRange := AFileRange;
  FPrefix := APrefix;
  FFiles := TList.Create;
  for i := 1 to FFileRange do
  begin
    FileInfo := TFileInfo.Create;
    FFiles.Add(FileInfo);
  end;
end;

destructor TBackupFileList.Destroy;
begin
  ClearObjects(FFiles);
  FFiles.Free;
  inherited;
end;

function TBackupFileList.LastestFile: TFileInfo;
var
  i: Integer;
  FileInfo: TFileInfo;
begin
  Result := TFileInfo(FFiles.First);
  for i := 1 to FFiles.Count - 1 do
  begin
    FileInfo := TFileInfo(FFiles.Items[i]);
    if FileInfo.LastWriteDateTime > Result.LastWriteDateTime then
      Result := FileInfo;
  end;
end;

procedure TBackupFileList.LoadBackupFiles(APath: string);
var
  i: Integer;
  FileInfo: TFileInfo;
begin
  for i := 1 to FFileRange do
  begin
    FileInfo := TFileInfo(FFiles.Items[i-1]);
    FileInfo.FileName := Format('%s%d.mtf', [APath + FPrefix, i]);
  end;
end;

function TBackupFileList.OldestFile: TFileInfo;
var
  i: Integer;
  FileInfo: TFileInfo;
begin
  Result := TFileInfo(FFiles.First);
  for i := 1 to FFiles.Count - 1 do
  begin
    FileInfo := TFileInfo(FFiles.Items[i]);
    if FileInfo.LastWriteDateTime < Result.LastWriteDateTime then
      Result := FileInfo;
  end;
end;

end.
