unit TenderBackupDm;

interface

uses
  SysUtils, Classes, ADODB, sdDB, sdProvider, Variants;

type
  TTenderBackupData = class(TDataModule)
    sdpTenderBackup: TsdADOProvider;
    sddTenderBackup: TsdDataSet;
    sdvTenderBackup: TsdDataView;
    sdvSearchTB: TsdDataView;
    procedure sdvTenderBackupFilterRecord(ARecord: TsdDataRecord;
      var Allow: Boolean);
    procedure sdvTenderBackupGetText(var Text: String;
      ARecord: TsdDataRecord; AValue: TsdValue; AColumn: TsdViewColumn;
      DisplayText: Boolean);
  private
    FProjectID: Integer;
    FNewID: Integer;

    function TenderBackupCount(AProjectID, AType: Integer): Integer;
    function NewTenderBackup(AProjectID, AType: Integer): TsdDataRecord;
    function OldestTenderBackup(AProjectID, AType: Integer): TsdDataRecord;
    function AddTenderBackup(AProjectID, AType: Integer): TsdDataRecord;

    procedure SetProjectID(const Value: Integer);
  public
    procedure Open(AConnection: TADOConnection);
    procedure Save;

    function AddOpenTenderBackup(AProjectID: Integer): TsdDataRecord;
    function AddSaveTenderBackup(AProjectID: Integer): TsdDataRecord;

    function HasTodayOpenTenderBackup(AProjectID: Integer): Boolean;

    property ProjectID: Integer read FProjectID write SetProjectID;
  end;

implementation

uses DateUtils;

{$R *.dfm}

{ TTenderBackupData }

procedure TTenderBackupData.Open(AConnection: TADOConnection);
var
  idxID: TsdIndex;
begin
  sdpTenderBackup.Connection := AConnection;
  sddTenderBackup.Open;
  sddTenderBackup.AddIndex('idxID', 'ID');
  sddTenderBackup.AddIndex('idxFilter', 'ProjectID;Type');
  sddTenderBackup.AddIndex('idxView', 'ProjectID;Type;ID');
  sdvTenderBackup.Open;
  sdvTenderBackup.IndexName := 'idxView';
  sdvSearchTB.Open;
  sdvSearchTB.IndexName := 'idxFilter';

  if sddTenderBackup.RecordCount > 0 then
  begin
    idxID := sddTenderBackup.FindIndex('idxID');
    FNewID := idxID.Records[sddTenderBackup.RecordCount - 1].ValueByName('ID').AsInteger + 1;
  end
  else
    FNewID := 1;
end;

procedure TTenderBackupData.Save;
begin
  sddTenderBackup.Save;
end;

procedure TTenderBackupData.SetProjectID(const Value: Integer);
begin
  FProjectID := Value;
  sdvTenderBackup.RefreshFilter;
end;

procedure TTenderBackupData.sdvTenderBackupFilterRecord(
  ARecord: TsdDataRecord; var Allow: Boolean);
begin
  Allow := ARecord.ValueByName('ProjectID').AsInteger = ProjectID;
end;

function TTenderBackupData.TenderBackupCount(AProjectID,
  AType: Integer): Integer;
var
  idx: TsdIndex;
  iFirst, iLast: Integer;
  Rec: TsdDataRecord;
begin
  Result := 0;
  sdvSearchTB.IndexName := 'idxFilter';
  sdvSearchTB.SetRange([AProjectID, AType], [AProjectID, AType]);
  Result := sdvSearchTB.RecordCount;
  sdvSearchTB.CancelRange;
{  idx := sddTenderBackup.FindIndex('idxFilter');
  iFirst := idx.FindKeyIndex(VarArrayOf([AProjectID, AType]));
  iLast := idx.FindKeyLastIndex(VarArrayOf([AProjectID, AType]));
  if iFirst > 0 then
    Result := iLast - iFirst + 1;}
end;

function TTenderBackupData.AddOpenTenderBackup(
  AProjectID: Integer): TsdDataRecord;
begin
  Result := AddTenderBackup(AProjectID, 1);
