unit rpgGatherControl;

interface

uses
  Classes, rpgGatherData, ADODB;

type
  TrpgGatherControl = class
  private
    // 当前打开项目，根据其筛选项目
    FProjectID: Integer;
    FHistroyProjs: TList;
    // 选择的汇总项目
    FSelectProjs: TList;
    // 汇总数据
    FGatherData: TrpgGatherData;

    function SelectProject: Boolean;
    function SameSelect: Boolean;

    procedure RefreshGather;
  public
    constructor Create(AProjectID: Integer);
    destructor Destroy; override;

    function RefreshConnection: TADOConnection;
  end;

implementation

uses
  ZhAPI, GatherProjInfo, ProjGather, ProjGatherSelectFrm, Globals;

{ TrpgGatherControl }

constructor TrpgGatherControl.Create(AProjectID: Integer);
begin
  FProjectID := AProjectID;
  FHistroyProjs := TList.Create;
  FSelectProjs := TList.Create;
  FGatherData := TrpgGatherData.Create;
end;

destructor TrpgGatherControl.Destroy;
begin
  FGatherData.Free;
  ClearObjects(FSelectProjs);
  FSelectProjs.Free;
  ClearObjects(FHistroyProjs);
  FHistroyProjs.Free;
  inherited;
end;

function TrpgGatherControl.RefreshConnection: TADOConnection;
begin
  if SelectProject and not SameSelect then
    RefreshGather;
  Result := FGatherData.Connection;
end;

procedure TrpgGatherControl.RefreshGather;
var
  Gather: TProjGather;
begin
  Gather := TProjGather.Create(FGatherData.WriteGatherData,
    ReportConfig.XmjCompare, ReportConfig.GclCompare);
  try
    Gather.Gather(FSelectProjs);
    FGatherData.LoadRelaData(FProjectID);
    ClearObjects(FHistroyProjs);
    FHistroyProjs.Assign(FSelectProjs);
  finally
    Gather.Free;
  end;
end;

function TrpgGatherControl.SameSelect: Boolean;

  function IncludeProj(AList: TList; AProj: TGatherProjInfo): Boolean;
  var
    i: Integer;
  begin
    Result := False;
    for i := 0 to AList.Count - 1 do
    begin
      if AProj.ProjectID = TGatherProjInfo(AList.Items[i]).ProjectID then
      begin
        Result := True;
        Break;
      end;
    end;
  end;

  function IncludeList(ALarge, ASmall: TList): Boolean;
  var
    iSmall: Integer;
  begin
    Result := True;
    for iSmall := 0 to ASmall.Count - 1 do
    begin
      if IncludeProj(ALarge, TGatherProjInfo(ASmall.Items[iSmall])) then
      begin
        Result := False;
        Break;
      end;
    end;
  end;

begin
  if FHistroyProjs.Count = FSelectProjs.Count then
    Result := IncludeList(FHistroyProjs, FSelectProjs) and IncludeList(FSelectProjs, FHistroyProjs)
  else
    Result := False;
end;

function TrpgGatherControl.SelectProject: Boolean;
begin
  Result := SelectGatherProject(FProjectID, FHistroyProjs, FSelectProjs);
end;

end.
