unit stgGatherControl;

interface

uses
  Classes, stgGatherCacheData, stgGatherDm, stgResultFrm,
  stgGather;

type
  TstgGatherControl = class
  private
    FProjects: TList;
    FSumBaseFile: string;

    FGatherCacheData: tstgGatherCacheData;
    FGatherData: TstgGatherData;
    FResultForm: TstgResultForm;

    FHintPosition: Integer;

    procedure LoadHint(const ATenderName: string);

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

    procedure Gather;

    property Projects: TList read FProjects;
    property SumBaseFile: string read FSumBaseFile write FSumBaseFile;
  end;

implementation

uses
  ProgressHintFrm, SysUtils;

{ TstgGatherControl }

procedure TstgGatherControl.CheckErrorData;
var
  vChecker: TstgErrorChecker;
begin
  vChecker := TstgErrorChecker.Create;
  try
    vChecker.Check(FGatherCacheData);
  finally
    vChecker.Free;
  end;
end;

constructor TstgGatherControl.Create;
begin
  FProjects := TList.Create;
  FGatherCacheData := TstgGatherCacheData.Create;
  FGatherData := TstgGatherData.Create(nil);
end;

destructor TstgGatherControl.Destroy;
begin
  FGatherData.Free;
  FGatherCacheData.Free;
  FProjects.Free;
  inherited;
end;

procedure TstgGatherControl.Gather;
begin
  FHintPosition := 0;
  ShowProgressHint('正在导入总包基准文件数据...', FProjects.Count + 2);
  LoadSumBaseFile;
  GatherSubTenderFiles;
  UpdateProgressHint('正在检查汇总结果...');
  UpdateProgressPosition(FProjects.Count + 1);
  CheckErrorData;
  UpdateProgressPosition(FProjects.Count + 2);
  SaveGatherResult;
  CloseProgressHint;
  ShowGatherResult;
end;

procedure TstgGatherControl.GatherSubTenderFiles;
var
  vGather: TstgSubTenderFileGather;
begin
  vGather := TstgSubTenderFileGather.Create;
  try
    vGather.LoadHint := LoadHint;
    vGather.GatherTo(FGatherCacheData, FProjects);
  finally
    vGather.Free;
  end;
end;

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

procedure TstgGatherControl.LoadSumBaseFile;
var
  vLoader: TstgSumBaseFileLoader;
begin
  vLoader := TstgSumBaseFileLoader.Create(FGatherCacheData.GatherTree, FSumBaseFile);
  try
    vLoader.LoadData;
  finally
    vLoader.Free;
  end;
end;

procedure TstgGatherControl.SaveGatherResult;
begin
  FGatherData.LoadGatherData(FGatherCacheData);
end;

procedure TstgGatherControl.ShowGatherResult; 
var
  Form: TstgResultForm;
begin
  Form := TstgResultForm.Create(nil);
  try
    Form.SetGatherData(FGatherData);
    Form.ShowModal;
  finally
    Form.Free;
  end;
end;

end.