end;

function TTenderBackupData.AddSaveTenderBackup(
  AProjectID: Integer): TsdDataRecord;
begin
  Result := AddTenderBackup(AProjectID, 0);
end;

function TTenderBackupData.AddTenderBackup(AProjectID,
  AType: Integer): TsdDataRecord;
var
  iCount: Integer;
begin
  iCount := TenderBackupCount(AProjectID, AType);
  if iCount < 5 then
    Result := NewTenderBackup(AProjectID, AType)
  else
    Result := OldestTenderBackup(AProjectID, AType);
end;

function TTenderBackupData.NewTenderBackup(AProjectID,
  AType: Integer): TsdDataRecord;
var
  iCount: Integer;
begin
  iCount := TenderBackupCount(AProjectID, AType);
  Result := sddTenderBackup.Add;
  Result.ValueByName('ID').AsInteger := FNewID;
  Inc(FNewID);
  Result.ValueByName('ProjectID').AsInteger := FProjectID;
  Result.ValueByName('Type').AsInteger := AType;
  if AType = 0 then
    Result.ValueByName('FileName').AsString := Format('s%d.mtf', [iCount + 1])
  else
    Result.ValueByName('FileName').AsString := Format('o%d.mtf', [iCount + 1]);
end;

function TTenderBackupData.OldestTenderBackup(AProjectID,
  AType: Integer): TsdDataRecord;
var
  idx: TsdIndex;
  iIndex, iFirst, iLast: Integer;
  Rec: TsdDataRecord;
begin
  idx := sddTenderBackup.FindIndex('idxFilter');
  iFirst := idx.FindKeyIndex(VarArrayOf([AProjectID, AType]));
  iLast := idx.FindKeyLastIndex(VarArrayOf([AProjectID, AType]));
  Result := idx.Records[iFirst];
  for iIndex := iFirst + 1 to iLast do
  begin
    Rec := idx.Records[iIndex];
    if Result.ValueByName('CreateDateTime').AsDateTime > Rec.ValueByName('CreateDateTime').AsDateTime then
      Result := Rec;
  end;
end;

function TTenderBackupData.HasTodayOpenTenderBackup(
  AProjectID: Integer): Boolean;
var
  idx: TsdIndex;
  iIndex, iFirst, iLast: Integer;
  Rec: TsdDataRecord;
begin
  Result := False;
  sdvSearchTB.IndexName := 'idxFilter';
  sdvSearchTB.SetRange([AProjectID, 1], [AProjectID, 1]);
  for iIndex := 0 to sdvSearchTB.RecordCount - 1 do
  begin
    Rec := sdvSearchTB.Records[iIndex];
    if Date = Trunc(Rec.ValueByName('CreateDateTime').AsDateTime) then
    begin
      Result := True;
      Break;
    end;
  end;
  sdvSearchTB.CancelRange;
  {idx := sddTenderBackup.FindIndex('idxFilter');
  iFirst := idx.FindKeyIndex(VarArrayOf([AProjectID, 1]));
  iLast := idx.FindKeyLastIndex(VarArrayOf([AProjectID, 1]));
  if iFirst = -1 then Exit;
  for iIndex := iFirst to iLast do
  begin
    Rec := idx.Records[iIndex];
    if Date = Trunc(Rec.ValueByName('CreateDateTime').AsDateTime) then
    begin
      Result := True;
      Break;
    end;
  end;}
end;

procedure TTenderBackupData.sdvTenderBackupGetText(var Text: String;
  ARecord: TsdDataRecord; AValue: TsdValue; AColumn: TsdViewColumn;
  DisplayText: Boolean);
begin
  if SameText(AValue.FieldName, 'Type') then
  begin
    if AValue.AsInteger = 0 then
      Text := '保存备份点'
    else if AValue.AsInteger = 1 then
      Text := '打开备份点';
  end
  else if SameText(AValue.FieldName, 'CreateDateTime') then
  begin
    Text := DateTimeToStr(AValue.AsDateTime);
  end;
end;

end.
