unit stgGclGatherControl;

interface

uses
  Classes, stgGatherGclCacheData, stgGclGatherDm, stgGclResultFrm,
  stgGclGather;

type
  TstgGclGatherControl = class
  private
    FProjects: TList;

    FGatherGclCacheData: TstgGatherGclCacheData;
    FGatherGclData: TstgGclGatherData;

    FHintPosition: Integer;

    procedure LoadHint(const ATenderName: string);

    procedure GatherSubTenderFiles;
    procedure SaveGatherResult;
    procedure ShowGatherResult;
  public
    constructor Create;
    destructor Destroy; override;

    procedure Gather;

    property Projects: TList read FProjects;
  end;

implementation

uses
  ProgressHintFrm, SysUtils;

{ TstgGatherControl }

constructor TstgGclGatherControl.Create;
begin
  FProjects := TList.Create;
  FGatherGclCacheData := TstgGatherGclCacheData.Create;
  FGatherGclData := TstgGclGatherData.Create(nil);
end;

destructor TstgGclGatherControl.Destroy;
begin
  FGatherGclData.Free;
  FGatherGclCacheData.Free;
  FProjects.Free;
  inherited;
end;

procedure TstgGclGatherControl.Gather;
begin
  FHintPosition := 0;
  ShowProgressHint('正在准备数据...', FProjects.Count + 1);
  GatherSubTenderFiles;
  FGatherGclCacheData.CalculateAll;
  UpdateProgressHint('正在检查汇总结果...');
  UpdateProgressPosition(FProjects.Count + 1);
  SaveGatherResult;
  CloseProgressHint;
  ShowGatherResult;
end;

procedure TstgGclGatherControl.GatherSubTenderFiles;
var
  vGather: TstgSubTenderFileGatherGcl;
begin
  vGather := TstgSubTenderFileGatherGcl.Create;
  try
    vGather.LoadHint := LoadHint;
    vGather.GatherTo(FGatherGclCacheData, FProjects);
  finally
    vGather.Free;
  end;
end;

procedure TstgGclGatherControl.LoadHint(const ATenderName: string);
begin
  Inc(FHintPosition);
  UpdateProgressHint(Format('正在汇总分包标段"%s"...', [ATenderName]));
  UpdateProgressPosition(FHintPosition);
end;

procedure TstgGclGatherControl.SaveGatherResult;
begin
  FGatherGclData.LoadGatherData(FGatherGclCacheData);
end;

procedure TstgGclGatherControl.ShowGatherResult;
var
  Form: TstgGclResultForm;
begin
  Form := TstgGclResultForm.Create(nil);
  try
    Form.SetGatherData(FGatherGclData);
    Form.ShowModal;
  finally
    Form.Free;
  end;
end;

end.
